-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstats.py
executable file
·79 lines (71 loc) · 1.98 KB
/
stats.py
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
#!/usr/bin/env python3
import math
import random
def coumpound_all_prob(prob_list):
total_prob = 0.0
l = len(prob_list)
if(l==1):
return prob_list[0]
elif(l==2):
return 2.0*prob_list[0]*prob_list[1]
# else recursive step
for i, el in enumerate(prob_list):
# create sublist
sl = prob_list[:]
sl.pop(i)
total_prob += el * coumpound_all_prob(sl)
return total_prob
def coumpound_all_prob_fact(prob_list, tries):
if(tries < len(prob_list)):
return None
total_prob = 1.0
for i in prob_list:
total_prob *= i
accum_mult = 0.0
for i in range(tries - len(prob_list)):
for j in prob_list:
accum_mult += math.pow(j, 1.0+i)
fact_num = math.factorial(tries) / math.factorial(1+tries-len(prob_list))
print(total_prob, accum_mult, tries, fact_num)
return accum_mult * total_prob * fact_num
def single_run_mc(prob_list, tries):
# build a simple list with cumulated values
accum = 0.0
a_pl = []
res_pl = []
for i in prob_list:
accum += i
a_pl.append(accum)
res_pl.append(0)
#print(a_pl)
for i in range(tries):
cr = random.randint(1, 1000000)/1000000.0
#print(cr)
for j in range(len(a_pl)):
if cr <= a_pl[j]:
res_pl[j] = 1
break
#print(res_pl)
accum = 0
for i in res_pl:
accum += i
#print(accum >= len(res_pl), "\n")
return accum >= len(res_pl)
def coumpound_all_prob_mc(prob_list, tries):
if(tries < len(prob_list)):
return None
total_ok = 0
samples = 1000000
i = 0
while i < samples:
total_ok += 1 if single_run_mc(prob_list, tries) else 0
i += 1
return 1.0 * total_ok / samples
def main():
mv = [0.3872, 0.3872, 0.2256]
#mv = [0.1, 0.1, 0.1]
n_tries = 5
print(coumpound_all_prob_fact(mv, n_tries))
print(coumpound_all_prob_mc(mv, n_tries))
if __name__ == "__main__":
main()