File: t_Event_system.py

package info (click to toggle)
openturns 1.26-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 67,708 kB
  • sloc: cpp: 261,605; python: 67,030; ansic: 4,378; javascript: 406; sh: 185; xml: 164; makefile: 101
file content (192 lines) | stat: -rwxr-xr-x 6,206 bytes parent folder | download | duplicates (2)
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
#! /usr/bin/env python

import openturns as ot
import openturns.testing as ott

ot.TESTPREAMBLE()

dim = 2
distribution = ot.Normal(dim)


X = ot.RandomVector(distribution)


# 1. Composite/Composite
f1 = ot.SymbolicFunction(["x" + str(i) for i in range(dim)], ["x0"])
f2 = ot.SymbolicFunction(["x" + str(i) for i in range(dim)], ["x1"])

Y1 = ot.CompositeRandomVector(f1, X)
l2 = ot.LevelSet(f2, ot.Greater(), 0.0)

e1 = ot.ThresholdEvent(Y1, ot.Less(), 0.0)
e2 = ot.DomainEvent(X, l2)

e3 = e1.intersect(e2)
# print('e3=', e3)

proba_e3 = e3.getSample(10000).computeMean()[0]
print("proba_e3 = %.3g" % proba_e3)
ott.assert_almost_equal(proba_e3, 0.25, 1e-2, 1e-2)

e4 = e1.join(e2)
# print('e4=', e4)

proba_e4 = e4.getSample(10000).computeMean()[0]
print("proba_e4 = %.3g" % proba_e4)
ott.assert_almost_equal(proba_e4, 0.75, 1e-2, 1e-2)

e5 = ot.IntersectionEvent([e1, e2])
# print('e5=', e5)

proba_e5 = e5.getSample(10000).computeMean()[0]
print("proba_e5 = %.3g" % proba_e5)
ott.assert_almost_equal(proba_e5, 0.25, 1e-2, 1e-2)

e6 = ot.UnionEvent([e1, e2])
# print('e6=', e6)

proba_e6 = e6.getSample(10000).computeMean()[0]
print("proba_e6 = %.3g" % proba_e6)
ott.assert_almost_equal(proba_e6, 0.75, 1e-2, 1e-2)

# intersection of intersection
e7 = ot.IntersectionEvent([e1, ot.IntersectionEvent([e1, e2])])
ott.assert_almost_equal(e7.getSample(10000).computeMean()[0], 0.25, 1e-2, 1e-2)

e8 = ot.IntersectionEvent([ot.IntersectionEvent([e1, e2]), e1])
ott.assert_almost_equal(e8.getSample(10000).computeMean()[0], 0.25, 1e-2, 1e-2)

# union of union
e9 = ot.UnionEvent([e1, ot.UnionEvent([e1, e2])])
ott.assert_almost_equal(e9.getSample(10000).computeMean()[0], 0.75, 1e-2, 1e-2)

e10 = ot.UnionEvent([ot.UnionEvent([e1, e2]), e1])
ott.assert_almost_equal(e10.getSample(10000).computeMean()[0], 0.75, 1e-2, 1e-2)

# intersection of union
e11 = ot.IntersectionEvent([ot.UnionEvent([e1, e2]), ot.UnionEvent([e1, e2])])
ott.assert_almost_equal(e11.getSample(10000).computeMean()[0], 0.75, 1e-2, 1e-2)

# union of intersection
e12 = ot.UnionEvent([ot.IntersectionEvent([e1, e2]), ot.IntersectionEvent([e1, e2])])
ott.assert_almost_equal(e12.getSample(10000).computeMean()[0], 0.25, 1e-2, 1e-2)

# DomainEvent
domain = ot.Interval([-1.0] * dim, [1.0] * dim)
e13 = ot.DomainEvent(X, domain)
e13_probability = distribution.computeProbability(domain)
ott.assert_almost_equal(
    e13.getSample(10000).computeMean()[0], e13_probability, 1e-2, 1e-2
)

# Union with DomainEvent
e14 = ot.UnionEvent([e1, e2, e13])
ott.assert_almost_equal(
    e14.getSample(10000).computeMean()[0], 0.75 + e13_probability / 4, 1e-2, 1e-2
)

# Intersection with DomainEvent
e15 = ot.IntersectionEvent([e1, e2, e13])
ott.assert_almost_equal(
    e15.getSample(10000).computeMean()[0], e13_probability / 4, 1e-2, 1e-2
)

# through simulation


def sim_event(ev):
    experiment = ot.MonteCarloExperiment()
    algo = ot.ProbabilitySimulationAlgorithm(ev, experiment)
    algo.setMaximumOuterSampling(2500)
    algo.setBlockSize(4)
    algo.setMaximumCoefficientOfVariation(-1.0)
    algo.run()
    result = algo.getResult()
    return result.getProbabilityEstimate()


ott.assert_almost_equal(sim_event(e5), 0.25, 1e-2, 1e-2)
ott.assert_almost_equal(sim_event(e6), 0.75, 1e-2, 1e-2)
ott.assert_almost_equal(sim_event(e7), 0.25, 1e-2, 1e-2)
ott.assert_almost_equal(sim_event(e8), 0.25, 1e-2, 1e-2)
ott.assert_almost_equal(sim_event(e9), 0.75, 1e-2, 1e-2)
ott.assert_almost_equal(sim_event(e10), 0.75, 1e-2, 1e-2)
ott.assert_almost_equal(sim_event(e11), 0.75, 1e-2, 1e-2)
ott.assert_almost_equal(sim_event(e12), 0.25, 1e-2, 2e-2)
ott.assert_almost_equal(sim_event(e13), e13_probability, 1e-2, 1e-2)
ott.assert_almost_equal(sim_event(e14), 0.75 + e13_probability / 4, 1e-2, 1e-2)
ott.assert_almost_equal(sim_event(e15), e13_probability / 4, 1e-2, 1e-2)


def subset_event(ev):
    algo = ot.SubsetSampling(ev)
    algo.setMaximumOuterSampling(2500)
    algo.setBlockSize(4)
    algo.run()
    result = algo.getResult()
    return result.getProbabilityEstimate()


ott.assert_almost_equal(subset_event(e3), 0.25, 1e-2, 1e-2)
ott.assert_almost_equal(subset_event(e5), 0.25, 1e-2, 1e-2)
ott.assert_almost_equal(subset_event(e6), 0.75, 1e-2, 1e-2)
ott.assert_almost_equal(subset_event(e7), 0.25, 1e-2, 1e-2)
ott.assert_almost_equal(subset_event(e8), 0.25, 1e-2, 1e-2)
ott.assert_almost_equal(subset_event(e9), 0.75, 1e-2, 1e-2)
ott.assert_almost_equal(subset_event(e10), 0.75, 1e-2, 1e-2)
ott.assert_almost_equal(subset_event(e11), 0.75, 1e-2, 1e-2)
ott.assert_almost_equal(subset_event(e12), 0.25, 1e-2, 1e-2)


# check that f2 is not called when not needed
f1 = ot.SymbolicFunction(["x" + str(i) for i in range(dim)], ["x0"])
f2 = ot.MemoizeFunction(ot.SymbolicFunction(["x" + str(i) for i in range(dim)], ["x1"]))
Y1 = ot.CompositeRandomVector(f1, X)
Y2 = ot.CompositeRandomVector(f2, X)
e1 = ot.ThresholdEvent(Y1, ot.Less(), 1e6)
e2 = ot.ThresholdEvent(Y2, ot.Less(), 0.0)
union = ot.UnionEvent([e1, e2])
proba = union.getSample(1000).computeMean()[0]
print(proba)
assert proba == 1.0, "always true"
assert f2.getCallsNumber() == 0, "union prune"
e1 = ot.ThresholdEvent(Y1, ot.Greater(), 1e6)
e2 = ot.ThresholdEvent(Y2, ot.Less(), 0.0)
intersection = ot.IntersectionEvent([e1, e2])
proba = intersection.getSample(1000).computeMean()[0]
print(proba)
assert proba == 0.0, "always false"
assert f2.getCallsNumber() == 0, "intersection prune"


# check batch evaluation
n = 10000


def f1py(x):
    x0 = ot.Sample(x).getMarginal(0)
    print("eval f1")
    assert x0.getSize() == n, "no batch eval"
    return x0


def f2py(x):
    x1 = ot.Sample(x).getMarginal(1)
    print("eval f2")
    assert x1.getSize() > 1, "no batch eval"
    return x1


f1 = ot.PythonFunction(2, 1, func_sample=f1py)
f2 = ot.PythonFunction(2, 1, func_sample=f2py)
Y1 = ot.CompositeRandomVector(f1, X)
Y2 = ot.CompositeRandomVector(f2, X)
e1 = ot.ThresholdEvent(Y1, ot.Less(), 0.0)
e2 = ot.ThresholdEvent(Y2, ot.Greater(), 0.0)
e3 = ot.UnionEvent([e1, e2])
p3 = e3.getSample(n).computeMean()[0]
ott.assert_almost_equal(p3, 0.75, 1e-2, 1e-2)
e4 = ot.IntersectionEvent([e1, e2])
p4 = e4.getSample(n).computeMean()[0]
ott.assert_almost_equal(p4, 0.25, 1e-2, 1e-2)