Create a random vector

The RandomVector object represents the concept of random variable.

This class can be cerated by:

  • Case 1: directly using the RandomVector class,

  • Case 2: from python, using the PythonRandomVector class which enables to redefine some methods (as the sampling method for example).

import openturns as ot

Case 1: use the RandomVector class

We create the random vector (X_0, X_1, X_2) following the Normal distribution with zero mean and unit variance.

dist3d = ot.Normal(3)
X = ot.RandomVector(dist3d)

Get the dimension

X.getDimension()
3

Get the mean

X.getMean()
class=Point name=Unnamed dimension=3 values=[0,0,0]


Get the covariance

X.getCovariance()

[[ 1 0 0 ]
[ 0 1 0 ]
[ 0 0 1 ]]



Draw a sample

X.getSample(5)
X0X1X2
00.6082017-1.266173-0.4382656
11.205478-2.1813850.3500421
2-0.3550071.4372490.810668
30.793156-0.47052560.2610179
4-2.290062-1.282885-1.311781


Extract the component X_1:

X1 = X.getMarginal(1)
X1.getSample(5)
X1
0-0.09078383
10.9957932
2-0.1394528
3-0.5602056
40.4454897


Extract the component (X_1, X_2).

X02 = X.getMarginal([0, 2])
X02.getSample(5)
X0X2
00.3229250.4457853
1-1.038077-0.8567123
20.4736169-0.1254977
30.35141781.782359
40.07020736-0.7813665


Case 2: use the PythonRandomVector class

We create a random vector using the PythonRandomVector class, which enables to overload the following methods: getRealization, getSample, getMean and getCovariance.

Inherit PythonRandomVector Here, we create a random vector of dimension 2 (X_1, X_2), where X_1 \sim \cU(0,1) and X_1 \sim \cU(1,3) with independent components.

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 / 12.0, 0.0], [0.0, 1.0 / 12.0]]

Instantiate the distribution

randomVector = ot.RandomVector(RVEC())

Get a sample

randomVector.getSample(5)
v0v1
00.20365272.855795
10.43507832.361664
20.0021076152.239434
30.25515632.591534
40.16921652.977334


Get its mean

randomVector.getMean()
class=Point name=Unnamed dimension=2 values=[0.5,2.5]


Compute the probability contained in an interval

randomVector.getCovariance()

[[ 0.0833333 0 ]
[ 0 0.0833333 ]]