Akshay Agrawal commited on
Commit
9489c61
·
unverified ·
2 Parent(s): 90cc7e2 047ff93

Merge pull request #32 from marimo-team/haleshot/03_probability_of_Or

Browse files
Files changed (1) hide show
  1. probability/03_probability_of_or.py +356 -0
probability/03_probability_of_or.py ADDED
@@ -0,0 +1,356 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # /// script
2
+ # requires-python = ">=3.10"
3
+ # dependencies = [
4
+ # "marimo",
5
+ # "matplotlib",
6
+ # "matplotlib-venn"
7
+ # ]
8
+ # ///
9
+
10
+ import marimo
11
+
12
+ __generated_with = "0.11.2"
13
+ app = marimo.App(width="medium")
14
+
15
+
16
+ @app.cell
17
+ def _():
18
+ import marimo as mo
19
+ return (mo,)
20
+
21
+
22
+ @app.cell
23
+ def _():
24
+ import matplotlib.pyplot as plt
25
+ from matplotlib_venn import venn2
26
+ import numpy as np
27
+ return np, plt, venn2
28
+
29
+
30
+ @app.cell(hide_code=True)
31
+ def _(mo):
32
+ mo.md(
33
+ r"""
34
+ # Probability of Or
35
+
36
+ When calculating the probability of either one event _or_ another occurring, we need to be careful about how we combine probabilities. The method depends on whether the events can happen together[<sup>1</sup>](https://chrispiech.github.io/probabilityForComputerScientists/en/part1/prob_or/).
37
+
38
+ Let's explore how to calculate $P(E \cup F)$, i.e. $P(E \text{ or } F)$, in different scenarios.
39
+ """
40
+ )
41
+ return
42
+
43
+
44
+ @app.cell(hide_code=True)
45
+ def _(mo):
46
+ mo.md(
47
+ r"""
48
+ ## Mutually Exclusive Events
49
+
50
+ Two events $E$ and $F$ are **mutually exclusive** if they cannot occur simultaneously.
51
+ In set notation, this means:
52
+
53
+ $E \cap F = \emptyset$
54
+
55
+ For example:
56
+
57
+ - Rolling an even number (2,4,6) vs rolling an odd number (1,3,5)
58
+ - Drawing a heart vs drawing a spade from a deck
59
+ - Passing vs failing a test
60
+
61
+ Here's a Python function to check if two sets of outcomes are mutually exclusive:
62
+ """
63
+ )
64
+ return
65
+
66
+
67
+ @app.cell
68
+ def _():
69
+ def are_mutually_exclusive(event1, event2):
70
+ return len(event1.intersection(event2)) == 0
71
+
72
+ # Example with dice rolls
73
+ even_numbers = {2, 4, 6}
74
+ odd_numbers = {1, 3, 5}
75
+ prime_numbers = {2, 3, 5, 7}
76
+ return are_mutually_exclusive, even_numbers, odd_numbers, prime_numbers
77
+
78
+
79
+ @app.cell
80
+ def _(are_mutually_exclusive, even_numbers, odd_numbers):
81
+ are_mutually_exclusive(even_numbers, odd_numbers)
82
+ return
83
+
84
+
85
+ @app.cell
86
+ def _(are_mutually_exclusive, even_numbers, prime_numbers):
87
+ are_mutually_exclusive(even_numbers, prime_numbers)
88
+ return
89
+
90
+
91
+ @app.cell(hide_code=True)
92
+ def _(mo):
93
+ mo.md(
94
+ r"""
95
+ ## Or with Mutually Exclusive Events
96
+
97
+ For mutually exclusive events, the probability of either event occurring is simply the sum of their individual probabilities:
98
+
99
+ $P(E \cup F) = P(E) + P(F)$
100
+
101
+ This extends to multiple events. For $n$ mutually exclusive events $E_1, E_2, \ldots, E_n$:
102
+
103
+ $P(E_1 \cup E_2 \cup \cdots \cup E_n) = \sum_{i=1}^n P(E_i)$
104
+
105
+ Let's implement this calculation:
106
+ """
107
+ )
108
+ return
109
+
110
+
111
+ @app.cell
112
+ def _():
113
+ def prob_union_mutually_exclusive(probabilities):
114
+ return sum(probabilities)
115
+
116
+ # Example: Rolling a die
117
+ # P(even) = P(2) + P(4) + P(6)
118
+ p_even_mutually_exclusive = prob_union_mutually_exclusive([1/6, 1/6, 1/6])
119
+ print(f"P(rolling an even number) = {p_even_mutually_exclusive}")
120
+
121
+ # P(prime) = P(2) + P(3) + P(5)
122
+ p_prime_mutually_exclusive = prob_union_mutually_exclusive([1/6, 1/6, 1/6])
123
+ print(f"P(rolling a prime number) = {p_prime_mutually_exclusive}")
124
+ return (
125
+ p_even_mutually_exclusive,
126
+ p_prime_mutually_exclusive,
127
+ prob_union_mutually_exclusive,
128
+ )
129
+
130
+
131
+ @app.cell(hide_code=True)
132
+ def _(mo):
133
+ mo.md(
134
+ r"""
135
+ ## Or with Non-Mutually Exclusive Events
136
+
137
+ When events can occur together, we need to use the **inclusion-exclusion principle**:
138
+
139
+ $P(E \cup F) = P(E) + P(F) - P(E \cap F)$
140
+
141
+ Why subtract $P(E \cap F)$? Because when we add $P(E)$ and $P(F)$, we count the overlap twice!
142
+
143
+ For example, consider calculating $P(\text{prime or even})$ when rolling a die:
144
+
145
+ - Prime numbers: {2, 3, 5}
146
+ - Even numbers: {2, 4, 6}
147
+ - The number 2 is counted twice unless we subtract its probability
148
+
149
+ Here's how to implement this calculation:
150
+ """
151
+ )
152
+ return
153
+
154
+
155
+ @app.cell
156
+ def _():
157
+ def prob_union_general(p_a, p_b, p_intersection):
158
+ """Calculate probability of union for any two events"""
159
+ return p_a + p_b - p_intersection
160
+
161
+ # Example: Rolling a die
162
+ # P(prime or even)
163
+ p_prime_general = 3/6 # P(prime) = P(2,3,5)
164
+ p_even_general = 3/6 # P(even) = P(2,4,6)
165
+ p_intersection = 1/6 # P(intersection) = P(2)
166
+
167
+ result = prob_union_general(p_prime_general, p_even_general, p_intersection)
168
+ print(f"P(prime or even) = {p_prime_general} + {p_even_general} - {p_intersection} = {result}")
169
+ return (
170
+ p_even_general,
171
+ p_intersection,
172
+ p_prime_general,
173
+ prob_union_general,
174
+ result,
175
+ )
176
+
177
+
178
+ @app.cell(hide_code=True)
179
+ def _(mo):
180
+ mo.md(
181
+ r"""
182
+ ### Extension to Three Events
183
+
184
+ For three events, the inclusion-exclusion principle becomes:
185
+
186
+ $P(E_1 \cup E_2 \cup E_3) = P(E_1) + P(E_2) + P(E_3)$
187
+ $- P(E_1 \cap E_2) - P(E_1 \cap E_3) - P(E_2 \cap E_3)$
188
+ $+ P(E_1 \cap E_2 \cap E_3)$
189
+
190
+ The pattern is:
191
+
192
+ 1. Add individual probabilities
193
+ 2. Subtract probabilities of pairs
194
+ 3. Add probability of triple intersection
195
+ """
196
+ )
197
+ return
198
+
199
+
200
+ @app.cell(hide_code=True)
201
+ def _(mo):
202
+ mo.md(r"""### Interactive example:""")
203
+ return
204
+
205
+
206
+ @app.cell
207
+ def _(event_type):
208
+ event_type
209
+ return
210
+
211
+
212
+ @app.cell(hide_code=True)
213
+ def _(mo):
214
+ # Create a dropdown to select the type of events to visualize
215
+ event_type = mo.ui.dropdown(
216
+ options=[
217
+ "Mutually Exclusive Events (Rolling Odd vs Even)",
218
+ "Non-Mutually Exclusive Events (Prime vs Even)",
219
+ "Three Events (Less than 3, Even, Prime)"
220
+ ],
221
+ value="Mutually Exclusive Events (Rolling Odd vs Even)",
222
+ label="Select Event Type"
223
+ )
224
+ return (event_type,)
225
+
226
+
227
+ @app.cell(hide_code=True)
228
+ def _(event_type, mo, plt, venn2):
229
+ # Define the events and their probabilities
230
+ events_data = {
231
+ "Mutually Exclusive Events (Rolling Odd vs Even)": {
232
+ "sets": (round(3/6, 2), round(3/6, 2), 0), # (odd, even, intersection)
233
+ "labels": ("Odd\n{1,3,5}", "Even\n{2,4,6}"),
234
+ "title": "Mutually Exclusive Events: Odd vs Even Numbers",
235
+ "explanation": r"""
236
+ ### Mutually Exclusive Events
237
+
238
+ $P(\text{Odd}) = \frac{3}{6} = 0.5$
239
+
240
+ $P(\text{Even}) = \frac{3}{6} = 0.5$
241
+
242
+ $P(\text{Odd} \cap \text{Even}) = 0$
243
+
244
+ $P(\text{Odd} \cup \text{Even}) = P(\text{Odd}) + P(\text{Even}) = 1$
245
+
246
+ These events are mutually exclusive because a number cannot be both odd and even.
247
+ """
248
+ },
249
+ "Non-Mutually Exclusive Events (Prime vs Even)": {
250
+ "sets": (round(2/6, 2), round(2/6, 2), round(1/6, 2)), # (prime-only, even-only, intersection)
251
+ "labels": ("Prime\n{3,5}", "Even\n{4,6}"),
252
+ "title": "Non-Mutually Exclusive: Prime vs Even Numbers",
253
+ "explanation": r"""
254
+ ### Non-Mutually Exclusive Events
255
+
256
+ $P(\text{Prime}) = \frac{3}{6} = 0.5$ (2,3,5)
257
+
258
+ $P(\text{Even}) = \frac{3}{6} = 0.5$ (2,4,6)
259
+
260
+ $P(\text{Prime} \cap \text{Even}) = \frac{1}{6}$ (2)
261
+
262
+ $P(\text{Prime} \cup \text{Even}) = \frac{3}{6} + \frac{3}{6} - \frac{1}{6} = \frac{5}{6}$
263
+
264
+ These events overlap because 2 is both prime and even.
265
+ """
266
+ },
267
+ "Three Events (Less than 3, Even, Prime)": {
268
+ "sets": (round(1/6, 2), round(2/6, 2), round(1/6, 2)), # (less than 3, even, intersection)
269
+ "labels": ("<3\n{1,2}", "Even\n{2,4,6}"),
270
+ "title": "Complex Example: Numbers < 3 and Even Numbers",
271
+ "explanation": r"""
272
+ ### Complex Event Interaction
273
+
274
+ $P(x < 3) = \frac{2}{6}$ (1,2)
275
+
276
+ $P(\text{Even}) = \frac{3}{6}$ (2,4,6)
277
+
278
+ $P(x < 3 \cap \text{Even}) = \frac{1}{6}$ (2)
279
+
280
+ $P(x < 3 \cup \text{Even}) = \frac{2}{6} + \frac{3}{6} - \frac{1}{6} = \frac{4}{6}$
281
+
282
+ The number 2 belongs to both sets, requiring the inclusion-exclusion principle.
283
+ """
284
+ }
285
+ }
286
+
287
+ # Get data for selected event type
288
+ data = events_data[event_type.value]
289
+
290
+ # Create visualization
291
+ plt.figure(figsize=(10, 5))
292
+ v = venn2(subsets=data["sets"],
293
+ set_labels=data["labels"])
294
+ plt.title(data["title"])
295
+
296
+ # Display explanation alongside visualization
297
+ mo.hstack([
298
+ plt.gcf(),
299
+ mo.md(data["explanation"])
300
+ ])
301
+ return data, events_data, v
302
+
303
+
304
+ @app.cell(hide_code=True)
305
+ def _(mo):
306
+ mo.md(
307
+ r"""
308
+ ## 🤔 Test Your Understanding
309
+
310
+ Consider rolling a six-sided die. Which of these statements are true?
311
+
312
+ <details>
313
+ <summary>1. P(even or less than 3) = P(even) + P(less than 3)</summary>
314
+
315
+ ❌ Incorrect! These events are not mutually exclusive (2 is both even and less than 3).
316
+ We need to use the inclusion-exclusion principle.
317
+ </details>
318
+
319
+ <details>
320
+ <summary>2. P(even or greater than 4) = 4/6</summary>
321
+
322
+ ✅ Correct! {2,4,6} ∪ {5,6} = {2,4,5,6}, so probability is 4/6.
323
+ </details>
324
+
325
+ <details>
326
+ <summary>3. P(prime or odd) = 5/6</summary>
327
+
328
+ ✅ Correct! {2,3,5} ∪ {1,3,5} = {1,2,3,5}, so probability is 5/6.
329
+ </details>
330
+ """
331
+ )
332
+ return
333
+
334
+
335
+ @app.cell(hide_code=True)
336
+ def _(mo):
337
+ mo.md(
338
+ """
339
+ ## Summary
340
+
341
+ You've learned:
342
+
343
+ - How to identify mutually exclusive events
344
+ - The addition rule for mutually exclusive events
345
+ - The inclusion-exclusion principle for overlapping events
346
+ - How to extend these concepts to multiple events
347
+
348
+ In the next lesson, we'll explore **conditional probability** - how the probability
349
+ of one event changes when we know another event has occurred.
350
+ """
351
+ )
352
+ return
353
+
354
+
355
+ if __name__ == "__main__":
356
+ app.run()