marimo-learn / _site /probability /08_bayes_theorem.html
Haleshot's picture
relevant assets and workflow
1cce1df unverified
raw
history blame
32.3 kB
<!DOCTYPE html>
<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": {"default_width": "medium", "dataframes": "rich", "theme": "light", "code_editor_font_size": 14, "cell_output": "above"}, "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", "app_title": "Bayes Theorem"}' hidden></marimo-app-config>
<marimo-server-token data-token='123' hidden></marimo-server-token>
<title>Bayes Theorem</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 !important;
}
#filename-input {
display: none !important;
}
</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%2C%20app_title%3D%22Bayes%20Theorem%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%20import%20numpy%20as%20np%0A%20%20%20%20return%20np%2C%20plt%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%20Bayes'%20Theorem%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%2Fbayes_theorem%2F)%2C%20by%20Stanford%20professor%20Chris%20Piech._%0A%0A%20%20%20%20%20%20%20%20In%20the%201740s%2C%20an%20English%20minister%20named%20Thomas%20Bayes%20discovered%20a%20profound%20mathematical%20relationship%20that%20would%20revolutionize%20how%20we%20reason%20about%20uncertainty.%20His%20theorem%20provides%20an%20elegant%20framework%20for%20calculating%20the%20probability%20of%20a%20hypothesis%20being%20true%20given%20observed%20evidence.%0A%0A%20%20%20%20%20%20%20%20At%20its%20core%2C%20Bayes'%20Theorem%20connects%20two%20different%20types%20of%20probabilities%3A%20the%20probability%20of%20a%20hypothesis%20given%20evidence%20%24P(H%7CE)%24%2C%20and%20its%20reverse%20-%20the%20probability%20of%20evidence%20given%20a%20hypothesis%20%24P(E%7CH)%24.%20This%20relationship%20is%20particularly%20powerful%20because%20it%20allows%20us%20to%20compute%20difficult%20probabilities%20using%20ones%20that%20are%20easier%20to%20measure.%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%20Heart%20of%20Bayesian%20Reasoning%0A%0A%20%20%20%20%20%20%20%20The%20fundamental%20insight%20of%20Bayes'%20Theorem%20lies%20in%20its%20ability%20to%20relate%20what%20we%20want%20to%20know%20with%20what%20we%20can%20measure.%20When%20we%20observe%20evidence%20%24E%24%2C%20we%20often%20want%20to%20know%20the%20probability%20of%20a%20hypothesis%20%24H%24%20being%20true.%20However%2C%20it's%20typically%20much%20easier%20to%20measure%20how%20likely%20we%20are%20to%20observe%20the%20evidence%20when%20we%20know%20the%20hypothesis%20is%20true.%0A%0A%20%20%20%20%20%20%20%20This%20reversal%20of%20perspective%20-%20from%20%24P(H%7CE)%24%20to%20%24P(E%7CH)%24%20-%20is%20powerful%20because%20it%20lets%20us%3A%0A%20%20%20%20%20%20%20%201.%20Start%20with%20what%20we%20know%20(prior%20beliefs)%0A%20%20%20%20%20%20%20%202.%20Use%20easily%20measurable%20relationships%20(likelihood)%0A%20%20%20%20%20%20%20%203.%20Update%20our%20beliefs%20with%20new%20evidence%0A%0A%20%20%20%20%20%20%20%20This%20approach%20mirrors%20both%20how%20humans%20naturally%20learn%20and%20the%20scientific%20method%3A%20we%20begin%20with%20prior%20beliefs%2C%20gather%20evidence%2C%20and%20update%20our%20understanding%20based%20on%20that%20evidence.%20This%20makes%20Bayes'%20Theorem%20not%20just%20a%20mathematical%20tool%2C%20but%20a%20framework%20for%20rational%20thinking.%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%20Formula%0A%0A%20%20%20%20%20%20%20%20Bayes'%20Theorem%20states%3A%0A%0A%20%20%20%20%20%20%20%20%24P(H%7CE)%20%3D%20%5Cfrac%7BP(E%7CH)P(H)%7D%7BP(E)%7D%24%0A%0A%20%20%20%20%20%20%20%20Where%3A%0A%0A%20%20%20%20%20%20%20%20-%20%24P(H%7CE)%24%20is%20the%20**posterior%20probability**%20-%20probability%20of%20hypothesis%20H%20given%20evidence%20E%0A%20%20%20%20%20%20%20%20-%20%24P(E%7CH)%24%20is%20the%20**likelihood**%20-%20probability%20of%20evidence%20E%20given%20hypothesis%20H%0A%20%20%20%20%20%20%20%20-%20%24P(H)%24%20is%20the%20**prior%20probability**%20-%20initial%20probability%20of%20hypothesis%20H%0A%20%20%20%20%20%20%20%20-%20%24P(E)%24%20is%20the%20**evidence**%20-%20total%20probability%20of%20observing%20evidence%20E%0A%0A%20%20%20%20%20%20%20%20The%20denominator%20%24P(E)%24%20can%20be%20expanded%20using%20the%20%5BLaw%20of%20Total%20Probability%5D(https%3A%2F%2Fmarimo.app%2Fgh%2Fmarimo-team%2Flearn%2Fmain%3Fentrypoint%3Dprobability%252F07_law_of_total_probability.py)%3A%0A%0A%20%20%20%20%20%20%20%20%24P(E)%20%3D%20P(E%7CH)P(H)%20%2B%20P(E%7CH%5Ec)P(H%5Ec)%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(%0A%20%20%20%20%20%20%20%20r%22%22%22%0A%20%20%20%20%20%20%20%20%23%23%20Understanding%20Each%20Component%0A%0A%20%20%20%20%20%20%20%20%23%23%23%201.%20Prior%20Probability%20-%20%24P(H)%24%0A%20%20%20%20%20%20%20%20-%20Initial%20belief%20about%20hypothesis%20before%20seeing%20evidence%0A%20%20%20%20%20%20%20%20-%20Based%20on%20previous%20knowledge%20or%20assumptions%0A%20%20%20%20%20%20%20%20-%20Example%3A%20Probability%20of%20having%20a%20disease%20before%20any%20tests%0A%0A%20%20%20%20%20%20%20%20%23%23%23%202.%20Likelihood%20-%20%24P(E%7CH)%24%0A%20%20%20%20%20%20%20%20-%20Probability%20of%20evidence%20given%20hypothesis%20is%20true%0A%20%20%20%20%20%20%20%20-%20Often%20known%20from%20data%20or%20scientific%20studies%0A%20%20%20%20%20%20%20%20-%20Example%3A%20Probability%20of%20positive%20test%20given%20disease%20present%0A%0A%20%20%20%20%20%20%20%20%23%23%23%203.%20Evidence%20-%20%24P(E)%24%0A%20%20%20%20%20%20%20%20-%20Total%20probability%20of%20observing%20the%20evidence%0A%20%20%20%20%20%20%20%20-%20Acts%20as%20a%20normalizing%20constant%0A%20%20%20%20%20%20%20%20-%20Can%20be%20calculated%20using%20Law%20of%20Total%20Probability%0A%0A%20%20%20%20%20%20%20%20%23%23%23%204.%20Posterior%20-%20%24P(H%7CE)%24%0A%20%20%20%20%20%20%20%20-%20Updated%20probability%20after%20considering%20evidence%0A%20%20%20%20%20%20%20%20-%20Combines%20prior%20knowledge%20with%20new%20evidence%0A%20%20%20%20%20%20%20%20-%20Becomes%20new%20prior%20for%20future%20updates%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%20Real-World%20Examples%0A%0A%20%20%20%20%20%20%20%20%23%23%23%201.%20Medical%20Testing%0A%20%20%20%20%20%20%20%20-%20**Want%20to%20know**%3A%20%24P(%5Ctext%7BDisease%7D%7C%5Ctext%7BPositive%7D)%24%20-%20Probability%20of%20disease%20given%20positive%20test%0A%20%20%20%20%20%20%20%20-%20**Easy%20to%20know**%3A%20%24P(%5Ctext%7BPositive%7D%7C%5Ctext%7BDisease%7D)%24%20-%20Test%20accuracy%20for%20sick%20people%0A%20%20%20%20%20%20%20%20-%20**Causality**%3A%20Disease%20causes%20test%20results%2C%20not%20vice%20versa%0A%0A%20%20%20%20%20%20%20%20%23%23%23%202.%20Student%20Ability%0A%20%20%20%20%20%20%20%20-%20**Want%20to%20know**%3A%20%24P(%5Ctext%7BHigh%20Ability%7D%7C%5Ctext%7BGood%20Grade%7D)%24%20-%20Probability%20student%20is%20skilled%20given%20good%20grade%0A%20%20%20%20%20%20%20%20-%20**Easy%20to%20know**%3A%20%24P(%5Ctext%7BGood%20Grade%7D%7C%5Ctext%7BHigh%20Ability%7D)%24%20-%20Probability%20good%20students%20get%20good%20grades%0A%20%20%20%20%20%20%20%20-%20**Causality**%3A%20Ability%20influences%20grades%2C%20not%20vice%20versa%0A%0A%20%20%20%20%20%20%20%20%23%23%23%203.%20Cell%20Phone%20Location%0A%20%20%20%20%20%20%20%20-%20**Want%20to%20know**%3A%20%24P(%5Ctext%7BLocation%7D%7C%5Ctext%7BSignal%20Strength%7D)%24%20-%20Probability%20of%20phone%20location%20given%20signal%0A%20%20%20%20%20%20%20%20-%20**Easy%20to%20know**%3A%20%24P(%5Ctext%7BSignal%20Strength%7D%7C%5Ctext%7BLocation%7D)%24%20-%20Signal%20strength%20at%20known%20locations%0A%20%20%20%20%20%20%20%20-%20**Causality**%3A%20Location%20determines%20signal%20strength%2C%20not%20vice%20versa%0A%0A%20%20%20%20%20%20%20%20These%20examples%20highlight%20a%20common%20pattern%3A%20what%20we%20want%20to%20know%20(posterior)%20is%20harder%20to%20measure%20directly%20than%20its%20reverse%20(likelihood).%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%20calculate_posterior(prior%2C%20likelihood%2C%20false_positive_rate)%3A%0A%20%20%20%20%20%20%20%20%23%20Calculate%20P(E)%20using%20Law%20of%20Total%20Probability%0A%20%20%20%20%20%20%20%20p_e%20%3D%20likelihood%20*%20prior%20%2B%20false_positive_rate%20*%20(1%20-%20prior)%0A%0A%20%20%20%20%20%20%20%20%23%20Calculate%20posterior%20using%20Bayes'%20Theorem%0A%20%20%20%20%20%20%20%20posterior%20%3D%20(likelihood%20*%20prior)%20%2F%20p_e%0A%20%20%20%20%20%20%20%20return%20posterior%2C%20p_e%0A%20%20%20%20return%20(calculate_posterior%2C)%0A%0A%0A%40app.cell%0Adef%20_(calculate_posterior)%3A%0A%20%20%20%20%23%20Medical%20test%20example%0A%20%20%20%20p_disease%20%3D%200.01%20%20%23%20Prior%3A%201%25%20have%20the%20disease%0A%20%20%20%20p_positive_given_disease%20%3D%200.95%20%20%23%20Likelihood%3A%2095%25%20test%20accuracy%0A%20%20%20%20p_positive_given_healthy%20%3D%200.10%20%20%23%20False%20positive%20rate%3A%2010%25%0A%0A%20%20%20%20medical_posterior%2C%20medical_evidence%20%3D%20calculate_posterior(%0A%20%20%20%20%20%20%20%20p_disease%2C%0A%20%20%20%20%20%20%20%20p_positive_given_disease%2C%0A%20%20%20%20%20%20%20%20p_positive_given_healthy%0A%20%20%20%20)%0A%20%20%20%20return%20(%0A%20%20%20%20%20%20%20%20medical_evidence%2C%0A%20%20%20%20%20%20%20%20medical_posterior%2C%0A%20%20%20%20%20%20%20%20p_disease%2C%0A%20%20%20%20%20%20%20%20p_positive_given_disease%2C%0A%20%20%20%20%20%20%20%20p_positive_given_healthy%2C%0A%20%20%20%20)%0A%0A%0A%40app.cell%0Adef%20_(medical_explanation)%3A%0A%20%20%20%20medical_explanation%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(medical_posterior%2C%20mo)%3A%0A%20%20%20%20medical_explanation%20%3D%20mo.md(f%22%22%22%0A%20%20%20%20%23%23%23%20Medical%20Testing%20Example%0A%0A%20%20%20%20Consider%20a%20medical%20test%20for%20a%20rare%20disease%3A%0A%0A%20%20%20%20-%20Prior%3A%201%25%20of%20population%20has%20the%20disease%0A%20%20%20%20-%20Likelihood%3A%2095%25%20test%20accuracy%20for%20sick%20people%0A%20%20%20%20-%20False%20positive%3A%2010%25%20of%20healthy%20people%20test%20positive%0A%0A%20%20%20%20Using%20Bayes'%20Theorem%3A%0A%20%20%20%20%24P(D%7C%2B)%20%3D%20%5C%5Cfrac%7B%7B0.95%20times%200.01%7D%7D%7B%7B0.95%20times%200.01%20%2B%200.10%20times%200.99%7D%7D%20%3D%20%7Bmedical_posterior%3A.3f%7D%24%0A%0A%20%20%20%20Despite%20a%20positive%20test%2C%20there's%20only%20a%20%7Bmedical_posterior%3A.1%25%7D%20chance%20of%20having%20the%20disease!%0A%20%20%20%20This%20counterintuitive%20result%20occurs%20because%20the%20disease%20is%20rare%20(low%20prior%20probability).%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%20(medical_explanation%2C)%0A%0A%0A%40app.cell%0Adef%20_(calculate_posterior)%3A%0A%20%20%20%20%23%20Student%20ability%20example%0A%20%20%20%20p_high_ability%20%3D%200.30%20%20%23%20Prior%3A%2030%25%20of%20students%20have%20high%20ability%0A%20%20%20%20p_good_grade_given_high%20%3D%200.90%20%20%23%20Likelihood%3A%2090%25%20of%20high%20ability%20students%20get%20good%20grades%0A%20%20%20%20p_good_grade_given_low%20%3D%200.40%20%20%23%2040%25%20of%20lower%20ability%20students%20also%20get%20good%20grades%0A%0A%20%20%20%20student_posterior%2C%20student_evidence%20%3D%20calculate_posterior(%0A%20%20%20%20%20%20%20%20p_high_ability%2C%0A%20%20%20%20%20%20%20%20p_good_grade_given_high%2C%0A%20%20%20%20%20%20%20%20p_good_grade_given_low%0A%20%20%20%20)%0A%20%20%20%20return%20(%0A%20%20%20%20%20%20%20%20p_good_grade_given_high%2C%0A%20%20%20%20%20%20%20%20p_good_grade_given_low%2C%0A%20%20%20%20%20%20%20%20p_high_ability%2C%0A%20%20%20%20%20%20%20%20student_evidence%2C%0A%20%20%20%20%20%20%20%20student_posterior%2C%0A%20%20%20%20)%0A%0A%0A%40app.cell%0Adef%20_(student_explanation)%3A%0A%20%20%20%20student_explanation%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo%2C%20student_posterior)%3A%0A%20%20%20%20student_explanation%20%3D%20mo.md(f%22%22%22%0A%20%20%20%20%23%23%23%20Student%20Ability%20Example%0A%0A%20%20%20%20If%20a%20student%20gets%20a%20good%20grade%2C%20what's%20the%20probability%20they%20have%20high%20ability%3F%0A%0A%20%20%20%20Using%20Bayes'%20Theorem%3A%0A%0A%20%20%20%20-%20Prior%3A%2030%25%20have%20high%20ability%0A%20%20%20%20-%20Likelihood%3A%2090%25%20of%20high%20ability%20students%20get%20good%20grades%0A%20%20%20%20-%20False%20positive%3A%2040%25%20of%20lower%20ability%20students%20get%20good%20grades%0A%0A%20%20%20%20Result%3A%20P(High%20Ability%7CGood%20Grade)%20%3D%20%7Bstudent_posterior%3A.2f%7D%0A%0A%20%20%20%20So%20a%20good%20grade%20increases%20our%20confidence%20in%20high%20ability%20from%2030%25%20to%20%7Bstudent_posterior%3A.1%25%7D%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%20(student_explanation%2C)%0A%0A%0A%40app.cell%0Adef%20_(calculate_posterior)%3A%0A%20%20%20%20%23%20Cell%20phone%20location%20example%0A%20%20%20%20p_location_a%20%3D%200.25%20%20%23%20Prior%20probability%20of%20being%20in%20location%20A%0A%20%20%20%20p_strong_signal_at_a%20%3D%200.85%20%20%23%20Likelihood%20of%20strong%20signal%20at%20A%0A%20%20%20%20p_strong_signal_elsewhere%20%3D%200.15%20%20%23%20False%20positive%20rate%0A%0A%20%20%20%20location_posterior%2C%20location_evidence%20%3D%20calculate_posterior(%0A%20%20%20%20%20%20%20%20p_location_a%2C%0A%20%20%20%20%20%20%20%20p_strong_signal_at_a%2C%0A%20%20%20%20%20%20%20%20p_strong_signal_elsewhere%0A%20%20%20%20)%0A%20%20%20%20return%20(%0A%20%20%20%20%20%20%20%20location_evidence%2C%0A%20%20%20%20%20%20%20%20location_posterior%2C%0A%20%20%20%20%20%20%20%20p_location_a%2C%0A%20%20%20%20%20%20%20%20p_strong_signal_at_a%2C%0A%20%20%20%20%20%20%20%20p_strong_signal_elsewhere%2C%0A%20%20%20%20)%0A%0A%0A%40app.cell%0Adef%20_(location_explanation)%3A%0A%20%20%20%20location_explanation%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(location_posterior%2C%20mo)%3A%0A%20%20%20%20location_explanation%20%3D%20mo.md(f%22%22%22%0A%20%20%20%20%23%23%23%20Cell%20Phone%20Location%20Example%0A%0A%20%20%20%20Given%20a%20strong%20signal%2C%20what's%20the%20probability%20the%20phone%20is%20in%20location%20A%3F%0A%0A%20%20%20%20Using%20Bayes'%20Theorem%3A%0A%0A%20%20%20%20-%20Prior%3A%2025%25%20chance%20of%20being%20in%20location%20A%0A%20%20%20%20-%20Likelihood%3A%2085%25%20chance%20of%20strong%20signal%20at%20A%0A%20%20%20%20-%20False%20positive%3A%2015%25%20chance%20of%20strong%20signal%20elsewhere%0A%0A%20%20%20%20Result%3A%20P(Location%20A%7CStrong%20Signal)%20%3D%20%7Blocation_posterior%3A.2f%7D%0A%0A%20%20%20%20The%20strong%20signal%20increases%20our%20confidence%20in%20location%20A%20from%2025%25%20to%20%7Blocation_posterior%3A.1%25%7D%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%20(location_explanation%2C)%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%22%22%22)%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%0A%20%20%20%20%20%20%20%20_This%20interactive%20example%20was%20made%20with%20%5Bmarimo%5D(https%3A%2F%2Fgithub.com%2Fmarimo-team%2Fmarimo%2Fblob%2Fmain%2Fexamples%2Fmisc%2Fbayes_theorem.py)%2C%20and%20is%20%5Bbased%20on%20an%20explanation%20of%20Bayes'%20Theorem%20by%20Grant%20Sanderson%5D(https%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DHZGCoVF3YvM%26list%3DPLzq7odmtfKQw2KIbQq0rzWrqgifHKkPG1%26index%3D1%26t%3D3s)_.%0A%0A%20%20%20%20%20%20%20%20Bayes%20theorem%20provides%20a%20convenient%20way%20to%20calculate%20the%20probability%0A%20%20%20%20%20%20%20%20of%20a%20hypothesis%20event%20%24H%24%20given%20evidence%20%24E%24%3A%0A%0A%20%20%20%20%20%20%20%20%5C%5B%0A%20%20%20%20%20%20%20%20P(H%20%5Cmid%20E)%20%3D%20%5Cfrac%7BP(H)%20P(E%20%5Cmid%20H)%7D%7BP(E)%7D.%0A%20%20%20%20%20%20%20%20%5C%5D%0A%0A%0A%20%20%20%20%20%20%20%20**The%20numerator.**%20The%20numerator%20is%20the%20probability%20of%20events%20%24E%24%20and%20%24H%24%20happening%0A%20%20%20%20%20%20%20%20together%3B%20that%20is%2C%0A%0A%20%20%20%20%20%20%20%20%5C%5B%0A%20%20%20%20%20%20%20%20%20%20%20P(H)%20P(E%20%5Cmid%20H)%20%3D%20P(E%20%5Ccap%20H).%0A%20%20%20%20%20%20%20%20%5C%5D%0A%0A%20%20%20%20%20%20%20%20**The%20denominator.**%0A%20%20%20%20%20%20%20%20In%20most%20calculations%2C%20it%20is%20helpful%20to%20rewrite%20the%20denominator%20%24P(E)%24%20as%20%0A%0A%20%20%20%20%20%20%20%20%5C%5B%0A%20%20%20%20%20%20%20%20P(E)%20%3D%20P(H)P(E%20%5Cmid%20H)%20%2B%20P(%5Cneg%20H)%20P%20(E%20%5Cmid%20%5Cneg%20H)%2C%0A%20%20%20%20%20%20%20%20%5C%5D%0A%0A%20%20%20%20%20%20%20%20which%20in%20turn%20can%20also%20be%20written%20as%0A%0A%0A%20%20%20%20%20%20%20%20%5C%5B%0A%20%20%20%20%20%20%20%20P(E)%20%3D%20P(E%20%5Ccap%20H)%20%2B%20P(E%20%5Ccap%20%5Cneg%20H).%0A%20%20%20%20%20%20%20%20%5C%5D%0A%20%20%20%20%20%20%20%20%22%22%22%0A%20%20%20%20).left()%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(%0A%20%20%20%20bayes_result%2C%0A%20%20%20%20construct_probability_plot%2C%0A%20%20%20%20mo%2C%0A%20%20%20%20p_e%2C%0A%20%20%20%20p_e_given_h%2C%0A%20%20%20%20p_e_given_not_h%2C%0A%20%20%20%20p_h%2C%0A)%3A%0A%20%20%20%20mo.hstack(%0A%20%20%20%20%20%20%20%20%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20mo.md(%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20rf%22%22%22%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%23%23%23%20Probability%20parameters%0A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20You%20can%20configure%20the%20probabilities%20of%20the%20events%20%24H%24%2C%20%24E%20%5Cmid%20H%24%2C%20and%20%24E%20%5Cmid%20%5Cneg%20H%24%0A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7Bmo.as_html(%5Bp_h%2C%20p_e_given_h%2C%20p_e_given_not_h%5D)%7D%0A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20The%20plot%20on%20the%20right%20visualizes%20the%20probabilities%20of%20these%20events.%20%0A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%201.%20The%20yellow%20rectangle%20represents%20the%20event%20%24H%24%2C%20and%20its%20area%20is%20%24P(H)%20%3D%20%7Bp_h.value%3A0.2f%7D%24.%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%202.%20The%20teal%20rectangle%20overlapping%20with%20the%20yellow%20one%20represents%20the%20event%20%24E%20%5Ccap%20H%24%2C%20and%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20its%20area%20is%20%24P(H)%20%5Ccdot%20P(E%20%5Cmid%20H)%20%3D%20%7Bp_h.value%20*%20p_e_given_h.value%3A0.2f%7D%24.%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%203.%20The%20teal%20rectangle%20that%20doesn't%20overlap%20the%20yellow%20rectangle%20represents%20the%20event%20%24E%20%5Ccap%20%5Cneg%20H%24%2C%20and%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20its%20area%20is%20%24P(%5Cneg%20H)%20%5Ccdot%20P(E%20%5Cmid%20%5Cneg%20H)%20%3D%20%7B(1%20-%20p_h.value)%20*%20p_e_given_not_h.value%3A0.2f%7D%24.%0A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20Notice%20that%20the%20sum%20of%20the%20areas%20in%20%242%24%20and%20%243%24%20is%20the%20probability%20%24P(E)%20%3D%20%7Bp_e%3A0.2f%7D%24.%20%0A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20One%20way%20to%20think%20about%20Bayes'%20Theorem%20is%20the%20following%3A%20the%20probability%20%24P(H%20%5Cmid%20E)%24%20is%20the%20probability%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20of%20%24E%24%20and%20%24H%24%20happening%20together%20(the%20area%20of%20the%20rectangle%20%242%24)%2C%20divided%20by%20the%20probability%20of%20%24E%24%20happening%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20at%20all%20(the%20sum%20of%20the%20areas%20of%20%242%24%20and%20%243%24).%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20In%20this%20case%2C%20Bayes'%20Theorem%20says%0A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%5C%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20P(H%20%5Cmid%20E)%20%3D%20%5Cfrac%7B%7BP(H)%20P(E%20%5Cmid%20H)%7D%7D%7B%7BP(E)%7D%7D%20%3D%20%5Cfrac%7B%7B%7Bp_h.value%7D%20%5Ccdot%20%7Bp_e_given_h.value%7D%7D%7D%7B%7B%7Bp_e%3A0.2f%7D%7D%7D%20%3D%20%7Bbayes_result%3A0.2f%7D%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%5C%5D%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22%22%22%0A%20%20%20%20%20%20%20%20%20%20%20%20)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20construct_probability_plot()%2C%0A%20%20%20%20%20%20%20%20%5D%2C%0A%20%20%20%20%20%20%20%20justify%3D%22start%22%2C%0A%20%20%20%20%20%20%20%20gap%3D4%2C%0A%20%20%20%20%20%20%20%20align%3D%22start%22%2C%0A%20%20%20%20%20%20%20%20widths%3D%5B0.33%2C%200.5%5D%2C%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%20Applications%20in%20Computer%20Science%0A%0A%20%20%20%20%20%20%20%20Bayes'%20Theorem%20is%20fundamental%20in%20many%20computing%20applications%3A%0A%0A%20%20%20%20%20%20%20%201.%20**Spam%20Filtering**%0A%0A%20%20%20%20%20%20%20%20%20%20%20%20-%20%24P(%5Ctext%7BSpam%7D%7C%5Ctext%7BWords%7D)%24%20%3D%20Probability%20email%20is%20spam%20given%20its%20words%0A%20%20%20%20%20%20%20%20%20%20%20%20-%20Updates%20as%20new%20emails%20are%20classified%0A%0A%20%20%20%20%20%20%20%202.%20**Machine%20Learning**%0A%0A%20%20%20%20%20%20%20%20%20%20%20%20-%20Naive%20Bayes%20classifiers%0A%20%20%20%20%20%20%20%20%20%20%20%20-%20Probabilistic%20graphical%20models%0A%20%20%20%20%20%20%20%20%20%20%20%20-%20Bayesian%20neural%20networks%0A%0A%20%20%20%20%20%20%20%203.%20**Computer%20Vision**%0A%0A%20%20%20%20%20%20%20%20%20%20%20%20-%20Object%20detection%20confidence%0A%20%20%20%20%20%20%20%20%20%20%20%20-%20Face%20recognition%20systems%0A%20%20%20%20%20%20%20%20%20%20%20%20-%20Image%20classification%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%20%F0%9F%A4%94%20Test%20Your%20Understanding%0A%0A%20%20%20%20%20%20%20%20Pick%20which%20of%20these%20statements%20about%20Bayes'%20Theorem%20you%20think%20are%20correct%3A%0A%0A%20%20%20%20%20%20%20%20%3Cdetails%3E%0A%20%20%20%20%20%20%20%20%3Csummary%3EThe%20posterior%20probability%20will%20always%20be%20larger%20than%20the%20prior%20probability%3C%2Fsummary%3E%0A%20%20%20%20%20%20%20%20%E2%9D%8C%20Incorrect!%20Evidence%20can%20either%20increase%20or%20decrease%20our%20belief%20in%20the%20hypothesis.%20For%20example%2C%20a%20negative%20medical%20test%20decreases%20the%20probability%20of%20having%20a%20disease.%0A%20%20%20%20%20%20%20%20%3C%2Fdetails%3E%0A%0A%20%20%20%20%20%20%20%20%3Cdetails%3E%0A%20%20%20%20%20%20%20%20%3Csummary%3EIf%20the%20likelihood%20is%200.9%20and%20the%20prior%20is%200.5%2C%20then%20the%20posterior%20must%20equal%200.9%3C%2Fsummary%3E%0A%20%20%20%20%20%20%20%20%E2%9D%8C%20Incorrect!%20We%20also%20need%20the%20false%20positive%20rate%20to%20calculate%20the%20posterior%20probability.%20The%20likelihood%20alone%20doesn't%20determine%20the%20posterior.%0A%20%20%20%20%20%20%20%20%3C%2Fdetails%3E%0A%0A%20%20%20%20%20%20%20%20%3Cdetails%3E%0A%20%20%20%20%20%20%20%20%3Csummary%3EThe%20denominator%20acts%20as%20a%20normalizing%20constant%20to%20ensure%20the%20posterior%20is%20a%20valid%20probability%3C%2Fsummary%3E%0A%20%20%20%20%20%20%20%20%E2%9C%85%20Correct!%20The%20denominator%20ensures%20the%20posterior%20probability%20is%20between%200%20and%201%20by%20considering%20all%20ways%20the%20evidence%20could%20occur.%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-%20The%20components%20and%20intuition%20behind%20Bayes'%20Theorem%0A%20%20%20%20%20%20%20%20-%20How%20to%20update%20probabilities%20when%20new%20evidence%20arrives%0A%20%20%20%20%20%20%20%20-%20Why%20posterior%20probabilities%20can%20be%20counterintuitive%0A%20%20%20%20%20%20%20%20-%20Real-world%20applications%20in%20computer%20science%0A%0A%20%20%20%20%20%20%20%20In%20the%20next%20lesson%2C%20we'll%20explore%20Random%20Variables%2C%20which%20help%20us%20work%20with%20numerical%20outcomes%20in%20probability.%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%23%20Appendix%0A%20%20%20%20%20%20%20%20Below%20(hidden)%20cell%20blocks%20are%20responsible%20for%20the%20interactive%20example%20above%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_(p_e_given_h%2C%20p_e_given_not_h%2C%20p_h)%3A%0A%20%20%20%20p_e%20%3D%20p_h.value*p_e_given_h.value%20%2B%20(1%20-%20p_h.value)*p_e_given_not_h.value%0A%20%20%20%20bayes_result%20%3D%20p_h.value%20*%20p_e_given_h.value%20%2F%20p_e%0A%20%20%20%20return%20bayes_result%2C%20p_e%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20p_h%20%3D%20mo.ui.slider(0.0%2C%201%2C%20label%3D%22%24P(H)%24%22%2C%20value%3D0.1%2C%20step%3D0.1)%0A%20%20%20%20p_e_given_h%20%3D%20mo.ui.slider(0.0%2C%201%2C%20label%3D%22%24P(E%20%5Cmid%20H)%24%22%2C%20value%3D0.3%2C%20step%3D0.1)%0A%20%20%20%20p_e_given_not_h%20%3D%20mo.ui.slider(%0A%20%20%20%20%20%20%20%200.0%2C%201%2C%20label%3Dr%22%24P(E%20%5Cmid%20%5Cneg%20H)%24%22%2C%20value%3D0.3%2C%20step%3D0.1%0A%20%20%20%20)%0A%20%20%20%20return%20p_e_given_h%2C%20p_e_given_not_h%2C%20p_h%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(p_e_given_h%2C%20p_e_given_not_h%2C%20p_h)%3A%0A%20%20%20%20def%20construct_probability_plot()%3A%0A%20%20%20%20%20%20%20%20import%20matplotlib.pyplot%20as%20plt%0A%0A%20%20%20%20%20%20%20%20plt.axes()%0A%0A%20%20%20%20%20%20%20%20%23%20Radius%3A%201%2C%20face-color%3A%20red%2C%20edge-color%3A%20blue%0A%20%20%20%20%20%20%20%20plt.figure(figsize%3D(6%2C6))%0A%20%20%20%20%20%20%20%20base%20%3D%20plt.Rectangle((0%2C%200)%2C%201%2C%201%2C%20fc%3D%22black%22%2C%20ec%3D%22white%22%2C%20alpha%3D0.25)%0A%20%20%20%20%20%20%20%20h%20%3D%20plt.Rectangle((0%2C%200)%2C%20p_h.value%2C%201%2C%20fc%3D%22yellow%22%2C%20ec%3D%22white%22%2C%20label%3D%22H%22)%0A%20%20%20%20%20%20%20%20e_given_h%20%3D%20plt.Rectangle(%0A%20%20%20%20%20%20%20%20%20%20%20%20(0%2C%200)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20p_h.value%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20p_e_given_h.value%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20fc%3D%22teal%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20ec%3D%22white%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20alpha%3D0.5%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20label%3D%22E%22%2C%0A%20%20%20%20%20%20%20%20)%0A%20%20%20%20%20%20%20%20e_given_not_h%20%3D%20plt.Rectangle(%0A%20%20%20%20%20%20%20%20%20%20%20%20(p_h.value%2C%200)%2C%201%20-%20p_h.value%2C%20p_e_given_not_h.value%2C%20fc%3D%22teal%22%2C%20ec%3D%22white%22%2C%20alpha%3D0.5%0A%20%20%20%20%20%20%20%20)%0A%20%20%20%20%20%20%20%20plt.gca().add_patch(base)%0A%20%20%20%20%20%20%20%20plt.gca().add_patch(h)%0A%20%20%20%20%20%20%20%20plt.gca().add_patch(e_given_not_h)%0A%20%20%20%20%20%20%20%20plt.gca().add_patch(e_given_h)%0A%20%20%20%20%20%20%20%20plt.legend()%0A%20%20%20%20%20%20%20%20return%20plt.gca()%0A%20%20%20%20return%20(construct_probability_plot%2C)%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>