|
27 | 27 | "wicked-roberts", |
28 | 28 | "close-trinity" |
29 | 29 | ], |
| 30 | + "toc_visible": true, |
30 | 31 | "include_colab_link": true |
31 | 32 | } |
32 | 33 | }, |
|
47 | 48 | "id": "painted-boutique" |
48 | 49 | }, |
49 | 50 | "source": [ |
50 | | - "## Activity: We have a coin. The probability of getting Head is P(H) = a and probability of getting Tail is P(T) = 1 - a.\n", |
| 51 | + "## Activity 17: Modelling a Fair Coin\n", |
51 | 52 | "\n", |
52 | | - "- We toss this coin three times. What is the probability that we get two heads?\n", |
| 53 | + "Let's say your friend gives you a fair coin. \n", |
53 | 54 | "\n", |
54 | | - "- Calculate this probability by hand [TODO]: add a binary tree to show how it is 3/8\n", |
| 55 | + "Let $P(H)$ = a, the probability of getting heads when you flip the coin. Of course, this also makes the probability of getting tails, $P(T)$, equal to 1 - a.\n", |
55 | 56 | "\n", |
56 | | - "- Write a code that shows you are correct. Your function should take two input arguments: \n", |
57 | | - " - Given probability of Head \n", |
58 | | - " - Given number of trials\n", |
59 | | - " \n", |
60 | | - "- For the coding part, we examine the coin flip for 1000 trials, each trial is tossing the coin 3 times.\n", |
61 | | - " - Then only count (C) if the result for each trial be HHT or HTH or THH. Therefore, C/1000 is the answer\n", |
62 | | - "\n", |
63 | | - "- Hint: For code part, we can generate events with given probability as follows:\n", |
| 57 | + "Now, say we toss this coin three times. What is the probability that we get heads on two out of the three flips?\n", |
64 | 58 | "\n", |
65 | | - "TODO: rephrase this question" |
| 59 | + "Calculate this value by hand, and then please write Python code to verify your answer on 1,000 coin flips (hint: use the `random` module)!\n", |
| 60 | + "\n" |
66 | 61 | ], |
67 | 62 | "id": "painted-boutique" |
68 | 63 | }, |
| 64 | + { |
| 65 | + "cell_type": "markdown", |
| 66 | + "metadata": { |
| 67 | + "id": "GEgcbkprcQnM" |
| 68 | + }, |
| 69 | + "source": [ |
| 70 | + "### The Mathematical Solution\n", |
| 71 | + "\n", |
| 72 | + "Let's start by breaking down the probabilities of 1 coin flip. As you may already know, this only has 2 outcomes, each of which are equally likely: heads or tails.\n", |
| 73 | + "\n", |
| 74 | + "To give a visual, observe the following diagram which shows the possible outcomes for 1 random coin toss:\n", |
| 75 | + "\n", |
| 76 | + "<img src=\"https://i.postimg.cc/FznFdxdm/Screen-Shot-2021-05-31-at-3-59-29-PM.png\" alt=\"1 random coin toss\" height=\"225px\" width=\"350px\">\n", |
| 77 | + "\n", |
| 78 | + "So how many ways can we have 2 heads on 3 of the coin flips?\n", |
| 79 | + "\n", |
| 80 | + "As more and more flips happen, we will continue to have either heads or tails. Essentially, this means our original diagram can be expanded, and to show all the possibile results we could have:\n", |
| 81 | + "\n", |
| 82 | + "<img src=\"https://i.postimg.cc/G3vZ4KDj/Screen-Shot-2021-05-31-at-4-10-13-PM.png\" alt=\"3 random coin tosses\" height=\"225px\" width=\"355px\">\n", |
| 83 | + "\n", |
| 84 | + "As you can see, there are 8 total permutations of what 3 faces our coin will land on. As well, there are 3 in particular which have 2 heads: HHT, HTH, and THH (try to spot them in the diagram above, in case you are not convinced)!\n", |
| 85 | + "\n", |
| 86 | + "Since all of the 8 permutations are equally likely to occur, we can then conclude our probability is `3/8`, or 0.375." |
| 87 | + ], |
| 88 | + "id": "GEgcbkprcQnM" |
| 89 | + }, |
| 90 | + { |
| 91 | + "cell_type": "markdown", |
| 92 | + "metadata": { |
| 93 | + "id": "sN8IlbOFcVOj" |
| 94 | + }, |
| 95 | + "source": [ |
| 96 | + "### The Python Solution\n", |
| 97 | + "\n", |
| 98 | + "To model this experiment in code, one approach we can take is:\n", |
| 99 | + "\n", |
| 100 | + "```\n", |
| 101 | + "Pseudocode for Modelling a Fair Coin\n", |
| 102 | + "1. Examine the results of 1000 trials, where each trial is defined tossing the coin 3 times.\n", |
| 103 | + "2. Then, we can keep track of an integer called `occurences` to record the number of trials in which we have one of our desired permutations (i.e. either HHT, HTH, or THH).\n", |
| 104 | + "3. Finally, we can compute the answer by dividing the number of trials where our desired outcome occurred, by the total number of trials: `occurences/1000`.\n", |
| 105 | + "```\n", |
| 106 | + "Before actually writing this function though, you may still have several questions. For example, how are we supposed to generate random events in Python?" |
| 107 | + ], |
| 108 | + "id": "sN8IlbOFcVOj" |
| 109 | + }, |
| 110 | + { |
| 111 | + "cell_type": "markdown", |
| 112 | + "metadata": { |
| 113 | + "id": "YoPPT-ZzgydJ" |
| 114 | + }, |
| 115 | + "source": [ |
| 116 | + "#### Mini-Lesson: How to Simulate Random Events in Python\n", |
| 117 | + "\n", |
| 118 | + "The following code snippet provides a straightforward process you can follow for modelling probability. For more on the `random.choices` function, you may read more on the [Python documentation](https://docs.python.org/3/library/random.html?highlight=random#random.choices) as you wish." |
| 119 | + ], |
| 120 | + "id": "YoPPT-ZzgydJ" |
| 121 | + }, |
69 | 122 | { |
70 | 123 | "cell_type": "code", |
71 | 124 | "metadata": { |
72 | 125 | "colab": { |
73 | 126 | "base_uri": "https://localhost:8080/" |
74 | 127 | }, |
75 | 128 | "id": "english-federation", |
76 | | - "outputId": "4d419a11-ba33-4962-e36e-124834c3ae5c" |
| 129 | + "outputId": "cc8243fd-5a2d-4e39-8f87-5f587f338ad8" |
77 | 130 | }, |
78 | 131 | "source": [ |
79 | | - "# TODO: add \"Mini-Lesson: How to Simulate Random Events in Python\" cell above\n", |
80 | 132 | "from random import choices\n", |
81 | 133 | "\n", |
82 | | - "event = ['H', 'T'] # these are the events that can occur\n", |
83 | | - "weights = [0.3, 0.7] # these are their corresp\n", |
84 | | - "for _ in range(10): # run 10 trials of the event\n", |
| 134 | + "# A: first, define the possible outcomes that can occur\n", |
| 135 | + "event = ['H', 'T'] \n", |
| 136 | + "# B: then, define their respective probabilities\n", |
| 137 | + "weights = [0.3, 0.7] \n", |
| 138 | + "# C: run the simulation!\n", |
| 139 | + "for _ in range(10): \n", |
| 140 | + " # D: print the results, to verify it follows our assigned probabilities\n", |
85 | 141 | " print(choices(event, weights))" |
86 | 142 | ], |
87 | 143 | "id": "english-federation", |
88 | | - "execution_count": 2, |
| 144 | + "execution_count": 46, |
89 | 145 | "outputs": [ |
90 | 146 | { |
91 | 147 | "output_type": "stream", |
92 | 148 | "text": [ |
93 | 149 | "['T']\n", |
| 150 | + "['H']\n", |
94 | 151 | "['T']\n", |
95 | 152 | "['T']\n", |
96 | 153 | "['T']\n", |
97 | 154 | "['T']\n", |
98 | | - "['H']\n", |
99 | | - "['H']\n", |
100 | 155 | "['T']\n", |
101 | 156 | "['T']\n", |
| 157 | + "['H']\n", |
102 | 158 | "['H']\n" |
103 | 159 | ], |
104 | 160 | "name": "stdout" |
105 | 161 | } |
106 | 162 | ] |
107 | 163 | }, |
| 164 | + { |
| 165 | + "cell_type": "markdown", |
| 166 | + "metadata": { |
| 167 | + "id": "89D-ZjmTgvym" |
| 168 | + }, |
| 169 | + "source": [ |
| 170 | + "#### Code Implementation\n", |
| 171 | + "\n", |
| 172 | + "From above, we know modelling a random event needs 3 parameters:\n", |
| 173 | + "1. The possible events that can happen\n", |
| 174 | + "2. The probabilities of each of those events\n", |
| 175 | + "3. The number of trials to simulate\n", |
| 176 | + "\n", |
| 177 | + "For this solution, let's leave out 1, since we already know this is for a fair coin.\n", |
| 178 | + "\n", |
| 179 | + "Therefore, our function only needs to take in the remaining two input arguments: \n", |
| 180 | + "1. The probability of landing on heads (since we can already compute the $P(T)$ with that information),\n", |
| 181 | + "2. And the number of trials to run.\n", |
| 182 | + " \n", |
| 183 | + "Using the pseudocode above and what we now know about the `random` module, we can therefore implement something like the code below to see if our mathematical answer is truly correct:" |
| 184 | + ], |
| 185 | + "id": "89D-ZjmTgvym" |
| 186 | + }, |
108 | 187 | { |
109 | 188 | "cell_type": "code", |
110 | 189 | "metadata": { |
111 | | - "colab": { |
112 | | - "base_uri": "https://localhost:8080/" |
113 | | - }, |
114 | | - "id": "academic-bankruptcy", |
115 | | - "outputId": "cdb99386-3594-4dd3-b158-ce39f6ad2a0c" |
| 190 | + "id": "academic-bankruptcy" |
116 | 191 | }, |
117 | 192 | "source": [ |
118 | 193 | "def compute_proba(probability_heads, trials):\n", |
|
124 | 199 | " for _ in range(trials):\n", |
125 | 200 | " # C: check the results after 1 trial\n", |
126 | 201 | " trial_results = [\n", |
127 | | - " choices(events, weights)[0], # 1st coin toss\n", |
128 | | - " choices(events, weights)[0], # 2nd coin toss\n", |
129 | | - " choices(events, weights)[0] # 3rd coin toss\n", |
| 202 | + " choices(events, weights)[0], # 1st coin flip\n", |
| 203 | + " choices(events, weights)[0], # 2nd coin flip\n", |
| 204 | + " choices(events, weights)[0] # 3rd coin flip\n", |
130 | 205 | " ]\n", |
131 | 206 | " if trial_results.count(\"H\") == 2:\n", |
132 | 207 | " occurences += 1\n", |
133 | 208 | " # D: return the probability\n", |
134 | | - " return (occurences / trials)\n", |
135 | | - "\n", |
136 | | - "N = 10000\n", |
137 | | - "a = 0.6 \n", |
138 | | - "print(compute_proba(a, N)) # computed probability after 1,000 trials\n", |
139 | | - "print(3*(a**2)*(1-a)) # expected probability" |
| 209 | + " return (occurences / trials)\n" |
140 | 210 | ], |
141 | 211 | "id": "academic-bankruptcy", |
142 | | - "execution_count": 12, |
| 212 | + "execution_count": 48, |
| 213 | + "outputs": [] |
| 214 | + }, |
| 215 | + { |
| 216 | + "cell_type": "markdown", |
| 217 | + "metadata": { |
| 218 | + "id": "9BXSOLuTkT5D" |
| 219 | + }, |
| 220 | + "source": [ |
| 221 | + "#### Test Out the Python Solution" |
| 222 | + ], |
| 223 | + "id": "9BXSOLuTkT5D" |
| 224 | + }, |
| 225 | + { |
| 226 | + "cell_type": "code", |
| 227 | + "metadata": { |
| 228 | + "colab": { |
| 229 | + "base_uri": "https://localhost:8080/" |
| 230 | + }, |
| 231 | + "id": "QY52_-8-kXo7", |
| 232 | + "outputId": "1a751a16-7886-4eaa-c969-aa8dab978a98" |
| 233 | + }, |
| 234 | + "source": [ |
| 235 | + "a = 0.5 # the probability of landing on heads \n", |
| 236 | + "num_trials = 1000\n", |
| 237 | + "print(compute_proba(a, num_trials)) # computed probability after 1,000 trials\n", |
| 238 | + "print(3*(a**2)*(1-a)) # expected probability - does it match with the above?" |
| 239 | + ], |
| 240 | + "id": "QY52_-8-kXo7", |
| 241 | + "execution_count": 49, |
143 | 242 | "outputs": [ |
144 | 243 | { |
145 | 244 | "output_type": "stream", |
146 | 245 | "text": [ |
147 | | - "0.4385\n", |
148 | | - "0.43200000000000005\n" |
| 246 | + "0.369\n", |
| 247 | + "0.375\n" |
149 | 248 | ], |
150 | 249 | "name": "stdout" |
151 | 250 | } |
|
0 commit comments