1 // SWIG file RandomVector.i 2 3 %{ 4 #include "openturns/RandomVector.hxx" 5 #include "openturns/PythonRandomVector.hxx" 6 %} 7 8 %include RandomVector_doc.i 9 10 %pythoncode %{ 11 class PythonRandomVector(object): 12 """ 13 Allow to overload RandomVector from Python. 14 15 Parameters 16 ---------- 17 dim : positive int 18 Vector dimension. 19 Default is 0. 20 21 See also 22 -------- 23 RandomVector 24 25 Examples 26 -------- 27 >>> import openturns as ot 28 >>> ot.RandomGenerator.SetSeed(0) 29 30 Overload RandomVector from Python: 31 32 >>> class RVEC(ot.PythonRandomVector): 33 ... def __init__(self): 34 ... super(RVEC, self).__init__(2) 35 ... self.setDescription(['R', 'S']) 36 ... 37 ... def getRealization(self): 38 ... X = [ot.RandomGenerator.Generate(), 2 + ot.RandomGenerator.Generate()] 39 ... return X 40 ... 41 ... def getSample(self, size): 42 ... X = [] 43 ... for i in range(size): 44 ... X.append([ot.RandomGenerator.Generate(), 2 + ot.RandomGenerator.Generate()]) 45 ... return X 46 ... 47 ... def getMean(self): 48 ... return [0.5, 2.5] 49 ... 50 ... def getCovariance(self): 51 ... return [[0.0833333, 0.], [0., 0.0833333]] 52 53 Use the overloaded class: 54 55 >>> R = RVEC() 56 >>> # Instance creation 57 >>> myRV = ot.RandomVector(R) 58 >>> # Realization 59 >>> print(myRV.getRealization()) 60 [0.629877,2.88281] 61 >>> # Sample 62 >>> print(myRV.getSample(5)) 63 0 : [ 0.135276 2.0325 ] 64 1 : [ 0.347057 2.96942 ] 65 2 : [ 0.92068 2.50304 ] 66 3 : [ 0.0632061 2.29276 ] 67 4 : [ 0.714382 2.38336 ] 68 >>> # Mean 69 >>> print(myRV.getMean()) 70 [0.5,2.5] 71 >>> # Covariance 72 >>> print(myRV.getCovariance()) 73 [[ 0.0833333 0 ] 74 [ 0 0.0833333 ]] 75 76 Random vectors can admit parameters. 77 78 In the following example, we define a `RandomVector` 79 to sample from a normal multivariate normal distribution 80 truncated to a ball. 81 We implement the `setParameter` method to define the ball's center. 82 83 >>> class NormalTruncatedToBall(ot.PythonRandomVector): 84 ... def __init__(self, dim, max_dist): 85 ... super().__init__(dim) 86 ... self._center = ot.Point(dim) 87 ... self._normal = ot.Normal(dim) 88 ... self._max_dist = max_dist 89 ... self.setParameter(ot.Point(dim)) 90 ... 91 ... def getRealization(self): 92 ... dist = ot.SpecFunc.MaxScalar 93 ... while dist>self._max_dist: 94 ... candidate = self._normal.getRealization() 95 ... dist = (candidate - self._center).norm() 96 ... return candidate 97 ... 98 ... def setParameter(self, center): # the parameter influences sampling 99 ... self._center = center 100 ... 101 ... def getParameter(self): # implemented for the sake of consistency 102 ... return self._center 103 ... 104 ... def getParameterDescription(self): # optional 105 ... return ["center_{}".format(i) for i in range(self.getDimension())] 106 107 Define an instance of this `RandomVector` and set the parameter: 108 109 >>> myRV = ot.RandomVector(NormalTruncatedToBall(2, 1.5)) 110 >>> myRV.setParameter([1.3, 0.6]) 111 112 Get a sample and plot it: 113 114 >>> sample = myRV.getSample(100) 115 >>> graph = ot.Graph("Sample from a PythonRandomVector", "", "", True, '') 116 >>> cloud = ot.Cloud(sample) 117 >>> graph.add(cloud) 118 >>> from openturns.viewer import View 119 >>> view = View(graph) 120 121 """ 122 def __init__(self, dim=0): 123 # Warning: these names are used in PythonRandomVector class. Synchronize the files if changed 124 self.__dim = dim 125 self.__desc = ['x' + str(i) for i in range(dim)] 126 127 def __str__(self): 128 return 'PythonRandomVector -> %s #%d' % (self.__desc, self.__dim) 129 130 def __repr__(self): 131 return self.__str__() 132 133 def getDimension(self): 134 """ 135 Get the dimension. 136 137 Returns 138 ------- 139 dim : positive int 140 Dimension of the RandomVector. 141 """ 142 return self.__dim 143 144 def setDescription(self, desc): 145 """ 146 Set the description. 147 148 Parameters 149 ---------- 150 desc : sequence of str 151 *desc* describes the components of the RandomVector. 152 Its size must be equal to the dimension of the RandomVector. 153 """ 154 if (len(desc) != self.__dim): 155 raise ValueError('Description size does NOT match dimension') 156 self.__desc = desc 157 158 def getDescription(self): 159 """ 160 Get the description. 161 162 Returns 163 ------- 164 desc : :class:`~openturns.Description` 165 *desc* describes the components of the RandomVector. 166 """ 167 return self.__desc 168 169 %} 170 171 OTTypedInterfaceObjectHelper(RandomVector) 172 173 %include openturns/RandomVector.hxx 174 namespace OT { %extend RandomVector { 175 176 RandomVector(const RandomVector & other) 177 { 178 return new OT::RandomVector(other); 179 } 180 181 RandomVector(PyObject * pyObj) 182 { 183 return new OT::RandomVector( new OT::PythonRandomVector(pyObj) ); 184 } 185 186 } // class RandomVector 187 } // namespace OT 188