Repository navigation
Expand file tree
/
Copy pathstick_triangle.py
More file actions
127 lines (104 loc) · 3.75 KB
/
Copy pathstick_triangle.py
File metadata and controls
127 lines (104 loc) · 3.75 KB
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
import numpy as np
import mkl_random as rnd
__doc__ = r"""
Let's solve a classic problem of MC-estimating a probability that 3 segments
of a unit stick randomly broken in 2 places can form a triangle.
Let $u_1$ and $u_2$ be standard uniform random variables, denoting positions
where the stick has been broken.
Let $w_1 = \min(u_1, u_2)$ and $w_2 = \max(u_1, u_2)$. Then, length of segments
are $x_1 = w_1$, $x_2 = w_2-w_1$, $x_3 = 1-w_2$.
These lengths must satisfy triangle inequality.
The closed form result is known to be $\frac{1}{4}$.
"""
def triangle_inequality(x1, x2, x3):
"""
Efficiently finds
`np.less(x1, x2 + x3) * np.less(x2, x1 + x3) * np.less(x3, x1 + x2)`
"""
tmp_sum = x2 + x3
res = np.less(x1, tmp_sum) # x1 < x2 + x3
np.add(x1, x3, out=tmp_sum)
buf = np.less(x2, tmp_sum) # x2 < x1 + x3
np.logical_and(res, buf, out=res)
np.add(x1, x2, out=tmp_sum)
np.less(x3, tmp_sum, out=buf) # x3 < x1 + x2
np.logical_and(res, buf, out=res)
return res
def mc_dist(rs, n):
"""
Monte Carlo estimate of probability on sample of size `n`, using given
random state object `rs`
"""
ws = np.sort(rs.rand(2, n), axis=0)
x2 = np.empty(n, dtype=np.double)
x3 = np.empty(n, dtype=np.double)
x1 = ws[0]
np.subtract(ws[1], ws[0], out=x2)
np.subtract(1, ws[1], out=x3)
mc_prob = triangle_inequality(x1, x2, x3).sum() / n
return mc_prob
def init_worker(w_rs, barrier=None):
"""Assign process local random state variable `rs` the given value"""
assert "rs" not in globals(), (
"Here comes trouble. Process is not "
"expected to have global variable `rs`"
)
global rs
rs = w_rs
# wait to ensure that the assignment takes place for each worker
barrier.wait()
def worker_compute(w_id, batch_size=None):
return mc_dist(rs, batch_size)
if __name__ == "__main__":
import multiprocessing as mp
from functools import partial
from timeit import default_timer as timer
seed = 77777
n_workers = 12
batch_size = 1024 * 256
batches = 10000
print("Parallel Monte-Carlo estimation of stick triangle probability")
print(
f"Parameters: n_workers={n_workers}, batch_size={batch_size}, "
f"n_batches={batches}, seed={seed}"
)
print("")
t0 = timer()
# Create instances of RandomState for each worker process from MT2203
# family of generators
rss = [
rnd.RandomState(seed, brng=("MT2203", idx)) for idx in range(n_workers)
]
with mp.Manager() as manager:
# use of Barrier ensures that every worker gets one
b = manager.Barrier(n_workers)
with mp.Pool(processes=n_workers) as pool:
# map over every worker once to distribute RandomState instances
pool.map(partial(init_worker, barrier=b), rss, chunksize=1)
# Perform computations on workers
r = pool.map(
partial(worker_compute, batch_size=batch_size),
range(batches),
chunksize=1,
)
# retrieve values of estimates into numpy array
ps = np.fromiter(r, dtype=np.double)
# compute sample estimator's mean and standard deviation
p_est = ps.mean()
pop_std = ps.std()
t1 = timer()
dig = 3 - int(np.log10(pop_std))
frm_str = "{0:0." + str(dig) + "f}"
print(("Monte-Carlo estimate of probability: " + frm_str).format(p_est))
print(
(
"Population estimate of the estimator's standard deviation: "
+ frm_str
).format(pop_std)
)
print(
("Expected standard deviation of the estimator: " + frm_str).format(
np.sqrt(p_est * (1 - p_est) / batch_size)
)
)
print("Execution time: {0:0.3f} seconds".format(t1 - t0))