# Creation of a custom random vectorΒΆ

In this example we are going to create a distribution or copula.

The way to go is inheriting the PythonRandomVector class and overload its methods:

• getRealization

• getSample

• getMean

• getCovariance

[1]:

from __future__ import print_function
import openturns as ot
import math as m

[2]:

# Inherit PythonRandomVector
class RVEC(ot.PythonRandomVector):

def __init__(self):
super(RVEC, self).__init__(2)
self.setDescription(['R', 'S'])

def getRealization(self):
X = [ot.RandomGenerator.Generate(), 2.0 + ot.RandomGenerator.Generate()]
return X

def getSample(self, size):
X = []
for i in range(size):
X.append(
[ot.RandomGenerator.Generate(), 2.0 + ot.RandomGenerator.Generate()])
return X

def getMean(self):
return [0.5, 2.5]

def getCovariance(self):
return [[1.0, 0.0], [0.0, 1.0]]

[3]:

# Instanciate our distribution
randomVector = ot.RandomVector(RVEC())

[4]:

# Get a sample
randomVector.getSample(5)

[4]:

v0 v1 0.6298765566077771 2.8828052237192603 0.13527635081884104 2.032502751204385 0.3470570412135219 2.9694230211349892 0.9206795933570506 2.5030401514493823 0.06320607651305044 2.2927568937477316
[5]:

# Get mean
randomVector.getMean()

[5]:


[0.5,2.5]

[6]:

# Compute the probability contained in an interval
randomVector.getCovariance()

[6]:


[[ 1 0 ]
[ 0 1 ]]