Haleshot commited on
Commit
225e16e
·
unverified ·
1 Parent(s): 898d23c

Add sets notebook to probability series

Browse files
data_science/00.01-set-theory-fundamentals.py ADDED
@@ -0,0 +1,352 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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()