marimo-learn / _site /probability /11_expectation.html
Haleshot's picture
relevant assets and workflow
1cce1df unverified
raw
history blame
50.4 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", "code_editor_font_size": 14, "theme": "light", "cell_output": "above", "dataframes": "rich"}, "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": "Expectation"}' hidden></marimo-app-config>
<marimo-server-token data-token='123' hidden></marimo-server-token>
<title>Expectation</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%22Expectation%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%20Expectation%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%2Fexpectation%2F)%2C%20by%20Stanford%20professor%20Chris%20Piech._%0A%0A%20%20%20%20%20%20%20%20A%20random%20variable%20is%20fully%20represented%20by%20its%20Probability%20Mass%20Function%20(PMF)%2C%20which%20describes%20each%20value%20the%20random%20variable%20can%20take%20on%20and%20the%20corresponding%20probabilities.%20However%2C%20a%20PMF%20can%20contain%20a%20lot%20of%20information.%20Sometimes%20it's%20useful%20to%20summarize%20a%20random%20variable%20with%20a%20single%20value!%0A%0A%20%20%20%20%20%20%20%20The%20most%20common%2C%20and%20arguably%20the%20most%20useful%2C%20summary%20of%20a%20random%20variable%20is%20its%20**Expectation**%20(also%20called%20the%20expected%20value%20or%20mean).%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%20Definition%20of%20Expectation%0A%0A%20%20%20%20%20%20%20%20The%20expectation%20of%20a%20random%20variable%20%24X%24%2C%20written%20%24E%5BX%5D%24%2C%20is%20the%20average%20of%20all%20the%20values%20the%20random%20variable%20can%20take%20on%2C%20each%20weighted%20by%20the%20probability%20that%20the%20random%20variable%20will%20take%20on%20that%20value.%0A%0A%20%20%20%20%20%20%20%20%24%24E%5BX%5D%20%3D%20%5Csum_x%20x%20%5Ccdot%20P(X%3Dx)%24%24%0A%0A%20%20%20%20%20%20%20%20Expectation%20goes%20by%20many%20other%20names%3A%20Mean%2C%20Weighted%20Average%2C%20Center%20of%20Mass%2C%201st%20Moment.%20All%20of%20these%20are%20calculated%20using%20the%20same%20formula.%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%20Intuition%20Behind%20Expectation%0A%0A%20%20%20%20%20%20%20%20The%20expected%20value%20represents%20the%20long-run%20average%20value%20of%20a%20random%20variable%20over%20many%20independent%20repetitions%20of%20an%20experiment.%0A%0A%20%20%20%20%20%20%20%20For%20example%2C%20if%20you%20roll%20a%20fair%20six-sided%20die%20many%20times%20and%20calculate%20the%20average%20of%20all%20rolls%2C%20that%20average%20will%20approach%20the%20expected%20value%20of%203.5%20as%20the%20number%20of%20rolls%20increases.%0A%0A%20%20%20%20%20%20%20%20Let's%20visualize%20this%20concept%3A%0A%20%20%20%20%20%20%20%20%22%22%22%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(np%2C%20plt)%3A%0A%20%20%20%20%23%20Set%20random%20seed%20for%20reproducibility%0A%20%20%20%20np.random.seed(42)%0A%0A%20%20%20%20%23%20Simulate%20rolling%20a%20die%20many%20times%0A%20%20%20%20exp_num_rolls%20%3D%201000%0A%20%20%20%20exp_die_rolls%20%3D%20np.random.randint(1%2C%207%2C%20size%3Dexp_num_rolls)%0A%0A%20%20%20%20%23%20Calculate%20the%20running%20average%0A%20%20%20%20exp_running_avg%20%3D%20np.cumsum(exp_die_rolls)%20%2F%20np.arange(1%2C%20exp_num_rolls%20%2B%201)%0A%0A%20%20%20%20%23%20Create%20the%20plot%0A%20%20%20%20plt.figure(figsize%3D(10%2C%205))%0A%20%20%20%20plt.plot(range(1%2C%20exp_num_rolls%20%2B%201)%2C%20exp_running_avg%2C%20label%3D'Running%20Average')%0A%20%20%20%20plt.axhline(y%3D3.5%2C%20color%3D'r'%2C%20linestyle%3D'--'%2C%20label%3D'Expected%20Value%20(3.5)')%0A%20%20%20%20plt.xlabel('Number%20of%20Rolls')%0A%20%20%20%20plt.ylabel('Average%20Value')%0A%20%20%20%20plt.title('Running%20Average%20of%20Die%20Rolls%20Approaching%20Expected%20Value')%0A%20%20%20%20plt.legend()%0A%20%20%20%20plt.grid(alpha%3D0.3)%0A%20%20%20%20plt.xscale('log')%20%20%23%20Log%20scale%20to%20better%20see%20convergence%0A%0A%20%20%20%20%23%20Add%20annotations%0A%20%20%20%20plt.annotate('As%20the%20number%20of%20rolls%20increases%2C%5Cnthe%20average%20approaches%20the%20expected%20value'%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20xy%3D(exp_num_rolls%2C%20exp_running_avg%5B-1%5D)%2C%20xytext%3D(exp_num_rolls%2F3%2C%204)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20arrowprops%3Ddict(facecolor%3D'black'%2C%20shrink%3D0.05%2C%20width%3D1.5))%0A%0A%20%20%20%20plt.gca()%0A%20%20%20%20return%20exp_die_rolls%2C%20exp_num_rolls%2C%20exp_running_avg%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%23%23%20Properties%20of%20Expectation%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.accordion(%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%221.%20Linearity%20of%20Expectation%22%3A%20mo.md(%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20r%22%22%22%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%24%24E%5BaX%20%2B%20b%5D%20%3D%20a%20%5Ccdot%20E%5BX%5D%20%2B%20b%24%24%0A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20Where%20%24a%24%20and%20%24b%24%20are%20constants%20(not%20random%20variables).%0A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20This%20means%20that%20if%20you%20multiply%20a%20random%20variable%20by%20a%20constant%2C%20the%20expectation%20is%20multiplied%20by%20that%20constant.%20And%20if%20you%20add%20a%20constant%20to%20a%20random%20variable%2C%20the%20expectation%20increases%20by%20that%20constant.%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%20%222.%20Expectation%20of%20the%20Sum%20of%20Random%20Variables%22%3A%20mo.md(%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20r%22%22%22%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%24%24E%5BX%20%2B%20Y%5D%20%3D%20E%5BX%5D%20%2B%20E%5BY%5D%24%24%0A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20This%20is%20true%20regardless%20of%20the%20relationship%20between%20%24X%24%20and%20%24Y%24.%20They%20can%20be%20dependent%2C%20and%20they%20can%20have%20different%20distributions.%20This%20also%20applies%20with%20more%20than%20two%20random%20variables%3A%0A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%24%24E%5Cleft%5B%5Csum_%7Bi%3D1%7D%5En%20X_i%5Cright%5D%20%3D%20%5Csum_%7Bi%3D1%7D%5En%20E%5BX_i%5D%24%24%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%20%223.%20Law%20of%20the%20Unconscious%20Statistician%20(LOTUS)%22%3A%20mo.md(%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20r%22%22%22%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%24%24E%5Bg(X)%5D%20%3D%20%5Csum_x%20g(x)%20%5Ccdot%20P(X%3Dx)%24%24%0A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20This%20allows%20us%20to%20calculate%20the%20expected%20value%20of%20a%20function%20%24g(X)%24%20of%20a%20random%20variable%20%24X%24%20when%20we%20know%20the%20probability%20distribution%20of%20%24X%24%20but%20don't%20explicitly%20know%20the%20distribution%20of%20%24g(X)%24.%0A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20This%20theorem%20has%20the%20humorous%20name%20%22Law%20of%20the%20Unconscious%20Statistician%22%20(LOTUS)%20because%20it's%20so%20useful%20that%20you%20should%20be%20able%20to%20employ%20it%20unconsciously.%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%20%224.%20Expectation%20of%20a%20Constant%22%3A%20mo.md(%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20r%22%22%22%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%24%24E%5Ba%5D%20%3D%20a%24%24%0A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20Sometimes%20in%20proofs%2C%20you'll%20end%20up%20with%20the%20expectation%20of%20a%20constant%20(rather%20than%20a%20random%20variable).%20Since%20a%20constant%20doesn't%20change%2C%20its%20expected%20value%20is%20just%20the%20constant%20itself.%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%7D%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%20Calculating%20Expectation%0A%0A%20%20%20%20%20%20%20%20Let's%20calculate%20the%20expected%20value%20for%20some%20common%20examples%3A%0A%0A%20%20%20%20%20%20%20%20%23%23%23%20Example%201%3A%20Fair%20Die%20Roll%0A%0A%20%20%20%20%20%20%20%20For%20a%20fair%20six-sided%20die%2C%20the%20PMF%20is%3A%0A%0A%20%20%20%20%20%20%20%20%24%24P(X%3Dx)%20%3D%20%5Cfrac%7B1%7D%7B6%7D%20%5Ctext%7B%20for%20%7D%20x%20%5Cin%20%5C%7B1%2C%202%2C%203%2C%204%2C%205%2C%206%5C%7D%24%24%0A%0A%20%20%20%20%20%20%20%20The%20expected%20value%20is%3A%0A%0A%20%20%20%20%20%20%20%20%24%24E%5BX%5D%20%3D%201%20%5Ccdot%20%5Cfrac%7B1%7D%7B6%7D%20%2B%202%20%5Ccdot%20%5Cfrac%7B1%7D%7B6%7D%20%2B%203%20%5Ccdot%20%5Cfrac%7B1%7D%7B6%7D%20%2B%204%20%5Ccdot%20%5Cfrac%7B1%7D%7B6%7D%20%2B%205%20%5Ccdot%20%5Cfrac%7B1%7D%7B6%7D%20%2B%206%20%5Ccdot%20%5Cfrac%7B1%7D%7B6%7D%20%3D%20%5Cfrac%7B21%7D%7B6%7D%20%3D%203.5%24%24%0A%0A%20%20%20%20%20%20%20%20Let's%20implement%20this%20calculation%20in%20Python%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%20calc_expectation_die()%3A%0A%20%20%20%20%20%20%20%20%22%22%22Calculate%20the%20expected%20value%20of%20a%20fair%20six-sided%20die%20roll.%22%22%22%0A%20%20%20%20%20%20%20%20exp_die_values%20%3D%20range(1%2C%207)%0A%20%20%20%20%20%20%20%20exp_die_probs%20%3D%20%5B1%2F6%5D%20*%206%0A%0A%20%20%20%20%20%20%20%20exp_die_expected%20%3D%20sum(x%20*%20p%20for%20x%2C%20p%20in%20zip(exp_die_values%2C%20exp_die_probs))%0A%20%20%20%20%20%20%20%20return%20exp_die_expected%0A%0A%20%20%20%20exp_die_result%20%3D%20calc_expectation_die()%0A%20%20%20%20print(f%22Expected%20value%20of%20a%20fair%20die%20roll%3A%20%7Bexp_die_result%7D%22)%0A%20%20%20%20return%20calc_expectation_die%2C%20exp_die_result%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%202%3A%20Sum%20of%20Two%20Dice%0A%0A%20%20%20%20%20%20%20%20Now%20let's%20calculate%20the%20expected%20value%20for%20the%20sum%20of%20two%20fair%20dice.%20First%2C%20we%20need%20the%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_()%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%20%23%20Count%20the%20number%20of%20ways%20to%20get%20sum%20y%0A%20%20%20%20%20%20%20%20exp_count%20%3D%200%0A%20%20%20%20%20%20%20%20for%20dice1%20in%20range(1%2C%207)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20for%20dice2%20in%20range(1%2C%207)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20if%20dice1%20%2B%20dice2%20%3D%3D%20y_val%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20exp_count%20%2B%3D%201%0A%20%20%20%20%20%20%20%20return%20exp_count%20%2F%2036%20%20%23%20There%20are%2036%20possible%20outcomes%20(6%C3%83%E2%80%946)%0A%0A%20%20%20%20%23%20Test%20the%20function%20for%20a%20few%20values%0A%20%20%20%20exp_test_values%20%3D%20%5B2%2C%207%2C%2012%5D%0A%20%20%20%20for%20exp_test_y%20in%20exp_test_values%3A%0A%20%20%20%20%20%20%20%20print(f%22P(Y%20%3D%20%7Bexp_test_y%7D)%20%3D%20%7Bpmf_sum_two_dice(exp_test_y)%7D%22)%0A%20%20%20%20return%20exp_test_values%2C%20exp_test_y%2C%20pmf_sum_two_dice%0A%0A%0A%40app.cell%0Adef%20_(pmf_sum_two_dice)%3A%0A%20%20%20%20def%20calc_expectation_sum_two_dice()%3A%0A%20%20%20%20%20%20%20%20%22%22%22Calculate%20the%20expected%20value%20of%20the%20sum%20of%20two%20dice.%22%22%22%0A%20%20%20%20%20%20%20%20exp_sum_two_dice%20%3D%200%0A%20%20%20%20%20%20%20%20%23%20Sum%20of%20dice%20can%20take%20on%20the%20values%202%20through%2012%0A%20%20%20%20%20%20%20%20for%20exp_x%20in%20range(2%2C%2013)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20exp_pr_x%20%3D%20pmf_sum_two_dice(exp_x)%20%20%23%20PMF%20gives%20P(sum%20is%20x)%0A%20%20%20%20%20%20%20%20%20%20%20%20exp_sum_two_dice%20%2B%3D%20exp_x%20*%20exp_pr_x%0A%20%20%20%20%20%20%20%20return%20exp_sum_two_dice%0A%0A%20%20%20%20exp_sum_result%20%3D%20calc_expectation_sum_two_dice()%0A%0A%20%20%20%20%23%20Round%20to%202%20decimal%20places%20for%20display%0A%20%20%20%20exp_sum_result_rounded%20%3D%20round(exp_sum_result%2C%202)%0A%0A%20%20%20%20print(f%22Expected%20value%20of%20the%20sum%20of%20two%20dice%3A%20%7Bexp_sum_result_rounded%7D%22)%0A%0A%20%20%20%20%23%20Let's%20also%20verify%20this%20with%20a%20direct%20calculation%0A%20%20%20%20exp_direct_calc%20%3D%20sum(x%20*%20pmf_sum_two_dice(x)%20for%20x%20in%20range(2%2C%2013))%0A%20%20%20%20exp_direct_calc_rounded%20%3D%20round(exp_direct_calc%2C%202)%0A%0A%20%20%20%20print(f%22Direct%20calculation%3A%20%7Bexp_direct_calc_rounded%7D%22)%0A%0A%20%20%20%20%23%20Verify%20that%20this%20equals%207%0A%20%20%20%20print(f%22Is%20the%20expected%20value%20exactly%207%3F%20%7Babs(exp_sum_result%20-%207)%20%3C%201e-10%7D%22)%0A%20%20%20%20return%20(%0A%20%20%20%20%20%20%20%20calc_expectation_sum_two_dice%2C%0A%20%20%20%20%20%20%20%20exp_direct_calc%2C%0A%20%20%20%20%20%20%20%20exp_direct_calc_rounded%2C%0A%20%20%20%20%20%20%20%20exp_sum_result%2C%0A%20%20%20%20%20%20%20%20exp_sum_result_rounded%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%23%20Visualizing%20Expectation%0A%0A%20%20%20%20%20%20%20%20Let's%20visualize%20the%20expectation%20for%20the%20sum%20of%20two%20dice.%20The%20expected%20value%20is%20the%20%22center%20of%20mass%22%20of%20the%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(hide_code%3DTrue)%0Adef%20_(plt%2C%20pmf_sum_two_dice)%3A%0A%20%20%20%20%23%20Create%20the%20visualization%0A%20%20%20%20exp_y_values%20%3D%20list(range(2%2C%2013))%0A%20%20%20%20exp_probabilities%20%3D%20%5Bpmf_sum_two_dice(y)%20for%20y%20in%20exp_y_values%5D%0A%0A%20%20%20%20dice_fig%2C%20dice_ax%20%3D%20plt.subplots(figsize%3D(10%2C%205))%0A%20%20%20%20dice_ax.bar(exp_y_values%2C%20exp_probabilities%2C%20width%3D0.4)%0A%20%20%20%20dice_ax.axvline(x%3D7%2C%20color%3D'r'%2C%20linestyle%3D'--'%2C%20linewidth%3D2%2C%20label%3D'Expected%20Value%20(7)')%0A%0A%20%20%20%20dice_ax.set_xticks(exp_y_values)%0A%20%20%20%20dice_ax.set_xlabel('Sum%20of%20two%20dice%20(y)')%0A%20%20%20%20dice_ax.set_ylabel('Probability%3A%20P(Y%20%3D%20y)')%0A%20%20%20%20dice_ax.set_title('PMF%20of%20Sum%20of%20Two%20Dice%20with%20Expected%20Value')%0A%20%20%20%20dice_ax.grid(alpha%3D0.3)%0A%20%20%20%20dice_ax.legend()%0A%0A%20%20%20%20%23%20Add%20probability%20values%20on%20top%20of%20bars%0A%20%20%20%20for%20exp_i%2C%20exp_prob%20in%20enumerate(exp_probabilities)%3A%0A%20%20%20%20%20%20%20%20dice_ax.text(exp_y_values%5Bexp_i%5D%2C%20exp_prob%20%2B%200.001%2C%20f'%7Bexp_prob%3A.3f%7D'%2C%20ha%3D'center')%0A%0A%20%20%20%20plt.tight_layout()%0A%20%20%20%20plt.gca()%0A%20%20%20%20return%20dice_ax%2C%20dice_fig%2C%20exp_i%2C%20exp_prob%2C%20exp_probabilities%2C%20exp_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%20Demonstrating%20the%20Properties%20of%20Expectation%0A%0A%20%20%20%20%20%20%20%20Let's%20demonstrate%20some%20of%20these%20properties%20with%20examples%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_(exp_die_result)%3A%0A%20%20%20%20%23%20Demonstrate%20linearity%20of%20expectation%20(1)%0A%20%20%20%20%23%20E%5BaX%20%2B%20b%5D%20%3D%20a*E%5BX%5D%20%2B%20b%0A%0A%20%20%20%20%23%20For%20a%20die%20roll%20X%20with%20E%5BX%5D%20%3D%203.5%0A%20%20%20%20prop_a%20%3D%202%0A%20%20%20%20prop_b%20%3D%2010%0A%0A%20%20%20%20%23%20Calculate%20E%5B2X%20%2B%2010%5D%20using%20the%20property%0A%20%20%20%20prop_expected_using_property%20%3D%20prop_a%20*%20exp_die_result%20%2B%20prop_b%0A%20%20%20%20prop_expected_using_property_rounded%20%3D%20round(prop_expected_using_property%2C%202)%0A%0A%20%20%20%20print(f%22Using%20linearity%20property%3A%20E%5B%7Bprop_a%7DX%20%2B%20%7Bprop_b%7D%5D%20%3D%20%7Bprop_a%7D%20*%20E%5BX%5D%20%2B%20%7Bprop_b%7D%20%3D%20%7Bprop_expected_using_property_rounded%7D%22)%0A%0A%20%20%20%20%23%20Calculate%20E%5B2X%20%2B%2010%5D%20directly%0A%20%20%20%20prop_expected_direct%20%3D%20sum((prop_a%20*%20x%20%2B%20prop_b)%20*%20(1%2F6)%20for%20x%20in%20range(1%2C%207))%0A%20%20%20%20prop_expected_direct_rounded%20%3D%20round(prop_expected_direct%2C%202)%0A%0A%20%20%20%20print(f%22Direct%20calculation%3A%20E%5B%7Bprop_a%7DX%20%2B%20%7Bprop_b%7D%5D%20%3D%20%7Bprop_expected_direct_rounded%7D%22)%0A%0A%20%20%20%20%23%20Verify%20they%20match%0A%20%20%20%20print(f%22Do%20they%20match%3F%20%7Babs(prop_expected_using_property%20-%20prop_expected_direct)%20%3C%201e-10%7D%22)%0A%20%20%20%20return%20(%0A%20%20%20%20%20%20%20%20prop_a%2C%0A%20%20%20%20%20%20%20%20prop_b%2C%0A%20%20%20%20%20%20%20%20prop_expected_direct%2C%0A%20%20%20%20%20%20%20%20prop_expected_direct_rounded%2C%0A%20%20%20%20%20%20%20%20prop_expected_using_property%2C%0A%20%20%20%20%20%20%20%20prop_expected_using_property_rounded%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%23%20Law%20of%20the%20Unconscious%20Statistician%20(LOTUS)%0A%0A%20%20%20%20%20%20%20%20Let's%20use%20LOTUS%20to%20calculate%20%24E%5BX%5E2%5D%24%20for%20a%20die%20roll%2C%20which%20will%20be%20useful%20when%20we%20study%20variance%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%20%23%20Calculate%20E%5BX%5E2%5D%20for%20a%20die%20roll%20using%20LOTUS%20(3)%0A%20%20%20%20lotus_die_values%20%3D%20range(1%2C%207)%0A%20%20%20%20lotus_die_probs%20%3D%20%5B1%2F6%5D%20*%206%0A%0A%20%20%20%20%23%20Using%20LOTUS%3A%20E%5BX%5E2%5D%20%3D%20sum(x%5E2%20*%20P(X%3Dx))%0A%20%20%20%20lotus_expected_x_squared%20%3D%20sum(x**2%20*%20p%20for%20x%2C%20p%20in%20zip(lotus_die_values%2C%20lotus_die_probs))%0A%20%20%20%20lotus_expected_x_squared_rounded%20%3D%20round(lotus_expected_x_squared%2C%202)%0A%0A%20%20%20%20expected_x_squared%20%3D%203.5**2%0A%20%20%20%20expected_x_squared_rounded%20%3D%20round(expected_x_squared%2C%202)%0A%0A%20%20%20%20print(f%22E%5BX%5E2%5D%20for%20a%20die%20roll%20%3D%20%7Blotus_expected_x_squared_rounded%7D%22)%0A%20%20%20%20print(f%22(E%5BX%5D)%5E2%20for%20a%20die%20roll%20%3D%20%7Bexpected_x_squared_rounded%7D%22)%0A%20%20%20%20return%20(%0A%20%20%20%20%20%20%20%20expected_x_squared%2C%0A%20%20%20%20%20%20%20%20expected_x_squared_rounded%2C%0A%20%20%20%20%20%20%20%20lotus_die_probs%2C%0A%20%20%20%20%20%20%20%20lotus_die_values%2C%0A%20%20%20%20%20%20%20%20lotus_expected_x_squared%2C%0A%20%20%20%20%20%20%20%20lotus_expected_x_squared_rounded%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%2F%2F%2F%20Note%0A%20%20%20%20%20%20%20%20Note%20that%20E%5BX%5E2%5D%20!%3D%20(E%5BX%5D)%5E2%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%20Interactive%20Example%0A%0A%20%20%20%20%20%20%20%20Let's%20explore%20how%20the%20expected%20value%20changes%20as%20we%20adjust%20the%20parameters%20of%20common%20probability%20distributions.%20This%20interactive%20visualization%20focuses%20specifically%20on%20the%20relationship%20between%20distribution%20parameters%20and%20expected%20values.%0A%0A%20%20%20%20%20%20%20%20Use%20the%20controls%20below%20to%20select%20a%20distribution%20and%20adjust%20its%20parameters.%20The%20graph%20will%20show%20how%20the%20expected%20value%20changes%20across%20a%20range%20of%20parameter%20values.%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%20%23%20Create%20UI%20elements%20for%20distribution%20selection%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%20%20%20%20return%20(dist_selection%2C)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(dist_selection)%3A%0A%20%20%20%20dist_selection.center()%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(dist_description)%3A%0A%20%20%20%20dist_description%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(%22%22%22%23%23%23%20Adjust%20Parameters%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(controls)%3A%0A%20%20%20%20controls%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(%0A%20%20%20%20dist_selection%2C%0A%20%20%20%20lambda_range%2C%0A%20%20%20%20np%2C%0A%20%20%20%20param_lambda%2C%0A%20%20%20%20param_n%2C%0A%20%20%20%20param_p%2C%0A%20%20%20%20param_range%2C%0A%20%20%20%20plt%2C%0A)%3A%0A%20%20%20%20%23%20Calculate%20expected%20values%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%20Get%20parameter%20range%20for%20visualization%0A%20%20%20%20%20%20%20%20p_min%2C%20p_max%20%3D%20param_range.value%0A%20%20%20%20%20%20%20%20param_values%20%3D%20np.linspace(p_min%2C%20p_max%2C%20100)%0A%0A%20%20%20%20%20%20%20%20%23%20E%5BX%5D%20%3D%20p%20for%20Bernoulli%0A%20%20%20%20%20%20%20%20expected_values%20%3D%20param_values%0A%20%20%20%20%20%20%20%20current_param%20%3D%20param_p.value%0A%20%20%20%20%20%20%20%20current_expected%20%3D%20round(current_param%2C%202)%0A%20%20%20%20%20%20%20%20x_label%20%3D%20%22p%20(probability%20of%20success)%22%0A%20%20%20%20%20%20%20%20title%20%3D%20%22Expected%20Value%20of%20Bernoulli%20Distribution%22%0A%20%20%20%20%20%20%20%20formula%20%3D%20%22E%5BX%5D%20%3D%20p%22%0A%0A%20%20%20%20elif%20dist_selection.value%20%3D%3D%20%22binomial%22%3A%0A%20%20%20%20%20%20%20%20p_min%2C%20p_max%20%3D%20param_range.value%0A%20%20%20%20%20%20%20%20param_values%20%3D%20np.linspace(p_min%2C%20p_max%2C%20100)%0A%0A%20%20%20%20%20%20%20%20%23%20E%5BX%5D%20%3D%20np%20for%20Binomial%0A%20%20%20%20%20%20%20%20n%20%3D%20int(param_n.value)%0A%20%20%20%20%20%20%20%20expected_values%20%3D%20%5Bn%20*%20p%20for%20p%20in%20param_values%5D%0A%20%20%20%20%20%20%20%20current_param%20%3D%20param_p.value%0A%20%20%20%20%20%20%20%20current_expected%20%3D%20round(n%20*%20current_param%2C%202)%0A%20%20%20%20%20%20%20%20x_label%20%3D%20%22p%20(probability%20of%20success)%22%0A%20%20%20%20%20%20%20%20title%20%3D%20f%22Expected%20Value%20of%20Binomial%20Distribution%20(n%3D%7Bn%7D)%22%0A%20%20%20%20%20%20%20%20formula%20%3D%20f%22E%5BX%5D%20%3D%20n%20%C3%97%20p%20%3D%20%7Bn%7D%20%C3%97%20p%22%0A%0A%20%20%20%20elif%20dist_selection.value%20%3D%3D%20%22geometric%22%3A%0A%20%20%20%20%20%20%20%20p_min%2C%20p_max%20%3D%20param_range.value%0A%20%20%20%20%20%20%20%20%23%20Ensure%20p%20is%20not%200%20for%20geometric%20distribution%0A%20%20%20%20%20%20%20%20p_min%20%3D%20max(0.01%2C%20p_min)%0A%20%20%20%20%20%20%20%20param_values%20%3D%20np.linspace(p_min%2C%20p_max%2C%20100)%0A%0A%20%20%20%20%20%20%20%20%23%20E%5BX%5D%20%3D%201%2Fp%20for%20Geometric%0A%20%20%20%20%20%20%20%20expected_values%20%3D%20%5B1%2Fp%20for%20p%20in%20param_values%5D%0A%20%20%20%20%20%20%20%20current_param%20%3D%20param_p.value%0A%20%20%20%20%20%20%20%20current_expected%20%3D%20round(1%20%2F%20current_param%2C%202)%0A%20%20%20%20%20%20%20%20x_label%20%3D%20%22p%20(probability%20of%20success)%22%0A%20%20%20%20%20%20%20%20title%20%3D%20%22Expected%20Value%20of%20Geometric%20Distribution%22%0A%20%20%20%20%20%20%20%20formula%20%3D%20%22E%5BX%5D%20%3D%201%2Fp%22%0A%0A%20%20%20%20else%3A%20%20%23%20Poisson%0A%20%20%20%20%20%20%20%20lambda_min%2C%20lambda_max%20%3D%20lambda_range.value%0A%20%20%20%20%20%20%20%20param_values%20%3D%20np.linspace(lambda_min%2C%20lambda_max%2C%20100)%0A%0A%20%20%20%20%20%20%20%20%23%20E%5BX%5D%20%3D%20lambda%20for%20Poisson%0A%20%20%20%20%20%20%20%20expected_values%20%3D%20param_values%0A%20%20%20%20%20%20%20%20current_param%20%3D%20param_lambda.value%0A%20%20%20%20%20%20%20%20current_expected%20%3D%20round(current_param%2C%202)%0A%20%20%20%20%20%20%20%20x_label%20%3D%20%22%CE%BB%20(rate%20parameter)%22%0A%20%20%20%20%20%20%20%20title%20%3D%20%22Expected%20Value%20of%20Poisson%20Distribution%22%0A%20%20%20%20%20%20%20%20formula%20%3D%20%22E%5BX%5D%20%3D%20%CE%BB%22%0A%0A%20%20%20%20%23%20Create%20the%20plot%0A%20%20%20%20dist_fig%2C%20dist_ax%20%3D%20plt.subplots(figsize%3D(10%2C%206))%0A%0A%20%20%20%20%23%20Plot%20the%20expected%20value%20function%0A%20%20%20%20dist_ax.plot(param_values%2C%20expected_values%2C%20'b-'%2C%20linewidth%3D2%2C%20label%3D%22Expected%20Value%20Function%22)%0A%0A%20%20%20%20dist_ax.plot(current_param%2C%20current_expected%2C%20'ro'%2C%20markersize%3D10%2C%20label%3Df%22Current%20Value%3A%20E%5BX%5D%20%3D%20%7Bcurrent_expected%7D%22)%0A%0A%20%20%20%20dist_ax.hlines(current_expected%2C%20param_values%5B0%5D%2C%20current_param%2C%20colors%3D'r'%2C%20linestyles%3D'dashed')%0A%0A%20%20%20%20dist_ax.vlines(current_param%2C%200%2C%20current_expected%2C%20colors%3D'r'%2C%20linestyles%3D'dashed')%0A%0A%20%20%20%20dist_ax.fill_between(param_values%2C%200%2C%20expected_values%2C%20alpha%3D0.2%2C%20color%3D'blue')%0A%0A%20%20%20%20dist_ax.set_xlabel(x_label%2C%20fontsize%3D12)%0A%20%20%20%20dist_ax.set_ylabel(%22Expected%20Value%3A%20E%5BX%5D%22%2C%20fontsize%3D12)%0A%20%20%20%20dist_ax.set_title(title%2C%20fontsize%3D14%2C%20fontweight%3D'bold')%0A%20%20%20%20dist_ax.grid(True%2C%20alpha%3D0.3)%0A%0A%20%20%20%20%23%20Move%20legend%20to%20lower%20right%20to%20avoid%20overlap%20with%20formula%0A%20%20%20%20dist_ax.legend(loc%3D'lower%20right'%2C%20fontsize%3D10)%0A%0A%20%20%20%20%23%20Add%20formula%20text%20box%20in%20upper%20left%0A%20%20%20%20dist_props%20%3D%20dict(boxstyle%3D'round'%2C%20facecolor%3D'white'%2C%20alpha%3D0.8)%0A%20%20%20%20dist_ax.text(0.02%2C%200.95%2C%20formula%2C%20transform%3Ddist_ax.transAxes%2C%20fontsize%3D12%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20verticalalignment%3D'top'%2C%20bbox%3Ddist_props)%0A%0A%20%20%20%20if%20dist_selection.value%20%3D%3D%20%22geometric%22%3A%0A%20%20%20%20%20%20%20%20max_y%20%3D%20min(50%2C%202%2Fmax(0.01%2C%20param_values%5B0%5D))%0A%20%20%20%20%20%20%20%20dist_ax.set_ylim(0%2C%20max_y)%0A%20%20%20%20elif%20dist_selection.value%20%3D%3D%20%22binomial%22%3A%0A%20%20%20%20%20%20%20%20dist_ax.set_ylim(0%2C%20int(param_n.value)%20%2B%201)%0A%20%20%20%20else%3A%0A%20%20%20%20%20%20%20%20dist_ax.set_ylim(0%2C%20max(expected_values)%20*%201.1)%0A%0A%20%20%20%20annotation_x%20%3D%20current_param%20%2B%20(param_values%5B-1%5D%20-%20param_values%5B0%5D)%20*%200.05%0A%20%20%20%20annotation_y%20%3D%20current_expected%0A%0A%20%20%20%20%23%20Adjust%20annotation%20position%20if%20it%20would%20go%20off%20the%20chart%0A%20%20%20%20if%20annotation_x%20%3E%20param_values%5B-1%5D%20*%200.9%3A%0A%20%20%20%20%20%20%20%20annotation_x%20%3D%20current_param%20-%20(param_values%5B-1%5D%20-%20param_values%5B0%5D)%20*%200.2%0A%0A%20%20%20%20dist_ax.annotate(%0A%20%20%20%20%20%20%20%20f%22Parameter%3A%20%7Bcurrent_param%3A.2f%7D%5CnE%5BX%5D%20%3D%20%7Bcurrent_expected%7D%22%2C%0A%20%20%20%20%20%20%20%20xy%3D(current_param%2C%20current_expected)%2C%0A%20%20%20%20%20%20%20%20xytext%3D(annotation_x%2C%20annotation_y)%2C%0A%20%20%20%20%20%20%20%20arrowprops%3Ddict(facecolor%3D'black'%2C%20shrink%3D0.05%2C%20width%3D1.5%2C%20alpha%3D0.7)%2C%0A%20%20%20%20%20%20%20%20bbox%3Ddist_props%0A%20%20%20%20)%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%20annotation_x%2C%0A%20%20%20%20%20%20%20%20annotation_y%2C%0A%20%20%20%20%20%20%20%20current_expected%2C%0A%20%20%20%20%20%20%20%20current_param%2C%0A%20%20%20%20%20%20%20%20dist_ax%2C%0A%20%20%20%20%20%20%20%20dist_fig%2C%0A%20%20%20%20%20%20%20%20dist_props%2C%0A%20%20%20%20%20%20%20%20expected_values%2C%0A%20%20%20%20%20%20%20%20formula%2C%0A%20%20%20%20%20%20%20%20lambda_max%2C%0A%20%20%20%20%20%20%20%20lambda_min%2C%0A%20%20%20%20%20%20%20%20max_y%2C%0A%20%20%20%20%20%20%20%20n%2C%0A%20%20%20%20%20%20%20%20p_max%2C%0A%20%20%20%20%20%20%20%20p_min%2C%0A%20%20%20%20%20%20%20%20param_values%2C%0A%20%20%20%20%20%20%20%20title%2C%0A%20%20%20%20%20%20%20%20x_label%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%20Expectation%20vs.%20Mode%0A%0A%20%20%20%20%20%20%20%20The%20expected%20value%20(mean)%20of%20a%20random%20variable%20is%20not%20always%20the%20same%20as%20its%20most%20likely%20value%20(mode).%20Let's%20explore%20this%20with%20an%20example%3A%0A%20%20%20%20%20%20%20%20%22%22%22%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(np%2C%20plt%2C%20stats)%3A%0A%20%20%20%20%23%20Create%20a%20skewed%20distribution%0A%20%20%20%20skew_n%20%3D%2010%0A%20%20%20%20skew_p%20%3D%200.25%0A%0A%20%20%20%20%23%20Binomial%20PMF%0A%20%20%20%20skew_x_values%20%3D%20np.arange(0%2C%20skew_n%2B1)%0A%20%20%20%20skew_pmf_values%20%3D%20stats.binom.pmf(skew_x_values%2C%20skew_n%2C%20skew_p)%0A%0A%20%20%20%20%23%20Find%20the%20mode%20(most%20likely%20value)%0A%20%20%20%20skew_mode%20%3D%20skew_x_values%5Bnp.argmax(skew_pmf_values)%5D%0A%0A%20%20%20%20%23%20Calculate%20the%20expected%20value%0A%20%20%20%20skew_expected%20%3D%20skew_n%20*%20skew_p%0A%20%20%20%20skew_expected_rounded%20%3D%20round(skew_expected%2C%202)%0A%0A%20%20%20%20skew_fig%2C%20skew_ax%20%3D%20plt.subplots(figsize%3D(10%2C%205))%0A%20%20%20%20skew_ax.bar(skew_x_values%2C%20skew_pmf_values%2C%20alpha%3D0.7%2C%20width%3D0.4)%0A%0A%20%20%20%20%23%20Add%20vertical%20lines%20for%20mode%20and%20expected%20value%0A%20%20%20%20skew_ax.axvline(x%3Dskew_mode%2C%20color%3D'g'%2C%20linestyle%3D'--'%2C%20linewidth%3D2%2C%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20label%3Df'Mode%20%3D%20%7Bskew_mode%7D%20(Most%20likely%20value)')%0A%20%20%20%20skew_ax.axvline(x%3Dskew_expected%2C%20color%3D'r'%2C%20linestyle%3D'--'%2C%20linewidth%3D2%2C%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20label%3Df'Expected%20Value%20%3D%20%7Bskew_expected_rounded%7D%20(Mean)')%0A%0A%20%20%20%20skew_ax.annotate('Mode'%2C%20xy%3D(skew_mode%2C%200.05)%2C%20xytext%3D(skew_mode-2.0%2C%200.1)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20arrowprops%3Ddict(facecolor%3D'green'%2C%20shrink%3D0.05%2C%20width%3D1.5)%2C%20color%3D'green')%0A%20%20%20%20skew_ax.annotate('Expected%20Value'%2C%20xy%3D(skew_expected%2C%200.05)%2C%20xytext%3D(skew_expected%2B1%2C%200.15)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20arrowprops%3Ddict(facecolor%3D'red'%2C%20shrink%3D0.05%2C%20width%3D1.5)%2C%20color%3D'red')%0A%0A%20%20%20%20if%20skew_mode%20!%3D%20int(skew_expected)%3A%0A%20%20%20%20%20%20%20%20min_x%20%3D%20min(skew_mode%2C%20skew_expected)%0A%20%20%20%20%20%20%20%20max_x%20%3D%20max(skew_mode%2C%20skew_expected)%0A%20%20%20%20%20%20%20%20skew_ax.axvspan(min_x%2C%20max_x%2C%20alpha%3D0.2%2C%20color%3D'purple')%0A%0A%20%20%20%20%20%20%20%20%23%20Add%20text%20explaining%20the%20difference%0A%20%20%20%20%20%20%20%20mid_x%20%3D%20(skew_mode%20%2B%20skew_expected)%20%2F%202%0A%20%20%20%20%20%20%20%20skew_ax.text(mid_x%2C%20max(skew_pmf_values)%20*%200.5%2C%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20f%22Difference%3A%20%7Babs(skew_mode%20-%20skew_expected_rounded)%3A.2f%7D%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20ha%3D'center'%2C%20va%3D'center'%2C%20bbox%3Ddict(facecolor%3D'white'%2C%20alpha%3D0.7))%0A%0A%20%20%20%20skew_ax.set_xlabel('Number%20of%20Successes')%0A%20%20%20%20skew_ax.set_ylabel('Probability')%0A%20%20%20%20skew_ax.set_title(f'Binomial%20Distribution%20(n%3D%7Bskew_n%7D%2C%20p%3D%7Bskew_p%7D)')%0A%20%20%20%20skew_ax.grid(alpha%3D0.3)%0A%20%20%20%20skew_ax.legend()%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%20max_x%2C%0A%20%20%20%20%20%20%20%20mid_x%2C%0A%20%20%20%20%20%20%20%20min_x%2C%0A%20%20%20%20%20%20%20%20skew_ax%2C%0A%20%20%20%20%20%20%20%20skew_expected%2C%0A%20%20%20%20%20%20%20%20skew_expected_rounded%2C%0A%20%20%20%20%20%20%20%20skew_fig%2C%0A%20%20%20%20%20%20%20%20skew_mode%2C%0A%20%20%20%20%20%20%20%20skew_n%2C%0A%20%20%20%20%20%20%20%20skew_p%2C%0A%20%20%20%20%20%20%20%20skew_pmf_values%2C%0A%20%20%20%20%20%20%20%20skew_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%2F%2F%2F%20NOTE%0A%20%20%20%20%20%20%20%20For%20the%20sum%20of%20two%20dice%20we%20calculated%20earlier%2C%20we%20found%20the%20expected%20value%20to%20be%20exactly%207.%20In%20that%20case%2C%207%20also%20happens%20to%20be%20the%20mode%20(most%20likely%20outcome)%20of%20the%20distribution.%20However%2C%20this%20is%20just%20a%20coincidence%20for%20this%20particular%20example!%0A%0A%20%20%20%20%20%20%20%20As%20we%20can%20see%20from%20the%20binomial%20distribution%20above%2C%20the%20expected%20value%20(2.50)%20and%20the%20mode%20(2)%20are%20often%20different%20values%20(this%20is%20common%20in%20skewed%20distributions).%20The%20expected%20value%20represents%20the%20%22center%20of%20mass%22%20of%20the%20distribution%2C%20while%20the%20mode%20represents%20the%20most%20likely%20single%20outcome.%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%20%F0%9F%A4%94%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%3EThe%20expected%20value%20of%20a%20random%20variable%20is%20always%20one%20of%20the%20possible%20values%20the%20random%20variable%20can%20take.%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%0A%20%20%20%20%20%20%20%20%3Cdetails%3E%0A%20%20%20%20%20%20%20%20%3Csummary%3EIf%20X%20and%20Y%20are%20independent%20random%20variables%2C%20then%20E%5BX%C3%82%C2%B7Y%5D%20%3D%20E%5BX%5D%C3%82%C2%B7E%5BY%5D.%3C%2Fsummary%3E%0A%20%20%20%20%20%20%20%20%E2%9C%85%20True!%20For%20independent%20random%20variables%2C%20the%20expectation%20of%20their%20product%20equals%20the%20product%20of%20their%20expectations.%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%20constant%20random%20variable%20(one%20that%20always%20takes%20the%20same%20value)%20is%20that%20constant.%3C%2Fsummary%3E%0A%20%20%20%20%20%20%20%20%E2%9C%85%20True!%20If%20X%20%3D%20c%20with%20probability%201%2C%20then%20E%5BX%5D%20%3D%20c.%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%20the%20sum%20of%20two%20random%20variables%20is%20always%20the%20sum%20of%20their%20expected%20values%2C%20regardless%20of%20whether%20they%20are%20independent.%3C%2Fsummary%3E%0A%20%20%20%20%20%20%20%20%E2%9C%85%20True!%20This%20is%20the%20linearity%20of%20expectation%20property%3A%20E%5BX%20%2B%20Y%5D%20%3D%20E%5BX%5D%20%2B%20E%5BY%5D%2C%20which%20holds%20regardless%20of%20dependence.%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%20Expectation%0A%0A%20%20%20%20%20%20%20%20Expected%20values%20show%20up%20everywhere%20-%20from%20investment%20decisions%20and%20insurance%20pricing%20to%20machine%20learning%20algorithms%20and%20game%20design.%20Engineers%20use%20them%20to%20predict%20system%20reliability%2C%20data%20scientists%20to%20understand%20customer%20behavior%2C%20and%20economists%20to%20model%20market%20outcomes.%20They're%20essential%20for%20risk%20assessment%20in%20project%20management%20and%20for%20optimizing%20resource%20allocation%20in%20operations%20research.%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%20Expectation%20gives%20us%20a%20single%20value%20that%20summarizes%20a%20random%20variable's%20central%20tendency%20-%20it's%20the%20weighted%20average%20of%20all%20possible%20outcomes%2C%20where%20the%20weights%20are%20probabilities.%20The%20linearity%20property%20makes%20expectations%20easy%20to%20work%20with%2C%20even%20for%20complex%20combinations%20of%20random%20variables.%20While%20a%20PMF%20gives%20the%20complete%20probability%20picture%2C%20expectation%20provides%20an%20essential%20summary%20that%20helps%20us%20make%20decisions%20under%20uncertainty.%20In%20our%20next%20notebook%2C%20we'll%20explore%20variance%2C%20which%20measures%20how%20spread%20out%20a%20random%20variable's%20values%20are%20around%20its%20expectation.%0A%20%20%20%20%20%20%20%20%22%22%22%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%23%23%23%23%20Appendix%20(containing%20helper%20code)%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20import%20marimo%20as%20mo%0A%20%20%20%20return%20(mo%2C)%0A%0A%0A%40app.cell(hide_code%3DTrue)%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%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(dist_selection%2C%20mo)%3A%0A%20%20%20%20%23%20Parameter%20controls%20for%20probability-based%20distributions%0A%20%20%20%20param_p%20%3D%20mo.ui.slider(%0A%20%20%20%20%20%20%20%20start%3D0.01%2C%20%0A%20%20%20%20%20%20%20%20stop%3D0.99%2C%20%0A%20%20%20%20%20%20%20%20step%3D0.01%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(probability%20of%20success)%22%2C%0A%20%20%20%20%20%20%20%20full_width%3DTrue%0A%20%20%20%20)%0A%0A%20%20%20%20%23%20Parameter%20control%20for%20binomial%20distribution%0A%20%20%20%20param_n%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%3D50%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(number%20of%20trials)%22%2C%0A%20%20%20%20%20%20%20%20full_width%3DTrue%0A%20%20%20%20)%0A%0A%20%20%20%20%23%20Parameter%20control%20for%20Poisson%20distribution%0A%20%20%20%20param_lambda%20%3D%20mo.ui.slider(%0A%20%20%20%20%20%20%20%20start%3D0.1%2C%20%0A%20%20%20%20%20%20%20%20stop%3D20%2C%20%0A%20%20%20%20%20%20%20%20step%3D0.1%2C%20%0A%20%20%20%20%20%20%20%20value%3D5%2C%20%0A%20%20%20%20%20%20%20%20label%3D%22%CE%BB%20(rate%20parameter)%22%2C%0A%20%20%20%20%20%20%20%20full_width%3DTrue%0A%20%20%20%20)%0A%0A%20%20%20%20%23%20Parameter%20range%20sliders%20for%20visualization%0A%20%20%20%20param_range%20%3D%20mo.ui.range_slider(%0A%20%20%20%20%20%20%20%20start%3D0%2C%20%0A%20%20%20%20%20%20%20%20stop%3D1%2C%20%0A%20%20%20%20%20%20%20%20step%3D0.01%2C%20%0A%20%20%20%20%20%20%20%20value%3D%5B0%2C%201%5D%2C%20%0A%20%20%20%20%20%20%20%20label%3D%22Parameter%20range%20to%20visualize%22%2C%0A%20%20%20%20%20%20%20%20full_width%3DTrue%0A%20%20%20%20)%0A%0A%20%20%20%20lambda_range%20%3D%20mo.ui.range_slider(%0A%20%20%20%20%20%20%20%20start%3D0%2C%20%0A%20%20%20%20%20%20%20%20stop%3D20%2C%20%0A%20%20%20%20%20%20%20%20step%3D0.1%2C%20%0A%20%20%20%20%20%20%20%20value%3D%5B0%2C%2020%5D%2C%20%0A%20%20%20%20%20%20%20%20label%3D%22%CE%BB%20range%20to%20visualize%22%2C%0A%20%20%20%20%20%20%20%20full_width%3DTrue%0A%20%20%20%20)%0A%0A%20%20%20%20%23%20Display%20appropriate%20controls%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%20controls%20%3D%20mo.hstack(%5Bparam_p%2C%20param_range%5D%2C%20justify%3D%22space-around%22)%0A%20%20%20%20elif%20dist_selection.value%20%3D%3D%20%22binomial%22%3A%0A%20%20%20%20%20%20%20%20controls%20%3D%20mo.hstack(%5Bparam_p%2C%20param_n%2C%20param_range%5D%2C%20justify%3D%22space-around%22)%0A%20%20%20%20elif%20dist_selection.value%20%3D%3D%20%22geometric%22%3A%0A%20%20%20%20%20%20%20%20controls%20%3D%20mo.hstack(%5Bparam_p%2C%20param_range%5D%2C%20justify%3D%22space-around%22)%0A%20%20%20%20else%3A%20%20%23%20poisson%0A%20%20%20%20%20%20%20%20controls%20%3D%20mo.hstack(%5Bparam_lambda%2C%20lambda_range%5D%2C%20justify%3D%22space-around%22)%0A%20%20%20%20return%20controls%2C%20lambda_range%2C%20param_lambda%2C%20param_n%2C%20param_p%2C%20param_range%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(dist_selection%2C%20mo)%3A%0A%20%20%20%20%23%20Create%20distribution%20descriptions%20based%20on%20selection%0A%20%20%20%20if%20dist_selection.value%20%3D%3D%20%22bernoulli%22%3A%0A%20%20%20%20%20%20%20%20dist_description%20%3D%20mo.md(%0A%20%20%20%20%20%20%20%20%20%20%20%20r%22%22%22%0A%20%20%20%20%20%20%20%20%20%20%20%20**Bernoulli%20Distribution**%0A%0A%20%20%20%20%20%20%20%20%20%20%20%20A%20Bernoulli%20distribution%20models%20a%20single%20trial%20with%20two%20possible%20outcomes%3A%20success%20(1)%20or%20failure%20(0).%0A%0A%20%20%20%20%20%20%20%20%20%20%20%20-%20Parameter%3A%20%24p%24%20%3D%20probability%20of%20success%0A%20%20%20%20%20%20%20%20%20%20%20%20-%20Expected%20Value%3A%20%24E%5BX%5D%20%3D%20p%24%0A%20%20%20%20%20%20%20%20%20%20%20%20-%20Example%3A%20Flipping%20a%20coin%20once%20(p%20%3D%200.5%20for%20a%20fair%20coin)%0A%20%20%20%20%20%20%20%20%20%20%20%20%22%22%22%0A%20%20%20%20%20%20%20%20)%0A%20%20%20%20elif%20dist_selection.value%20%3D%3D%20%22binomial%22%3A%0A%20%20%20%20%20%20%20%20dist_description%20%3D%20mo.md(%0A%20%20%20%20%20%20%20%20%20%20%20%20r%22%22%22%0A%20%20%20%20%20%20%20%20%20%20%20%20**Binomial%20Distribution**%0A%0A%20%20%20%20%20%20%20%20%20%20%20%20A%20Binomial%20distribution%20models%20the%20number%20of%20successes%20in%20%24n%24%20independent%20trials.%0A%0A%20%20%20%20%20%20%20%20%20%20%20%20-%20Parameters%3A%20%24n%24%20%3D%20number%20of%20trials%2C%20%24p%24%20%3D%20probability%20of%20success%0A%20%20%20%20%20%20%20%20%20%20%20%20-%20Expected%20Value%3A%20%24E%5BX%5D%20%3D%20np%24%0A%20%20%20%20%20%20%20%20%20%20%20%20-%20Example%3A%20Number%20of%20heads%20in%2010%20coin%20flips%0A%20%20%20%20%20%20%20%20%20%20%20%20%22%22%22%0A%20%20%20%20%20%20%20%20)%0A%20%20%20%20elif%20dist_selection.value%20%3D%3D%20%22geometric%22%3A%0A%20%20%20%20%20%20%20%20dist_description%20%3D%20mo.md(%0A%20%20%20%20%20%20%20%20%20%20%20%20r%22%22%22%0A%20%20%20%20%20%20%20%20%20%20%20%20**Geometric%20Distribution**%0A%0A%20%20%20%20%20%20%20%20%20%20%20%20A%20Geometric%20distribution%20models%20the%20number%20of%20trials%20until%20the%20first%20success.%0A%0A%20%20%20%20%20%20%20%20%20%20%20%20-%20Parameter%3A%20%24p%24%20%3D%20probability%20of%20success%0A%20%20%20%20%20%20%20%20%20%20%20%20-%20Expected%20Value%3A%20%24E%5BX%5D%20%3D%20%5Cfrac%7B1%7D%7Bp%7D%24%0A%20%20%20%20%20%20%20%20%20%20%20%20-%20Example%3A%20Number%20of%20coin%20flips%20until%20first%20heads%0A%20%20%20%20%20%20%20%20%20%20%20%20%22%22%22%0A%20%20%20%20%20%20%20%20)%0A%20%20%20%20else%3A%20%20%23%20poisson%0A%20%20%20%20%20%20%20%20dist_description%20%3D%20mo.md(%0A%20%20%20%20%20%20%20%20%20%20%20%20r%22%22%22%0A%20%20%20%20%20%20%20%20%20%20%20%20**Poisson%20Distribution**%0A%0A%20%20%20%20%20%20%20%20%20%20%20%20A%20Poisson%20distribution%20models%20the%20number%20of%20events%20occurring%20in%20a%20fixed%20interval.%0A%0A%20%20%20%20%20%20%20%20%20%20%20%20-%20Parameter%3A%20%24%5Clambda%24%20%3D%20average%20rate%20of%20events%0A%20%20%20%20%20%20%20%20%20%20%20%20-%20Expected%20Value%3A%20%24E%5BX%5D%20%3D%20%5Clambda%24%0A%20%20%20%20%20%20%20%20%20%20%20%20-%20Example%3A%20Number%20of%20emails%20received%20per%20hour%0A%20%20%20%20%20%20%20%20%20%20%20%20%22%22%22%0A%20%20%20%20%20%20%20%20)%0A%20%20%20%20return%20(dist_description%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>