-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdemo.py
executable file
·91 lines (61 loc) · 1.79 KB
/
demo.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
79
80
81
82
83
84
85
86
87
88
89
90
91
#!/usr/bin/env python
""" Empirically simulate the birthday paradox
The birthday paradox is the surprisingly high change that two
people in a group of people share the same birthday.
See https://en.wikipedia.org/wiki/Birthday_problem for details.
"""
import random
from matplotlib import pyplot as plt
import numpy as np
NUMBER_OF_SIMULATIONS = 100000
def simulate_number_of_people_in_room_before_collision():
"""
:return: Number of people added to room to achieve first collision
:rtype: int
"""
unique_birthdays = set()
all_birthdays = []
while True:
birthday_ordinal = random.randint(0, 365 - 1)
unique_birthdays.add(birthday_ordinal)
all_birthdays.append(birthday_ordinal)
if len(unique_birthdays) != len(all_birthdays):
return len(all_birthdays)
def generate_histogram(s):
plt.hist(
s,
bins=xrange(max(s)),
color='green',
)
plt.ylabel('Number of simulations')
plt.xlabel('Number of people in room when first birthday collision occurs')
plt.axvline(
float(sum(s)) / NUMBER_OF_SIMULATIONS,
color='red',
linewidth=2,
linestyle='--'
)
plt.show()
def generate_cdf(s):
normed_counts, bin_edges = np.histogram(s, bins=max(s), normed=True)
cdf = np.cumsum(normed_counts)
plt.plot(
bin_edges[1:],
cdf,
linewidth=2,
)
plt.ylabel('CDF')
plt.xlabel('Number of people in room when first birthday collision occurs')
plt.axhline(
.5,
color='red',
linewidth=2,
linestyle='--'
)
plt.show()
simulations = [
simulate_number_of_people_in_room_before_collision()
for _ in xrange(0, NUMBER_OF_SIMULATIONS)
]
generate_histogram(simulations)
generate_cdf(simulations)