Skip to content

Commit c3809be

Browse files
committed
fix monty hall example
1 parent de03ca3 commit c3809be

1 file changed

Lines changed: 251 additions & 45 deletions

File tree

lec/lec31/monty_hall.ipynb

Lines changed: 251 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"cells": [
33
{
44
"cell_type": "code",
5-
"execution_count": null,
5+
"execution_count": 2,
66
"id": "09843234-d9df-4a83-b17c-5eca5d933238",
77
"metadata": {},
88
"outputs": [],
@@ -26,26 +26,95 @@
2626
},
2727
{
2828
"cell_type": "code",
29-
"execution_count": null,
29+
"execution_count": 3,
3030
"id": "af5a8201-63ae-4b77-9026-196fc7e9d59b",
3131
"metadata": {},
32-
"outputs": [],
32+
"outputs": [
33+
{
34+
"data": {
35+
"text/html": [
36+
"<table border=\"1\" class=\"dataframe\">\n",
37+
" <thead>\n",
38+
" <tr>\n",
39+
" <th>Door</th> <th>Prize</th>\n",
40+
" </tr>\n",
41+
" </thead>\n",
42+
" <tbody>\n",
43+
" <tr>\n",
44+
" <td>1 </td> <td>goat 2</td>\n",
45+
" </tr>\n",
46+
" <tr>\n",
47+
" <td>2 </td> <td>goat 1</td>\n",
48+
" </tr>\n",
49+
" <tr>\n",
50+
" <td>3 </td> <td>car </td>\n",
51+
" </tr>\n",
52+
" </tbody>\n",
53+
"</table>"
54+
],
55+
"text/plain": [
56+
"Door | Prize\n",
57+
"1 | goat 2\n",
58+
"2 | goat 1\n",
59+
"3 | car"
60+
]
61+
},
62+
"execution_count": 3,
63+
"metadata": {},
64+
"output_type": "execute_result"
65+
}
66+
],
3367
"source": [
3468
"three_door_prizes = ['car', 'goat 1', 'goat 2']\n",
3569
"def hide(prizes):\n",
3670
" prize_table = Table().with_column('Prize', prizes)\n",
3771
" return (prize_table.sample(with_replacement=False)\n",
3872
" .with_column('Door', np.arange(1, len(prizes) + 1))\n",
3973
" .select('Door', 'Prize'))\n",
74+
" \n",
4075
"hide(three_door_prizes)"
4176
]
4277
},
4378
{
4479
"cell_type": "code",
45-
"execution_count": null,
80+
"execution_count": 4,
4681
"id": "c9aa84bb-5590-4aed-ac05-fe124e19fa68",
4782
"metadata": {},
48-
"outputs": [],
83+
"outputs": [
84+
{
85+
"data": {
86+
"text/html": [
87+
"<table border=\"1\" class=\"dataframe\">\n",
88+
" <thead>\n",
89+
" <tr>\n",
90+
" <th>Door</th> <th>Prize</th> <th>Status</th>\n",
91+
" </tr>\n",
92+
" </thead>\n",
93+
" <tbody>\n",
94+
" <tr>\n",
95+
" <td>1 </td> <td>goat 1</td> <td>Final choice</td>\n",
96+
" </tr>\n",
97+
" <tr>\n",
98+
" <td>2 </td> <td>goat 2</td> <td>Revealed </td>\n",
99+
" </tr>\n",
100+
" <tr>\n",
101+
" <td>3 </td> <td>car </td> <td>Picked first</td>\n",
102+
" </tr>\n",
103+
" </tbody>\n",
104+
"</table>"
105+
],
106+
"text/plain": [
107+
"Door | Prize | Status\n",
108+
"1 | goat 1 | Final choice\n",
109+
"2 | goat 2 | Revealed\n",
110+
"3 | car | Picked first"
111+
]
112+
},
113+
"execution_count": 4,
114+
"metadata": {},
115+
"output_type": "execute_result"
116+
}
117+
],
49118
"source": [
50119
"def pick(t):\n",
51120
" \"\"\"Return the door number of a randomly selected row of a doors table.\"\"\"\n",
@@ -61,30 +130,54 @@
61130
" what_happened = Table().with_columns(\n",
62131
" 'Door', [contestant_initial_choice, revealed, contestant_final_choice],\n",
63132
" 'Status', ['Picked first', 'Revealed', 'Final choice'])\n",
64-
" return doors.join('Door', what_happened, 'Door')"
133+
" return doors.join('Door', what_happened, 'Door')\n",
134+
"\n",
135+
"monty_hall()"
65136
]
66137
},
67138
{
68139
"cell_type": "code",
69-
"execution_count": null,
70-
"id": "93dba6b3-26f3-44e7-a734-c5903b4e1476",
140+
"execution_count": 5,
141+
"id": "8630e911-ddfd-4b3b-8d5b-719ee88b2142",
71142
"metadata": {},
72-
"outputs": [],
143+
"outputs": [
144+
{
145+
"data": {
146+
"text/plain": [
147+
"False"
148+
]
149+
},
150+
"execution_count": 5,
151+
"metadata": {},
152+
"output_type": "execute_result"
153+
}
154+
],
73155
"source": [
74-
"monty_hall()"
156+
"def won_the_car(result_table):\n",
157+
" return result_table.where('Status', 'Final choice').column('Prize').item(0) == 'car'\n",
158+
"\n",
159+
"won_the_car(monty_hall())"
75160
]
76161
},
77162
{
78163
"cell_type": "code",
79-
"execution_count": null,
164+
"execution_count": 6,
80165
"id": "982c4064-0e60-4818-bc47-ccd68fb2107a",
81166
"metadata": {},
82-
"outputs": [],
167+
"outputs": [
168+
{
169+
"name": "stdout",
170+
"output_type": "stream",
171+
"text": [
172+
"66.13 percent cars\n"
173+
]
174+
}
175+
],
83176
"source": [
84177
"trials = 10000\n",
85178
"cars = 0\n",
86179
"for i in np.arange(trials):\n",
87-
" if monty_hall().where('Status', 'Final choice').column('Prize').item(0) == 'car':\n",
180+
" if won_the_car(monty_hall()):\n",
88181
" cars = cars + 1\n",
89182
"print(100 * cars / trials, 'percent cars')"
90183
]
@@ -99,21 +192,138 @@
99192
},
100193
{
101194
"cell_type": "code",
102-
"execution_count": null,
195+
"execution_count": 7,
103196
"id": "bee4e522-e1d5-4da2-b02c-9f81a732c1b3",
104197
"metadata": {},
105-
"outputs": [],
198+
"outputs": [
199+
{
200+
"data": {
201+
"text/html": [
202+
"<table border=\"1\" class=\"dataframe\">\n",
203+
" <thead>\n",
204+
" <tr>\n",
205+
" <th>Door</th> <th>Prize</th>\n",
206+
" </tr>\n",
207+
" </thead>\n",
208+
" <tbody>\n",
209+
" <tr>\n",
210+
" <td>1 </td> <td>goat 1</td>\n",
211+
" </tr>\n",
212+
" <tr>\n",
213+
" <td>2 </td> <td>goat 2</td>\n",
214+
" </tr>\n",
215+
" <tr>\n",
216+
" <td>3 </td> <td>car </td>\n",
217+
" </tr>\n",
218+
" <tr>\n",
219+
" <td>4 </td> <td>goat 3</td>\n",
220+
" </tr>\n",
221+
" </tbody>\n",
222+
"</table>"
223+
],
224+
"text/plain": [
225+
"Door | Prize\n",
226+
"1 | goat 1\n",
227+
"2 | goat 2\n",
228+
"3 | car\n",
229+
"4 | goat 3"
230+
]
231+
},
232+
"execution_count": 7,
233+
"metadata": {},
234+
"output_type": "execute_result"
235+
}
236+
],
106237
"source": [
107238
"four_door_prizes = ['car', 'goat 1', 'goat 2', 'goat 3']\n",
108239
"hide(four_door_prizes)"
109240
]
110241
},
111242
{
112243
"cell_type": "code",
113-
"execution_count": null,
244+
"execution_count": 8,
114245
"id": "bfa79a80-79d9-44ff-b531-59cf51525129",
115246
"metadata": {},
116-
"outputs": [],
247+
"outputs": [
248+
{
249+
"name": "stdout",
250+
"output_type": "stream",
251+
"text": [
252+
"With Lock\n"
253+
]
254+
},
255+
{
256+
"data": {
257+
"text/html": [
258+
"<table border=\"1\" class=\"dataframe\">\n",
259+
" <thead>\n",
260+
" <tr>\n",
261+
" <th>Door</th> <th>Prize</th> <th>Status</th>\n",
262+
" </tr>\n",
263+
" </thead>\n",
264+
" <tbody>\n",
265+
" <tr>\n",
266+
" <td>1 </td> <td>car </td> <td>Locked </td>\n",
267+
" </tr>\n",
268+
" <tr>\n",
269+
" <td>2 </td> <td>goat 2</td> <td>Picked first</td>\n",
270+
" </tr>\n",
271+
" <tr>\n",
272+
" <td>3 </td> <td>goat 3</td> <td>Revealed </td>\n",
273+
" </tr>\n",
274+
" <tr>\n",
275+
" <td>4 </td> <td>goat 1</td> <td>Final choice</td>\n",
276+
" </tr>\n",
277+
" </tbody>\n",
278+
"</table>"
279+
],
280+
"text/plain": [
281+
"<IPython.core.display.HTML object>"
282+
]
283+
},
284+
"metadata": {},
285+
"output_type": "display_data"
286+
},
287+
{
288+
"name": "stdout",
289+
"output_type": "stream",
290+
"text": [
291+
"Without Lock\n"
292+
]
293+
},
294+
{
295+
"data": {
296+
"text/html": [
297+
"<table border=\"1\" class=\"dataframe\">\n",
298+
" <thead>\n",
299+
" <tr>\n",
300+
" <th>Door</th> <th>Prize</th> <th>Status</th>\n",
301+
" </tr>\n",
302+
" </thead>\n",
303+
" <tbody>\n",
304+
" <tr>\n",
305+
" <td>1 </td> <td>goat 3</td> <td>Picked first</td>\n",
306+
" </tr>\n",
307+
" <tr>\n",
308+
" <td>2 </td> <td>goat 2</td> <td>Revealed </td>\n",
309+
" </tr>\n",
310+
" <tr>\n",
311+
" <td>3 </td> <td>goat 1</td> <td>Ignored </td>\n",
312+
" </tr>\n",
313+
" <tr>\n",
314+
" <td>4 </td> <td>car </td> <td>Final choice</td>\n",
315+
" </tr>\n",
316+
" </tbody>\n",
317+
"</table>"
318+
],
319+
"text/plain": [
320+
"<IPython.core.display.HTML object>"
321+
]
322+
},
323+
"metadata": {},
324+
"output_type": "display_data"
325+
}
326+
],
117327
"source": [
118328
"def monty_hall_4(lock):\n",
119329
" doors = hide(four_door_prizes)\n",
@@ -141,43 +351,39 @@
141351
" locked, ignored],\n",
142352
" 'Status', ['Picked first', 'Revealed', 'Final choice',\n",
143353
" 'Locked', 'Ignored'])\n",
144-
" return doors.join('Door', what_happened, 'Door')"
145-
]
146-
},
147-
{
148-
"cell_type": "code",
149-
"execution_count": null,
150-
"id": "5ab4f959-6455-4d25-a3ed-ad7a0f545bec",
151-
"metadata": {},
152-
"outputs": [],
153-
"source": [
154-
"monty_hall_4(True)"
155-
]
156-
},
157-
{
158-
"cell_type": "code",
159-
"execution_count": null,
160-
"id": "f7898c1d-41c9-49ba-b07f-0b5d214f1fed",
161-
"metadata": {},
162-
"outputs": [],
163-
"source": [
164-
"monty_hall_4(False)"
354+
" return doors.join('Door', what_happened, 'Door')\n",
355+
"\n",
356+
"print('With Lock')\n",
357+
"monty_hall_4(True).show()\n",
358+
"print('Without Lock')\n",
359+
"monty_hall_4(False).show()"
165360
]
166361
},
167362
{
168363
"cell_type": "code",
169-
"execution_count": null,
364+
"execution_count": 9,
170365
"id": "fb5f52bf-f6e1-4a9e-8791-b366a9242e70",
171366
"metadata": {},
172-
"outputs": [],
367+
"outputs": [
368+
{
369+
"name": "stdout",
370+
"output_type": "stream",
371+
"text": [
372+
"With lock equal to True win percentage is 50.26\n",
373+
"With lock equal to False win percentage is 37.79\n"
374+
]
375+
}
376+
],
173377
"source": [
174378
"trials = 10000\n",
175379
"lock = True\n",
176-
"cars = 0\n",
177-
"for i in np.arange(trials):\n",
178-
" if monty_hall_4(lock).where('Status', 'Final choice').column('Prize').item(0) == 'car':\n",
179-
" cars = cars + 1\n",
180-
"print(100 * cars / trials, 'percent cars')"
380+
"\n",
381+
"for lock in [True, False]:\n",
382+
" cars = 0\n",
383+
" for i in np.arange(trials):\n",
384+
" if won_the_car(monty_hall_4(lock)):\n",
385+
" cars = cars + 1\n",
386+
" print('With lock equal to', lock, 'win percentage is', 100 * cars / trials)"
181387
]
182388
}
183389
],

0 commit comments

Comments
 (0)