{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Creation of a custom random vector"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "In this example we are going to create a distribution or copula.\n",
    "\n",
    "The way to go is inheriting the PythonRandomVector class and overload its methods:\n",
    "\n",
    "- getRealization\n",
    "- getSample\n",
    "- getMean\n",
    "- getCovariance"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "from __future__ import print_function\n",
    "import openturns as ot\n",
    "import math as m"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Inherit PythonRandomVector\n",
    "class RVEC(ot.PythonRandomVector):\n",
    "\n",
    "    def __init__(self):\n",
    "        super(RVEC, self).__init__(2)\n",
    "        self.setDescription(['R', 'S'])\n",
    "\n",
    "    def getRealization(self):\n",
    "        X = [ot.RandomGenerator.Generate(), 2.0 + ot.RandomGenerator.Generate()]\n",
    "        return X\n",
    "\n",
    "    def getSample(self, size):\n",
    "        X = []\n",
    "        for i in range(size):\n",
    "            X.append(\n",
    "                [ot.RandomGenerator.Generate(), 2.0 + ot.RandomGenerator.Generate()])\n",
    "        return X\n",
    "\n",
    "    def getMean(self):\n",
    "        return [0.5, 2.5]\n",
    "\n",
    "    def getCovariance(self):\n",
    "        return [[1.0, 0.0], [0.0, 1.0]]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Instanciate our distribution\n",
    "randomVector = ot.RandomVector(RVEC())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<TABLE><TR><TD></TD><TH>v0</TH><TH>v1</TH></TR><TR><TH>0</TH><TD>0.6298765566077771</TD><TD>2.8828052237192603</TD></TR><TR><TH>1</TH><TD>0.13527635081884104</TD><TD>2.032502751204385</TD></TR><TR><TH>2</TH><TD>0.3470570412135219</TD><TD>2.9694230211349892</TD></TR><TR><TH>3</TH><TD>0.9206795933570506</TD><TD>2.5030401514493823</TD></TR><TR><TH>4</TH><TD>0.06320607651305044</TD><TD>2.2927568937477316</TD></TR></TABLE>"
      ],
      "text/plain": [
       "class=Sample name=Unnamed implementation=class=SampleImplementation name=Unnamed size=5 dimension=2 data=[[0.629877,2.88281],[0.135276,2.0325],[0.347057,2.96942],[0.92068,2.50304],[0.0632061,2.29276]]"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Get a sample\n",
    "randomVector.getSample(5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<p>[0.5,2.5]<br>\n",
       "</p>"
      ],
      "text/plain": [
       "class=Point name=Unnamed dimension=2 values=[0.5,2.5]"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Get mean\n",
    "randomVector.getMean()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<p>[[ 1 0 ]<br>\n",
       " [ 0 1 ]]<br>\n",
       "</p>"
      ],
      "text/plain": [
       "class=CovarianceMatrix dimension=2 implementation=class=MatrixImplementation name=Unnamed rows=2 columns=2 values=[1,0,0,1]"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Compute the probability contained in an interval\n",
    "randomVector.getCovariance()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
