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 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
# /// script
# requires-python = ">=3.10"
# dependencies = [
#     "marimo",
#     "matplotlib==3.10.0",
#     "numpy==2.2.3",
# ]
# ///

import marimo

__generated_with = "0.11.8"
app = marimo.App(width="medium", app_title="Bayes Theorem")


@app.cell
def _():
    import marimo as mo
    return (mo,)


@app.cell
def _():
    import matplotlib.pyplot as plt
    import numpy as np
    return np, plt


@app.cell(hide_code=True)
def _(mo):
    mo.md(
        r"""
        # Bayes' Theorem

        _This notebook is a computational companion to the book ["Probability for Computer Scientists"](https://chrispiech.github.io/probabilityForComputerScientists/en/part1/bayes_theorem/), by Stanford professor Chris Piech._

        In the 1740s, an English minister named Thomas Bayes discovered a profound mathematical relationship that would revolutionize how we reason about uncertainty. His theorem provides an elegant framework for calculating the probability of a hypothesis being true given observed evidence.

        At its core, Bayes' Theorem connects two different types of probabilities: the probability of a hypothesis given evidence $P(H|E)$, and its reverse - the probability of evidence given a hypothesis $P(E|H)$. This relationship is particularly powerful because it allows us to compute difficult probabilities using ones that are easier to measure.
        """
    )
    return


@app.cell(hide_code=True)
def _(mo):
    mo.md(
        r"""
        ## The Heart of Bayesian Reasoning

        The fundamental insight of Bayes' Theorem lies in its ability to relate what we want to know with what we can measure. When we observe evidence $E$, we often want to know the probability of a hypothesis $H$ being true. However, it's typically much easier to measure how likely we are to observe the evidence when we know the hypothesis is true.

        This reversal of perspective - from $P(H|E)$ to $P(E|H)$ - is powerful because it lets us:
        1. Start with what we know (prior beliefs)
        2. Use easily measurable relationships (likelihood)
        3. Update our beliefs with new evidence

        This approach mirrors both how humans naturally learn and the scientific method: we begin with prior beliefs, gather evidence, and update our understanding based on that evidence. This makes Bayes' Theorem not just a mathematical tool, but a framework for rational thinking.
        """
    )
    return


@app.cell(hide_code=True)
def _(mo):
    mo.md(
        r"""
        ## The Formula

        Bayes' Theorem states:

        $P(H|E) = \frac{P(E|H)P(H)}{P(E)}$

        Where:

        - $P(H|E)$ is the **posterior probability** - probability of hypothesis H given evidence E
        - $P(E|H)$ is the **likelihood** - probability of evidence E given hypothesis H
        - $P(H)$ is the **prior probability** - initial probability of hypothesis H
        - $P(E)$ is the **evidence** - total probability of observing evidence E

        The denominator $P(E)$ can be expanded using the [Law of Total Probability](https://marimo.app/gh/marimo-team/learn/main?entrypoint=probability%2F07_law_of_total_probability.py):

        $P(E) = P(E|H)P(H) + P(E|H^c)P(H^c)$
        """
    )
    return


@app.cell(hide_code=True)
def _(mo):
    mo.md(
        r"""
        ## Understanding Each Component

        ### 1. Prior Probability - $P(H)$
        - Initial belief about hypothesis before seeing evidence
        - Based on previous knowledge or assumptions
        - Example: Probability of having a disease before any tests

        ### 2. Likelihood - $P(E|H)$
        - Probability of evidence given hypothesis is true
        - Often known from data or scientific studies
        - Example: Probability of positive test given disease present

        ### 3. Evidence - $P(E)$
        - Total probability of observing the evidence
        - Acts as a normalizing constant
        - Can be calculated using Law of Total Probability

        ### 4. Posterior - $P(H|E)$
        - Updated probability after considering evidence
        - Combines prior knowledge with new evidence
        - Becomes new prior for future updates
        """
    )
    return


@app.cell(hide_code=True)
def _(mo):
    mo.md(
        r"""
        ## Real-World Examples

        ### 1. Medical Testing
        - **Want to know**: $P(\text{Disease}|\text{Positive})$ - Probability of disease given positive test
        - **Easy to know**: $P(\text{Positive}|\text{Disease})$ - Test accuracy for sick people
        - **Causality**: Disease causes test results, not vice versa

        ### 2. Student Ability
        - **Want to know**: $P(\text{High Ability}|\text{Good Grade})$ - Probability student is skilled given good grade
        - **Easy to know**: $P(\text{Good Grade}|\text{High Ability})$ - Probability good students get good grades
        - **Causality**: Ability influences grades, not vice versa

        ### 3. Cell Phone Location
        - **Want to know**: $P(\text{Location}|\text{Signal Strength})$ - Probability of phone location given signal
        - **Easy to know**: $P(\text{Signal Strength}|\text{Location})$ - Signal strength at known locations
        - **Causality**: Location determines signal strength, not vice versa

        These examples highlight a common pattern: what we want to know (posterior) is harder to measure directly than its reverse (likelihood).
        """
    )
    return


@app.cell
def _():
    def calculate_posterior(prior, likelihood, false_positive_rate):
        # Calculate P(E) using Law of Total Probability
        p_e = likelihood * prior + false_positive_rate * (1 - prior)

        # Calculate posterior using Bayes' Theorem
        posterior = (likelihood * prior) / p_e
        return posterior, p_e
    return (calculate_posterior,)


@app.cell
def _(calculate_posterior):
    # Medical test example
    p_disease = 0.01  # Prior: 1% have the disease
    p_positive_given_disease = 0.95  # Likelihood: 95% test accuracy
    p_positive_given_healthy = 0.10  # False positive rate: 10%

    medical_posterior, medical_evidence = calculate_posterior(
        p_disease,
        p_positive_given_disease,
        p_positive_given_healthy
    )
    return (
        medical_evidence,
        medical_posterior,
        p_disease,
        p_positive_given_disease,
        p_positive_given_healthy,
    )


@app.cell
def _(medical_explanation):
    medical_explanation
    return


@app.cell(hide_code=True)
def _(medical_posterior, mo):
    medical_explanation = mo.md(f"""
    ### Medical Testing Example

    Consider a medical test for a rare disease:

    - Prior: 1% of population has the disease
    - Likelihood: 95% test accuracy for sick people
    - False positive: 10% of healthy people test positive

    Using Bayes' Theorem:
    $P(D|+) = \\frac{{0.95 times 0.01}}{{0.95 times 0.01 + 0.10 times 0.99}} = {medical_posterior:.3f}$

    Despite a positive test, there's only a {medical_posterior:.1%} chance of having the disease!
    This counterintuitive result occurs because the disease is rare (low prior probability).
    """)
    return (medical_explanation,)


@app.cell
def _(calculate_posterior):
    # Student ability example
    p_high_ability = 0.30  # Prior: 30% of students have high ability
    p_good_grade_given_high = 0.90  # Likelihood: 90% of high ability students get good grades
    p_good_grade_given_low = 0.40  # 40% of lower ability students also get good grades

    student_posterior, student_evidence = calculate_posterior(
        p_high_ability,
        p_good_grade_given_high,
        p_good_grade_given_low
    )
    return (
        p_good_grade_given_high,
        p_good_grade_given_low,
        p_high_ability,
        student_evidence,
        student_posterior,
    )


@app.cell
def _(student_explanation):
    student_explanation
    return


@app.cell(hide_code=True)
def _(mo, student_posterior):
    student_explanation = mo.md(f"""
    ### Student Ability Example

    If a student gets a good grade, what's the probability they have high ability?

    Using Bayes' Theorem:

    - Prior: 30% have high ability
    - Likelihood: 90% of high ability students get good grades
    - False positive: 40% of lower ability students get good grades

    Result: P(High Ability|Good Grade) = {student_posterior:.2f}

    So a good grade increases our confidence in high ability from 30% to {student_posterior:.1%}
    """)
    return (student_explanation,)


@app.cell
def _(calculate_posterior):
    # Cell phone location example
    p_location_a = 0.25  # Prior probability of being in location A
    p_strong_signal_at_a = 0.85  # Likelihood of strong signal at A
    p_strong_signal_elsewhere = 0.15  # False positive rate

    location_posterior, location_evidence = calculate_posterior(
        p_location_a,
        p_strong_signal_at_a,
        p_strong_signal_elsewhere
    )
    return (
        location_evidence,
        location_posterior,
        p_location_a,
        p_strong_signal_at_a,
        p_strong_signal_elsewhere,
    )


@app.cell
def _(location_explanation):
    location_explanation
    return


@app.cell(hide_code=True)
def _(location_posterior, mo):
    location_explanation = mo.md(f"""
    ### Cell Phone Location Example

    Given a strong signal, what's the probability the phone is in location A?

    Using Bayes' Theorem:

    - Prior: 25% chance of being in location A
    - Likelihood: 85% chance of strong signal at A
    - False positive: 15% chance of strong signal elsewhere

    Result: P(Location A|Strong Signal) = {location_posterior:.2f}

    The strong signal increases our confidence in location A from 25% to {location_posterior:.1%}
    """)
    return (location_explanation,)


@app.cell(hide_code=True)
def _(mo):
    mo.md(r"""## Interactive example""")
    return


@app.cell(hide_code=True)
def _(mo):
    mo.md(
        r"""

        _This interactive example was made with [marimo](https://github.com/marimo-team/marimo/blob/main/examples/misc/bayes_theorem.py), and is [based on an explanation of Bayes' Theorem by Grant Sanderson](https://www.youtube.com/watch?v=HZGCoVF3YvM&list=PLzq7odmtfKQw2KIbQq0rzWrqgifHKkPG1&index=1&t=3s)_.

        Bayes theorem provides a convenient way to calculate the probability
        of a hypothesis event $H$ given evidence $E$:

        \[
        P(H \mid E) = \frac{P(H) P(E \mid H)}{P(E)}.
        \]


        **The numerator.** The numerator is the probability of events $E$ and $H$ happening
        together; that is,

        \[
           P(H) P(E \mid H) = P(E \cap H).
        \]

        **The denominator.**
        In most calculations, it is helpful to rewrite the denominator $P(E)$ as 

        \[
        P(E) = P(H)P(E \mid H) + P(\neg H) P (E \mid \neg H),
        \]

        which in turn can also be written as


        \[
        P(E) = P(E \cap H) + P(E \cap \neg H).
        \]
        """
    ).left()
    return


@app.cell(hide_code=True)
def _(
    bayes_result,
    construct_probability_plot,
    mo,
    p_e,
    p_e_given_h,
    p_e_given_not_h,
    p_h,
):
    mo.hstack(
        [
            mo.md(
                rf"""
                ### Probability parameters

                You can configure the probabilities of the events $H$, $E \mid H$, and $E \mid \neg H$

                {mo.as_html([p_h, p_e_given_h, p_e_given_not_h])}

                The plot on the right visualizes the probabilities of these events. 

                1. The yellow rectangle represents the event $H$, and its area is $P(H) = {p_h.value:0.2f}$.
                2. The teal rectangle overlapping with the yellow one represents the event $E \cap H$, and
                   its area is $P(H) \cdot P(E \mid H) = {p_h.value * p_e_given_h.value:0.2f}$.
                3. The teal rectangle that doesn't overlap the yellow rectangle represents the event $E \cap \neg H$, and
                   its area is $P(\neg H) \cdot P(E \mid \neg H) = {(1 - p_h.value) * p_e_given_not_h.value:0.2f}$.

                Notice that the sum of the areas in $2$ and $3$ is the probability $P(E) = {p_e:0.2f}$. 

                One way to think about Bayes' Theorem is the following: the probability $P(H \mid E)$ is the probability
                of $E$ and $H$ happening together (the area of the rectangle $2$), divided by the probability of $E$ happening
                at all (the sum of the areas of $2$ and $3$).
                In this case, Bayes' Theorem says

                \[
                P(H \mid E) = \frac{{P(H) P(E \mid H)}}{{P(E)}} = \frac{{{p_h.value} \cdot {p_e_given_h.value}}}{{{p_e:0.2f}}} = {bayes_result:0.2f}
                \]
                """
            ),
            construct_probability_plot(),
        ],
        justify="start",
        gap=4,
        align="start",
        widths=[0.33, 0.5],
    )
    return


@app.cell(hide_code=True)
def _(mo):
    mo.md(
        r"""
        ## Applications in Computer Science

        Bayes' Theorem is fundamental in many computing applications:

        1. **Spam Filtering**

            - $P(\text{Spam}|\text{Words})$ = Probability email is spam given its words
            - Updates as new emails are classified

        2. **Machine Learning**

            - Naive Bayes classifiers
            - Probabilistic graphical models
            - Bayesian neural networks

        3. **Computer Vision**

            - Object detection confidence
            - Face recognition systems
            - Image classification
        """
    )
    return


@app.cell(hide_code=True)
def _(mo):
    mo.md(
        """
        ## 🤔 Test Your Understanding

        Pick which of these statements about Bayes' Theorem you think are correct:

        <details>
        <summary>The posterior probability will always be larger than the prior probability</summary>
        ❌ Incorrect! Evidence can either increase or decrease our belief in the hypothesis. For example, a negative medical test decreases the probability of having a disease.
        </details>

        <details>
        <summary>If the likelihood is 0.9 and the prior is 0.5, then the posterior must equal 0.9</summary>
        ❌ Incorrect! We also need the false positive rate to calculate the posterior probability. The likelihood alone doesn't determine the posterior.
        </details>

        <details>
        <summary>The denominator acts as a normalizing constant to ensure the posterior is a valid probability</summary>
        ✅ Correct! The denominator ensures the posterior probability is between 0 and 1 by considering all ways the evidence could occur.
        </details>
        """
    )
    return


@app.cell(hide_code=True)
def _(mo):
    mo.md(
        """
        ## Summary

        You've learned:

        - The components and intuition behind Bayes' Theorem
        - How to update probabilities when new evidence arrives
        - Why posterior probabilities can be counterintuitive
        - Real-world applications in computer science

        In the next lesson, we'll explore Random Variables, which help us work with numerical outcomes in probability.
        """
    )
    return


@app.cell(hide_code=True)
def _(mo):
    mo.md(
        r"""
        ### Appendix
        Below (hidden) cell blocks are responsible for the interactive example above
        """
    )
    return


@app.cell(hide_code=True)
def _(p_e_given_h, p_e_given_not_h, p_h):
    p_e = p_h.value*p_e_given_h.value + (1 - p_h.value)*p_e_given_not_h.value
    bayes_result = p_h.value * p_e_given_h.value / p_e
    return bayes_result, p_e


@app.cell(hide_code=True)
def _(mo):
    p_h = mo.ui.slider(0.0, 1, label="$P(H)$", value=0.1, step=0.1)
    p_e_given_h = mo.ui.slider(0.0, 1, label="$P(E \mid H)$", value=0.3, step=0.1)
    p_e_given_not_h = mo.ui.slider(
        0.0, 1, label=r"$P(E \mid \neg H)$", value=0.3, step=0.1
    )
    return p_e_given_h, p_e_given_not_h, p_h


@app.cell(hide_code=True)
def _(p_e_given_h, p_e_given_not_h, p_h):
    def construct_probability_plot():
        import matplotlib.pyplot as plt

        plt.axes()

        # Radius: 1, face-color: red, edge-color: blue
        plt.figure(figsize=(6,6))
        base = plt.Rectangle((0, 0), 1, 1, fc="black", ec="white", alpha=0.25)
        h = plt.Rectangle((0, 0), p_h.value, 1, fc="yellow", ec="white", label="H")
        e_given_h = plt.Rectangle(
            (0, 0),
            p_h.value,
            p_e_given_h.value,
            fc="teal",
            ec="white",
            alpha=0.5,
            label="E",
        )
        e_given_not_h = plt.Rectangle(
            (p_h.value, 0), 1 - p_h.value, p_e_given_not_h.value, fc="teal", ec="white", alpha=0.5
        )
        plt.gca().add_patch(base)
        plt.gca().add_patch(h)
        plt.gca().add_patch(e_given_not_h)
        plt.gca().add_patch(e_given_h)
        plt.legend()
        return plt.gca()
    return (construct_probability_plot,)


if __name__ == "__main__":
    app.run()