CoolFace
Apppublic

marimo-team/marimo-learn

sourceHugging Faceupdated 5mo agoView on Hugging Face
3likes
01_sets.py296 linesDownload Raw Back to probability
1# /// script2# requires-python = ">=3.10"3# dependencies = [4#     "marimo",5# ]6# ///7 8import marimo9 10__generated_with = "0.18.4"11app = marimo.App()12 13 14@app.cell(hide_code=True)15def _(mo):16    mo.md(r"""17    # Sets18 19    Probability is the study of "events", assigning numerical values to how likely20    events are to occur. For example, probability lets us quantify how likely it is for it to rain or shine on a given day.21 22 23    Typically we reason about _sets_ of events. In mathematics,24    a set is a collection of elements, with no element included more than once.25    Elements can be any kind of object.26 27    For example:28 29    - ☀️ Weather events: $\{\text{Rain}, \text{Overcast}, \text{Clear}\}$30    - 🎲 Die rolls: $\{1, 2, 3, 4, 5, 6\}$31    - 🪙 Pairs of coin flips = $\{ \text{(Heads, Heads)}, \text{(Heads, Tails)}, \text{(Tails, Tails)} \text{(Tails, Heads)}\}$32 33    Sets are the building blocks of probability, and will arise frequently in our study.34    """)35    return36 37 38@app.cell(hide_code=True)39def _(mo):40    mo.md(r"""41    ## Set operations42    """)43    return44 45 46@app.cell(hide_code=True)47def _(mo):48    mo.md(r"""49    In Python, sets are made with the `set` function:50    """)51    return52 53 54@app.cell55def _():56    A = set([2, 3, 5, 7])57    A58    return (A,)59 60 61@app.cell62def _():63    B = set([0, 1, 2, 3, 5, 8])64    B65    return (B,)66 67 68@app.cell(hide_code=True)69def _(mo):70    mo.md(r"""71    Below we explain common operations on sets.72 73    _**Try it!** Try modifying the definitions of `A` and `B` above, and see how the results change below._74 75    The **union** $A \cup B$ of sets $A$ and $B$ is the set of elements in $A$, $B$, or both.76    """)77    return78 79 80@app.cell81def _(A, B):82    A | B83    return84 85 86@app.cell(hide_code=True)87def _(mo):88    mo.md(r"""89    The **intersection** $A \cap B$ is the set of elements in both $A$ and $B$90    """)91    return92 93 94@app.cell95def _(A, B):96    A & B97    return98 99 100@app.cell(hide_code=True)101def _(mo):102    mo.md(r"""103    The **difference** $A \setminus B$ is the set of elements in $A$ that are not in $B$.104    """)105    return106 107 108@app.cell109def _(A, B):110    A - B111    return112 113 114@app.cell(hide_code=True)115def _(mo):116    mo.md("""117    ### 🎬 An interactive example118 119    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.120    """)121    return122 123 124@app.cell(hide_code=True)125def _(mo):126    viewer_type = mo.ui.radio(127        options={128            "I like action and drama!": "New Viewer",129            "I only like action shows": "Action Fan",130            "I only like dramas": "Drama Fan",131        },132        value="I like action and drama!",133        label="Which genre do you prefer?",134    )135    return (viewer_type,)136 137 138@app.cell(hide_code=True)139def _(viewer_type):140    viewer_type141    return142 143 144@app.cell145def _():146    action_shows = {"Stranger Things", "The Witcher", "Money Heist"}147    drama_shows = {"The Crown", "Money Heist", "Bridgerton"}148    return action_shows, drama_shows149 150 151@app.cell152def _(action_shows, drama_shows):153    recommendations = {154        "New Viewer": action_shows | drama_shows,  # Union for new viewers155        "Action Fan": action_shows - drama_shows,  # Unique action shows156        "Drama Fan": drama_shows - action_shows,  # Unique drama shows157    }158    return (recommendations,)159 160 161@app.cell(hide_code=True)162def _(mo, recommendations, viewer_type):163    result = recommendations[viewer_type.value]164 165    explanation = {166        "New Viewer": "You get everything to explore!",167        "Action Fan": "Pure action, no drama!",168        "Drama Fan": "Drama-focused selections!",169    }170 171    mo.md(f"""172    **🎬 Recommended shows.** Based on your preference for **{viewer_type.value}**,173    we recommend:174 175    {", ".join(result)}176 177    **Why these shows?** 178    {explanation[viewer_type.value]}179    """)180    return181 182 183@app.cell(hide_code=True)184def _(mo):185    mo.md("""186    ### Exercise187 188    Given these sets:189 190    - A = {🎮, 📱, 💻}191 192    - B = {📱, 💻, 🖨️}193 194    - C = {💻, 🖨️, ⌨️}195 196    Can you:197 198    1. Find all elements that are in A or B199 200    2. Find elements common to all three sets201 202    3. Find elements in A that aren't in C203 204    <details>205 206    <summary>Check your answers!</summary>207 208    1. A ∪ B = {🎮, 📱, 💻, 🖨️}<br>209    2. A ∩ B ∩ C = {💻}<br>210    3. A - C = {🎮, 📱}211 212    </details>213    """)214    return215 216 217@app.cell(hide_code=True)218def _(mo):219    mo.md(r"""220    ## 🧮 Set properties221 222    Here are some important properties of the set operations:223 224    1. **Commutative**: $A \cup B = B \cup A$225    2. **Associative**: $(A \cup B) \cup C = A \cup (B \cup C)$226    3. **Distributive**: $A \cup (B \cap C) = (A \cup B) \cap (A \cup C)$227    """)228    return229 230 231@app.cell(hide_code=True)232def _(mo):233    mo.md(r"""234    ## Set builder notation235 236    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.237 238    For example, here is how to specify the set of positive numbers less than 10:239 240    \[241    \{x \mid 0 < x < 10 \}242    \]243 244    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.245 246    In Python, set builder notation is called a "set comprehension."247    """)248    return249 250 251@app.function252def predicate(x):253    return x > 0 and x < 10254 255 256@app.cell257def _():258    set(x for x in range(100) if predicate(x))259    return260 261 262@app.cell(hide_code=True)263def _(mo):264    mo.md("""265    **Try it!** Try modifying the `predicate` function above and see how the set changes.266    """)267    return268 269 270@app.cell(hide_code=True)271def _(mo):272    mo.md("""273    ## Summary274 275    You've learned:276 277    - Basic set operations278    - Set properties279    - Real-world applications280 281    In the next lesson, we'll define probability from the ground up, using sets.282 283    Remember: In probability, every event is a set, and every set can be an event!284    """)285    return286 287 288@app.cell289def _():290    import marimo as mo291    return (mo,)292 293 294if __name__ == "__main__":295    app.run()296