1+ {
2+ "nbformat" : 4 ,
3+ "nbformat_minor" : 0 ,
4+ "metadata" : {
5+ "colab" : {
6+ "name" : " 05_monte_carlo_bday_solve.ipynb" ,
7+ "provenance" : []
8+ },
9+ "kernelspec" : {
10+ "name" : " python3" ,
11+ "display_name" : " Python 3"
12+ }
13+ },
14+ "cells" : [
15+ {
16+ "cell_type" : " markdown" ,
17+ "metadata" : {
18+ "id" : " rsASi4xKIbOe"
19+ },
20+ "source" : [
21+ " ##Project P5\n " ,
22+ " \n " ,
23+ " **There is a famous problem in statistics that concerns a room full of people: Same Birthday!**\n " ,
24+ " \n " ,
25+ " An instructor offers a prize of $20.00 to anyone who thinks that two people in the room have the same birthday.\n " ,
26+ " \n " ,
27+ " Your assignment is to build a Monte Carlo simulation to tell the instructor how many people need to be in the room to give him/her a better than 50% chance of winning the $20.\n " ,
28+ " \n " ,
29+ " That is to say how many people need to be in a room in order for the probability of two of them having the same birthday is greater than 50%. Do the same for 95% and 99%."
30+ ]
31+ },
32+ {
33+ "cell_type" : " code" ,
34+ "metadata" : {
35+ "id" : " NQRJE-IBADy6"
36+ },
37+ "source" : [
38+ " import numpy as np"
39+ ],
40+ "execution_count" : null ,
41+ "outputs" : []
42+ },
43+ {
44+ "cell_type" : " markdown" ,
45+ "metadata" : {
46+ "id" : " GfQ3DTFRHbrj"
47+ },
48+ "source" : [
49+ " ### Create functions to run simulations"
50+ ]
51+ },
52+ {
53+ "cell_type" : " code" ,
54+ "metadata" : {
55+ "id" : " Micplh7EAIRU"
56+ },
57+ "source" : [
58+ " def mc_bday(x, y):\n " ,
59+ " \n " ,
60+ " \"\"\"\n " ,
61+ " Calculate probability that at least one pair of x people in a room have the same bday\n " ,
62+ " \n " ,
63+ " x (int) = number of people in the room\n " ,
64+ " y (int) = number of simulations to run\n " ,
65+ " \"\"\"\n " ,
66+ " \n " ,
67+ " # hold results from simulations, create list same length of y\n " ,
68+ " results_ls = [0 for x in range(y)]\n " ,
69+ " \n " ,
70+ " # Conduct y simulations \n " ,
71+ " for simulation in range(y):\n " ,
72+ " \n " ,
73+ " # Create list of bdays of size x and sample with replacement\n " ,
74+ " bday_ls = np.random.choice(range(365), size=x, replace=True)\n " ,
75+ " \n " ,
76+ " # Return 1 if matching bdays found else return 0\n " ,
77+ " results_ls[simulation] = [1 if len(bday_ls) != len(set(bday_ls)) else 0][0]\n " ,
78+ " \n " ,
79+ " # Return the mean of the results_ls\n " ,
80+ " return sum(results_ls)/len(results_ls)\n " ,
81+ " \n " ,
82+ " \n " ,
83+ " def find_bdays(probability, num_people, sims):\n " ,
84+ " \"\"\"\n " ,
85+ " Print how many people are required to have the same birthday for a desired probability\n " ,
86+ " \n " ,
87+ " probability (list) = desired list of probabilities\n " ,
88+ " num_people (int) = range of people to try\n " ,
89+ " sims (int) = number of sims to run before determining results of test\n " ,
90+ " \"\"\"\n " ,
91+ " \n " ,
92+ " # use the function to find out how many people it takes to achieve 95% chance of two people having the same birthday\n " ,
93+ " for proba in probability:\n " ,
94+ " print(f\" **How many people need to be in the room for {proba*100}% chance of having the same birthday**\\ n\" )\n " ,
95+ " for x in range(num_people):\n " ,
96+ " \n " ,
97+ " #Run x people and sims simulations to determine average\n " ,
98+ " results = mc_bday(x, sims)\n " ,
99+ " print(f\" Results of {x} people in a room: {results}\" )\n " ,
100+ " \n " ,
101+ " # If results equals probability then stop loop and print results\n " ,
102+ " if results >= proba:\n " ,
103+ " print(f\"\\ n--------------\\ nIt takes {x} people to achieve atleast a {proba*100}% chance of 2 people having the same bday.\" )\n " ,
104+ " print(\" You win $20!\\ n--------------\\ n\" ) \n " ,
105+ " break\n " ,
106+ " else:\n " ,
107+ " pass\n " ,
108+ " return"
109+ ],
110+ "execution_count" : null ,
111+ "outputs" : []
112+ },
113+ {
114+ "cell_type" : " markdown" ,
115+ "metadata" : {
116+ "id" : " Q9tPdUTNGYI4"
117+ },
118+ "source" : [
119+ " ### Run the experiment"
120+ ]
121+ },
122+ {
123+ "cell_type" : " code" ,
124+ "metadata" : {
125+ "colab" : {
126+ "base_uri" : " https://localhost:8080/"
127+ },
128+ "id" : " skQgCopJLepG" ,
129+ "outputId" : " 64898769-71a3-4893-d1e7-11b901f712b0"
130+ },
131+ "source" : [
132+ " # Probabilities of interest\n " ,
133+ " p_ls = [0.50, 0.95, 0.99]\n " ,
134+ " \n " ,
135+ " # Run experiment\n " ,
136+ " find_bdays(p_ls, 100, 5000)"
137+ ],
138+ "execution_count" : null ,
139+ "outputs" : [
140+ {
141+ "output_type" : " stream" ,
142+ "text" : [
143+ " **How many people need to be in the room for 50.0% chance of having the same birthday**\n " ,
144+ " \n " ,
145+ " Results of 0 people in a room: 0.0\n " ,
146+ " Results of 1 people in a room: 0.0\n " ,
147+ " Results of 2 people in a room: 0.0038\n " ,
148+ " Results of 3 people in a room: 0.008\n " ,
149+ " Results of 4 people in a room: 0.015\n " ,
150+ " Results of 5 people in a room: 0.025\n " ,
151+ " Results of 6 people in a room: 0.043\n " ,
152+ " Results of 7 people in a room: 0.0552\n " ,
153+ " Results of 8 people in a room: 0.0796\n " ,
154+ " Results of 9 people in a room: 0.0966\n " ,
155+ " Results of 10 people in a room: 0.122\n " ,
156+ " Results of 11 people in a room: 0.1384\n " ,
157+ " Results of 12 people in a room: 0.1668\n " ,
158+ " Results of 13 people in a room: 0.185\n " ,
159+ " Results of 14 people in a room: 0.2366\n " ,
160+ " Results of 15 people in a room: 0.2516\n " ,
161+ " Results of 16 people in a room: 0.276\n " ,
162+ " Results of 17 people in a room: 0.3178\n " ,
163+ " Results of 18 people in a room: 0.346\n " ,
164+ " Results of 19 people in a room: 0.385\n " ,
165+ " Results of 20 people in a room: 0.4038\n " ,
166+ " Results of 21 people in a room: 0.4526\n " ,
167+ " Results of 22 people in a room: 0.4772\n " ,
168+ " Results of 23 people in a room: 0.5056\n " ,
169+ " \n " ,
170+ " --------------\n " ,
171+ " It takes 23 people to achieve atleast a 50.0% chance of 2 people having the same bday.\n " ,
172+ " You win $20!\n " ,
173+ " --------------\n " ,
174+ " \n " ,
175+ " **How many people need to be in the room for 95.0% chance of having the same birthday**\n " ,
176+ " \n " ,
177+ " Results of 0 people in a room: 0.0\n " ,
178+ " Results of 1 people in a room: 0.0\n " ,
179+ " Results of 2 people in a room: 0.0018\n " ,
180+ " Results of 3 people in a room: 0.0102\n " ,
181+ " Results of 4 people in a room: 0.0144\n " ,
182+ " Results of 5 people in a room: 0.028\n " ,
183+ " Results of 6 people in a room: 0.0402\n " ,
184+ " Results of 7 people in a room: 0.0572\n " ,
185+ " Results of 8 people in a room: 0.0756\n " ,
186+ " Results of 9 people in a room: 0.0972\n " ,
187+ " Results of 10 people in a room: 0.1238\n " ,
188+ " Results of 11 people in a room: 0.1428\n " ,
189+ " Results of 12 people in a room: 0.1744\n " ,
190+ " Results of 13 people in a room: 0.1862\n " ,
191+ " Results of 14 people in a room: 0.2246\n " ,
192+ " Results of 15 people in a room: 0.2502\n " ,
193+ " Results of 16 people in a room: 0.276\n " ,
194+ " Results of 17 people in a room: 0.3192\n " ,
195+ " Results of 18 people in a room: 0.3548\n " ,
196+ " Results of 19 people in a room: 0.3738\n " ,
197+ " Results of 20 people in a room: 0.418\n " ,
198+ " Results of 21 people in a room: 0.4408\n " ,
199+ " Results of 22 people in a room: 0.4718\n " ,
200+ " Results of 23 people in a room: 0.4918\n " ,
201+ " Results of 24 people in a room: 0.5348\n " ,
202+ " Results of 25 people in a room: 0.5614\n " ,
203+ " Results of 26 people in a room: 0.6026\n " ,
204+ " Results of 27 people in a room: 0.6332\n " ,
205+ " Results of 28 people in a room: 0.6562\n " ,
206+ " Results of 29 people in a room: 0.6794\n " ,
207+ " Results of 30 people in a room: 0.7064\n " ,
208+ " Results of 31 people in a room: 0.7282\n " ,
209+ " Results of 32 people in a room: 0.7512\n " ,
210+ " Results of 33 people in a room: 0.7722\n " ,
211+ " Results of 34 people in a room: 0.7886\n " ,
212+ " Results of 35 people in a room: 0.8058\n " ,
213+ " Results of 36 people in a room: 0.8292\n " ,
214+ " Results of 37 people in a room: 0.8502\n " ,
215+ " Results of 38 people in a room: 0.8616\n " ,
216+ " Results of 39 people in a room: 0.8776\n " ,
217+ " Results of 40 people in a room: 0.8904\n " ,
218+ " Results of 41 people in a room: 0.9058\n " ,
219+ " Results of 42 people in a room: 0.9136\n " ,
220+ " Results of 43 people in a room: 0.9294\n " ,
221+ " Results of 44 people in a room: 0.9304\n " ,
222+ " Results of 45 people in a room: 0.9412\n " ,
223+ " Results of 46 people in a room: 0.9542\n " ,
224+ " \n " ,
225+ " --------------\n " ,
226+ " It takes 46 people to achieve atleast a 95.0% chance of 2 people having the same bday.\n " ,
227+ " You win $20!\n " ,
228+ " --------------\n " ,
229+ " \n " ,
230+ " **How many people need to be in the room for 99.0% chance of having the same birthday**\n " ,
231+ " \n " ,
232+ " Results of 0 people in a room: 0.0\n " ,
233+ " Results of 1 people in a room: 0.0\n " ,
234+ " Results of 2 people in a room: 0.002\n " ,
235+ " Results of 3 people in a room: 0.0084\n " ,
236+ " Results of 4 people in a room: 0.015\n " ,
237+ " Results of 5 people in a room: 0.0246\n " ,
238+ " Results of 6 people in a room: 0.0398\n " ,
239+ " Results of 7 people in a room: 0.0592\n " ,
240+ " Results of 8 people in a room: 0.075\n " ,
241+ " Results of 9 people in a room: 0.1002\n " ,
242+ " Results of 10 people in a room: 0.1136\n " ,
243+ " Results of 11 people in a room: 0.1326\n " ,
244+ " Results of 12 people in a room: 0.1714\n " ,
245+ " Results of 13 people in a room: 0.1922\n " ,
246+ " Results of 14 people in a room: 0.2214\n " ,
247+ " Results of 15 people in a room: 0.2506\n " ,
248+ " Results of 16 people in a room: 0.2692\n " ,
249+ " Results of 17 people in a room: 0.3242\n " ,
250+ " Results of 18 people in a room: 0.3366\n " ,
251+ " Results of 19 people in a room: 0.3806\n " ,
252+ " Results of 20 people in a room: 0.4076\n " ,
253+ " Results of 21 people in a room: 0.4432\n " ,
254+ " Results of 22 people in a room: 0.479\n " ,
255+ " Results of 23 people in a room: 0.5076\n " ,
256+ " Results of 24 people in a room: 0.5418\n " ,
257+ " Results of 25 people in a room: 0.5604\n " ,
258+ " Results of 26 people in a room: 0.5868\n " ,
259+ " Results of 27 people in a room: 0.6134\n " ,
260+ " Results of 28 people in a room: 0.648\n " ,
261+ " Results of 29 people in a room: 0.677\n " ,
262+ " Results of 30 people in a room: 0.7074\n " ,
263+ " Results of 31 people in a room: 0.7304\n " ,
264+ " Results of 32 people in a room: 0.76\n " ,
265+ " Results of 33 people in a room: 0.7736\n " ,
266+ " Results of 34 people in a room: 0.795\n " ,
267+ " Results of 35 people in a room: 0.8156\n " ,
268+ " Results of 36 people in a room: 0.8384\n " ,
269+ " Results of 37 people in a room: 0.8514\n " ,
270+ " Results of 38 people in a room: 0.8702\n " ,
271+ " Results of 39 people in a room: 0.8882\n " ,
272+ " Results of 40 people in a room: 0.8872\n " ,
273+ " Results of 41 people in a room: 0.903\n " ,
274+ " Results of 42 people in a room: 0.918\n " ,
275+ " Results of 43 people in a room: 0.9244\n " ,
276+ " Results of 44 people in a room: 0.937\n " ,
277+ " Results of 45 people in a room: 0.9428\n " ,
278+ " Results of 46 people in a room: 0.941\n " ,
279+ " Results of 47 people in a room: 0.9524\n " ,
280+ " Results of 48 people in a room: 0.9604\n " ,
281+ " Results of 49 people in a room: 0.9634\n " ,
282+ " Results of 50 people in a room: 0.969\n " ,
283+ " Results of 51 people in a room: 0.973\n " ,
284+ " Results of 52 people in a room: 0.9766\n " ,
285+ " Results of 53 people in a room: 0.9798\n " ,
286+ " Results of 54 people in a room: 0.9836\n " ,
287+ " Results of 55 people in a room: 0.9866\n " ,
288+ " Results of 56 people in a room: 0.9882\n " ,
289+ " Results of 57 people in a room: 0.992\n " ,
290+ " \n " ,
291+ " --------------\n " ,
292+ " It takes 57 people to achieve atleast a 99.0% chance of 2 people having the same bday.\n " ,
293+ " You win $20!\n " ,
294+ " --------------\n " ,
295+ " \n "
296+ ],
297+ "name" : " stdout"
298+ }
299+ ]
300+ }
301+ ]
302+ }
0 commit comments