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
In [1]:
from __future__ import print_function
import openturns as ot
import math as m
In [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]]
In [3]:
# Instanciate our distribution
randomVector = ot.RandomVector(RVEC())
In [4]:
# Get a sample
randomVector.getSample(5)
Out[4]:
v0v1
00.62987655660777712.8828052237192603
10.135276350818841042.032502751204385
20.34705704121352192.9694230211349892
30.92067959335705062.5030401514493823
40.063206076513050442.2927568937477316
In [5]:
# Get mean
randomVector.getMean()
Out[5]:

[0.5,2.5]

In [6]:
# Compute the probability contained in an interval
randomVector.getCovariance()
Out[6]:

[[ 1 0 ]
[ 0 1 ]]