1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
|
import openturns as ot
import openturns.viewer as otv
class NormalTruncatedToBall(ot.PythonRandomVector):
def __init__(self, dim, max_dist):
super().__init__(dim)
self._center = ot.Point(dim)
self._normal = ot.Normal(dim)
self._max_dist = max_dist
self.setParameter(ot.Point(dim))
def getRealization(self):
dist = ot.SpecFunc.Infinity
while dist > self._max_dist:
candidate = self._normal.getRealization()
dist = (candidate - self._center).norm()
return candidate
def setParameter(self, center):
self._center = center
def getParameter(self):
return self._center
def getParameterDescription(self):
return [f"center_{i}" for i in range(self.getDimension())]
myRV = ot.RandomVector(NormalTruncatedToBall(2, 1.5))
myRV.setParameter([1.3, 0.6])
sample = myRV.getSample(1000)
graph = ot.Graph("Sample from a PythonRandomVector", "", "", True, "")
cloud = ot.Cloud(sample)
graph.add(cloud)
otv.View(graph)
|