Spaces:
Running
Running
<html lang="en"> | |
<head> | |
<meta charset="utf-8" /> | |
<link rel="icon" href="./favicon.ico" /> | |
<!-- Preload is necessary because we show these images when we disconnect from the server, | |
but at that point we cannot load these images from the server --> | |
<link rel="preload" href="./assets/gradient-yHQUC_QB.png" as="image" /> | |
<link rel="preload" href="./assets/noise-60BoTA8O.png" as="image" /> | |
<!-- Preload the fonts --> | |
<link rel="preload" href="./assets/Lora-VariableFont_wght-B2ootaw-.ttf" as="font" crossorigin="anonymous" /> | |
<link rel="preload" href="./assets/PTSans-Regular-CxL0S8W7.ttf" as="font" crossorigin="anonymous" /> | |
<link rel="preload" href="./assets/PTSans-Bold-D9fedIX3.ttf" as="font" crossorigin="anonymous" /> | |
<link rel="preload" href="./assets/FiraMono-Regular-BTCkDNvf.ttf" as="font" crossorigin="anonymous" /> | |
<link rel="preload" href="./assets/FiraMono-Medium-DU3aDxX5.ttf" as="font" crossorigin="anonymous" /> | |
<link rel="preload" href="./assets/FiraMono-Bold-CLVRCuM9.ttf" as="font" crossorigin="anonymous" /> | |
<meta name="viewport" content="width=device-width, initial-scale=1" /> | |
<meta name="theme-color" content="#000000" /> | |
<meta name="description" content="a marimo app" /> | |
<link rel="apple-touch-icon" href="./apple-touch-icon.png" /> | |
<link rel="manifest" href="./manifest.json" /> | |
<script data-marimo="true"> | |
function __resizeIframe(obj) { | |
var scrollbarHeight = 20; // Max between windows, mac, and linux | |
function setHeight() { | |
var element = obj.contentWindow.document.documentElement; | |
// If there is no vertical scrollbar, we don't need to resize the iframe | |
if (element.scrollHeight === element.clientHeight) { | |
return; | |
} | |
// Create a new height that includes the scrollbar height if it's visible | |
var hasHorizontalScrollbar = element.scrollWidth > element.clientWidth; | |
var newHeight = element.scrollHeight + (hasHorizontalScrollbar ? scrollbarHeight : 0); | |
// Only update the height if it's different from the current height | |
if (obj.style.height !== `${newHeight}px`) { | |
obj.style.height = `${newHeight}px`; | |
} | |
} | |
// Resize the iframe to the height of the content and bottom scrollbar height | |
setHeight(); | |
// Resize the iframe when the content changes | |
const resizeObserver = new ResizeObserver((entries) => { | |
setHeight(); | |
}); | |
resizeObserver.observe(obj.contentWindow.document.body); | |
} | |
</script> | |
<marimo-filename hidden>notebook.py</marimo-filename> | |
<marimo-mode data-mode='edit' hidden></marimo-mode> | |
<marimo-version data-version='0.11.9' hidden></marimo-version> | |
<marimo-user-config data-config='{"completion": {"activate_on_typing": true, "copilot": false}, "display": {"dataframes": "rich", "cell_output": "above", "code_editor_font_size": 14, "default_width": "medium", "theme": "light"}, "formatting": {"line_length": 79}, "keymap": {"preset": "default", "overrides": {}}, "runtime": {"auto_instantiate": true, "auto_reload": "off", "on_cell_change": "autorun", "watcher_on_save": "lazy", "output_max_bytes": 8000000, "std_stream_max_bytes": 1000000}, "save": {"autosave": "off", "autosave_delay": 1000, "format_on_save": false}, "package_management": {"manager": "pip"}, "server": {"browser": "default", "follow_symlink": false}, "snippets": {"custom_paths": [], "include_default_snippets": true}}' data-overrides='{}' hidden></marimo-user-config> | |
<marimo-app-config data-config='{"width": "medium"}' hidden></marimo-app-config> | |
<marimo-server-token data-token='123' hidden></marimo-server-token> | |
<title>07 law of total probability</title> | |
<script type="module" crossorigin src="./assets/index-BiV-b1K2.js"></script> | |
<link rel="stylesheet" crossorigin href="./assets/index-DkqMrX_B.css"> | |
<marimo-wasm hidden=""></marimo-wasm> | |
<script> | |
if (window.location.protocol === 'file:') { | |
alert('Warning: This file must be served by an HTTP server to function correctly.'); | |
} | |
</script> | |
<style> | |
#save-button { | |
display: none ; | |
} | |
#filename-input { | |
display: none ; | |
} | |
</style> | |
<marimo-code hidden="" data-show-code="false">import%20marimo%0A%0A__generated_with%20%3D%20%220.11.9%22%0Aapp%20%3D%20marimo.App(width%3D%22medium%22)%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20import%20marimo%20as%20mo%0A%20%20%20%20return%20(mo%2C)%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20import%20matplotlib.pyplot%20as%20plt%0A%20%20%20%20from%20matplotlib_venn%20import%20venn2%0A%20%20%20%20import%20numpy%20as%20np%0A%20%20%20%20return%20np%2C%20plt%2C%20venn2%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(%0A%20%20%20%20%20%20%20%20r%22%22%22%0A%20%20%20%20%20%20%20%20%23%20Law%20of%20Total%20Probability%0A%0A%20%20%20%20%20%20%20%20_This%20notebook%20is%20a%20computational%20companion%20to%20the%20book%20%5B%22Probability%20for%20Computer%20Scientists%22%5D(https%3A%2F%2Fchrispiech.github.io%2FprobabilityForComputerScientists%2Fen%2Fpart1%2Flaw_total%2F)%2C%20by%20Stanford%20professor%20Chris%20Piech._%0A%0A%20%20%20%20%20%20%20%20The%20Law%20of%20Total%20Probability%20is%20a%20fundamental%20rule%20that%20helps%20us%20calculate%20probabilities%20by%20breaking%20down%20complex%20events%20into%20simpler%20parts.%20It's%20particularly%20useful%20when%20we%20want%20to%20compute%20the%20probability%20of%20an%20event%20that%20can%20occur%20through%20multiple%20distinct%20scenarios.%0A%20%20%20%20%20%20%20%20%22%22%22%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(%0A%20%20%20%20%20%20%20%20r%22%22%22%0A%20%20%20%20%20%20%20%20%23%23%20The%20Core%20Concept%0A%0A%20%20%20%20%20%20%20%20The%20Law%20of%20Total%20Probability%20emerged%20from%20a%20simple%20but%20powerful%20observation%3A%20any%20event%20E%20can%20be%20broken%20down%20into%20parts%20based%20on%20another%20event%20F%20and%20its%20complement%20F%E1%B6%9C.%0A%0A%20%20%20%20%20%20%20%20%23%23%23%20From%20Simple%20Observation%20to%20Powerful%20Law%0A%0A%20%20%20%20%20%20%20%20Consider%20an%20event%20E%20that%20can%20occur%20in%20two%20ways%3A%0A%0A%20%20%20%20%20%20%20%201.%20When%20F%20occurs%20(E%20%E2%88%A9%20F)%0A%20%20%20%20%20%20%20%202.%20When%20F%20doesn't%20occur%20(E%20%E2%88%A9%20F%E1%B6%9C)%0A%0A%20%20%20%20%20%20%20%20This%20leads%20to%20our%20first%20insight%3A%0A%0A%20%20%20%20%20%20%20%20%24P(E)%20%3D%20P(E%20%5Ccap%20F)%20%2B%20P(E%20%5Ccap%20F%5Ec)%24%0A%0A%20%20%20%20%20%20%20%20Applying%20the%20chain%20rule%20to%20each%20term%3A%0A%0A%20%20%20%20%20%20%20%20%5Cbegin%7Balign%7D%0A%20%20%20%20%20%20%20%20P(E)%20%26%3D%20P(E%20%5Ccap%20F)%20%2B%20P(E%20%5Ccap%20F%5Ec)%20%5C%5C%0A%20%20%20%20%20%20%20%20%26%3D%20P(E%7CF)P(F)%20%2B%20P(E%7CF%5Ec)P(F%5Ec)%0A%20%20%20%20%20%20%20%20%5Cend%7Balign%7D%0A%0A%20%20%20%20%20%20%20%20This%20two-part%20version%20generalizes%20to%20any%20number%20of%20%5Bmutually%20exclusive%5D(marimo.app%2Fhttps%3A%2F%2Fgithub.com%2Fmarimo-team%2Flearn%2Fblob%2Fmain%2Fprobability%2F03_probability_of_or.py)%20events%20that%20cover%20the%20sample%20space%3A%0A%0A%20%20%20%20%20%20%20%20%24P(A)%20%3D%20%5Csum_%7Bi%3D1%7D%5En%20P(A%7CB_i)P(B_i)%24%0A%0A%20%20%20%20%20%20%20%20where%20%7BB%E2%82%81%2C%20B%E2%82%82%2C%20...%2C%20B%E2%82%99%7D%20forms%20a%20partition%20of%20the%20sample%20space.%0A%20%20%20%20%20%20%20%20%22%22%22%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20def%20is_valid_partition(events%2C%20sample_space)%3A%0A%20%20%20%20%20%20%20%20%22%22%22Check%20if%20events%20form%20a%20valid%20partition%20of%20the%20sample%20space%22%22%22%0A%20%20%20%20%20%20%20%20%23%20Check%20if%20events%20are%20mutually%20exclusive%0A%20%20%20%20%20%20%20%20for%20i%2C%20event1%20in%20enumerate(events)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20for%20j%2C%20event2%20in%20enumerate(events%5Bi%2B1%3A%5D%2C%20i%2B1)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20if%20event1.intersection(event2)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20return%20False%0A%0A%20%20%20%20%20%20%20%20%23%20Check%20if%20events%20cover%20sample%20space%0A%20%20%20%20%20%20%20%20union%20%3D%20set().union(*events)%0A%20%20%20%20%20%20%20%20return%20union%20%3D%3D%20sample_space%0A%0A%20%20%20%20%23%20Example%20with%20dice%0A%20%20%20%20sample_space%20%3D%20%7B1%2C%202%2C%203%2C%204%2C%205%2C%206%7D%0A%20%20%20%20partition1%20%3D%20%5B%7B1%2C%203%2C%205%7D%2C%20%7B2%2C%204%2C%206%7D%5D%20%20%23%20odd%20vs%20even%0A%20%20%20%20partition2%20%3D%20%5B%7B1%2C%202%7D%2C%20%7B3%2C%204%7D%2C%20%7B5%2C%206%7D%5D%20%20%23%20pairs%0A%0A%20%20%20%20print(%22Odd%2FEven%20partition%3A%22%2C%20is_valid_partition(partition1%2C%20sample_space))%0A%20%20%20%20print(%22Number%20pairs%20partition%3A%22%2C%20is_valid_partition(partition2%2C%20sample_space))%0A%20%20%20%20return%20is_valid_partition%2C%20partition1%2C%20partition2%2C%20sample_space%0A%0A%0A%40app.cell%0Adef%20_(is_valid_partition)%3A%0A%20%20%20%20%23%20Example%3A%20Student%20Grades%0A%20%20%20%20grade_space%20%3D%20%7B'A'%2C%20'B'%2C%20'C'%2C%20'D'%2C%20'F'%7D%0A%20%20%20%20passing_partition%20%3D%20%5B%7B'A'%2C%20'B'%2C%20'C'%7D%2C%20%7B'D'%2C%20'F'%7D%5D%20%20%23%20Pass%2FFail%0A%20%20%20%20letter_groups%20%3D%20%5B%7B'A'%7D%2C%20%7B'B'%7D%2C%20%7B'C'%7D%2C%20%7B'D'%7D%2C%20%7B'F'%7D%5D%20%20%23%20Individual%20grades%0A%0A%20%20%20%20print(%22Student%20Grades%20Examples%3A%22)%0A%20%20%20%20print(%22Pass%2FFail%20partition%3A%22%2C%20is_valid_partition(passing_partition%2C%20grade_space))%0A%20%20%20%20print(%22Individual%20grades%20partition%3A%22%2C%20is_valid_partition(letter_groups%2C%20grade_space))%0A%20%20%20%20return%20grade_space%2C%20letter_groups%2C%20passing_partition%0A%0A%0A%40app.cell%0Adef%20_(is_valid_partition)%3A%0A%20%20%20%20%23%20Example%3A%20Card%20Suits%0A%20%20%20%20card_space%20%3D%20%7B'%E2%99%A0'%2C%20'%E2%99%A3'%2C%20'%E2%99%A5'%2C%20'%E2%99%A6'%7D%0A%20%20%20%20color_partition%20%3D%20%5B%7B'%E2%99%A0'%2C%20'%E2%99%A3'%7D%2C%20%7B'%E2%99%A5'%2C%20'%E2%99%A6'%7D%5D%20%20%23%20Black%2FRed%0A%20%20%20%20invalid_partition%20%3D%20%5B%7B'%E2%99%A0'%2C%20'%E2%99%A5'%7D%2C%20%7B'%E2%99%A3'%7D%5D%20%20%23%20Invalid%3A%20Doesn't%20cover%20full%20space%0A%0A%20%20%20%20print(%22%5CnPlaying%20Cards%20Examples%3A%22)%0A%20%20%20%20print(%22Color-based%20partition%3A%22%2C%20is_valid_partition(color_partition%2C%20card_space))%20%20%23%20True%0A%20%20%20%20print(%22Invalid%20partition%3A%22%2C%20is_valid_partition(invalid_partition%2C%20card_space))%20%20%20%20%23%20False%0A%20%20%20%20return%20card_space%2C%20color_partition%2C%20invalid_partition%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo%2C%20plt%2C%20venn2)%3A%0A%20%20%20%20%23%20Create%20Venn%20diagram%20for%20E%20and%20F%0A%20%20%20%20plt.figure(figsize%3D(10%2C%205))%0A%20%20%20%20v%20%3D%20venn2(subsets%3D(0.3%2C%200.4%2C%200.2)%2C%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20set_labels%3D('F'%2C%20'E'))%0A%20%20%20%20plt.title(%22Decomposing%20Event%20E%20using%20F%22)%0A%0A%20%20%20%20viz_explanation%20%3D%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%23%20Visual%20Intuition%0A%0A%20%20%20%20In%20this%20diagram%3A%0A%0A%20%20%20%20-%20The%20red%20region%20(E)%20is%20split%20into%20two%20parts%3A%0A%20%20%20%20%20%20%20%20%20%201.%20Part%20inside%20F%20(E%20%E2%88%A9%20F)%0A%20%20%20%20%20%20%20%20%20%202.%20Part%20outside%20F%20(E%20%E2%88%A9%20F%E1%B6%9C)%0A%0A%20%20%20%20This%20visualization%20shows%20why%3A%0A%20%20%20%20%24P(E)%20%3D%20P(E%7CF)P(F)%20%2B%20P(E%7CF%5Ec)P(F%5Ec)%24%0A%0A%20%20%20%20The%20same%20principle%20extends%20to%20any%20number%20of%20mutually%20exclusive%20parts!%0A%20%20%20%20%22%22%22)%0A%0A%20%20%20%20mo.hstack(%5Bplt.gca()%2C%20viz_explanation%5D)%0A%20%20%20%20return%20v%2C%20viz_explanation%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(%0A%20%20%20%20%20%20%20%20r%22%22%22%0A%20%20%20%20%20%20%20%20%23%23%20Computing%20Total%20Probability%0A%0A%20%20%20%20%20%20%20%20To%20use%20the%20Law%20of%20Total%20Probability%3A%0A%0A%20%20%20%20%20%20%20%201.%20Identify%20a%20partition%20of%20the%20sample%20space%0A%20%20%20%20%20%20%20%202.%20Calculate%20%24P(B_i)%24%20for%20each%20part%0A%20%20%20%20%20%20%20%203.%20Calculate%20%24P(A%7CB_i)%24%20for%20each%20part%0A%20%20%20%20%20%20%20%204.%20Sum%20the%20products%20%24P(A%7CB_i)P(B_i)%24%0A%20%20%20%20%20%20%20%20%22%22%22%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22Let's%20implement%20this%20calculation%3A%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20def%20total_probability(conditional_probs%2C%20partition_probs)%3A%0A%20%20%20%20%20%20%20%20%22%22%22Calculate%20total%20probability%20using%20Law%20of%20Total%20Probability%0A%20%20%20%20%20%20%20%20conditional_probs%3A%20List%20of%20P(A%7CBi)%0A%20%20%20%20%20%20%20%20partition_probs%3A%20List%20of%20P(Bi)%0A%20%20%20%20%20%20%20%20%22%22%22%0A%20%20%20%20%20%20%20%20if%20len(conditional_probs)%20!%3D%20len(partition_probs)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20raise%20ValueError(%22Must%20have%20same%20number%20of%20conditional%20and%20partition%20probabilities%22)%0A%0A%20%20%20%20%20%20%20%20if%20abs(sum(partition_probs)%20-%201)%20%3E%201e-10%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20raise%20ValueError(%22Partition%20probabilities%20must%20sum%20to%201%22)%0A%0A%20%20%20%20%20%20%20%20return%20sum(c%20*%20p%20for%20c%2C%20p%20in%20zip(conditional_probs%2C%20partition_probs))%0A%20%20%20%20return%20(total_probability%2C)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(%0A%20%20%20%20%20%20%20%20r%22%22%22%0A%20%20%20%20%20%20%20%20%23%23%20Example%3A%20System%20Reliability%0A%0A%20%20%20%20%20%20%20%20Consider%20a%20computer%20system%20that%20can%20be%20in%20three%20states%3A%0A%0A%20%20%20%20%20%20%20%20-%20Normal%20(70%25%20of%20time)%0A%20%20%20%20%20%20%20%20-%20Degraded%20(20%25%20of%20time)%0A%20%20%20%20%20%20%20%20-%20Critical%20(10%25%20of%20time)%0A%0A%20%20%20%20%20%20%20%20The%20probability%20of%20errors%20in%20each%20state%3A%0A%0A%20%20%20%20%20%20%20%20-%20P(Error%20%7C%20Normal)%20%3D%200.01%20(1%25)%0A%20%20%20%20%20%20%20%20-%20P(Error%20%7C%20Degraded)%20%3D%200.15%20(15%25)%0A%20%20%20%20%20%20%20%20-%20P(Error%20%7C%20Critical)%20%3D%200.45%20(45%25)%0A%0A%20%20%20%20%20%20%20%20Let's%20calculate%20the%20overall%20probability%20of%20encountering%20an%20error%3A%0A%20%20%20%20%20%20%20%20%22%22%22%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(mo%2C%20total_probability)%3A%0A%20%20%20%20%23%20System%20states%20and%20probabilities%0A%20%20%20%20states%20%3D%20%5B%22Normal%22%2C%20%22Degraded%22%2C%20%22Critical%22%5D%0A%20%20%20%20state_probs%20%3D%20%5B0.7%2C%200.2%2C%200.1%5D%20%20%23%20System%20spends%2070%25%2C%2020%25%2C%2010%25%20of%20time%20in%20each%20state%0A%20%20%20%20error_probs%20%3D%20%5B0.01%2C%200.15%2C%200.45%5D%20%20%23%20Error%20rates%20increase%20with%20system%20degradation%0A%0A%20%20%20%20%23%20Calculate%20total%20probability%0A%20%20%20%20total_error%20%3D%20total_probability(error_probs%2C%20state_probs)%0A%0A%20%20%20%20explanation%20%3D%20mo.md(f%22%22%22%0A%20%20%20%20%23%23%23%20System%20Error%20Analysis%0A%0A%20%20%20%20Given%3A%0A%0A%20%20%20%20-%20Normal%20State%20(70%25%20of%20time)%3A%0A%20%20%20%20%20%20%20%20%20%20-%20Only%201%25%20chance%20of%20errors%0A%20%20%20%20-%20Degraded%20State%20(20%25%20of%20time)%3A%0A%20%20%20%20%20%20%20%20%20%20-%20Higher%2015%25%20chance%20of%20errors%0A%20%20%20%20-%20Critical%20State%20(10%25%20of%20time)%3A%0A%20%20%20%20%20%20%20%20%20%20-%20Highest%2045%25%20chance%20of%20errors%0A%0A%20%20%20%20Using%20Law%20of%20Total%20Probability%3A%0A%20%20%20%20%24P(%5Ctext%7B%7BError%7D%7D)%20%3D%20%5Csum_%7B%7Bi%3D1%7D%7D%5E3%20P(%5Ctext%7B%7BError%7D%7D%7CB_i)P(B_i)%24%0A%0A%20%20%20%20Step%20by%20step%3A%0A%0A%20%20%20%201.%20Normal%3A%200.01%20%C3%97%200.7%20%3D%200.007%20(0.7%25)%0A%20%20%20%202.%20Degraded%3A%200.15%20%C3%97%200.2%20%3D%200.030%20(3.0%25)%0A%20%20%20%203.%20Critical%3A%200.45%20%C3%97%200.1%20%3D%200.045%20(4.5%25)%0A%0A%20%20%20%20Total%3A%20%7Btotal_error%3A.3f%7D%20or%20%7Btotal_error%3A.1%25%7D%20chance%20of%20error%0A%20%20%20%20%22%22%22)%0A%20%20%20%20explanation%0A%20%20%20%20return%20error_probs%2C%20explanation%2C%20state_probs%2C%20states%2C%20total_error%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%23%23%20Interactive%20Example%3A%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(late_given_dry%2C%20late_given_rain%2C%20mo%2C%20weather_prob)%3A%0A%20%20%20%20mo.hstack(%5Bweather_prob%2C%20late_given_rain%2C%20late_given_dry%5D)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20%23%20Create%20sliders%20for%20interactive%20example%0A%20%20%20%20weather_prob%20%3D%20mo.ui.slider(0%2C%201%2C%20value%3D0.3%2C%20label%3D%22P(Rain)%22)%0A%20%20%20%20late_given_rain%20%3D%20mo.ui.slider(0%2C%201%2C%20value%3D0.6%2C%20label%3D%22P(Late%7CRain)%22)%0A%20%20%20%20late_given_dry%20%3D%20mo.ui.slider(0%2C%201%2C%20value%3D0.2%2C%20label%3D%22P(Late%7CNo%20Rain)%22)%0A%20%20%20%20return%20late_given_dry%2C%20late_given_rain%2C%20weather_prob%0A%0A%0A%40app.cell%0Adef%20_(late_given_dry%2C%20late_given_rain%2C%20mo%2C%20plt%2C%20venn2%2C%20weather_prob)%3A%0A%20%20%20%20%23%20Calculate%20probabilities%0A%20%20%20%20p_rain%20%3D%20weather_prob.value%0A%20%20%20%20p_dry%20%3D%201%20-%20p_rain%0A%20%20%20%20p_late%20%3D%20late_given_rain.value%20*%20p_rain%20%2B%20late_given_dry.value%20*%20p_dry%0A%0A%20%20%20%20%23%20Create%20explanation%0A%20%20%20%20explanation_example%20%3D%20mo.md(f%22%22%22%0A%20%20%20%20%23%23%23%20Weather%20and%20Traffic%20Analysis%0A%0A%20%20%20%20Given%3A%0A%0A%20%20%20%20-%20P(Rain)%20%3D%20%7Bp_rain%3A.2f%7D%0A%20%20%20%20-%20P(No%20Rain)%20%3D%20%7Bp_dry%3A.2f%7D%0A%20%20%20%20-%20P(Late%7CRain)%20%3D%20%7Blate_given_rain.value%3A.2f%7D%0A%20%20%20%20-%20P(Late%7CNo%20Rain)%20%3D%20%7Blate_given_dry.value%3A.2f%7D%0A%0A%20%20%20%20Using%20Law%20of%20Total%20Probability%3A%0A%0A%20%20%20%20%24P(%5Ctext%7B%7BLate%7D%7D)%20%3D%20P(%5Ctext%7B%7BLate%7D%7D%7C%5Ctext%7B%7BRain%7D%7D)P(%5Ctext%7B%7BRain%7D%7D)%20%2B%20P(%5Ctext%7B%7BLate%7D%7D%7C%5Ctext%7B%7BNo%20Rain%7D%7D)P(%5Ctext%7B%7BNo%20Rain%7D%7D)%24%0A%0A%20%20%20%20%24P(%5Ctext%7B%7BLate%7D%7D)%20%3D%20(%7Blate_given_rain.value%3A.2f%7D%20%5C%20times%20%7Bp_rain%3A.2f%7D)%20%2B%20(%7Blate_given_dry.value%3A.2f%7D%20%5C%20times%20%7Bp_dry%3A.2f%7D)%20%3D%20%7Bp_late%3A.2f%7D%24%0A%20%20%20%20%22%22%22)%0A%0A%20%20%20%20%23%20Visualize%20with%20Venn%20diagram%0A%20%20%20%20plt.figure(figsize%3D(10%2C%205))%0A%20%20%20%20_v%20%3D%20venn2(subsets%3D(%0A%20%20%20%20%20%20%20%20round(p_rain%20*%20(1%20-%20late_given_rain.value)%2C%202)%2C%20%20%23%20Rain%20only%0A%20%20%20%20%20%20%20%20round(p_dry%20*%20(1%20-%20late_given_dry.value)%2C%202)%2C%20%20%20%20%23%20No%20Rain%20only%0A%20%20%20%20%20%20%20%20round(p_rain%20*%20late_given_rain.value%2C%202)%20%20%20%20%20%20%20%20%20%23%20Intersection%0A%20%20%20%20)%2C%20set_labels%3D('Rain'%2C%20'Late'))%0A%20%20%20%20plt.title(%22Weather%20and%20Traffic%20Probability%22)%0A%0A%20%20%20%20mo.hstack(%5Bplt.gca()%2C%20explanation_example%5D)%0A%20%20%20%20return%20explanation_example%2C%20p_dry%2C%20p_late%2C%20p_rain%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(%0A%20%20%20%20%20%20%20%20r%22%22%22%0A%20%20%20%20%20%20%20%20%23%23%20Visual%20Intuition%0A%0A%20%20%20%20%20%20%20%20The%20Law%20of%20Total%20Probability%20works%20because%3A%0A%0A%20%20%20%20%20%20%20%201.%20The%20partition%20divides%20the%20sample%20space%20into%20non-overlapping%20regions%0A%20%20%20%20%20%20%20%202.%20Every%20outcome%20belongs%20to%20exactly%20one%20region%0A%20%20%20%20%20%20%20%203.%20We%20account%20for%20all%20possible%20ways%20an%20event%20can%20occur%0A%0A%20%20%20%20%20%20%20%20Let's%20visualize%20this%20with%20a%20tree%20diagram%3A%0A%20%20%20%20%20%20%20%20%22%22%22%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(plt)%3A%0A%20%20%20%20%23%20Create%20tree%20diagram%20with%20better%20spacing%0A%20%20%20%20plt.figure(figsize%3D(12%2C%208))%0A%0A%20%20%20%20%23%20First%20level%20-%20partition%20probabilities%20sum%20to%201%0A%20%20%20%20plt.plot(%5B0%2C%202%5D%2C%20%5B6%2C%209%5D%2C%20'k-'%2C%20linewidth%3D2)%20%20%23%20B%E2%82%81%20branch%0A%20%20%20%20plt.plot(%5B0%2C%202%5D%2C%20%5B6%2C%206%5D%2C%20'k-'%2C%20linewidth%3D2)%20%20%23%20B%E2%82%82%20branch%0A%20%20%20%20plt.plot(%5B0%2C%202%5D%2C%20%5B6%2C%203%5D%2C%20'k-'%2C%20linewidth%3D2)%20%20%23%20B%E2%82%83%20branch%0A%0A%20%20%20%20%23%20Second%20level%20-%20conditional%20probabilities%20sum%20to%201%20for%20each%20branch%0A%20%20%20%20plt.plot(%5B2%2C%204%5D%2C%20%5B9%2C%2010%5D%2C%20'b-'%2C%20linewidth%3D2)%20%20%23%20A%7CB%E2%82%81%0A%20%20%20%20plt.plot(%5B2%2C%204%5D%2C%20%5B9%2C%208%5D%2C%20'r-'%2C%20linewidth%3D2)%20%20%20%23%20A%E1%B6%9C%7CB%E2%82%81%0A%20%20%20%20plt.plot(%5B2%2C%204%5D%2C%20%5B6%2C%207%5D%2C%20'b-'%2C%20linewidth%3D2)%20%20%20%23%20A%7CB%E2%82%82%0A%20%20%20%20plt.plot(%5B2%2C%204%5D%2C%20%5B6%2C%205%5D%2C%20'r-'%2C%20linewidth%3D2)%20%20%20%23%20A%E1%B6%9C%7CB%E2%82%82%0A%20%20%20%20plt.plot(%5B2%2C%204%5D%2C%20%5B3%2C%204%5D%2C%20'b-'%2C%20linewidth%3D2)%20%20%20%23%20A%7CB%E2%82%83%0A%20%20%20%20plt.plot(%5B2%2C%204%5D%2C%20%5B3%2C%202%5D%2C%20'r-'%2C%20linewidth%3D2)%20%20%20%23%20A%E1%B6%9C%7CB%E2%82%83%0A%0A%20%20%20%20%23%20Add%20labels%20with%20actual%20probabilities%0A%20%20%20%20plt.text(0%2C%206.2%2C%20'S%20(1.0)'%2C%20fontsize%3D12)%0A%20%20%20%20plt.text(2%2C%209.2%2C%20'B%E2%82%81%20(1%2F3)'%2C%20fontsize%3D12)%0A%20%20%20%20plt.text(2%2C%206.2%2C%20'B%E2%82%82%20(1%2F3)'%2C%20fontsize%3D12)%0A%20%20%20%20plt.text(2%2C%203.2%2C%20'B%E2%82%83%20(1%2F3)'%2C%20fontsize%3D12)%0A%0A%20%20%20%20%23%20Add%20conditional%20probability%20labels%0A%20%20%20%20plt.text(4%2C%2010.2%2C%20'A%20(P(A%7CB%E2%82%81))'%2C%20fontsize%3D10%2C%20color%3D'blue')%0A%20%20%20%20plt.text(4%2C%207.8%2C%20'A%E1%B6%9C%20(1-P(A%7CB%E2%82%81))'%2C%20fontsize%3D10%2C%20color%3D'red')%0A%20%20%20%20plt.text(4%2C%207.2%2C%20'A%20(P(A%7CB%E2%82%82))'%2C%20fontsize%3D10%2C%20color%3D'blue')%0A%20%20%20%20plt.text(4%2C%204.8%2C%20'A%E1%B6%9C%20(1-P(A%7CB%E2%82%82))'%2C%20fontsize%3D10%2C%20color%3D'red')%0A%20%20%20%20plt.text(4%2C%204.2%2C%20'A%20(P(A%7CB%E2%82%83))'%2C%20fontsize%3D10%2C%20color%3D'blue')%0A%20%20%20%20plt.text(4%2C%201.8%2C%20'A%E1%B6%9C%20(1-P(A%7CB%E2%82%83))'%2C%20fontsize%3D10%2C%20color%3D'red')%0A%0A%20%20%20%20plt.axis('off')%0A%20%20%20%20plt.gca()%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(%0A%20%20%20%20%20%20%20%20r%22%22%22%0A%20%20%20%20%20%20%20%20%23%23%20%F0%9F%A4%94%20Test%20Your%20Understanding%0A%0A%20%20%20%20%20%20%20%20For%20a%20fair%20six-sided%20die%20with%20partitions%3A%0A%20%20%20%20%20%20%20%20-%20B%E2%82%81%3A%20Numbers%20less%20than%203%20%7B1%2C2%7D%0A%20%20%20%20%20%20%20%20-%20B%E2%82%82%3A%20Numbers%20from%203%20to%204%20%7B3%2C4%7D%0A%20%20%20%20%20%20%20%20-%20B%E2%82%83%3A%20Numbers%20greater%20than%204%20%7B5%2C6%7D%0A%0A%20%20%20%20%20%20%20%20**Question%201**%3A%20Which%20of%20these%20statements%20correctly%20describes%20the%20partition%3F%0A%20%20%20%20%20%20%20%20%3Cdetails%3E%0A%20%20%20%20%20%20%20%20%3Csummary%3EThe%20sets%20overlap%20at%20number%203%3C%2Fsummary%3E%0A%20%20%20%20%20%20%20%20%E2%9D%8C%20Incorrect!%20The%20sets%20are%20clearly%20separated%20with%20no%20overlapping%20numbers.%0A%20%20%20%20%20%20%20%20%3C%2Fdetails%3E%0A%20%20%20%20%20%20%20%20%3Cdetails%3E%0A%20%20%20%20%20%20%20%20%3Csummary%3ESome%20numbers%20are%20missing%20from%20the%20partition%3C%2Fsummary%3E%0A%20%20%20%20%20%20%20%20%E2%9D%8C%20Incorrect!%20All%20numbers%20from%201%20to%206%20are%20included%20exactly%20once.%0A%20%20%20%20%20%20%20%20%3C%2Fdetails%3E%0A%20%20%20%20%20%20%20%20%3Cdetails%3E%0A%20%20%20%20%20%20%20%20%3Csummary%3EThe%20sets%20form%20a%20valid%20partition%20of%20%7B1%2C2%2C3%2C4%2C5%2C6%7D%3C%2Fsummary%3E%0A%20%20%20%20%20%20%20%20%E2%9C%85%20Correct!%20The%20sets%20are%20mutually%20exclusive%20and%20their%20union%20covers%20all%20outcomes.%0A%20%20%20%20%20%20%20%20%3C%2Fdetails%3E%0A%20%20%20%20%20%20%20%20%22%22%22%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(%0A%20%20%20%20%20%20%20%20%22%22%22%0A%20%20%20%20%20%20%20%20%23%23%20Summary%0A%0A%20%20%20%20%20%20%20%20You've%20learned%3A%0A%0A%20%20%20%20%20%20%20%20-%20How%20to%20identify%20valid%20partitions%20of%20a%20sample%20space%0A%20%20%20%20%20%20%20%20-%20The%20Law%20of%20Total%20Probability%20formula%20and%20its%20components%0A%20%20%20%20%20%20%20%20-%20How%20to%20break%20down%20complex%20probability%20calculations%0A%20%20%20%20%20%20%20%20-%20Applications%20to%20real-world%20scenarios%0A%0A%20%20%20%20%20%20%20%20In%20the%20next%20lesson%2C%20we'll%20explore%20**Bayes'%20Theorem**%2C%20which%20builds%20on%20these%20concepts%20to%20solve%20even%20more%20sophisticated%20probability%20problems.%0A%20%20%20%20%20%20%20%20%22%22%22%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0Aif%20__name__%20%3D%3D%20%22__main__%22%3A%0A%20%20%20%20app.run()%0A</marimo-code></head> | |
<body> | |
<div id="root"></div> | |
</body> | |
</html> | |