{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "(lec7_live_complete)=\n", "# [complete] Lecture 7 live coding\n", "\n", "**2025-02-25**\n", "\n", "---" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Law of Total Expectation" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import pandas as pd\n", "import numpy as np\n", "\n", "import matplotlib.pyplot as plt\n", "import seaborn as sns\n", "import ipywidgets as widgets" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "# Set random seed for reproducibility\n", "rng = np.random.RandomState(42)\n", "n_samples = 10000" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Suppose we're looking at average coffee prices globally over two types of coffee beans: Arabica and Robusta.\n", "\n", "$$\n", "X = \\begin{cases}\n", "1 & \\text{if coffee is Arabica} \\\\\n", "0 & \\text{if coffee is Robusta}\n", "\\end{cases}\n", "$$" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([1, 0, 1, 1, 1])" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# # generate a random variable X with 80% arabica and 20% robusta\n", "X = rng.choice([0, 1], size=n_samples, p=[0.2, 0.8])" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "# COMPLETED CELL\n", "# # generate a random variable X with 80% arabica and 20% robusta\n", "# X = rng.choice([1, 0], size=n_samples, p=[0.8, 0.2])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Let $Y$ be the price of coffee in dollars per pound.\n", "\n", "Arabica prices are higher, on average:\n", "$$\n", "Y \\sim \\begin{cases}\n", "\\mathcal{N}(4.5, \\;0.75^2) & \\text{if coffee is Arabica} \\\\\n", "\\mathcal{N}(2.0, \\;0.25^2) & \\text{if coffee is Robusta}\n", "\\end{cases}\n", "$$" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " | is_arabica | \n", "price $/lb | \n", "
---|---|---|
0 | \n", "1 | \n", "3.517227 | \n", "
1 | \n", "0 | \n", "2.048971 | \n", "
2 | \n", "1 | \n", "4.078259 | \n", "
3 | \n", "1 | \n", "4.295241 | \n", "
4 | \n", "1 | \n", "5.240613 | \n", "