|
2 | 2 | "cells": [ |
3 | 3 | { |
4 | 4 | "cell_type": "code", |
5 | | - "execution_count": null, |
| 5 | + "execution_count": 2, |
6 | 6 | "id": "09843234-d9df-4a83-b17c-5eca5d933238", |
7 | 7 | "metadata": {}, |
8 | 8 | "outputs": [], |
|
26 | 26 | }, |
27 | 27 | { |
28 | 28 | "cell_type": "code", |
29 | | - "execution_count": null, |
| 29 | + "execution_count": 3, |
30 | 30 | "id": "af5a8201-63ae-4b77-9026-196fc7e9d59b", |
31 | 31 | "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 | + ], |
33 | 67 | "source": [ |
34 | 68 | "three_door_prizes = ['car', 'goat 1', 'goat 2']\n", |
35 | 69 | "def hide(prizes):\n", |
36 | 70 | " prize_table = Table().with_column('Prize', prizes)\n", |
37 | 71 | " return (prize_table.sample(with_replacement=False)\n", |
38 | 72 | " .with_column('Door', np.arange(1, len(prizes) + 1))\n", |
39 | 73 | " .select('Door', 'Prize'))\n", |
| 74 | + " \n", |
40 | 75 | "hide(three_door_prizes)" |
41 | 76 | ] |
42 | 77 | }, |
43 | 78 | { |
44 | 79 | "cell_type": "code", |
45 | | - "execution_count": null, |
| 80 | + "execution_count": 4, |
46 | 81 | "id": "c9aa84bb-5590-4aed-ac05-fe124e19fa68", |
47 | 82 | "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 | + ], |
49 | 118 | "source": [ |
50 | 119 | "def pick(t):\n", |
51 | 120 | " \"\"\"Return the door number of a randomly selected row of a doors table.\"\"\"\n", |
|
61 | 130 | " what_happened = Table().with_columns(\n", |
62 | 131 | " 'Door', [contestant_initial_choice, revealed, contestant_final_choice],\n", |
63 | 132 | " '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()" |
65 | 136 | ] |
66 | 137 | }, |
67 | 138 | { |
68 | 139 | "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", |
71 | 142 | "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 | + ], |
73 | 155 | "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())" |
75 | 160 | ] |
76 | 161 | }, |
77 | 162 | { |
78 | 163 | "cell_type": "code", |
79 | | - "execution_count": null, |
| 164 | + "execution_count": 6, |
80 | 165 | "id": "982c4064-0e60-4818-bc47-ccd68fb2107a", |
81 | 166 | "metadata": {}, |
82 | | - "outputs": [], |
| 167 | + "outputs": [ |
| 168 | + { |
| 169 | + "name": "stdout", |
| 170 | + "output_type": "stream", |
| 171 | + "text": [ |
| 172 | + "66.13 percent cars\n" |
| 173 | + ] |
| 174 | + } |
| 175 | + ], |
83 | 176 | "source": [ |
84 | 177 | "trials = 10000\n", |
85 | 178 | "cars = 0\n", |
86 | 179 | "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", |
88 | 181 | " cars = cars + 1\n", |
89 | 182 | "print(100 * cars / trials, 'percent cars')" |
90 | 183 | ] |
|
99 | 192 | }, |
100 | 193 | { |
101 | 194 | "cell_type": "code", |
102 | | - "execution_count": null, |
| 195 | + "execution_count": 7, |
103 | 196 | "id": "bee4e522-e1d5-4da2-b02c-9f81a732c1b3", |
104 | 197 | "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 | + ], |
106 | 237 | "source": [ |
107 | 238 | "four_door_prizes = ['car', 'goat 1', 'goat 2', 'goat 3']\n", |
108 | 239 | "hide(four_door_prizes)" |
109 | 240 | ] |
110 | 241 | }, |
111 | 242 | { |
112 | 243 | "cell_type": "code", |
113 | | - "execution_count": null, |
| 244 | + "execution_count": 8, |
114 | 245 | "id": "bfa79a80-79d9-44ff-b531-59cf51525129", |
115 | 246 | "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 | + ], |
117 | 327 | "source": [ |
118 | 328 | "def monty_hall_4(lock):\n", |
119 | 329 | " doors = hide(four_door_prizes)\n", |
|
141 | 351 | " locked, ignored],\n", |
142 | 352 | " 'Status', ['Picked first', 'Revealed', 'Final choice',\n", |
143 | 353 | " '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()" |
165 | 360 | ] |
166 | 361 | }, |
167 | 362 | { |
168 | 363 | "cell_type": "code", |
169 | | - "execution_count": null, |
| 364 | + "execution_count": 9, |
170 | 365 | "id": "fb5f52bf-f6e1-4a9e-8791-b366a9242e70", |
171 | 366 | "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 | + ], |
173 | 377 | "source": [ |
174 | 378 | "trials = 10000\n", |
175 | 379 | "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)" |
181 | 387 | ] |
182 | 388 | } |
183 | 389 | ], |
|
0 commit comments