ProbabilityStatistics


class ProbabilityStatistics:

@staticmethod
def simulation_of_probability(v, ratio=10000):
assert v >= 0 and v <= 1
_v = int(v * ratio)
complement = ratio - _v
l = [0 for _ in range(_v)] + [1 for _ in range(complement)]
import random
# TODO numpy scipy
return random.choice(l) == 0


if __name__ == '__main__':
vs = (0.1, 0.5, 0.5123, 0.9)
for v in vs:
for times in (100, 1000, 10000, 100000):
ok = 0
for i in range(times):
b = ProbabilityStatistics.simulation_of_probability(v)
if b:
ok += 1
print('got-times,run-times,p,target', ok, times, ok / times, v)

got-times,run-times,p,target 14 100 0.14 0.1

got-times,run-times,p,target 83 1000 0.083 0.1

got-times,run-times,p,target 957 10000 0.0957 0.1

got-times,run-times,p,target 10171 100000 0.10171 0.1

got-times,run-times,p,target 42 100 0.42 0.5

got-times,run-times,p,target 524 1000 0.524 0.5

got-times,run-times,p,target 4940 10000 0.494 0.5

got-times,run-times,p,target 50071 100000 0.50071 0.5

got-times,run-times,p,target 55 100 0.55 0.5123

got-times,run-times,p,target 475 1000 0.475 0.5123

got-times,run-times,p,target 5205 10000 0.5205 0.5123

got-times,run-times,p,target 51188 100000 0.51188 0.5123

got-times,run-times,p,target 92 100 0.92 0.9

got-times,run-times,p,target 902 1000 0.902 0.9

got-times,run-times,p,target 9035 10000 0.9035 0.9

got-times,run-times,p,target 90093 100000 0.90093 0.9


class ProbabilityStatistics:

@staticmethod
def simulation_of_probability(v, ratio=10000):
assert v >= 0 and v <= 1
_v = int(v * ratio)
complement = ratio - _v
l = [0 for _ in range(_v)] + [1 for _ in range(complement)]
import random
# TODO numpy scipy
return random.choice(l) == 0


if __name__ == '__main__':
vs = (0.1, 0.5, 0.5123, 0.9)
for v in vs:
for times in (100, 1000, 10000, 100000):
ok = 0
for i in range(times):
b = ProbabilityStatistics.simulation_of_probability(v)
if b:
ok += 1
print('got-times,run-times,p,target', ok, times, ok / times, v)
原文地址:https://www.cnblogs.com/rsapaper/p/13531655.html