Akshay Agrawal commited on
Commit
48f7de2
·
1 Parent(s): 225e16e

Edits to sets notebook

Browse files

* Expand prose, and write in complete sentences. Simplify the language.
* Use code cells as a teaching tool, encouraging users to modify code
and re-run, instead of always relying on UI elements. The focus is on
learning probability with an emphasis on computation, so sometimes the
code should be front and center.
* Add explanatory text for the netflix interactive example.
* Add a subsection on set builder notation, with a code example.

data_science/00.01-set-theory-fundamentals.py DELETED
@@ -1,352 +0,0 @@
1
- # /// script
2
- # requires-python = ">=3.10"
3
- # dependencies = [
4
- # "marimo",
5
- # ]
6
- # ///
7
-
8
- import marimo
9
-
10
- __generated_with = "0.10.17"
11
- app = marimo.App()
12
-
13
-
14
- @app.cell(hide_code=True)
15
- def _(mo):
16
- mo.md(
17
- """
18
- # 🎯 Set Theory: The Building Blocks of Probability
19
-
20
- Welcome to the magical world of sets! Think of sets as the LEGO® blocks of probability -
21
- they're the fundamental pieces we use to build more complex concepts.
22
-
23
- ## What is a Set?
24
-
25
- A set is a collection of distinct objects, called elements or members.
26
-
27
- For example:
28
-
29
- - 🎨 Colors = {red, blue, green}
30
-
31
- - 🔢 Even numbers under 10 = {2, 4, 6, 8}
32
-
33
- - 🐾 Pets = {dog, cat, hamster, fish}
34
- """
35
- )
36
- return
37
-
38
-
39
- @app.cell
40
- def _(elements):
41
- elements
42
- return
43
-
44
-
45
- @app.cell(hide_code=True)
46
- def _(mo):
47
- # interactive set creator
48
- elements = mo.ui.text(
49
- value="🐶,🐱,🐹",
50
- label="Create your own set (use commas to separate elements)"
51
- )
52
- return (elements,)
53
-
54
-
55
- @app.cell(hide_code=True)
56
- def _(elements, mo):
57
- user_set = set(elements.value.split(','))
58
-
59
- mo.md(f"""
60
- ### Your Custom Set:
61
-
62
- ${{{', '.join(user_set)}}}$
63
-
64
- Number of elements: {len(user_set)}
65
- """)
66
- return (user_set,)
67
-
68
-
69
- @app.cell(hide_code=True)
70
- def _(mo):
71
- mo.md(
72
- """
73
- ## 🎮 Set Operations Playground
74
-
75
- Let's explore the three fundamental set operations:
76
-
77
- - Union (∪): Combining sets
78
-
79
- - Intersection (∩): Finding common elements
80
-
81
- - Difference (-): What's unique to one set
82
-
83
- Try creating two sets below!
84
- """
85
- )
86
- return
87
-
88
-
89
- @app.cell
90
- def _(operation):
91
- operation
92
- return
93
-
94
-
95
- @app.cell(hide_code=True)
96
- def _(mo):
97
- set_a = mo.ui.text(value="🍕,🍔,🌭,🍟", label="Set A (Fast Food)")
98
- set_b = mo.ui.text(value="🍕,🥗,🥙,🍟", label="Set B (Healthy-ish Food)")
99
-
100
- operation = mo.ui.dropdown(
101
- options=["Union", "Intersection", "Difference"],
102
- value="Union",
103
- label="Choose Operation"
104
- )
105
- return operation, set_a, set_b
106
-
107
-
108
- @app.cell
109
- def _(mo, operation, set_a, set_b):
110
- A = set(set_a.value.split(','))
111
- B = set(set_b.value.split(','))
112
-
113
- results = {
114
- "Union": (A | B, "∪", "Everything from both sets"),
115
- "Intersection": (A & B, "∩", "Common elements"),
116
- "Difference": (A - B, "-", "In A but not in B")
117
- }
118
-
119
- _result, symbol, description = results[operation.value]
120
-
121
- mo.md(f"""
122
- ### Set Operation Result
123
-
124
- $A {symbol} B = {{{', '.join(_result)}}}$
125
-
126
- **What this means**: {description}
127
-
128
- **Set A**: {', '.join(A)}
129
- **Set B**: {', '.join(B)}
130
- """)
131
- return A, B, description, results, symbol
132
-
133
-
134
- @app.cell(hide_code=True)
135
- def _(mo):
136
- mo.md(
137
- """
138
- ## 🎬 Netflix Shows Example
139
-
140
- Let's use set theory to understand content recommendations!
141
- """
142
- )
143
- return
144
-
145
-
146
- @app.cell
147
- def _(viewer_type):
148
- viewer_type
149
- return
150
-
151
-
152
- @app.cell
153
- def _(mo):
154
- # Netflix genres example
155
- action_fans = {"Stranger Things", "The Witcher", "Money Heist"}
156
- drama_fans = {"The Crown", "Money Heist", "Bridgerton"}
157
-
158
- viewer_type = mo.ui.radio(
159
- options=["New Viewer", "Action Fan", "Drama Fan"],
160
- value="New Viewer",
161
- label="Select Viewer Type"
162
- )
163
- return action_fans, drama_fans, viewer_type
164
-
165
-
166
- @app.cell
167
- def _(action_fans, drama_fans, mo, viewer_type):
168
- recommendations = {
169
- "New Viewer": action_fans | drama_fans, # Union for new viewers
170
- "Action Fan": action_fans - drama_fans, # Unique action shows
171
- "Drama Fan": drama_fans - action_fans # Unique drama shows
172
- }
173
-
174
- result = recommendations[viewer_type.value]
175
-
176
- explanation = {
177
- "New Viewer": "You get everything to explore!",
178
- "Action Fan": "Pure action, no drama!",
179
- "Drama Fan": "Drama-focused selections!"
180
- }
181
-
182
- mo.md(f"""
183
- ### 🎬 Recommended Shows
184
-
185
- Based on your preference for **{viewer_type.value}**, we recommend:
186
-
187
- {', '.join(result)}
188
-
189
- **Why these shows?**
190
- {explanation[viewer_type.value]}
191
- """)
192
- return explanation, recommendations, result
193
-
194
-
195
- @app.cell(hide_code=True)
196
- def _(mo):
197
- mo.md(
198
- """
199
- ## 🧮 Set Properties
200
-
201
- Important properties of sets:
202
-
203
- 1. **Commutative**: A ∪ B = B ∪ A
204
- 2. **Associative**: (A ∪ B) ∪ C = A ∪ (B ∪ C)
205
- 3. **Distributive**: A ∪ (B ∩ C) = (A ∪ B) ∩ (A ∪ C)
206
-
207
- Let's verify these with a fun exercise!
208
- """
209
- )
210
- return
211
-
212
-
213
- @app.cell
214
- def _(mo, property_check, set_size):
215
- mo.hstack([property_check, set_size])
216
- return
217
-
218
-
219
- @app.cell
220
- def _(mo):
221
- # Interactive property verifier
222
- property_check = mo.ui.dropdown(
223
- options=[
224
- "Commutative (Union)",
225
- "Commutative (Intersection)",
226
- "Associative (Union)"
227
- ],
228
- value="Commutative (Union)",
229
- label="Select Property to Verify"
230
- )
231
-
232
- set_size = mo.ui.slider(
233
- value=3,
234
- start=2,
235
- stop=5,
236
- label="Set Size for Testing"
237
- )
238
- return property_check, set_size
239
-
240
-
241
- @app.cell
242
- def _(mo, property_check, set_size):
243
- import random
244
-
245
- # Create random emoji sets for verification
246
- emojis = ["😀", "😎", "🤓", "🤠", "😴", "🤯", "🤪", "😇"]
247
-
248
- set1 = set(random.sample(emojis, set_size.value))
249
- set2 = set(random.sample(emojis, set_size.value))
250
-
251
- operations = {
252
- "Commutative (Union)": (
253
- set1 | set2,
254
- set2 | set1,
255
- "A ∪ B = B ∪ A"
256
- ),
257
- "Commutative (Intersection)": (
258
- set1 & set2,
259
- set2 & set1,
260
- "A ∩ B = B ∩ A"
261
- ),
262
- "Associative (Union)": (
263
- (set1 | set2) | set(random.sample(emojis, set_size.value)),
264
- set1 | (set2 | set(random.sample(emojis, set_size.value))),
265
- "(A ∪ B) ∪ C = A ∪ (B ∪ C)"
266
- )
267
- }
268
-
269
- result1, result2, formula = operations[property_check.value]
270
-
271
- mo.md(f"""
272
- ### Property Verification
273
-
274
- **Testing**: {formula}
275
-
276
- Set A: {', '.join(set1)}
277
- Set B: {', '.join(set2)}
278
-
279
- **Left Side**: {', '.join(result1)}
280
- **Right Side**: {', '.join(result2)}
281
-
282
- **Property holds**: {'✅' if result1 == result2 else '❌'}
283
- """)
284
- return emojis, formula, operations, random, result1, result2, set1, set2
285
-
286
-
287
- @app.cell(hide_code=True)
288
- def _(mo):
289
- quiz = mo.md("""
290
- ## 🎯 Quick Challenge
291
-
292
- Given these sets:
293
-
294
- - A = {🎮, 📱, 💻}
295
-
296
- - B = {📱, 💻, 🖨️}
297
-
298
- - C = {💻, 🖨️, ⌨️}
299
-
300
- Can you:
301
-
302
- 1. Find all elements that are in either A or B
303
-
304
- 2. Find elements common to all three sets
305
-
306
- 3. Find elements in A that aren't in C
307
-
308
- <details>
309
-
310
- <summary>Check your answers!</summary>
311
-
312
- 1. A ∪ B = {🎮, 📱, 💻, 🖨️}<br>
313
- 2. A ∩ B ∩ C = {💻}<br>
314
- 3. A - C = {🎮, 📱}
315
-
316
- </details>
317
- """)
318
-
319
- mo.callout(quiz, kind="info")
320
- return (quiz,)
321
-
322
-
323
- @app.cell(hide_code=True)
324
- def _(mo):
325
- callout_text = mo.md("""
326
- ## 🎯 Set Theory Master in Training!
327
-
328
- You've learned:
329
-
330
- - Basic set operations
331
-
332
- - Set properties
333
-
334
- - Real-world applications
335
-
336
- Coming up next: Axiomatic Probability! 🎲✨
337
-
338
- Remember: In probability, every event is a set, and every set can be an event!
339
- """)
340
-
341
- mo.callout(callout_text, kind="success")
342
- return (callout_text,)
343
-
344
-
345
- @app.cell
346
- def _():
347
- import marimo as mo
348
- return (mo,)
349
-
350
-
351
- if __name__ == "__main__":
352
- app.run()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
probability/00-sets.py ADDED
@@ -0,0 +1,297 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # /// script
2
+ # requires-python = ">=3.10"
3
+ # dependencies = [
4
+ # "marimo",
5
+ # ]
6
+ # ///
7
+
8
+ import marimo
9
+
10
+ __generated_with = "0.11.0"
11
+ app = marimo.App()
12
+
13
+
14
+ @app.cell(hide_code=True)
15
+ def _(mo):
16
+ mo.md(
17
+ r"""
18
+ # Sets
19
+
20
+ Probability is the study of "events", assigning numerical values to how likely
21
+ events are to occur. For example, probability lets us quantify how likely it is for it to rain or shine on a given day.
22
+
23
+
24
+ Typically we reason about _sets_ of events. In mathematics,
25
+ a set is a collection of elements, with no element included more than once.
26
+ Elements can be any kind of object.
27
+
28
+ For example:
29
+
30
+ - ☀️ Weather events: $\{\text{Rain}, \text{Overcast}, \text{Clear}\}$
31
+ - 🎲 Die rolls: $\{1, 2, 3, 4, 5, 6\}$
32
+ - 🪙 Pairs of coin flips = $\{ \text{(Heads, Heads)}, \text{(Heads, Tails)}, \text{(Tails, Tails)} \text{(Tails, Heads)}\}$
33
+
34
+ Sets are the building blocks of probability, and will arise frequently in our study.
35
+ """
36
+ )
37
+ return
38
+
39
+
40
+ @app.cell(hide_code=True)
41
+ def _(mo):
42
+ mo.md(r"""## Set operations""")
43
+ return
44
+
45
+
46
+ @app.cell(hide_code=True)
47
+ def _(mo):
48
+ mo.md(r"""In Python, sets are made with the `set` function:""")
49
+ return
50
+
51
+
52
+ @app.cell
53
+ def _():
54
+ A = set([2, 3, 5, 7])
55
+ A
56
+ return (A,)
57
+
58
+
59
+ @app.cell
60
+ def _():
61
+ B = set([0, 1, 2, 3, 5, 8])
62
+ B
63
+ return (B,)
64
+
65
+
66
+ @app.cell(hide_code=True)
67
+ def _(mo):
68
+ mo.md(
69
+ r"""
70
+ Below we explain common operations on sets.
71
+
72
+ _**Try it!** Try modifying the definitions of `A` and `B` above, and see how the results change below._
73
+
74
+ The **union** $A \cup B$ of sets $A$ and $B$ is the set of elements in $A$, $B$, or both.
75
+ """
76
+ )
77
+ return
78
+
79
+
80
+ @app.cell
81
+ def _(A, B):
82
+ A | B
83
+ return
84
+
85
+
86
+ @app.cell(hide_code=True)
87
+ def _(mo):
88
+ mo.md(r"""The **intersection** $A \cap B$ is the set of elements in both $A$ and $B$""")
89
+ return
90
+
91
+
92
+ @app.cell
93
+ def _(A, B):
94
+ A & B
95
+ return
96
+
97
+
98
+ @app.cell(hide_code=True)
99
+ def _(mo):
100
+ mo.md(r"""The **difference** $A \setminus B$ is the set of elements in $A$ that are not in $B$.""")
101
+ return
102
+
103
+
104
+ @app.cell
105
+ def _(A, B):
106
+ A - B
107
+ return
108
+
109
+
110
+ @app.cell(hide_code=True)
111
+ def _(mo):
112
+ mo.md(
113
+ """
114
+ ### 🎬 An interactive example
115
+
116
+ Here's a simple example that classifies TV shows into sets by genre, and uses these sets to recommend shows to a user based on their preferences.
117
+ """
118
+ )
119
+ return
120
+
121
+
122
+ @app.cell(hide_code=True)
123
+ def _(mo):
124
+ viewer_type = mo.ui.radio(
125
+ options={
126
+ "I like action and drama!": "New Viewer",
127
+ "I only like action shows": "Action Fan",
128
+ "I only like dramas": "Drama Fan",
129
+ },
130
+ value="I like action and drama!",
131
+ label="Which genre do you prefer?",
132
+ )
133
+ return (viewer_type,)
134
+
135
+
136
+ @app.cell(hide_code=True)
137
+ def _(viewer_type):
138
+ viewer_type
139
+ return
140
+
141
+
142
+ @app.cell
143
+ def _():
144
+ action_shows = {"Stranger Things", "The Witcher", "Money Heist"}
145
+ drama_shows = {"The Crown", "Money Heist", "Bridgerton"}
146
+ return action_shows, drama_shows
147
+
148
+
149
+ @app.cell
150
+ def _(action_shows, drama_shows):
151
+ recommendations = {
152
+ "New Viewer": action_shows | drama_shows, # Union for new viewers
153
+ "Action Fan": action_shows - drama_shows, # Unique action shows
154
+ "Drama Fan": drama_shows - action_shows, # Unique drama shows
155
+ }
156
+ return (recommendations,)
157
+
158
+
159
+ @app.cell(hide_code=True)
160
+ def _(mo, recommendations, viewer_type):
161
+ result = recommendations[viewer_type.value]
162
+
163
+ explanation = {
164
+ "New Viewer": "You get everything to explore!",
165
+ "Action Fan": "Pure action, no drama!",
166
+ "Drama Fan": "Drama-focused selections!",
167
+ }
168
+
169
+ mo.md(f"""
170
+ **🎬 Recommended shows.** Based on your preference for **{viewer_type.value}**,
171
+ we recommend:
172
+
173
+ {", ".join(result)}
174
+
175
+ **Why these shows?**
176
+ {explanation[viewer_type.value]}
177
+ """)
178
+ return explanation, result
179
+
180
+
181
+ @app.cell(hide_code=True)
182
+ def _(mo):
183
+ mo.md("""
184
+ ### Exercise
185
+
186
+ Given these sets:
187
+
188
+ - A = {🎮, 📱, 💻}
189
+
190
+ - B = {📱, 💻, 🖨️}
191
+
192
+ - C = {💻, 🖨️, ⌨️}
193
+
194
+ Can you:
195
+
196
+ 1. Find all elements that are in A or B
197
+
198
+ 2. Find elements common to all three sets
199
+
200
+ 3. Find elements in A that aren't in C
201
+
202
+ <details>
203
+
204
+ <summary>Check your answers!</summary>
205
+
206
+ 1. A ∪ B = {🎮, 📱, 💻, 🖨️}<br>
207
+ 2. A ∩ B ∩ C = {💻}<br>
208
+ 3. A - C = {🎮, 📱}
209
+
210
+ </details>
211
+ """)
212
+ return
213
+
214
+
215
+ @app.cell(hide_code=True)
216
+ def _(mo):
217
+ mo.md(
218
+ r"""
219
+ ## 🧮 Set properties
220
+
221
+ Here are some important properties of the set operations:
222
+
223
+ 1. **Commutative**: $A \cup B = B \cup A$
224
+ 2. **Associative**: $(A \cup B) \cup C = A \cup (B \cup C)$
225
+ 3. **Distributive**: $A \cup (B \cap C) = (A \cup B) \cap (A \cup C)$
226
+ """
227
+ )
228
+ return
229
+
230
+
231
+ @app.cell(hide_code=True)
232
+ def _(mo):
233
+ mo.md(
234
+ r"""
235
+ ## Set builder notation
236
+
237
+ To compactly describe the elements in a set, we can use **set builder notation**, which specifies conditions that must be true for elements to be in the set.
238
+
239
+ For example, here is how to specify the set of positive numbers less than 10:
240
+
241
+ \[
242
+ \{x \mid 0 < x < 10 \}
243
+ \]
244
+
245
+ The predicate to the right of the vertical bar $\mid$ specifies conditions that must be true for an element to be in the set; the expression to the left of $\mid$ specifies the value being included.
246
+
247
+ In Python, set builder notation is called a "set comprehension."
248
+ """
249
+ )
250
+ return
251
+
252
+
253
+ @app.cell
254
+ def _():
255
+ def predicate(x):
256
+ return x > 0 and x < 10
257
+ return (predicate,)
258
+
259
+
260
+ @app.cell
261
+ def _(predicate):
262
+ set(x for x in range(100) if predicate(x))
263
+ return
264
+
265
+
266
+ @app.cell(hide_code=True)
267
+ def _(mo):
268
+ mo.md("""**Try it!** Try modifying the `predicate` function above and see how the set changes.""")
269
+ return
270
+
271
+
272
+ @app.cell(hide_code=True)
273
+ def _(mo):
274
+ mo.md("""
275
+ ## Summary
276
+
277
+ You've learned:
278
+
279
+ - Basic set operations
280
+ - Set properties
281
+ - Real-world applications
282
+
283
+ In the next lesson, we'll define probability from the ground up, using sets.
284
+
285
+ Remember: In probability, every event is a set, and every set can be an event!
286
+ """)
287
+ return
288
+
289
+
290
+ @app.cell
291
+ def _():
292
+ import marimo as mo
293
+ return (mo,)
294
+
295
+
296
+ if __name__ == "__main__":
297
+ app.run()