|
| 1 | +import marimo |
| 2 | + |
| 3 | +__generated_with = "0.11.2" |
| 4 | +app = marimo.App(width="medium") |
| 5 | + |
| 6 | + |
| 7 | +@app.cell |
| 8 | +def _(): |
| 9 | + import marimo as mo |
| 10 | + return (mo,) |
| 11 | + |
| 12 | + |
| 13 | +@app.cell(hide_code=True) |
| 14 | +def _(mo): |
| 15 | + mo.md( |
| 16 | + r""" |
| 17 | + # Axioms of Probability |
| 18 | +
|
| 19 | + Probability theory is built on three fundamental axioms, known as the [Kolmogorov axioms](https://en.wikipedia.org/wiki/Probability_axioms). These axioms form |
| 20 | + the mathematical foundation for all of probability theory[<sup>1</sup>](https://chrispiech.github.io/probabilityForComputerScientists/en/part1/probability). |
| 21 | +
|
| 22 | + Let's explore each axiom and understand why they make intuitive sense: |
| 23 | + """ |
| 24 | + ) |
| 25 | + return |
| 26 | + |
| 27 | + |
| 28 | +@app.cell(hide_code=True) |
| 29 | +def _(mo): |
| 30 | + mo.md( |
| 31 | + r""" |
| 32 | + ## The Three Axioms |
| 33 | +
|
| 34 | + | Axiom | Mathematical Form | Meaning | |
| 35 | + |-------|------------------|----------| |
| 36 | + | **Axiom 1** | $0 \leq P(E) \leq 1$ | All probabilities are between 0 and 1 | |
| 37 | + | **Axiom 2** | $P(S) = 1$ | The probability of the sample space is 1 | |
| 38 | + | **Axiom 3** | $P(E \cup F) = P(E) + P(F)$ | For mutually exclusive events, probabilities add | |
| 39 | +
|
| 40 | + where $S$ is the sample space (all possible outcomes), and $E$ and $F$ are events. |
| 41 | + """ |
| 42 | + ) |
| 43 | + return |
| 44 | + |
| 45 | + |
| 46 | +@app.cell(hide_code=True) |
| 47 | +def _(mo): |
| 48 | + mo.md( |
| 49 | + r""" |
| 50 | + ## Understanding Through Examples |
| 51 | +
|
| 52 | + Let's explore these axioms using a simple experiment: rolling a fair six-sided die. |
| 53 | + We'll use this to demonstrate why each axiom makes intuitive sense. |
| 54 | + """ |
| 55 | + ) |
| 56 | + return |
| 57 | + |
| 58 | + |
| 59 | +@app.cell |
| 60 | +def _(event): |
| 61 | + event |
| 62 | + return |
| 63 | + |
| 64 | + |
| 65 | +@app.cell(hide_code=True) |
| 66 | +def _(mo): |
| 67 | + # Create an interactive widget to explore different events |
| 68 | + |
| 69 | + event = mo.ui.dropdown( |
| 70 | + |
| 71 | + options=[ |
| 72 | + |
| 73 | + "Rolling an even number (2,4,6)", |
| 74 | + |
| 75 | + "Rolling an odd number (1,3,5)", |
| 76 | + |
| 77 | + "Rolling a prime number (2,3,5)", |
| 78 | + |
| 79 | + "Rolling less than 4 (1,2,3)", |
| 80 | + |
| 81 | + "Any possible roll (1,2,3,4,5,6)", |
| 82 | + |
| 83 | + ], |
| 84 | + |
| 85 | + value="Rolling an even number (2,4,6)", |
| 86 | + |
| 87 | + label="Select an event" |
| 88 | + |
| 89 | + ) |
| 90 | + return (event,) |
| 91 | + |
| 92 | + |
| 93 | +@app.cell(hide_code=True) |
| 94 | +def _(event, mo, np, plt): |
| 95 | + # Define the probabilities for each event |
| 96 | + event_map = { |
| 97 | + "Rolling an even number (2,4,6)": [2, 4, 6], |
| 98 | + "Rolling an odd number (1,3,5)": [1, 3, 5], |
| 99 | + "Rolling a prime number (2,3,5)": [2, 3, 5], |
| 100 | + "Rolling less than 4 (1,2,3)": [1, 2, 3], |
| 101 | + "Any possible roll (1,2,3,4,5,6)": [1, 2, 3, 4, 5, 6], |
| 102 | + } |
| 103 | + |
| 104 | + # Get outcomes directly from the event value |
| 105 | + outcomes = event_map[event.value] |
| 106 | + prob = len(outcomes) / 6 |
| 107 | + |
| 108 | + # Visualize the probability |
| 109 | + dice = np.arange(1, 7) |
| 110 | + colors = ['#1f77b4' if d in outcomes else '#d3d3d3' for d in dice] |
| 111 | + |
| 112 | + fig, ax = plt.subplots(figsize=(8, 2)) |
| 113 | + ax.bar(dice, np.ones_like(dice), color=colors) |
| 114 | + ax.set_xticks(dice) |
| 115 | + ax.set_ylim(0, 1.2) |
| 116 | + ax.set_title(f"P(Event) = {prob:.2f}") |
| 117 | + |
| 118 | + # Add explanation |
| 119 | + explanation = mo.md(f""" |
| 120 | + **Event**: {event.value} |
| 121 | +
|
| 122 | + **Probability**: {prob:.2f} |
| 123 | +
|
| 124 | + **Favorable outcomes**: {outcomes} |
| 125 | +
|
| 126 | + This example demonstrates: |
| 127 | +
|
| 128 | + - Axiom 1: The probability is between 0 and 1 |
| 129 | +
|
| 130 | + - Axiom 2: For the sample space, P(S) = 1 |
| 131 | +
|
| 132 | + - Axiom 3: The probability is the sum of individual outcome probabilities |
| 133 | + """) |
| 134 | + |
| 135 | + mo.hstack([plt.gcf(), explanation]) |
| 136 | + return ax, colors, dice, event_map, explanation, fig, outcomes, prob |
| 137 | + |
| 138 | + |
| 139 | +@app.cell(hide_code=True) |
| 140 | +def _(mo): |
| 141 | + mo.md( |
| 142 | + r""" |
| 143 | + ## Why These Axioms Matter |
| 144 | +
|
| 145 | + These axioms are more than just rules - they provide the foundation for all of probability theory: |
| 146 | +
|
| 147 | + 1. **Non-negativity** (Axiom 1) makes intuitive sense: you can't have a negative number of occurrences |
| 148 | + in any experiment. |
| 149 | +
|
| 150 | + 2. **Normalization** (Axiom 2) ensures that something must happen - the total probability must be 1. |
| 151 | +
|
| 152 | + 3. **Additivity** (Axiom 3) lets us build complex probabilities from simple ones, but only for events |
| 153 | + that can't happen together (mutually exclusive events). |
| 154 | +
|
| 155 | + From these simple rules, we can derive all the powerful tools of probability theory that are used in |
| 156 | + statistics, machine learning, and other fields. |
| 157 | + """ |
| 158 | + ) |
| 159 | + return |
| 160 | + |
| 161 | + |
| 162 | +@app.cell(hide_code=True) |
| 163 | +def _(mo): |
| 164 | + mo.md( |
| 165 | + r""" |
| 166 | + ## 🤔 Test Your Understanding |
| 167 | +
|
| 168 | + Consider rolling two dice. Which of these statements follow from the axioms? |
| 169 | +
|
| 170 | + <details> |
| 171 | + <summary>1. P(sum is 13) = 0</summary> |
| 172 | +
|
| 173 | + ✅ Correct! This follows from Axiom 1. Since no combination of dice can sum to 13, |
| 174 | + the probability must be non-negative but can be 0. |
| 175 | + </details> |
| 176 | +
|
| 177 | + <details> |
| 178 | + <summary>2. P(sum is 7) + P(sum is not 7) = 1</summary> |
| 179 | +
|
| 180 | + ✅ Correct! This follows from Axioms 2 and 3. These events are mutually exclusive and cover |
| 181 | + the entire sample space. |
| 182 | + </details> |
| 183 | +
|
| 184 | + <details> |
| 185 | + <summary>3. P(first die is 6 or second die is 6) = P(first die is 6) + P(second die is 6)</summary> |
| 186 | +
|
| 187 | + ❌ Incorrect! This doesn't follow from Axiom 3 because the events are not mutually exclusive - |
| 188 | + you could roll (6,6). |
| 189 | + </details> |
| 190 | + """ |
| 191 | + ) |
| 192 | + return |
| 193 | + |
| 194 | + |
| 195 | +@app.cell |
| 196 | +def _(): |
| 197 | + import numpy as np |
| 198 | + import matplotlib.pyplot as plt |
| 199 | + return np, plt |
| 200 | + |
| 201 | + |
| 202 | +if __name__ == "__main__": |
| 203 | + app.run() |
0 commit comments