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", "theme": "light", "cell_output": "above", "code_editor_font_size": 14, "default_width": "medium"}, "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": "Probability Mass Functions"}' hidden></marimo-app-config> | |
<marimo-server-token data-token='123' hidden></marimo-server-token> | |
<title>Probability Mass Functions</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%2C%20app_title%3D%22Probability%20Mass%20Functions%22)%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%20Probability%20Mass%20Functions%0A%0A%20%20%20%20%20%20%20%20_This%20notebook%20is%20a%20computational%20companion%20to%20%5B%22Probability%20for%20Computer%20Scientists%22%5D(https%3A%2F%2Fchrispiech.github.io%2FprobabilityForComputerScientists%2Fen%2Fpart2%2Fpmf%2F)%2C%20by%20Stanford%20professor%20Chris%20Piech._%0A%0A%20%20%20%20%20%20%20%20For%20a%20random%20variable%2C%20the%20most%20important%20thing%20to%20know%20is%3A%20how%20likely%20is%20each%20outcome%3F%20For%20a%20discrete%20random%20variable%2C%20this%20information%20is%20called%20the%20%22**Probability%20Mass%20Function**%22.%20The%20probability%20mass%20function%20(PMF)%20provides%20the%20%22mass%22%20(i.e.%20amount)%20of%20%22probability%22%20for%20each%20possible%20assignment%20of%20the%20random%20variable.%0A%0A%20%20%20%20%20%20%20%20Formally%2C%20the%20Probability%20Mass%20Function%20is%20a%20mapping%20between%20the%20values%20that%20the%20random%20variable%20could%20take%20on%20and%20the%20probability%20of%20the%20random%20variable%20taking%20on%20said%20value.%20In%20mathematics%2C%20we%20call%20these%20associations%20functions.%20There%20are%20many%20different%20ways%20of%20representing%20functions%3A%20you%20can%20write%20an%20equation%2C%20you%20can%20make%20a%20graph%2C%20you%20can%20even%20store%20many%20samples%20in%20a%20list.%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%20Properties%20of%20a%20PMF%0A%0A%20%20%20%20%20%20%20%20For%20a%20function%20%24p_X(x)%24%20to%20be%20a%20valid%20PMF%2C%20it%20must%20satisfy%3A%0A%0A%20%20%20%20%20%20%20%201.%20**Non-negativity**%3A%20%24p_X(x)%20%5Cgeq%200%24%20for%20all%20%24x%24%0A%20%20%20%20%20%20%20%202.%20**Unit%20total%20probability**%3A%20%24%5Csum_x%20p_X(x)%20%3D%201%24%0A%0A%20%20%20%20%20%20%20%20%23%23%23%20Probabilities%20Must%20Sum%20to%201%0A%0A%20%20%20%20%20%20%20%20For%20a%20variable%20(call%20it%20%24X%24)%20to%20be%20a%20proper%20random%20variable%2C%20it%20must%20be%20the%20case%20that%20if%20you%20summed%20up%20the%20values%20of%20%24P(X%3Dx)%24%20for%20all%20possible%20values%20%24x%24%20that%20%24X%24%20can%20take%20on%2C%20the%20result%20must%20be%201%3A%0A%0A%20%20%20%20%20%20%20%20%24%24%5Csum_x%20P(X%3Dx)%20%3D%201%24%24%0A%0A%20%20%20%20%20%20%20%20This%20is%20because%20a%20random%20variable%20taking%20on%20a%20value%20is%20an%20event%20(for%20example%20%24X%3D3%24).%20Each%20of%20those%20events%20is%20mutually%20exclusive%20because%20a%20random%20variable%20will%20take%20on%20exactly%20one%20value.%20Those%20mutually%20exclusive%20cases%20define%20an%20entire%20sample%20space.%20Why%3F%20Because%20%24X%24%20must%20take%20on%20some%20value.%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%20PMFs%20as%20Graphs%0A%0A%20%20%20%20%20%20%20%20Let's%20start%20by%20looking%20at%20PMFs%20as%20graphs%20where%20the%20%24x%24-axis%20is%20the%20values%20that%20the%20random%20variable%20could%20take%20on%20and%20the%20%24y%24-axis%20is%20the%20probability%20of%20the%20random%20variable%20taking%20on%20said%20value.%0A%0A%20%20%20%20%20%20%20%20In%20the%20following%20example%2C%20we%20show%20two%20PMFs%3A%0A%0A%20%20%20%20%20%20%20%20-%20On%20the%20left%3A%20PMF%20for%20the%20random%20variable%20%24X%24%20%3D%20the%20value%20of%20a%20single%20six-sided%20die%20roll%0A%20%20%20%20%20%20%20%20-%20On%20the%20right%3A%20PMF%20for%20the%20random%20variable%20%24Y%24%20%3D%20value%20of%20the%20sum%20of%20two%20dice%20rolls%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_(np%2C%20plt)%3A%0A%20%20%20%20%23%20Single%20die%20PMF%0A%20%20%20%20single_die_values%20%3D%20np.arange(1%2C%207)%0A%20%20%20%20single_die_probs%20%3D%20np.ones(6)%20%2F%206%0A%0A%20%20%20%20%23%20Two%20dice%20sum%20PMF%0A%20%20%20%20two_dice_values%20%3D%20np.arange(2%2C%2013)%0A%20%20%20%20two_dice_probs%20%3D%20%5B%5D%0A%0A%20%20%20%20for%20dice_sum%20in%20two_dice_values%3A%0A%20%20%20%20%20%20%20%20if%20dice_sum%20%3C%3D%207%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20dice_prob%20%3D%20(dice_sum-1)%20%2F%2036%0A%20%20%20%20%20%20%20%20else%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20dice_prob%20%3D%20(13-dice_sum)%20%2F%2036%0A%20%20%20%20%20%20%20%20two_dice_probs.append(dice_prob)%0A%0A%20%20%20%20%23%20Create%20side-by-side%20plots%0A%20%20%20%20dice_fig%2C%20(dice_ax1%2C%20dice_ax2)%20%3D%20plt.subplots(1%2C%202%2C%20figsize%3D(12%2C%204))%0A%0A%20%20%20%20%23%20Single%20die%20plot%0A%20%20%20%20dice_ax1.bar(single_die_values%2C%20single_die_probs%2C%20width%3D0.4)%0A%20%20%20%20dice_ax1.set_xticks(single_die_values)%0A%20%20%20%20dice_ax1.set_xlabel('Value%20of%20die%20roll%20(x)')%0A%20%20%20%20dice_ax1.set_ylabel('Probability%3A%20P(X%20%3D%20x)')%0A%20%20%20%20dice_ax1.set_title('PMF%20of%20a%20Single%20Die%20Roll')%0A%20%20%20%20dice_ax1.grid(alpha%3D0.3)%0A%0A%20%20%20%20%23%20Two%20dice%20sum%20plot%0A%20%20%20%20dice_ax2.bar(two_dice_values%2C%20two_dice_probs%2C%20width%3D0.4)%0A%20%20%20%20dice_ax2.set_xticks(two_dice_values)%0A%20%20%20%20dice_ax2.set_xlabel('Sum%20of%20two%20dice%20(y)')%0A%20%20%20%20dice_ax2.set_ylabel('Probability%3A%20P(Y%20%3D%20y)')%0A%20%20%20%20dice_ax2.set_title('PMF%20of%20Sum%20of%20Two%20Dice')%0A%20%20%20%20dice_ax2.grid(alpha%3D0.3)%0A%0A%20%20%20%20plt.tight_layout()%0A%20%20%20%20plt.gca()%0A%20%20%20%20return%20(%0A%20%20%20%20%20%20%20%20dice_ax1%2C%0A%20%20%20%20%20%20%20%20dice_ax2%2C%0A%20%20%20%20%20%20%20%20dice_fig%2C%0A%20%20%20%20%20%20%20%20dice_prob%2C%0A%20%20%20%20%20%20%20%20dice_sum%2C%0A%20%20%20%20%20%20%20%20single_die_probs%2C%0A%20%20%20%20%20%20%20%20single_die_values%2C%0A%20%20%20%20%20%20%20%20two_dice_probs%2C%0A%20%20%20%20%20%20%20%20two_dice_values%2C%0A%20%20%20%20)%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%20The%20information%20provided%20in%20these%20graphs%20shows%20the%20likelihood%20of%20a%20random%20variable%20taking%20on%20different%20values.%0A%0A%20%20%20%20%20%20%20%20In%20the%20graph%20on%20the%20right%2C%20the%20value%20%226%22%20on%20the%20%24x%24-axis%20is%20associated%20with%20the%20probability%20%24%5Cfrac%7B5%7D%7B36%7D%24%20on%20the%20%24y%24-axis.%20This%20%24x%24-axis%20refers%20to%20the%20event%20%22the%20sum%20of%20two%20dice%20is%206%22%20or%20%24Y%20%3D%206%24.%20The%20%24y%24-axis%20tells%20us%20that%20the%20probability%20of%20that%20event%20is%20%24%5Cfrac%7B5%7D%7B36%7D%24.%20In%20full%3A%20%24P(Y%20%3D%206)%20%3D%20%5Cfrac%7B5%7D%7B36%7D%24.%0A%0A%20%20%20%20%20%20%20%20The%20value%20%222%22%20is%20associated%20with%20%22%24%5Cfrac%7B1%7D%7B36%7D%24%22%20which%20tells%20us%20that%2C%20%24P(Y%20%3D%202)%20%3D%20%5Cfrac%7B1%7D%7B36%7D%24%2C%20the%20probability%20that%20two%20dice%20sum%20to%202%20is%20%24%5Cfrac%7B1%7D%7B36%7D%24.%20There%20is%20no%20value%20associated%20with%20%221%22%20because%20the%20sum%20of%20two%20dice%20cannot%20be%201.%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%20PMFs%20as%20Equations%0A%0A%20%20%20%20%20%20%20%20Here%20is%20the%20exact%20same%20information%20in%20equation%20form%3A%0A%0A%20%20%20%20%20%20%20%20For%20a%20single%20die%20roll%20%24X%24%3A%0A%20%20%20%20%20%20%20%20%24%24P(X%3Dx)%20%3D%20%5Cfrac%7B1%7D%7B6%7D%20%5Cquad%20%5Ctext%7B%20if%20%7D%201%20%5Cleq%20x%20%5Cleq%206%24%24%0A%0A%20%20%20%20%20%20%20%20For%20the%20sum%20of%20two%20dice%20%24Y%24%3A%0A%20%20%20%20%20%20%20%20%24%24P(Y%3Dy)%20%3D%20%5Cbegin%7Bcases%7D%0A%20%20%20%20%20%20%20%20%5Cfrac%7B(y-1)%7D%7B36%7D%20%26%20%5Ctext%7B%20if%20%7D%202%20%5Cleq%20y%20%5Cleq%207%5C%5C%0A%20%20%20%20%20%20%20%20%5Cfrac%7B(13-y)%7D%7B36%7D%20%26%20%5Ctext%7B%20if%20%7D%208%20%5Cleq%20y%20%5Cleq%2012%0A%20%20%20%20%20%20%20%20%5Cend%7Bcases%7D%24%24%0A%0A%20%20%20%20%20%20%20%20Let's%20implement%20the%20PMF%20for%20%24Y%24%2C%20the%20sum%20of%20two%20dice%2C%20in%20Python%20code%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_()%3A%0A%20%20%20%20def%20pmf_sum_two_dice(y_val)%3A%0A%20%20%20%20%20%20%20%20%22%22%22Returns%20the%20probability%20that%20the%20sum%20of%20two%20dice%20is%20y%22%22%22%0A%20%20%20%20%20%20%20%20if%20y_val%20%3C%202%20or%20y_val%20%3E%2012%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20return%200%0A%20%20%20%20%20%20%20%20if%20y_val%20%3C%3D%207%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20return%20(y_val-1)%20%2F%2036%0A%20%20%20%20%20%20%20%20else%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20return%20(13-y_val)%20%2F%2036%0A%0A%20%20%20%20%23%20Test%20the%20function%20for%20a%20few%20values%0A%20%20%20%20test_values%20%3D%20%5B1%2C%202%2C%207%2C%2012%2C%2013%5D%0A%20%20%20%20for%20test_y%20in%20test_values%3A%0A%20%20%20%20%20%20%20%20print(f%22P(Y%20%3D%20%7Btest_y%7D)%20%3D%20%7Bpmf_sum_two_dice(test_y)%7D%22)%0A%20%20%20%20return%20pmf_sum_two_dice%2C%20test_values%2C%20test_y%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22Now%2C%20let's%20verify%20that%20our%20PMF%20satisfies%20the%20property%20that%20the%20sum%20of%20all%20probabilities%20equals%201%3A%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(pmf_sum_two_dice)%3A%0A%20%20%20%20%23%20Verify%20that%20probabilities%20sum%20to%201%0A%20%20%20%20verify_total_prob%20%3D%20sum(pmf_sum_two_dice(y_val)%20for%20y_val%20in%20range(2%2C%2013))%0A%20%20%20%20%23%20Round%20to%2010%20decimal%20places%20to%20handle%20floating-point%20precision%0A%20%20%20%20verify_total_prob_rounded%20%3D%20round(verify_total_prob%2C%2010)%0A%20%20%20%20print(f%22Sum%20of%20all%20probabilities%3A%20%7Bverify_total_prob_rounded%7D%22)%0A%20%20%20%20return%20verify_total_prob%2C%20verify_total_prob_rounded%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(plt%2C%20pmf_sum_two_dice)%3A%0A%20%20%20%20%23%20Create%20a%20visual%20verification%0A%20%20%20%20verify_y_values%20%3D%20list(range(2%2C%2013))%0A%20%20%20%20verify_probabilities%20%3D%20%5Bpmf_sum_two_dice(y_val)%20for%20y_val%20in%20verify_y_values%5D%0A%0A%20%20%20%20plt.figure(figsize%3D(10%2C%204))%0A%20%20%20%20plt.bar(verify_y_values%2C%20verify_probabilities%2C%20width%3D0.4)%0A%20%20%20%20plt.xticks(verify_y_values)%0A%20%20%20%20plt.xlabel('Sum%20of%20two%20dice%20(y)')%0A%20%20%20%20plt.ylabel('Probability%3A%20P(Y%20%3D%20y)')%0A%20%20%20%20plt.title('PMF%20of%20Sum%20of%20Two%20Dice%20(Total%20Probability%20%3D%201)')%0A%20%20%20%20plt.grid(alpha%3D0.3)%0A%0A%20%20%20%20%23%20Add%20probability%20values%20on%20top%20of%20bars%0A%20%20%20%20for%20verify_i%2C%20verify_prob%20in%20enumerate(verify_probabilities)%3A%0A%20%20%20%20%20%20%20%20plt.text(verify_y_values%5Bverify_i%5D%2C%20verify_prob%20%2B%200.001%2C%20f'%7Bverify_prob%3A.3f%7D'%2C%20ha%3D'center')%0A%0A%20%20%20%20plt.gca()%20%20%23%20Return%20the%20current%20axes%20to%20ensure%20proper%20display%0A%20%20%20%20return%20verify_i%2C%20verify_prob%2C%20verify_probabilities%2C%20verify_y_values%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%20Data%20to%20Histograms%20to%20Probability%20Mass%20Functions%0A%0A%20%20%20%20%20%20%20%20One%20surprising%20way%20to%20store%20a%20likelihood%20function%20(recall%20that%20a%20PMF%20is%20the%20name%20of%20the%20likelihood%20function%20for%20discrete%20random%20variables)%20is%20simply%20a%20list%20of%20data.%20Let's%20simulate%20summing%20two%20dice%20many%20times%20to%20create%20an%20empirical%20PMF%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_(np)%3A%0A%20%20%20%20%23%20Simulate%20rolling%20two%20dice%20many%20times%0A%20%20%20%20sim_num_trials%20%3D%2010000%0A%20%20%20%20np.random.seed(42)%20%20%23%20For%20reproducibility%0A%0A%20%20%20%20%23%20Generate%20random%20dice%20rolls%0A%20%20%20%20sim_die1%20%3D%20np.random.randint(1%2C%207%2C%20size%3Dsim_num_trials)%0A%20%20%20%20sim_die2%20%3D%20np.random.randint(1%2C%207%2C%20size%3Dsim_num_trials)%0A%0A%20%20%20%20%23%20Calculate%20the%20sum%0A%20%20%20%20sim_dice_sums%20%3D%20sim_die1%20%2B%20sim_die2%0A%0A%20%20%20%20%23%20Display%20a%20small%20sample%20of%20the%20data%0A%20%20%20%20print(f%22First%2020%20dice%20sums%3A%20%7Bsim_dice_sums%5B%3A20%5D%7D%22)%0A%20%20%20%20print(f%22Total%20number%20of%20trials%3A%20%7Bsim_num_trials%7D%22)%0A%20%20%20%20return%20sim_dice_sums%2C%20sim_die1%2C%20sim_die2%2C%20sim_num_trials%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(collections%2C%20np%2C%20plt%2C%20sim_dice_sums)%3A%0A%20%20%20%20%23%20Count%20the%20frequency%20of%20each%20sum%0A%20%20%20%20sim_counter%20%3D%20collections.Counter(sim_dice_sums)%0A%0A%20%20%20%20%23%20Sort%20the%20values%0A%20%20%20%20sim_sorted_values%20%3D%20sorted(sim_counter.keys())%0A%0A%20%20%20%20%23%20Calculate%20the%20empirical%20PMF%0A%20%20%20%20sim_empirical_pmf%20%3D%20%5Bsim_counter%5Bx%5D%20%2F%20len(sim_dice_sums)%20for%20x%20in%20sim_sorted_values%5D%0A%0A%20%20%20%20%23%20Calculate%20the%20theoretical%20PMF%0A%20%20%20%20sim_theoretical_values%20%3D%20np.arange(2%2C%2013)%0A%20%20%20%20sim_theoretical_pmf%20%3D%20%5B%5D%0A%20%20%20%20for%20sim_y%20in%20sim_theoretical_values%3A%0A%20%20%20%20%20%20%20%20if%20sim_y%20%3C%3D%207%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20sim_prob%20%3D%20(sim_y-1)%20%2F%2036%0A%20%20%20%20%20%20%20%20else%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20sim_prob%20%3D%20(13-sim_y)%20%2F%2036%0A%20%20%20%20%20%20%20%20sim_theoretical_pmf.append(sim_prob)%0A%0A%20%20%20%20%23%20Create%20a%20comparison%20plot%0A%20%20%20%20sim_fig%2C%20(sim_ax1%2C%20sim_ax2)%20%3D%20plt.subplots(1%2C%202%2C%20figsize%3D(12%2C%204))%0A%0A%20%20%20%20%23%20Empirical%20PMF%20(normalized%20histogram)%0A%20%20%20%20sim_ax1.bar(sim_sorted_values%2C%20sim_empirical_pmf%2C%20width%3D0.4)%0A%20%20%20%20sim_ax1.set_xticks(sim_sorted_values)%0A%20%20%20%20sim_ax1.set_xlabel('Sum%20of%20two%20dice')%0A%20%20%20%20sim_ax1.set_ylabel('Empirical%20Probability')%0A%20%20%20%20sim_ax1.set_title(f'Empirical%20PMF%20from%20%7Blen(sim_dice_sums)%7D%20Trials')%0A%20%20%20%20sim_ax1.grid(alpha%3D0.3)%0A%0A%20%20%20%20%23%20Theoretical%20PMF%0A%20%20%20%20sim_ax2.bar(sim_theoretical_values%2C%20sim_theoretical_pmf%2C%20width%3D0.4)%0A%20%20%20%20sim_ax2.set_xticks(sim_theoretical_values)%0A%20%20%20%20sim_ax2.set_xlabel('Sum%20of%20two%20dice')%0A%20%20%20%20sim_ax2.set_ylabel('Theoretical%20Probability')%0A%20%20%20%20sim_ax2.set_title('Theoretical%20PMF')%0A%20%20%20%20sim_ax2.grid(alpha%3D0.3)%0A%0A%20%20%20%20plt.tight_layout()%0A%0A%20%20%20%20%23%20Let's%20also%20look%20at%20the%20raw%20counts%20(histogram)%0A%20%20%20%20plt.figure(figsize%3D(10%2C%204))%0A%20%20%20%20sim_counts%20%3D%20%5Bsim_counter%5Bx%5D%20for%20x%20in%20sim_sorted_values%5D%0A%20%20%20%20plt.bar(sim_sorted_values%2C%20sim_counts%2C%20width%3D0.4)%0A%20%20%20%20plt.xticks(sim_sorted_values)%0A%20%20%20%20plt.xlabel('Sum%20of%20two%20dice')%0A%20%20%20%20plt.ylabel('Frequency')%0A%20%20%20%20plt.title('Histogram%20of%20Dice%20Sum%20Frequencies')%0A%20%20%20%20plt.grid(alpha%3D0.3)%0A%0A%20%20%20%20%23%20Add%20count%20values%20on%20top%20of%20bars%0A%20%20%20%20for%20sim_i%2C%20sim_count%20in%20enumerate(sim_counts)%3A%0A%20%20%20%20%20%20%20%20plt.text(sim_sorted_values%5Bsim_i%5D%2C%20sim_count%20%2B%2019%2C%20str(sim_count)%2C%20ha%3D'center')%0A%0A%20%20%20%20plt.gca()%20%20%23%20Return%20the%20current%20axes%20to%20ensure%20proper%20display%0A%20%20%20%20return%20(%0A%20%20%20%20%20%20%20%20sim_ax1%2C%0A%20%20%20%20%20%20%20%20sim_ax2%2C%0A%20%20%20%20%20%20%20%20sim_count%2C%0A%20%20%20%20%20%20%20%20sim_counter%2C%0A%20%20%20%20%20%20%20%20sim_counts%2C%0A%20%20%20%20%20%20%20%20sim_empirical_pmf%2C%0A%20%20%20%20%20%20%20%20sim_fig%2C%0A%20%20%20%20%20%20%20%20sim_i%2C%0A%20%20%20%20%20%20%20%20sim_prob%2C%0A%20%20%20%20%20%20%20%20sim_sorted_values%2C%0A%20%20%20%20%20%20%20%20sim_theoretical_pmf%2C%0A%20%20%20%20%20%20%20%20sim_theoretical_values%2C%0A%20%20%20%20%20%20%20%20sim_y%2C%0A%20%20%20%20)%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%20A%20normalized%20histogram%20(where%20each%20value%20is%20divided%20by%20the%20length%20of%20your%20data%20list)%20is%20an%20approximation%20of%20the%20PMF.%20For%20a%20dataset%20of%20discrete%20numbers%2C%20a%20histogram%20shows%20the%20count%20of%20each%20value.%20By%20the%20definition%20of%20probability%2C%20if%20you%20divide%20this%20count%20by%20the%20number%20of%20experiments%20run%2C%20you%20arrive%20at%20an%20approximation%20of%20the%20probability%20of%20the%20event%20%24P(Y%3Dy)%24.%0A%0A%20%20%20%20%20%20%20%20Let's%20look%20at%20a%20specific%20example.%20If%20we%20want%20to%20approximate%20%24P(Y%3D3)%24%20(the%20probability%20that%20the%20sum%20of%20two%20dice%20is%203)%2C%20we%20can%20count%20the%20number%20of%20times%20that%20%223%22%20occurs%20in%20our%20data%20and%20divide%20by%20the%20total%20number%20of%20trials%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_(sim_counter%2C%20sim_dice_sums)%3A%0A%20%20%20%20%23%20Calculate%20P(Y%3D3)%20empirically%0A%20%20%20%20sim_count_of_3%20%3D%20sim_counter%5B3%5D%0A%20%20%20%20sim_empirical_prob%20%3D%20sim_count_of_3%20%2F%20len(sim_dice_sums)%0A%0A%20%20%20%20%23%20Calculate%20P(Y%3D3)%20theoretically%0A%20%20%20%20sim_theoretical_prob%20%3D%202%2F36%20%20%23%20There%20are%202%20ways%20to%20get%20a%20sum%20of%203%20out%20of%2036%20possible%20outcomes%0A%0A%20%20%20%20print(f%22Count%20of%20sum%3D3%3A%20%7Bsim_count_of_3%7D%22)%0A%20%20%20%20print(f%22Empirical%20P(Y%3D3)%3A%20%7Bsim_count_of_3%7D%2F%7Blen(sim_dice_sums)%7D%20%3D%20%7Bsim_empirical_prob%3A.4f%7D%22)%0A%20%20%20%20print(f%22Theoretical%20P(Y%3D3)%3A%202%2F36%20%3D%20%7Bsim_theoretical_prob%3A.4f%7D%22)%0A%20%20%20%20print(f%22Difference%3A%20%7Babs(sim_empirical_prob%20-%20sim_theoretical_prob)%3A.4f%7D%22)%0A%20%20%20%20return%20sim_count_of_3%2C%20sim_empirical_prob%2C%20sim_theoretical_prob%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%20As%20we%20can%20see%2C%20with%20a%20large%20number%20of%20trials%2C%20the%20empirical%20PMF%20becomes%20a%20very%20good%20approximation%20of%20the%20theoretical%20PMF.%20This%20is%20an%20example%20of%20the%20%5BLaw%20of%20Large%20Numbers%5D(https%3A%2F%2Fen.wikipedia.org%2Fwiki%2FLaw_of_large_numbers)%20in%20action.%0A%0A%20%20%20%20%20%20%20%20%23%23%20Interactive%20Example%3A%20Exploring%20PMFs%0A%0A%20%20%20%20%20%20%20%20Let's%20create%20an%20interactive%20tool%20to%20explore%20different%20PMFs%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_(dist_param1%2C%20dist_param2%2C%20dist_selection%2C%20mo)%3A%0A%20%20%20%20mo.hstack(%5Bdist_selection%2C%20dist_param1%2C%20dist_param2%5D%2C%20justify%3D%22space-around%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20dist_selection%20%3D%20mo.ui.dropdown(%0A%20%20%20%20%20%20%20%20options%3D%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20%22bernoulli%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22binomial%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22geometric%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22poisson%22%0A%20%20%20%20%20%20%20%20%5D%2C%0A%20%20%20%20%20%20%20%20value%3D%22bernoulli%22%2C%0A%20%20%20%20%20%20%20%20label%3D%22Select%20a%20distribution%22%0A%20%20%20%20)%0A%0A%20%20%20%20%23%20Parameters%20for%20different%20distributions%0A%20%20%20%20dist_param1%20%3D%20mo.ui.slider(%0A%20%20%20%20%20%20%20%20start%3D0.05%2C%20%0A%20%20%20%20%20%20%20%20stop%3D0.95%2C%20%0A%20%20%20%20%20%20%20%20step%3D0.05%2C%20%0A%20%20%20%20%20%20%20%20value%3D0.5%2C%20%0A%20%20%20%20%20%20%20%20label%3D%22p%20(success%20probability)%22%0A%20%20%20%20)%0A%0A%20%20%20%20dist_param2%20%3D%20mo.ui.slider(%0A%20%20%20%20%20%20%20%20start%3D1%2C%20%0A%20%20%20%20%20%20%20%20stop%3D20%2C%20%0A%20%20%20%20%20%20%20%20step%3D1%2C%20%0A%20%20%20%20%20%20%20%20value%3D10%2C%20%0A%20%20%20%20%20%20%20%20label%3D%22n%20(trials)%20or%20%CE%BB%20(rate)%22%0A%20%20%20%20)%0A%20%20%20%20return%20dist_param1%2C%20dist_param2%2C%20dist_selection%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(dist_param1%2C%20dist_param2%2C%20dist_selection%2C%20np%2C%20plt%2C%20stats)%3A%0A%20%20%20%20%23%20Set%20up%20the%20plot%20based%20on%20the%20selected%20distribution%0A%20%20%20%20if%20dist_selection.value%20%3D%3D%20%22bernoulli%22%3A%0A%20%20%20%20%20%20%20%20%23%20Bernoulli%20distribution%0A%20%20%20%20%20%20%20%20dist_p%20%3D%20dist_param1.value%0A%20%20%20%20%20%20%20%20dist_x_values%20%3D%20np.array(%5B0%2C%201%5D)%0A%20%20%20%20%20%20%20%20dist_pmf_values%20%3D%20%5B1-dist_p%2C%20dist_p%5D%0A%20%20%20%20%20%20%20%20dist_title%20%3D%20f%22Bernoulli%20PMF%20(p%20%3D%20%7Bdist_p%3A.2f%7D)%22%0A%20%20%20%20%20%20%20%20dist_x_label%20%3D%20%22Outcome%20(0%20%3D%20Failure%2C%201%20%3D%20Success)%22%0A%20%20%20%20%20%20%20%20dist_max_x%20%3D%201%0A%0A%20%20%20%20elif%20dist_selection.value%20%3D%3D%20%22binomial%22%3A%0A%20%20%20%20%20%20%20%20%23%20Binomial%20distribution%0A%20%20%20%20%20%20%20%20dist_n%20%3D%20int(dist_param2.value)%0A%20%20%20%20%20%20%20%20dist_p%20%3D%20dist_param1.value%0A%20%20%20%20%20%20%20%20dist_x_values%20%3D%20np.arange(0%2C%20dist_n%2B1)%0A%20%20%20%20%20%20%20%20dist_pmf_values%20%3D%20stats.binom.pmf(dist_x_values%2C%20dist_n%2C%20dist_p)%0A%20%20%20%20%20%20%20%20dist_title%20%3D%20f%22Binomial%20PMF%20(n%20%3D%20%7Bdist_n%7D%2C%20p%20%3D%20%7Bdist_p%3A.2f%7D)%22%0A%20%20%20%20%20%20%20%20dist_x_label%20%3D%20%22Number%20of%20Successes%22%0A%20%20%20%20%20%20%20%20dist_max_x%20%3D%20dist_n%0A%0A%20%20%20%20elif%20dist_selection.value%20%3D%3D%20%22geometric%22%3A%0A%20%20%20%20%20%20%20%20%23%20Geometric%20distribution%0A%20%20%20%20%20%20%20%20dist_p%20%3D%20dist_param1.value%0A%20%20%20%20%20%20%20%20dist_max_x%20%3D%20min(int(5%2Fdist_p)%2C%2050)%20%20%23%20Limit%20the%20range%20for%20visualization%0A%20%20%20%20%20%20%20%20dist_x_values%20%3D%20np.arange(1%2C%20dist_max_x%2B1)%0A%20%20%20%20%20%20%20%20dist_pmf_values%20%3D%20stats.geom.pmf(dist_x_values%2C%20dist_p)%0A%20%20%20%20%20%20%20%20dist_title%20%3D%20f%22Geometric%20PMF%20(p%20%3D%20%7Bdist_p%3A.2f%7D)%22%0A%20%20%20%20%20%20%20%20dist_x_label%20%3D%20%22Number%20of%20Trials%20Until%20First%20Success%22%0A%0A%20%20%20%20else%3A%20%20%23%20Poisson%0A%20%20%20%20%20%20%20%20%23%20Poisson%20distribution%0A%20%20%20%20%20%20%20%20dist_lam%20%3D%20dist_param2.value%0A%20%20%20%20%20%20%20%20dist_max_x%20%3D%20int(dist_lam*3)%20%2B%201%20%20%23%20Reasonable%20range%20for%20visualization%0A%20%20%20%20%20%20%20%20dist_x_values%20%3D%20np.arange(0%2C%20dist_max_x)%0A%20%20%20%20%20%20%20%20dist_pmf_values%20%3D%20stats.poisson.pmf(dist_x_values%2C%20dist_lam)%0A%20%20%20%20%20%20%20%20dist_title%20%3D%20f%22Poisson%20PMF%20(%CE%BB%20%3D%20%7Bdist_lam%7D)%22%0A%20%20%20%20%20%20%20%20dist_x_label%20%3D%20%22Number%20of%20Events%22%0A%0A%20%20%20%20%23%20Create%20the%20plot%0A%20%20%20%20plt.figure(figsize%3D(10%2C%205))%0A%0A%20%20%20%20%23%20For%20discrete%20distributions%2C%20use%20stem%20plot%20for%20clarity%0A%20%20%20%20dist_markerline%2C%20dist_stemlines%2C%20dist_baseline%20%3D%20plt.stem(%0A%20%20%20%20%20%20%20%20dist_x_values%2C%20dist_pmf_values%2C%20markerfmt%3D'o'%2C%20basefmt%3D'%20'%0A%20%20%20%20)%0A%20%20%20%20plt.setp(dist_markerline%2C%20markersize%3D6)%0A%20%20%20%20plt.setp(dist_stemlines%2C%20linewidth%3D1.5)%0A%0A%20%20%20%20%23%20Add%20a%20bar%20plot%20for%20better%20visibility%0A%20%20%20%20plt.bar(dist_x_values%2C%20dist_pmf_values%2C%20alpha%3D0.3%2C%20width%3D0.4)%0A%0A%20%20%20%20plt.xlabel(dist_x_label)%0A%20%20%20%20plt.ylabel(%22Probability%3A%20P(X%20%3D%20x)%22)%0A%20%20%20%20plt.title(dist_title)%0A%20%20%20%20plt.grid(alpha%3D0.3)%0A%0A%20%20%20%20%23%20Calculate%20and%20display%20expected%20value%20and%20variance%0A%20%20%20%20if%20dist_selection.value%20%3D%3D%20%22bernoulli%22%3A%0A%20%20%20%20%20%20%20%20dist_mean%20%3D%20dist_p%0A%20%20%20%20%20%20%20%20dist_variance%20%3D%20dist_p%20*%20(1-dist_p)%0A%20%20%20%20elif%20dist_selection.value%20%3D%3D%20%22binomial%22%3A%0A%20%20%20%20%20%20%20%20dist_mean%20%3D%20dist_n%20*%20dist_p%0A%20%20%20%20%20%20%20%20dist_variance%20%3D%20dist_n%20*%20dist_p%20*%20(1-dist_p)%0A%20%20%20%20elif%20dist_selection.value%20%3D%3D%20%22geometric%22%3A%0A%20%20%20%20%20%20%20%20dist_mean%20%3D%201%2Fdist_p%0A%20%20%20%20%20%20%20%20dist_variance%20%3D%20(1-dist_p)%2F(dist_p**2)%0A%20%20%20%20else%3A%20%20%23%20Poisson%0A%20%20%20%20%20%20%20%20dist_mean%20%3D%20dist_lam%0A%20%20%20%20%20%20%20%20dist_variance%20%3D%20dist_lam%0A%0A%20%20%20%20dist_std_dev%20%3D%20np.sqrt(dist_variance)%0A%0A%20%20%20%20%23%20Add%20text%20with%20distribution%20properties%0A%20%20%20%20dist_props_text%20%3D%20(%0A%20%20%20%20%20%20%20%20f%22Mean%3A%20%7Bdist_mean%3A.3f%7D%5Cn%22%0A%20%20%20%20%20%20%20%20f%22Variance%3A%20%7Bdist_variance%3A.3f%7D%5Cn%22%0A%20%20%20%20%20%20%20%20f%22Std%20Dev%3A%20%7Bdist_std_dev%3A.3f%7D%5Cn%22%0A%20%20%20%20%20%20%20%20f%22Sum%20of%20probabilities%3A%20%7Bsum(dist_pmf_values)%3A.6f%7D%22%0A%20%20%20%20)%0A%0A%20%20%20%20plt.text(0.95%2C%200.95%2C%20dist_props_text%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20transform%3Dplt.gca().transAxes%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20verticalalignment%3D'top'%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20horizontalalignment%3D'right'%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20bbox%3Ddict(boxstyle%3D'round'%2C%20facecolor%3D'white'%2C%20alpha%3D0.8))%0A%0A%20%20%20%20plt.gca()%20%20%23%20Return%20the%20current%20axes%20to%20ensure%20proper%20display%0A%20%20%20%20return%20(%0A%20%20%20%20%20%20%20%20dist_baseline%2C%0A%20%20%20%20%20%20%20%20dist_lam%2C%0A%20%20%20%20%20%20%20%20dist_markerline%2C%0A%20%20%20%20%20%20%20%20dist_max_x%2C%0A%20%20%20%20%20%20%20%20dist_mean%2C%0A%20%20%20%20%20%20%20%20dist_n%2C%0A%20%20%20%20%20%20%20%20dist_p%2C%0A%20%20%20%20%20%20%20%20dist_pmf_values%2C%0A%20%20%20%20%20%20%20%20dist_props_text%2C%0A%20%20%20%20%20%20%20%20dist_std_dev%2C%0A%20%20%20%20%20%20%20%20dist_stemlines%2C%0A%20%20%20%20%20%20%20%20dist_title%2C%0A%20%20%20%20%20%20%20%20dist_variance%2C%0A%20%20%20%20%20%20%20%20dist_x_label%2C%0A%20%20%20%20%20%20%20%20dist_x_values%2C%0A%20%20%20%20)%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%20Expected%20Value%20from%20a%20PMF%0A%0A%20%20%20%20%20%20%20%20The%20expected%20value%20(or%20mean)%20of%20a%20discrete%20random%20variable%20is%20calculated%20using%20its%20PMF%3A%0A%0A%20%20%20%20%20%20%20%20%24%24E%5BX%5D%20%3D%20%5Csum_x%20x%20%5Ccdot%20p_X(x)%24%24%0A%0A%20%20%20%20%20%20%20%20This%20represents%20the%20long-run%20average%20value%20of%20the%20random%20variable.%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_(dist_pmf_values%2C%20dist_x_values)%3A%0A%20%20%20%20def%20calc_expected_value(x_values%2C%20pmf_values)%3A%0A%20%20%20%20%20%20%20%20%22%22%22Calculate%20the%20expected%20value%20of%20a%20discrete%20random%20variable.%22%22%22%0A%20%20%20%20%20%20%20%20return%20sum(x%20*%20p%20for%20x%2C%20p%20in%20zip(x_values%2C%20pmf_values))%0A%0A%20%20%20%20%23%20Calculate%20expected%20value%20for%20the%20current%20distribution%0A%20%20%20%20ev_dist_mean%20%3D%20calc_expected_value(dist_x_values%2C%20dist_pmf_values)%0A%0A%20%20%20%20print(f%22Expected%20value%3A%20%7Bev_dist_mean%3A.4f%7D%22)%0A%20%20%20%20return%20calc_expected_value%2C%20ev_dist_mean%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%20Variance%20from%20a%20PMF%0A%0A%20%20%20%20%20%20%20%20The%20variance%20measures%20the%20spread%20or%20dispersion%20of%20a%20random%20variable%20around%20its%20mean%3A%0A%0A%20%20%20%20%20%20%20%20%24%24%5Ctext%7BVar%7D(X)%20%3D%20E%5B(X%20-%20E%5BX%5D)%5E2%5D%20%3D%20%5Csum_x%20(x%20-%20E%5BX%5D)%5E2%20%5Ccdot%20p_X(x)%24%24%0A%0A%20%20%20%20%20%20%20%20An%20alternative%20formula%20is%3A%0A%0A%20%20%20%20%20%20%20%20%24%24%5Ctext%7BVar%7D(X)%20%3D%20E%5BX%5E2%5D%20-%20(E%5BX%5D)%5E2%20%3D%20%5Csum_x%20x%5E2%20%5Ccdot%20p_X(x)%20-%20%5Cleft(%5Csum_x%20x%20%5Ccdot%20p_X(x)%5Cright)%5E2%24%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%0Adef%20_(dist_pmf_values%2C%20dist_x_values%2C%20ev_dist_mean%2C%20np)%3A%0A%20%20%20%20def%20calc_variance(x_values%2C%20pmf_values%2C%20mean_value)%3A%0A%20%20%20%20%20%20%20%20%22%22%22Calculate%20the%20variance%20of%20a%20discrete%20random%20variable.%22%22%22%0A%20%20%20%20%20%20%20%20return%20sum((x%20-%20mean_value)**2%20*%20p%20for%20x%2C%20p%20in%20zip(x_values%2C%20pmf_values))%0A%0A%20%20%20%20%23%20Calculate%20variance%20for%20the%20current%20distribution%0A%20%20%20%20var_dist_var%20%3D%20calc_variance(dist_x_values%2C%20dist_pmf_values%2C%20ev_dist_mean)%0A%20%20%20%20var_dist_std_dev%20%3D%20np.sqrt(var_dist_var)%0A%0A%20%20%20%20print(f%22Variance%3A%20%7Bvar_dist_var%3A.4f%7D%22)%0A%20%20%20%20print(f%22Standard%20deviation%3A%20%7Bvar_dist_std_dev%3A.4f%7D%22)%0A%20%20%20%20return%20calc_variance%2C%20var_dist_std_dev%2C%20var_dist_var%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%20PMF%20vs.%20CDF%0A%0A%20%20%20%20%20%20%20%20The%20**Cumulative%20Distribution%20Function%20(CDF)**%20is%20related%20to%20the%20PMF%20but%20gives%20the%20probability%20that%20the%20random%20variable%20%24X%24%20is%20less%20than%20or%20equal%20to%20a%20value%20%24x%24%3A%0A%0A%20%20%20%20%20%20%20%20%24%24F_X(x)%20%3D%20P(X%20%5Cleq%20x)%20%3D%20%5Csum_%7Bk%20%5Cleq%20x%7D%20p_X(k)%24%24%0A%0A%20%20%20%20%20%20%20%20While%20the%20PMF%20gives%20the%20probability%20mass%20at%20each%20point%2C%20the%20CDF%20accumulates%20these%20probabilities.%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_(dist_pmf_values%2C%20dist_x_values%2C%20np%2C%20plt)%3A%0A%20%20%20%20%23%20Calculate%20the%20CDF%20from%20the%20PMF%0A%20%20%20%20cdf_dist_values%20%3D%20np.cumsum(dist_pmf_values)%0A%0A%20%20%20%20%23%20Create%20a%20plot%20comparing%20PMF%20and%20CDF%0A%20%20%20%20cdf_fig%2C%20(cdf_ax1%2C%20cdf_ax2)%20%3D%20plt.subplots(1%2C%202%2C%20figsize%3D(12%2C%204))%0A%0A%20%20%20%20%23%20PMF%20plot%0A%20%20%20%20cdf_ax1.bar(dist_x_values%2C%20dist_pmf_values%2C%20width%3D0.4%2C%20alpha%3D0.7)%0A%20%20%20%20cdf_ax1.set_xlabel('x')%0A%20%20%20%20cdf_ax1.set_ylabel('P(X%20%3D%20x)')%0A%20%20%20%20cdf_ax1.set_title('Probability%20Mass%20Function%20(PMF)')%0A%20%20%20%20cdf_ax1.grid(alpha%3D0.3)%0A%0A%20%20%20%20%23%20CDF%20plot%20-%20using%20step%20function%20with%20'post'%20style%20for%20proper%20discrete%20representation%0A%20%20%20%20cdf_ax2.step(dist_x_values%2C%20cdf_dist_values%2C%20where%3D'post'%2C%20linewidth%3D2%2C%20color%3D'blue')%0A%20%20%20%20cdf_ax2.scatter(dist_x_values%2C%20cdf_dist_values%2C%20s%3D50%2C%20color%3D'blue')%0A%0A%20%20%20%20%23%20Set%20appropriate%20limits%20for%20better%20visualization%0A%20%20%20%20if%20len(dist_x_values)%20%3E%200%3A%0A%20%20%20%20%20%20%20%20x_min%20%3D%20min(dist_x_values)%20-%200.5%0A%20%20%20%20%20%20%20%20x_max%20%3D%20max(dist_x_values)%20%2B%200.5%0A%20%20%20%20%20%20%20%20cdf_ax2.set_xlim(x_min%2C%20x_max)%0A%20%20%20%20%20%20%20%20cdf_ax2.set_ylim(0%2C%201.05)%20%20%23%20CDF%20goes%20from%200%20to%201%0A%0A%20%20%20%20cdf_ax2.set_xlabel('x')%0A%20%20%20%20cdf_ax2.set_ylabel('P(X%20%E2%89%A4%20x)')%0A%20%20%20%20cdf_ax2.set_title('Cumulative%20Distribution%20Function%20(CDF)')%0A%20%20%20%20cdf_ax2.grid(alpha%3D0.3)%0A%0A%20%20%20%20plt.tight_layout()%0A%20%20%20%20plt.gca()%20%20%23%20Return%20the%20current%20axes%20to%20ensure%20proper%20display%0A%20%20%20%20return%20cdf_ax1%2C%20cdf_ax2%2C%20cdf_dist_values%2C%20cdf_fig%2C%20x_max%2C%20x_min%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%20The%20graphs%20above%20illustrate%20the%20key%20difference%20between%20PMF%20and%20CDF%3A%0A%0A%20%20%20%20%20%20%20%20-%20**PMF%20(left)**%3A%20Shows%20the%20probability%20of%20the%20random%20variable%20taking%20each%20specific%20value%3A%20P(X%20%3D%20x)%0A%20%20%20%20%20%20%20%20-%20**CDF%20(right)**%3A%20Shows%20the%20probability%20of%20the%20random%20variable%20being%20less%20than%20or%20equal%20to%20each%20value%3A%20P(X%20%E2%89%A4%20x)%0A%0A%20%20%20%20%20%20%20%20The%20CDF%20at%20any%20point%20is%20the%20sum%20of%20all%20PMF%20values%20up%20to%20and%20including%20that%20point.%20This%20is%20why%20the%20CDF%20is%20always%20non-decreasing%20and%20eventually%20reaches%201.%20For%20discrete%20distributions%20like%20this%20one%2C%20the%20CDF%20forms%20a%20step%20function%20that%20jumps%20at%20each%20value%20in%20the%20support%20of%20the%20random%20variable.%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%20Test%20Your%20Understanding%0A%0A%20%20%20%20%20%20%20%20Choose%20what%20you%20believe%20are%20the%20correct%20options%20in%20the%20questions%20below%3A%0A%0A%20%20%20%20%20%20%20%20%3Cdetails%3E%0A%20%20%20%20%20%20%20%20%3Csummary%3EIf%20X%20is%20a%20discrete%20random%20variable%20with%20PMF%20p(x)%2C%20then%20p(x)%20must%20always%20be%20less%20than%201%3C%2Fsummary%3E%0A%20%20%20%20%20%20%20%20%E2%9D%8C%20False!%20While%20most%20values%20in%20a%20PMF%20are%20typically%20less%20than%201%2C%20a%20PMF%20can%20have%20p(x)%20%3D%201%20for%20a%20specific%20value%20if%20the%20random%20variable%20always%20takes%20that%20value%20(with%20100%25%20probability).%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%20sum%20of%20all%20probabilities%20in%20a%20PMF%20must%20equal%20exactly%201%3C%2Fsummary%3E%0A%20%20%20%20%20%20%20%20%E2%9C%85%20True!%20This%20is%20a%20fundamental%20property%20of%20any%20valid%20PMF.%20The%20total%20probability%20across%20all%20possible%20values%20must%20be%201%2C%20as%20the%20random%20variable%20must%20take%20some%20value.%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%3EA%20PMF%20can%20be%20estimated%20from%20data%20by%20creating%20a%20normalized%20histogram%3C%2Fsummary%3E%0A%20%20%20%20%20%20%20%20%E2%9C%85%20True!%20Counting%20the%20frequency%20of%20each%20value%20and%20dividing%20by%20the%20total%20number%20of%20observations%20gives%20an%20empirical%20PMF.%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%20expected%20value%20of%20a%20discrete%20random%20variable%20is%20always%20one%20of%20the%20possible%20values%20of%20the%20variable%3C%2Fsummary%3E%0A%20%20%20%20%20%20%20%20%E2%9D%8C%20False!%20The%20expected%20value%20is%20a%20weighted%20average%20and%20may%20not%20be%20a%20value%20the%20random%20variable%20can%20actually%20take.%20For%20example%2C%20the%20expected%20value%20of%20a%20fair%20die%20roll%20is%203.5%2C%20which%20is%20not%20a%20possible%20outcome.%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%20r%22%22%22%0A%20%20%20%20%20%20%20%20%23%23%20Practical%20Applications%20of%20PMFs%0A%0A%20%20%20%20%20%20%20%20PMFs%20pop%20up%20everywhere%20-%20network%20engineers%20use%20them%20to%20model%20traffic%20patterns%2C%20reliability%20teams%20predict%20equipment%20failures%2C%20and%20marketers%20analyze%20purchase%20behavior.%20In%20finance%2C%20they%20help%20price%20options%3B%20in%20gaming%2C%20they're%20behind%20every%20dice%20roll.%20Machine%20learning%20algorithms%20like%20Naive%20Bayes%20rely%20on%20them%2C%20and%20they're%20essential%20for%20modeling%20rare%20events%20like%20genetic%20mutations%20or%20system%20failures.%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%20Key%20Takeaways%0A%0A%20%20%20%20%20%20%20%20PMFs%20give%20us%20the%20probability%20picture%20for%20discrete%20random%20variables%20-%20they%20tell%20us%20how%20likely%20each%20value%20is%2C%20must%20be%20non-negative%2C%20and%20always%20sum%20to%201.%20We%20can%20write%20them%20as%20equations%2C%20draw%20them%20as%20graphs%2C%20or%20estimate%20them%20from%20data.%20They're%20the%20foundation%20for%20calculating%20expected%20values%20and%20variances%2C%20which%20we'll%20explore%20in%20our%20next%20notebook%20on%20Expectation%2C%20where%20we'll%20learn%20how%20to%20summarize%20random%20variables%20with%20a%20single%2C%20most%20%22expected%22%20value.%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%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%20from%20scipy%20import%20stats%0A%20%20%20%20import%20collections%0A%20%20%20%20return%20collections%2C%20np%2C%20plt%2C%20stats%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> | |