Spaces:
Running
Running
File size: 30,069 Bytes
1cce1df e0f3bfe 1cce1df |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
<!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": {"dataframes": "rich", "theme": "light", "code_editor_font_size": 14, "default_width": "medium", "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": "Random Variables"}' hidden></marimo-app-config>
<marimo-server-token data-token='123' hidden></marimo-server-token>
<title>Random Variables</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%22Random%20Variables%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%20from%20scipy%20import%20stats%0A%20%20%20%20return%20np%2C%20plt%2C%20stats%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%20Random%20Variables%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%2Frvs%2F)%2C%20by%20Stanford%20professor%20Chris%20Piech._%0A%0A%20%20%20%20%20%20%20%20Random%20variables%20are%20functions%20that%20map%20outcomes%20from%20a%20probability%20space%20to%20numbers.%20This%20mathematical%20abstraction%20allows%20us%20to%3A%0A%0A%20%20%20%20%20%20%20%20-%20Work%20with%20numerical%20outcomes%20in%20probability%0A%20%20%20%20%20%20%20%20-%20Calculate%20expected%20values%20and%20variances%0A%20%20%20%20%20%20%20%20-%20Model%20real-world%20phenomena%20quantitatively%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%20Types%20of%20Random%20Variables%0A%0A%20%20%20%20%20%20%20%20%23%23%23%20Discrete%20Random%20Variables%0A%20%20%20%20%20%20%20%20-%20Take%20on%20countable%20values%20(finite%20or%20infinite)%0A%20%20%20%20%20%20%20%20-%20Described%20by%20a%20probability%20mass%20function%20(PMF)%0A%20%20%20%20%20%20%20%20-%20Example%3A%20Number%20of%20heads%20in%203%20coin%20flips%0A%0A%20%20%20%20%20%20%20%20%23%23%23%20Continuous%20Random%20Variables%0A%20%20%20%20%20%20%20%20-%20Take%20on%20uncountable%20values%20in%20an%20interval%0A%20%20%20%20%20%20%20%20-%20Described%20by%20a%20probability%20density%20function%20(PDF)%0A%20%20%20%20%20%20%20%20-%20Example%3A%20Height%20of%20a%20randomly%20selected%20person%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%20Random%20Variables%0A%0A%20%20%20%20%20%20%20%20Each%20random%20variable%20has%20several%20key%20properties%3A%0A%0A%20%20%20%20%20%20%20%20%7C%20Property%20%7C%20Description%20%7C%20Example%20%7C%0A%20%20%20%20%20%20%20%20%7C----------%7C-------------%7C---------%7C%0A%20%20%20%20%20%20%20%20%7C%20Meaning%20%7C%20Semantic%20description%20%7C%20Number%20of%20successes%20in%20n%20trials%20%7C%0A%20%20%20%20%20%20%20%20%7C%20Symbol%20%7C%20Notation%20used%20%7C%20%24X%24%2C%20%24Y%24%2C%20%24Z%24%20%7C%0A%20%20%20%20%20%20%20%20%7C%20Support%2FRange%20%7C%20Possible%20values%20%7C%20%24%5C%7B0%2C1%2C2%2C...%2Cn%5C%7D%24%20for%20binomial%20%7C%0A%20%20%20%20%20%20%20%20%7C%20Distribution%20%7C%20PMF%20or%20PDF%20%7C%20%24p_X(x)%24%20or%20%24f_X(x)%24%20%7C%0A%20%20%20%20%20%20%20%20%7C%20Expectation%20%7C%20Weighted%20average%20%7C%20%24E%5BX%5D%24%20%7C%0A%20%20%20%20%20%20%20%20%7C%20Variance%20%7C%20Measure%20of%20spread%20%7C%20%24%5Ctext%7BVar%7D(X)%24%20%7C%0A%20%20%20%20%20%20%20%20%7C%20Standard%20Deviation%20%7C%20Square%20root%20of%20variance%20%7C%20%24%5Csigma_X%24%20%7C%0A%20%20%20%20%20%20%20%20%7C%20Mode%20%7C%20Most%20likely%20value%20%7C%20argmax%24_x%24%20%24p_X(x)%24%20%7C%0A%0A%20%20%20%20%20%20%20%20Additional%20properties%20include%3A%0A%0A%20%20%20%20%20%20%20%20-%20%5BEntropy%5D(https%3A%2F%2Fen.wikipedia.org%2Fwiki%2FEntropy_(information_theory))%20(measure%20of%20uncertainty)%0A%20%20%20%20%20%20%20%20-%20%5BMedian%5D(https%3A%2F%2Fen.wikipedia.org%2Fwiki%2FMedian)%20(middle%20value)%0A%20%20%20%20%20%20%20%20-%20%5BSkewness%5D(https%3A%2F%2Fen.wikipedia.org%2Fwiki%2FSkewness)%20(asymmetry%20measure)%0A%20%20%20%20%20%20%20%20-%20%5BKurtosis%5D(https%3A%2F%2Fen.wikipedia.org%2Fwiki%2FKurtosis)%20(tail%20heaviness%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%20Probability%20Mass%20Functions%20(PMF)%0A%0A%20%20%20%20%20%20%20%20For%20discrete%20random%20variables%2C%20the%20PMF%20%24p_X(x)%24%20gives%20the%20probability%20that%20%24X%24%20equals%20%24x%24%3A%0A%0A%20%20%20%20%20%20%20%20%24p_X(x)%20%3D%20P(X%20%3D%20x)%24%0A%0A%20%20%20%20%20%20%20%20Properties%20of%20a%20PMF%3A%0A%0A%20%20%20%20%20%20%20%201.%20%24p_X(x)%20%5Cgeq%200%24%20for%20all%20%24x%24%0A%20%20%20%20%20%20%20%202.%20%24%5Csum_x%20p_X(x)%20%3D%201%24%0A%0A%20%20%20%20%20%20%20%20Let's%20implement%20a%20PMF%20for%20rolling%20a%20fair%20die%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%2C%20plt)%3A%0A%20%20%20%20def%20die_pmf(x)%3A%0A%20%20%20%20%20%20%20%20if%20x%20in%20%5B1%2C%202%2C%203%2C%204%2C%205%2C%206%5D%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20return%201%20%2F%206%0A%20%20%20%20%20%20%20%20return%200%0A%0A%20%20%20%20%23%20Plot%20the%20PMF%0A%20%20%20%20_x%20%3D%20np.arange(1%2C%207)%0A%20%20%20%20probabilities%20%3D%20%5Bdie_pmf(i)%20for%20i%20in%20_x%5D%0A%0A%20%20%20%20plt.figure(figsize%3D(8%2C%202))%0A%20%20%20%20plt.bar(_x%2C%20probabilities)%0A%20%20%20%20plt.title(%22PMF%20of%20Rolling%20a%20Fair%20Die%22)%0A%20%20%20%20plt.xlabel(%22Outcome%22)%0A%20%20%20%20plt.ylabel(%22Probability%22)%0A%20%20%20%20plt.grid(True%2C%20alpha%3D0.3)%0A%20%20%20%20plt.gca()%0A%20%20%20%20return%20die_pmf%2C%20probabilities%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%20Probability%20Density%20Functions%20(PDF)%0A%0A%20%20%20%20%20%20%20%20For%20continuous%20random%20variables%2C%20we%20use%20a%20PDF%20%24f_X(x)%24.%20The%20probability%20of%20%24X%24%20falling%20in%20an%20interval%20%24%5Ba%2Cb%5D%24%20is%3A%0A%0A%20%20%20%20%20%20%20%20%24P(a%20%5Cleq%20X%20%5Cleq%20b)%20%3D%20%5Cint_a%5Eb%20f_X(x)dx%24%0A%0A%20%20%20%20%20%20%20%20Properties%20of%20a%20PDF%3A%0A%0A%20%20%20%20%20%20%20%201.%20%24f_X(x)%20%5Cgeq%200%24%20for%20all%20%24x%24%0A%20%20%20%20%20%20%20%202.%20%24%5Cint_%7B-%5Cinfty%7D%5E%7B%5Cinfty%7D%20f_X(x)dx%20%3D%201%24%0A%0A%20%20%20%20%20%20%20%20Let's%20look%20at%20the%20normal%20distribution%2C%20a%20common%20continuous%20random%20variable%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%2C%20plt%2C%20stats)%3A%0A%20%20%20%20%23%20Generate%20points%20for%20plotting%0A%20%20%20%20_x%20%3D%20np.linspace(-4%2C%204%2C%20100)%0A%20%20%20%20_pdf%20%3D%20stats.norm.pdf(_x%2C%20loc%3D0%2C%20scale%3D1)%0A%0A%20%20%20%20plt.figure(figsize%3D(8%2C%204))%0A%20%20%20%20plt.plot(_x%2C%20_pdf%2C%20%22b-%22%2C%20label%3D%22PDF%22)%0A%20%20%20%20plt.fill_between(_x%2C%20_pdf%2C%20where%3D(_x%20%3E%3D%20-1)%20%26%20(_x%20%3C%3D%201)%2C%20alpha%3D0.3)%0A%20%20%20%20plt.title(%22Standard%20Normal%20Distribution%22)%0A%20%20%20%20plt.xlabel(%22x%22)%0A%20%20%20%20plt.ylabel(%22Density%22)%0A%20%20%20%20plt.grid(True%2C%20alpha%3D0.3)%0A%20%20%20%20plt.legend()%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%20Expected%20Value%0A%0A%20%20%20%20%20%20%20%20The%20expected%20value%20%24E%5BX%5D%24%20is%20the%20long-run%20average%20of%20a%20random%20variable.%0A%0A%20%20%20%20%20%20%20%20For%20discrete%20random%20variables%3A%0A%20%20%20%20%20%20%20%20%24E%5BX%5D%20%3D%20%5Csum_x%20x%20%5Ccdot%20p_X(x)%24%0A%0A%20%20%20%20%20%20%20%20For%20continuous%20random%20variables%3A%0A%20%20%20%20%20%20%20%20%24E%5BX%5D%20%3D%20%5Cint_%7B-%5Cinfty%7D%5E%7B%5Cinfty%7D%20x%20%5Ccdot%20f_X(x)dx%24%0A%0A%20%20%20%20%20%20%20%20Properties%3A%0A%0A%20%20%20%20%20%20%20%201.%20%24E%5BaX%20%2B%20b%5D%20%3D%20aE%5BX%5D%20%2B%20b%24%0A%20%20%20%20%20%20%20%202.%20%24E%5BX%20%2B%20Y%5D%20%3D%20E%5BX%5D%20%2B%20E%5BY%5D%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_(np)%3A%0A%20%20%20%20def%20expected_value_discrete(x_values%2C%20probabilities)%3A%0A%20%20%20%20%20%20%20%20return%20sum(x%20*%20p%20for%20x%2C%20p%20in%20zip(x_values%2C%20probabilities))%0A%0A%20%20%20%20%23%20Example%3A%20Expected%20value%20of%20a%20fair%20die%20roll%0A%20%20%20%20die_values%20%3D%20np.arange(1%2C%207)%0A%20%20%20%20die_probs%20%3D%20np.ones(6)%20%2F%206%0A%0A%20%20%20%20E_X%20%3D%20expected_value_discrete(die_values%2C%20die_probs)%0A%20%20%20%20return%20E_X%2C%20die_probs%2C%20die_values%2C%20expected_value_discrete%0A%0A%0A%40app.cell%0Adef%20_(E_X)%3A%0A%20%20%20%20E_X%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%20Variance%0A%0A%20%20%20%20%20%20%20%20The%20variance%20%24%5Ctext%7BVar%7D(X)%24%20measures%20the%20spread%20of%20a%20random%20variable%20around%20its%20mean%3A%0A%0A%20%20%20%20%20%20%20%20%24%5Ctext%7BVar%7D(X)%20%3D%20E%5B(X%20-%20E%5BX%5D)%5E2%5D%24%0A%0A%20%20%20%20%20%20%20%20This%20can%20be%20computed%20as%3A%0A%20%20%20%20%20%20%20%20%24%5Ctext%7BVar%7D(X)%20%3D%20E%5BX%5E2%5D%20-%20(E%5BX%5D)%5E2%24%0A%0A%20%20%20%20%20%20%20%20Properties%3A%0A%0A%20%20%20%20%20%20%20%201.%20%24%5Ctext%7BVar%7D(aX)%20%3D%20a%5E2Var(X)%24%0A%20%20%20%20%20%20%20%202.%20%24%5Ctext%7BVar%7D(X%20%2B%20b)%20%3D%20Var(X)%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_(E_X%2C%20die_probs%2C%20die_values%2C%20np)%3A%0A%20%20%20%20def%20variance_discrete(x_values%2C%20probabilities%2C%20expected_value)%3A%0A%20%20%20%20%20%20%20%20squared_diff%20%3D%20%5B(x%20-%20expected_value)%20**%202%20for%20x%20in%20x_values%5D%0A%20%20%20%20%20%20%20%20return%20sum(d%20*%20p%20for%20d%2C%20p%20in%20zip(squared_diff%2C%20probabilities))%0A%0A%20%20%20%20%23%20Example%3A%20Variance%20of%20a%20fair%20die%20roll%0A%20%20%20%20var_X%20%3D%20variance_discrete(die_values%2C%20die_probs%2C%20E_X)%0A%20%20%20%20std_X%20%3D%20np.sqrt(var_X)%0A%20%20%20%20return%20std_X%2C%20var_X%2C%20variance_discrete%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo%2C%20std_X%2C%20var_X)%3A%0A%20%20%20%20mo.md(%0A%20%20%20%20%20%20%20%20f%22%22%22%0A%20%20%20%20%20%20%20%20%23%23%23%20Examples%20of%20Variance%20Calculation%0A%0A%20%20%20%20%20%20%20%20For%20our%20fair%20die%20example%3A%0A%0A%20%20%20%20%20%20%20%20-%20Variance%3A%20%7Bvar_X%3A.2f%7D%0A%20%20%20%20%20%20%20%20-%20Standard%20Deviation%3A%20%7Bstd_X%3A.2f%7D%0A%0A%20%20%20%20%20%20%20%20This%20means%20that%20on%20average%2C%20a%20roll%20deviates%20from%20the%20mean%20(3.5)%20by%20about%20%7Bstd_X%3A.2f%7D%20units.%0A%0A%20%20%20%20%20%20%20%20Let's%20look%20another%20example%20for%20a%20fair%20coin%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_(variance_discrete)%3A%0A%20%20%20%20%23%20Fair%20coin%20(X%20%3D%200%20or%201)%0A%20%20%20%20coin_values%20%3D%20%5B0%2C%201%5D%0A%20%20%20%20coin_probs%20%3D%20%5B0.5%2C%200.5%5D%0A%20%20%20%20coin_mean%20%3D%20sum(x%20*%20p%20for%20x%2C%20p%20in%20zip(coin_values%2C%20coin_probs))%0A%20%20%20%20coin_var%20%3D%20variance_discrete(coin_values%2C%20coin_probs%2C%20coin_mean)%0A%20%20%20%20return%20coin_mean%2C%20coin_probs%2C%20coin_values%2C%20coin_var%0A%0A%0A%40app.cell%0Adef%20_(np%2C%20stats%2C%20variance_discrete)%3A%0A%20%20%20%20%23%20Standard%20normal%20(discretized%20for%20example)%0A%20%20%20%20normal_values%20%3D%20np.linspace(-3%2C%203%2C%20100)%0A%20%20%20%20normal_probs%20%3D%20stats.norm.pdf(normal_values)%0A%20%20%20%20normal_probs%20%3D%20normal_probs%20%2F%20sum(normal_probs)%20%20%23%20normalize%0A%20%20%20%20normal_mean%20%3D%200%0A%20%20%20%20normal_var%20%3D%20variance_discrete(normal_values%2C%20normal_probs%2C%20normal_mean)%0A%20%20%20%20return%20normal_mean%2C%20normal_probs%2C%20normal_values%2C%20normal_var%0A%0A%0A%40app.cell%0Adef%20_(np%2C%20variance_discrete)%3A%0A%20%20%20%20%23%20Uniform%20on%20%5B0%2C1%5D%20(discretized%20for%20example)%0A%20%20%20%20uniform_values%20%3D%20np.linspace(0%2C%201%2C%20100)%0A%20%20%20%20uniform_probs%20%3D%20np.ones_like(uniform_values)%20%2F%20len(uniform_values)%0A%20%20%20%20uniform_mean%20%3D%200.5%0A%20%20%20%20uniform_var%20%3D%20variance_discrete(uniform_values%2C%20uniform_probs%2C%20uniform_mean)%0A%20%20%20%20return%20uniform_mean%2C%20uniform_probs%2C%20uniform_values%2C%20uniform_var%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(coin_var%2C%20mo%2C%20normal_var%2C%20uniform_var)%3A%0A%20%20%20%20mo.md(%0A%20%20%20%20%20%20%20%20rf%22%22%22%0A%20%20%20%20%20%20%20%20Let's%20look%20at%20some%20calculated%20variances%3A%0A%0A%20%20%20%20%20%20%20%20-%20Fair%20coin%20(X%20%3D%200%20or%201)%3A%20%24%5Ctext%7B%7BVar%7D%7D(X)%20%3D%20%7Bcoin_var%3A.4f%7D%24%0A%20%20%20%20%20%20%20%20-%20Standard%20normal%20distribution%20(discretized)%3A%20%24%5Ctext%7B%7BVar(X)%7D%7D%20%E2%89%88%20%7Bnormal_var%3A.4f%7D%24%0A%20%20%20%20%20%20%20%20-%20Uniform%20distribution%20on%20%24%5B0%2C1%5D%24%20(discretized)%3A%20%24%5Ctext%7B%7BVar(X)%7D%7D%20%E2%89%88%20%7Buniform_var%3A.4f%7D%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%20Common%20Distributions%0A%0A%20%20%20%20%20%20%20%201.%20Bernoulli%20Distribution%0A%20%20%20%20%20%20%20%20%20%20%20%20-%20Models%20a%20single%20success%2Ffailure%20experiment%0A%20%20%20%20%20%20%20%20%20%20%20%20-%20%24P(X%20%3D%201)%20%3D%20p%24%2C%20%24P(X%20%3D%200)%20%3D%201-p%24%0A%20%20%20%20%20%20%20%20%20%20%20%20-%20%24E%5BX%5D%20%3D%20p%24%2C%20%24%5Ctext%7BVar%7D(X)%20%3D%20p(1-p)%24%0A%0A%20%20%20%20%20%20%20%202.%20Binomial%20Distribution%0A%0A%20%20%20%20%20%20%20%20%20%20%20%20-%20Models%20number%20of%20successes%20in%20%24n%24%20independent%20trials%0A%20%20%20%20%20%20%20%20%20%20%20%20-%20%24P(X%20%3D%20k)%20%3D%20%5Cbinom%7Bn%7D%7Bk%7Dp%5Ek(1-p)%5E%7Bn-k%7D%24%0A%20%20%20%20%20%20%20%20%20%20%20%20-%20%24E%5BX%5D%20%3D%20np%24%2C%20%24%5Ctext%7BVar%7D(X)%20%3D%20np(1-p)%24%0A%0A%20%20%20%20%20%20%20%203.%20Normal%20Distribution%0A%0A%20%20%20%20%20%20%20%20%20%20%20%20-%20Bell-shaped%20curve%20defined%20by%20mean%20%24%5Cmu%24%20and%20variance%20%24%5Csigma%5E2%24%0A%20%20%20%20%20%20%20%20%20%20%20%20-%20PDF%3A%20%24f_X(x)%20%3D%20%5Cfrac%7B1%7D%7B%5Csigma%5Csqrt%7B2%5Cpi%7D%7De%5E%7B-%5Cfrac%7B(x-%5Cmu)%5E2%7D%7B2%5Csigma%5E2%7D%7D%24%0A%20%20%20%20%20%20%20%20%20%20%20%20-%20%24E%5BX%5D%20%3D%20%5Cmu%24%2C%20%24%5Ctext%7BVar%7D(X)%20%3D%20%5Csigma%5E2%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%23%20Example%3A%20Comparing%20Discrete%20and%20Continuous%20Distributions%0A%0A%20%20%20%20%20%20%20%20This%20example%20shows%20the%20relationship%20between%20a%20Binomial%20distribution%20(discrete)%20and%20its%20Normal%20approximation%20(continuous).%0A%20%20%20%20%20%20%20%20The%20parameters%20control%20both%20distributions%3A%0A%0A%20%20%20%20%20%20%20%20-%20**Number%20of%20Trials**%3A%20Controls%20the%20range%20of%20possible%20values%20and%20the%20shape's%20width%0A%20%20%20%20%20%20%20%20-%20**Success%20Probability**%3A%20Affects%20the%20distribution's%20center%20and%20skewness%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%20n_trials%2C%20p_success)%3A%0A%20%20%20%20mo.hstack(%5Bn_trials%2C%20p_success%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%20%23%20Distribution%20parameters%0A%20%20%20%20n_trials%20%3D%20mo.ui.slider(1%2C%2020%2C%20value%3D10%2C%20label%3D%22Number%20of%20Trials%22)%0A%20%20%20%20p_success%20%3D%20mo.ui.slider(0%2C%201%2C%20value%3D0.5%2C%20step%3D0.05%2C%20label%3D%22Success%20Probability%22)%0A%20%20%20%20return%20n_trials%2C%20p_success%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(n_trials%2C%20np%2C%20p_success%2C%20plt%2C%20stats)%3A%0A%20%20%20%20fig%2C%20(ax1%2C%20ax2)%20%3D%20plt.subplots(1%2C%202%2C%20figsize%3D(12%2C%203))%0A%0A%20%20%20%20%23%20Discrete%3A%20Binomial%20PMF%0A%20%20%20%20k%20%3D%20np.arange(0%2C%20n_trials.value%20%2B%201)%0A%20%20%20%20pmf%20%3D%20stats.binom.pmf(k%2C%20n_trials.value%2C%20p_success.value)%0A%20%20%20%20ax1.bar(k%2C%20pmf%2C%20alpha%3D0.8%2C%20color%3D%22%231f77b4%22%2C%20label%3D%22PMF%22)%0A%20%20%20%20ax1.set_title(f%22Binomial%20PMF%20(n%3D%7Bn_trials.value%7D%2C%20p%3D%7Bp_success.value%7D)%22)%0A%20%20%20%20ax1.set_xlabel(%22Number%20of%20Successes%22)%0A%20%20%20%20ax1.set_ylabel(%22Probability%22)%0A%20%20%20%20ax1.grid(True%2C%20alpha%3D0.3)%0A%0A%20%20%20%20%23%20Continuous%3A%20Normal%20PDF%20approx.%0A%20%20%20%20mu%20%3D%20n_trials.value%20*%20p_success.value%0A%20%20%20%20sigma%20%3D%20np.sqrt(n_trials.value%20*%20p_success.value%20*%20(1%20-%20p_success.value))%0A%20%20%20%20x%20%3D%20np.linspace(max(0%2C%20mu%20-%204%20*%20sigma)%2C%20min(n_trials.value%2C%20mu%20%2B%204%20*%20sigma)%2C%20100)%0A%20%20%20%20pdf%20%3D%20stats.norm.pdf(x%2C%20mu%2C%20sigma)%0A%0A%20%20%20%20ax2.plot(x%2C%20pdf%2C%20%22r-%22%2C%20linewidth%3D2%2C%20label%3D%22PDF%22)%0A%20%20%20%20ax2.fill_between(x%2C%20pdf%2C%20alpha%3D0.3%2C%20color%3D%22red%22)%0A%20%20%20%20ax2.set_title(f%22Normal%20PDF%20(%CE%BC%3D%7Bmu%3A.1f%7D%2C%20%CF%83%3D%7Bsigma%3A.1f%7D)%22)%0A%20%20%20%20ax2.set_xlabel(%22Continuous%20Approximation%22)%0A%20%20%20%20ax2.set_ylabel(%22Density%22)%0A%20%20%20%20ax2.grid(True%2C%20alpha%3D0.3)%0A%0A%20%20%20%20%23%20Set%20consistent%20x-axis%20limits%20for%20better%20comparison%0A%20%20%20%20ax1.set_xlim(-0.5%2C%20n_trials.value%20%2B%200.5)%0A%20%20%20%20ax2.set_xlim(-0.5%2C%20n_trials.value%20%2B%200.5)%0A%0A%20%20%20%20plt.tight_layout()%0A%20%20%20%20plt.gca()%0A%20%20%20%20return%20ax1%2C%20ax2%2C%20fig%2C%20k%2C%20mu%2C%20pdf%2C%20pmf%2C%20sigma%2C%20x%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo%2C%20n_trials%2C%20np%2C%20p_success)%3A%0A%20%20%20%20mo.md(f%22%22%22%0A%20%20%20%20**Current%20Distribution%20Properties%3A**%0A%0A%20%20%20%20-%20Mean%20(%CE%BC)%20%3D%20%7Bn_trials.value%20*%20p_success.value%3A.2f%7D%0A%20%20%20%20-%20Standard%20Deviation%20(%CF%83)%20%3D%20%7Bnp.sqrt(n_trials.value%20*%20p_success.value%20*%20(1%20-%20p_success.value))%3A.2f%7D%0A%0A%20%20%20%20Notice%20how%20the%20Normal%20distribution%20(right)%20approximates%20the%20Binomial%20distribution%20(left)%20better%20when%3A%0A%0A%20%20%20%201.%20The%20number%20of%20trials%20is%20larger%0A%20%20%20%202.%20The%20success%20probability%20is%20closer%20to%200.5%0A%20%20%20%20%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%20%20%20%20%20%20%20%20%23%23%20Practice%20Problems%0A%0A%20%20%20%20%20%20%20%20%23%23%23%20Problem%201%3A%20Discrete%20Random%20Variable%0A%20%20%20%20%20%20%20%20Let%20%24X%24%20be%20the%20sum%20when%20rolling%20two%20fair%20dice.%20Find%3A%0A%0A%20%20%20%20%20%20%20%201.%20The%20support%20of%20%24X%24%0A%20%20%20%20%20%20%20%202.%20The%20PMF%20%24p_X(x)%24%0A%20%20%20%20%20%20%20%203.%20%24E%5BX%5D%24%20and%20%24%5Ctext%7BVar%7D(X)%24%0A%0A%20%20%20%20%20%20%20%20%3Cdetails%3E%0A%20%20%20%20%20%20%20%20%3Csummary%3ESolution%3C%2Fsummary%3E%0A%20%20%20%20%20%20%20%20Let's%20solve%20this%20step%20by%20step%3A%0A%20%20%20%20%20%20%20%20%60%60%60python%0A%20%20%20%20%20%20%20%20def%20two_dice_pmf(x)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20outcomes%20%3D%20%5B(i%2Cj)%20for%20i%20in%20range(1%2C7)%20for%20j%20in%20range(1%2C7)%5D%0A%20%20%20%20%20%20%20%20%20%20%20%20favorable%20%3D%20%5Bpair%20for%20pair%20in%20outcomes%20if%20sum(pair)%20%3D%3D%20x%5D%0A%20%20%20%20%20%20%20%20%20%20%20%20return%20len(favorable)%2F36%0A%0A%20%20%20%20%20%20%20%20%23%20Support%3A%20%7B2%2C3%2C...%2C12%7D%0A%20%20%20%20%20%20%20%20%23%20E%5BX%5D%20%3D%207%0A%20%20%20%20%20%20%20%20%23%20Var(X)%20%3D%205.83%0A%20%20%20%20%20%20%20%20%60%60%60%0A%20%20%20%20%20%20%20%20%3C%2Fdetails%3E%0A%0A%20%20%20%20%20%20%20%20%23%23%23%20Problem%202%3A%20Continuous%20Random%20Variable%0A%20%20%20%20%20%20%20%20For%20a%20uniform%20random%20variable%20on%20%24%5B0%2C1%5D%24%2C%20verify%20that%3A%0A%0A%20%20%20%20%20%20%20%201.%20The%20PDF%20integrates%20to%201%0A%20%20%20%20%20%20%20%202.%20%24E%5BX%5D%20%3D%201%2F2%24%0A%20%20%20%20%20%20%20%203.%20%24%5Ctext%7BVar%7D(X)%20%3D%201%2F12%24%0A%0A%20%20%20%20%20%20%20%20Try%20solving%20this%20yourself%20first%2C%20then%20check%20the%20solution%20below.%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%20%23%20DIY%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mktext%2C%20mo)%3A%0A%20%20%20%20mo.accordion(%7B%22Solution%22%3A%20mktext%7D%2C%20lazy%3DTrue)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mktext%20%3D%20mo.md(%0A%20%20%20%20%20%20%20%20r%22%22%22%0A%20%20%20%20%20%20%20%20Let's%20solve%20each%20part%3A%0A%0A%20%20%20%20%20%20%20%201.%20**PDF%20integrates%20to%201**%3A%0A%20%20%20%20%20%20%20%20%20%20%20%24%5Cint_0%5E1%201%20%5C%2C%20dx%20%3D%20%5Bx%5D_0%5E1%20%3D%201%20-%200%20%3D%201%24%0A%0A%20%20%20%20%20%20%20%202.%20**Expected%20Value**%3A%0A%20%20%20%20%20%20%20%20%20%20%20%24E%5BX%5D%20%3D%20%5Cint_0%5E1%20x%20%5Ccdot%201%20%5C%2C%20dx%20%3D%20%5B%5Cfrac%7Bx%5E2%7D%7B2%7D%5D_0%5E1%20%3D%20%5Cfrac%7B1%7D%7B2%7D%20-%200%20%3D%20%5Cfrac%7B1%7D%7B2%7D%24%0A%0A%20%20%20%20%20%20%20%203.%20**Variance**%3A%0A%20%20%20%20%20%20%20%20%20%20%20%24%5Ctext%7BVar%7D(X)%20%3D%20E%5BX%5E2%5D%20-%20(E%5BX%5D)%5E2%24%0A%0A%20%20%20%20%20%20%20%20%20%20%20First%20calculate%20%24E%5BX%5E2%5D%24%3A%0A%20%20%20%20%20%20%20%20%20%20%20%24E%5BX%5E2%5D%20%3D%20%5Cint_0%5E1%20x%5E2%20%5Ccdot%201%20%5C%2C%20dx%20%3D%20%5B%5Cfrac%7Bx%5E3%7D%7B3%7D%5D_0%5E1%20%3D%20%5Cfrac%7B1%7D%7B3%7D%24%0A%0A%20%20%20%20%20%20%20%20%20%20%20Then%3A%0A%20%20%20%20%20%20%20%20%20%20%20%24%5Ctext%7BVar%7D(X)%20%3D%20%5Cfrac%7B1%7D%7B3%7D%20-%20(%5Cfrac%7B1%7D%7B2%7D)%5E2%20%3D%20%5Cfrac%7B1%7D%7B3%7D%20-%20%5Cfrac%7B1%7D%7B4%7D%20%3D%20%5Cfrac%7B1%7D%7B12%7D%24%0A%20%20%20%20%20%20%20%20%22%22%22%0A%20%20%20%20)%0A%20%20%20%20return%20(mktext%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%20%F0%9F%A4%94%20Test%20Your%20Understanding%0A%0A%20%20%20%20%20%20%20%20Pick%20which%20of%20these%20statements%20about%20random%20variables%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%20probability%20density%20function%20can%20be%20greater%20than%201%3C%2Fsummary%3E%0A%20%20%20%20%20%20%20%20%E2%9C%85%20Correct!%20Unlike%20PMFs%2C%20PDFs%20can%20exceed%201%20as%20long%20as%20the%20total%20area%20equals%201.%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%20random%20variable%20must%20equal%20one%20of%20its%20possible%20values%3C%2Fsummary%3E%0A%20%20%20%20%20%20%20%20%E2%9D%8C%20Incorrect!%20For%20example%2C%20the%20expected%20value%20of%20a%20fair%20die%20is%203.5%2C%20which%20is%20not%20a%20possible%20outcome.%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%3EAdding%20a%20constant%20to%20a%20random%20variable%20changes%20its%20variance%3C%2Fsummary%3E%0A%20%20%20%20%20%20%20%20%E2%9D%8C%20Incorrect!%20Adding%20a%20constant%20shifts%20the%20distribution%20but%20doesn't%20affect%20its%20spread.%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%20difference%20between%20discrete%20and%20continuous%20random%20variables%0A%20%20%20%20%20%20%20%20-%20How%20PMFs%20and%20PDFs%20describe%20probability%20distributions%0A%20%20%20%20%20%20%20%20-%20Methods%20for%20calculating%20expected%20values%20and%20variances%0A%20%20%20%20%20%20%20%20-%20Properties%20of%20common%20probability%20distributions%0A%0A%20%20%20%20%20%20%20%20In%20the%20next%20lesson%2C%20we'll%20explore%20Probability%20Mass%20Functions%20in%20detail%2C%20focusing%20on%20their%20properties%20and%20applications.%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>
|