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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# /// script
|
| 2 |
+
# requires-python = ">=3.10"
|
| 3 |
+
# dependencies = [
|
| 4 |
+
# "marimo",
|
| 5 |
+
# "matplotlib==3.10.0",
|
| 6 |
+
# "numpy==2.2.3",
|
| 7 |
+
# "scipy==1.15.2",
|
| 8 |
+
# ]
|
| 9 |
+
# ///
|
| 10 |
+
|
| 11 |
+
import marimo
|
| 12 |
+
|
| 13 |
+
__generated_with = "0.11.10"
|
| 14 |
+
app = marimo.App(width="medium", app_title="Random Variables")
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
@app.cell
|
| 18 |
+
def _():
|
| 19 |
+
import marimo as mo
|
| 20 |
+
return (mo,)
|
| 21 |
+
|
| 22 |
+
|
| 23 |
+
@app.cell
|
| 24 |
+
def _():
|
| 25 |
+
import matplotlib.pyplot as plt
|
| 26 |
+
import numpy as np
|
| 27 |
+
from scipy import stats
|
| 28 |
+
return np, plt, stats
|
| 29 |
+
|
| 30 |
+
|
| 31 |
+
@app.cell(hide_code=True)
|
| 32 |
+
def _(mo):
|
| 33 |
+
mo.md(
|
| 34 |
+
r"""
|
| 35 |
+
# Random Variables
|
| 36 |
+
|
| 37 |
+
_This notebook is a computational companion to ["Probability for Computer Scientists"](https://chrispiech.github.io/probabilityForComputerScientists/en/part2/rvs/), by Stanford professor Chris Piech._
|
| 38 |
+
|
| 39 |
+
Random variables are functions that map outcomes from a probability space to numbers. This mathematical abstraction allows us to:
|
| 40 |
+
|
| 41 |
+
- Work with numerical outcomes in probability
|
| 42 |
+
- Calculate expected values and variances
|
| 43 |
+
- Model real-world phenomena quantitatively
|
| 44 |
+
"""
|
| 45 |
+
)
|
| 46 |
+
return
|
| 47 |
+
|
| 48 |
+
|
| 49 |
+
@app.cell(hide_code=True)
|
| 50 |
+
def _(mo):
|
| 51 |
+
mo.md(
|
| 52 |
+
r"""
|
| 53 |
+
## Types of Random Variables
|
| 54 |
+
|
| 55 |
+
### Discrete Random Variables
|
| 56 |
+
- Take on countable values (finite or infinite)
|
| 57 |
+
- Described by a probability mass function (PMF)
|
| 58 |
+
- Example: Number of heads in 3 coin flips
|
| 59 |
+
|
| 60 |
+
### Continuous Random Variables
|
| 61 |
+
- Take on uncountable values in an interval
|
| 62 |
+
- Described by a probability density function (PDF)
|
| 63 |
+
- Example: Height of a randomly selected person
|
| 64 |
+
"""
|
| 65 |
+
)
|
| 66 |
+
return
|
| 67 |
+
|
| 68 |
+
|
| 69 |
+
@app.cell(hide_code=True)
|
| 70 |
+
def _(mo):
|
| 71 |
+
mo.md(
|
| 72 |
+
r"""
|
| 73 |
+
## Properties of Random Variables
|
| 74 |
+
|
| 75 |
+
Each random variable has several key properties:
|
| 76 |
+
|
| 77 |
+
| Property | Description | Example |
|
| 78 |
+
|----------|-------------|---------|
|
| 79 |
+
| Meaning | Semantic description | Number of successes in n trials |
|
| 80 |
+
| Symbol | Notation used | $X$, $Y$, $Z$ |
|
| 81 |
+
| Support/Range | Possible values | $\{0,1,2,...,n\}$ for binomial |
|
| 82 |
+
| Distribution | PMF or PDF | $p_X(x)$ or $f_X(x)$ |
|
| 83 |
+
| Expectation | Weighted average | $E[X]$ |
|
| 84 |
+
| Variance | Measure of spread | $\text{Var}(X)$ |
|
| 85 |
+
| Standard Deviation | Square root of variance | $\sigma_X$ |
|
| 86 |
+
| Mode | Most likely value | argmax$_x$ $p_X(x)$ |
|
| 87 |
+
|
| 88 |
+
Additional properties include:
|
| 89 |
+
|
| 90 |
+
- [Entropy](https://en.wikipedia.org/wiki/Entropy_(information_theory)) (measure of uncertainty)
|
| 91 |
+
- [Median](https://en.wikipedia.org/wiki/Median) (middle value)
|
| 92 |
+
- [Skewness](https://en.wikipedia.org/wiki/Skewness) (asymmetry measure)
|
| 93 |
+
- [Kurtosis](https://en.wikipedia.org/wiki/Kurtosis) (tail heaviness measure)
|
| 94 |
+
"""
|
| 95 |
+
)
|
| 96 |
+
return
|
| 97 |
+
|
| 98 |
+
|
| 99 |
+
@app.cell(hide_code=True)
|
| 100 |
+
def _(mo):
|
| 101 |
+
mo.md(
|
| 102 |
+
r"""
|
| 103 |
+
## Probability Mass Functions (PMF)
|
| 104 |
+
|
| 105 |
+
For discrete random variables, the PMF $p_X(x)$ gives the probability that $X$ equals $x$:
|
| 106 |
+
|
| 107 |
+
$p_X(x) = P(X = x)$
|
| 108 |
+
|
| 109 |
+
Properties of a PMF:
|
| 110 |
+
|
| 111 |
+
1. $p_X(x) \geq 0$ for all $x$
|
| 112 |
+
2. $\sum_x p_X(x) = 1$
|
| 113 |
+
|
| 114 |
+
Let's implement a PMF for rolling a fair die:
|
| 115 |
+
"""
|
| 116 |
+
)
|
| 117 |
+
return
|
| 118 |
+
|
| 119 |
+
|
| 120 |
+
@app.cell
|
| 121 |
+
def _(np, plt):
|
| 122 |
+
def die_pmf(x):
|
| 123 |
+
if x in [1, 2, 3, 4, 5, 6]:
|
| 124 |
+
return 1 / 6
|
| 125 |
+
return 0
|
| 126 |
+
|
| 127 |
+
# Plot the PMF
|
| 128 |
+
_x = np.arange(1, 7)
|
| 129 |
+
probabilities = [die_pmf(i) for i in _x]
|
| 130 |
+
|
| 131 |
+
plt.figure(figsize=(8, 2))
|
| 132 |
+
plt.bar(_x, probabilities)
|
| 133 |
+
plt.title("PMF of Rolling a Fair Die")
|
| 134 |
+
plt.xlabel("Outcome")
|
| 135 |
+
plt.ylabel("Probability")
|
| 136 |
+
plt.grid(True, alpha=0.3)
|
| 137 |
+
plt.gca()
|
| 138 |
+
return die_pmf, probabilities
|
| 139 |
+
|
| 140 |
+
|
| 141 |
+
@app.cell(hide_code=True)
|
| 142 |
+
def _(mo):
|
| 143 |
+
mo.md(
|
| 144 |
+
r"""
|
| 145 |
+
## Probability Density Functions (PDF)
|
| 146 |
+
|
| 147 |
+
For continuous random variables, we use a PDF $f_X(x)$. The probability of $X$ falling in an interval $[a,b]$ is:
|
| 148 |
+
|
| 149 |
+
$P(a \leq X \leq b) = \int_a^b f_X(x)dx$
|
| 150 |
+
|
| 151 |
+
Properties of a PDF:
|
| 152 |
+
|
| 153 |
+
1. $f_X(x) \geq 0$ for all $x$
|
| 154 |
+
2. $\int_{-\infty}^{\infty} f_X(x)dx = 1$
|
| 155 |
+
|
| 156 |
+
Let's look at the normal distribution, a common continuous random variable:
|
| 157 |
+
"""
|
| 158 |
+
)
|
| 159 |
+
return
|
| 160 |
+
|
| 161 |
+
|
| 162 |
+
@app.cell
|
| 163 |
+
def _(np, plt, stats):
|
| 164 |
+
# Generate points for plotting
|
| 165 |
+
_x = np.linspace(-4, 4, 100)
|
| 166 |
+
_pdf = stats.norm.pdf(_x, loc=0, scale=1)
|
| 167 |
+
|
| 168 |
+
plt.figure(figsize=(8, 4))
|
| 169 |
+
plt.plot(_x, _pdf, "b-", label="PDF")
|
| 170 |
+
plt.fill_between(_x, _pdf, where=(_x >= -1) & (_x <= 1), alpha=0.3)
|
| 171 |
+
plt.title("Standard Normal Distribution")
|
| 172 |
+
plt.xlabel("x")
|
| 173 |
+
plt.ylabel("Density")
|
| 174 |
+
plt.grid(True, alpha=0.3)
|
| 175 |
+
plt.legend()
|
| 176 |
+
return
|
| 177 |
+
|
| 178 |
+
|
| 179 |
+
@app.cell(hide_code=True)
|
| 180 |
+
def _(mo):
|
| 181 |
+
mo.md(
|
| 182 |
+
r"""
|
| 183 |
+
## Expected Value
|
| 184 |
+
|
| 185 |
+
The expected value $E[X]$ is the long-run average of a random variable.
|
| 186 |
+
|
| 187 |
+
For discrete random variables:
|
| 188 |
+
$E[X] = \sum_x x \cdot p_X(x)$
|
| 189 |
+
|
| 190 |
+
For continuous random variables:
|
| 191 |
+
$E[X] = \int_{-\infty}^{\infty} x \cdot f_X(x)dx$
|
| 192 |
+
|
| 193 |
+
Properties:
|
| 194 |
+
|
| 195 |
+
1. $E[aX + b] = aE[X] + b$
|
| 196 |
+
2. $E[X + Y] = E[X] + E[Y]$
|
| 197 |
+
"""
|
| 198 |
+
)
|
| 199 |
+
return
|
| 200 |
+
|
| 201 |
+
|
| 202 |
+
@app.cell
|
| 203 |
+
def _(np):
|
| 204 |
+
def expected_value_discrete(x_values, probabilities):
|
| 205 |
+
return sum(x * p for x, p in zip(x_values, probabilities))
|
| 206 |
+
|
| 207 |
+
# Example: Expected value of a fair die roll
|
| 208 |
+
die_values = np.arange(1, 7)
|
| 209 |
+
die_probs = np.ones(6) / 6
|
| 210 |
+
|
| 211 |
+
E_X = expected_value_discrete(die_values, die_probs)
|
| 212 |
+
return E_X, die_probs, die_values, expected_value_discrete
|
| 213 |
+
|
| 214 |
+
|
| 215 |
+
@app.cell
|
| 216 |
+
def _(E_X):
|
| 217 |
+
E_X
|
| 218 |
+
return
|
| 219 |
+
|
| 220 |
+
|
| 221 |
+
@app.cell(hide_code=True)
|
| 222 |
+
def _(mo):
|
| 223 |
+
mo.md(
|
| 224 |
+
r"""
|
| 225 |
+
## Variance
|
| 226 |
+
|
| 227 |
+
The variance $\text{Var}(X)$ measures the spread of a random variable around its mean:
|
| 228 |
+
|
| 229 |
+
$\text{Var}(X) = E[(X - E[X])^2]$
|
| 230 |
+
|
| 231 |
+
This can be computed as:
|
| 232 |
+
$\text{Var}(X) = E[X^2] - (E[X])^2$
|
| 233 |
+
|
| 234 |
+
Properties:
|
| 235 |
+
|
| 236 |
+
1. $\text{Var}(aX) = a^2Var(X)$
|
| 237 |
+
2. $\text{Var}(X + b) = Var(X)$
|
| 238 |
+
"""
|
| 239 |
+
)
|
| 240 |
+
return
|
| 241 |
+
|
| 242 |
+
|
| 243 |
+
@app.cell
|
| 244 |
+
def _(E_X, die_probs, die_values, np):
|
| 245 |
+
def variance_discrete(x_values, probabilities, expected_value):
|
| 246 |
+
squared_diff = [(x - expected_value) ** 2 for x in x_values]
|
| 247 |
+
return sum(d * p for d, p in zip(squared_diff, probabilities))
|
| 248 |
+
|
| 249 |
+
# Example: Variance of a fair die roll
|
| 250 |
+
var_X = variance_discrete(die_values, die_probs, E_X)
|
| 251 |
+
std_X = np.sqrt(var_X)
|
| 252 |
+
return std_X, var_X, variance_discrete
|
| 253 |
+
|
| 254 |
+
|
| 255 |
+
@app.cell(hide_code=True)
|
| 256 |
+
def _(mo, std_X, var_X):
|
| 257 |
+
mo.md(
|
| 258 |
+
f"""
|
| 259 |
+
### Examples of Variance Calculation
|
| 260 |
+
|
| 261 |
+
For our fair die example:
|
| 262 |
+
|
| 263 |
+
- Variance: {var_X:.2f}
|
| 264 |
+
- Standard Deviation: {std_X:.2f}
|
| 265 |
+
|
| 266 |
+
This means that on average, a roll deviates from the mean (3.5) by about {std_X:.2f} units.
|
| 267 |
+
|
| 268 |
+
Let's look another example for a fair coin:
|
| 269 |
+
"""
|
| 270 |
+
)
|
| 271 |
+
return
|
| 272 |
+
|
| 273 |
+
|
| 274 |
+
@app.cell
|
| 275 |
+
def _(variance_discrete):
|
| 276 |
+
# Fair coin (X = 0 or 1)
|
| 277 |
+
coin_values = [0, 1]
|
| 278 |
+
coin_probs = [0.5, 0.5]
|
| 279 |
+
coin_mean = sum(x * p for x, p in zip(coin_values, coin_probs))
|
| 280 |
+
coin_var = variance_discrete(coin_values, coin_probs, coin_mean)
|
| 281 |
+
return coin_mean, coin_probs, coin_values, coin_var
|
| 282 |
+
|
| 283 |
+
|
| 284 |
+
@app.cell
|
| 285 |
+
def _(np, stats, variance_discrete):
|
| 286 |
+
# Standard normal (discretized for example)
|
| 287 |
+
normal_values = np.linspace(-3, 3, 100)
|
| 288 |
+
normal_probs = stats.norm.pdf(normal_values)
|
| 289 |
+
normal_probs = normal_probs / sum(normal_probs) # normalize
|
| 290 |
+
normal_mean = 0
|
| 291 |
+
normal_var = variance_discrete(normal_values, normal_probs, normal_mean)
|
| 292 |
+
return normal_mean, normal_probs, normal_values, normal_var
|
| 293 |
+
|
| 294 |
+
|
| 295 |
+
@app.cell
|
| 296 |
+
def _(np, variance_discrete):
|
| 297 |
+
# Uniform on [0,1] (discretized for example)
|
| 298 |
+
uniform_values = np.linspace(0, 1, 100)
|
| 299 |
+
uniform_probs = np.ones_like(uniform_values) / len(uniform_values)
|
| 300 |
+
uniform_mean = 0.5
|
| 301 |
+
uniform_var = variance_discrete(uniform_values, uniform_probs, uniform_mean)
|
| 302 |
+
return uniform_mean, uniform_probs, uniform_values, uniform_var
|
| 303 |
+
|
| 304 |
+
|
| 305 |
+
@app.cell(hide_code=True)
|
| 306 |
+
def _(coin_var, mo, normal_var, uniform_var):
|
| 307 |
+
mo.md(
|
| 308 |
+
rf"""
|
| 309 |
+
Let's look at some calculated variances:
|
| 310 |
+
|
| 311 |
+
- Fair coin (X = 0 or 1): $\text{{Var}}(X) = {coin_var:.4f}$
|
| 312 |
+
- Standard normal distribution (discretized): $\text{{Var(X)}} ≈ {normal_var:.4f}$
|
| 313 |
+
- Uniform distribution on $[0,1]$ (discretized): $\text{{Var(X)}} ≈ {uniform_var:.4f}$
|
| 314 |
+
"""
|
| 315 |
+
)
|
| 316 |
+
return
|
| 317 |
+
|
| 318 |
+
|
| 319 |
+
@app.cell(hide_code=True)
|
| 320 |
+
def _(mo):
|
| 321 |
+
mo.md(
|
| 322 |
+
r"""
|
| 323 |
+
## Common Distributions
|
| 324 |
+
|
| 325 |
+
1. Bernoulli Distribution
|
| 326 |
+
- Models a single success/failure experiment
|
| 327 |
+
- $P(X = 1) = p$, $P(X = 0) = 1-p$
|
| 328 |
+
- $E[X] = p$, $\text{Var}(X) = p(1-p)$
|
| 329 |
+
|
| 330 |
+
2. Binomial Distribution
|
| 331 |
+
|
| 332 |
+
- Models number of successes in $n$ independent trials
|
| 333 |
+
- $P(X = k) = \binom{n}{k}p^k(1-p)^{n-k}$
|
| 334 |
+
- $E[X] = np$, $\text{Var}(X) = np(1-p)$
|
| 335 |
+
|
| 336 |
+
3. Normal Distribution
|
| 337 |
+
|
| 338 |
+
- Bell-shaped curve defined by mean $\mu$ and variance $\sigma^2$
|
| 339 |
+
- PDF: $f_X(x) = \frac{1}{\sigma\sqrt{2\pi}}e^{-\frac{(x-\mu)^2}{2\sigma^2}}$
|
| 340 |
+
- $E[X] = \mu$, $\text{Var}(X) = \sigma^2$
|
| 341 |
+
"""
|
| 342 |
+
)
|
| 343 |
+
return
|
| 344 |
+
|
| 345 |
+
|
| 346 |
+
@app.cell(hide_code=True)
|
| 347 |
+
def _(mo):
|
| 348 |
+
mo.md(
|
| 349 |
+
r"""
|
| 350 |
+
### Example: Comparing Discrete and Continuous Distributions
|
| 351 |
+
|
| 352 |
+
This example shows the relationship between a Binomial distribution (discrete) and its Normal approximation (continuous).
|
| 353 |
+
The parameters control both distributions:
|
| 354 |
+
|
| 355 |
+
- **Number of Trials**: Controls the range of possible values and the shape's width
|
| 356 |
+
- **Success Probability**: Affects the distribution's center and skewness
|
| 357 |
+
"""
|
| 358 |
+
)
|
| 359 |
+
return
|
| 360 |
+
|
| 361 |
+
|
| 362 |
+
@app.cell
|
| 363 |
+
def _(mo, n_trials, p_success):
|
| 364 |
+
mo.hstack([n_trials, p_success], justify="space-around")
|
| 365 |
+
return
|
| 366 |
+
|
| 367 |
+
|
| 368 |
+
@app.cell(hide_code=True)
|
| 369 |
+
def _(mo):
|
| 370 |
+
# Distribution parameters
|
| 371 |
+
n_trials = mo.ui.slider(1, 20, value=10, label="Number of Trials")
|
| 372 |
+
p_success = mo.ui.slider(0, 1, value=0.5, step=0.05, label="Success Probability")
|
| 373 |
+
return n_trials, p_success
|
| 374 |
+
|
| 375 |
+
|
| 376 |
+
@app.cell(hide_code=True)
|
| 377 |
+
def _(n_trials, np, p_success, plt, stats):
|
| 378 |
+
fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(12, 3))
|
| 379 |
+
|
| 380 |
+
# Discrete: Binomial PMF
|
| 381 |
+
k = np.arange(0, n_trials.value + 1)
|
| 382 |
+
pmf = stats.binom.pmf(k, n_trials.value, p_success.value)
|
| 383 |
+
ax1.bar(k, pmf, alpha=0.8, color="#1f77b4", label="PMF")
|
| 384 |
+
ax1.set_title(f"Binomial PMF (n={n_trials.value}, p={p_success.value})")
|
| 385 |
+
ax1.set_xlabel("Number of Successes")
|
| 386 |
+
ax1.set_ylabel("Probability")
|
| 387 |
+
ax1.grid(True, alpha=0.3)
|
| 388 |
+
|
| 389 |
+
# Continuous: Normal PDF approx.
|
| 390 |
+
mu = n_trials.value * p_success.value
|
| 391 |
+
sigma = np.sqrt(n_trials.value * p_success.value * (1 - p_success.value))
|
| 392 |
+
x = np.linspace(max(0, mu - 4 * sigma), min(n_trials.value, mu + 4 * sigma), 100)
|
| 393 |
+
pdf = stats.norm.pdf(x, mu, sigma)
|
| 394 |
+
|
| 395 |
+
ax2.plot(x, pdf, "r-", linewidth=2, label="PDF")
|
| 396 |
+
ax2.fill_between(x, pdf, alpha=0.3, color="red")
|
| 397 |
+
ax2.set_title(f"Normal PDF (μ={mu:.1f}, σ={sigma:.1f})")
|
| 398 |
+
ax2.set_xlabel("Continuous Approximation")
|
| 399 |
+
ax2.set_ylabel("Density")
|
| 400 |
+
ax2.grid(True, alpha=0.3)
|
| 401 |
+
|
| 402 |
+
# Set consistent x-axis limits for better comparison
|
| 403 |
+
ax1.set_xlim(-0.5, n_trials.value + 0.5)
|
| 404 |
+
ax2.set_xlim(-0.5, n_trials.value + 0.5)
|
| 405 |
+
|
| 406 |
+
plt.tight_layout()
|
| 407 |
+
plt.gca()
|
| 408 |
+
return ax1, ax2, fig, k, mu, pdf, pmf, sigma, x
|
| 409 |
+
|
| 410 |
+
|
| 411 |
+
@app.cell(hide_code=True)
|
| 412 |
+
def _(mo, n_trials, np, p_success):
|
| 413 |
+
mo.md(f"""
|
| 414 |
+
**Current Distribution Properties:**
|
| 415 |
+
|
| 416 |
+
- Mean (μ) = {n_trials.value * p_success.value:.2f}
|
| 417 |
+
- Standard Deviation (σ) = {np.sqrt(n_trials.value * p_success.value * (1 - p_success.value)):.2f}
|
| 418 |
+
|
| 419 |
+
Notice how the Normal distribution (right) approximates the Binomial distribution (left) better when:
|
| 420 |
+
|
| 421 |
+
1. The number of trials is larger
|
| 422 |
+
2. The success probability is closer to 0.5
|
| 423 |
+
""")
|
| 424 |
+
return
|
| 425 |
+
|
| 426 |
+
|
| 427 |
+
@app.cell(hide_code=True)
|
| 428 |
+
def _(mo):
|
| 429 |
+
mo.md(
|
| 430 |
+
r"""
|
| 431 |
+
## Practice Problems
|
| 432 |
+
|
| 433 |
+
### Problem 1: Discrete Random Variable
|
| 434 |
+
Let $X$ be the sum when rolling two fair dice. Find:
|
| 435 |
+
|
| 436 |
+
1. The support of $X$
|
| 437 |
+
2. The PMF $p_X(x)$
|
| 438 |
+
3. $E[X]$ and $\text{Var}(X)$
|
| 439 |
+
|
| 440 |
+
<details>
|
| 441 |
+
<summary>Solution</summary>
|
| 442 |
+
Let's solve this step by step:
|
| 443 |
+
```python
|
| 444 |
+
def two_dice_pmf(x):
|
| 445 |
+
outcomes = [(i,j) for i in range(1,7) for j in range(1,7)]
|
| 446 |
+
favorable = [pair for pair in outcomes if sum(pair) == x]
|
| 447 |
+
return len(favorable)/36
|
| 448 |
+
|
| 449 |
+
# Support: {2,3,...,12}
|
| 450 |
+
# E[X] = 7
|
| 451 |
+
# Var(X) = 5.83
|
| 452 |
+
```
|
| 453 |
+
</details>
|
| 454 |
+
|
| 455 |
+
### Problem 2: Continuous Random Variable
|
| 456 |
+
For a uniform random variable on $[0,1]$, verify that:
|
| 457 |
+
|
| 458 |
+
1. The PDF integrates to 1
|
| 459 |
+
2. $E[X] = 1/2$
|
| 460 |
+
3. $\text{Var}(X) = 1/12$
|
| 461 |
+
|
| 462 |
+
Try solving this yourself first, then check the solution below.
|
| 463 |
+
"""
|
| 464 |
+
)
|
| 465 |
+
return
|
| 466 |
+
|
| 467 |
+
|
| 468 |
+
@app.cell
|
| 469 |
+
def _():
|
| 470 |
+
# DIY
|
| 471 |
+
return
|
| 472 |
+
|
| 473 |
+
|
| 474 |
+
@app.cell(hide_code=True)
|
| 475 |
+
def _(mktext, mo):
|
| 476 |
+
mo.accordion({"Solution": mktext}, lazy=True)
|
| 477 |
+
return
|
| 478 |
+
|
| 479 |
+
|
| 480 |
+
@app.cell(hide_code=True)
|
| 481 |
+
def _(mo):
|
| 482 |
+
mktext = mo.md(
|
| 483 |
+
r"""
|
| 484 |
+
Let's solve each part:
|
| 485 |
+
|
| 486 |
+
1. **PDF integrates to 1**:
|
| 487 |
+
$\int_0^1 1 \, dx = [x]_0^1 = 1 - 0 = 1$
|
| 488 |
+
|
| 489 |
+
2. **Expected Value**:
|
| 490 |
+
$E[X] = \int_0^1 x \cdot 1 \, dx = [\frac{x^2}{2}]_0^1 = \frac{1}{2} - 0 = \frac{1}{2}$
|
| 491 |
+
|
| 492 |
+
3. **Variance**:
|
| 493 |
+
$\text{Var}(X) = E[X^2] - (E[X])^2$
|
| 494 |
+
|
| 495 |
+
First calculate $E[X^2]$:
|
| 496 |
+
$E[X^2] = \int_0^1 x^2 \cdot 1 \, dx = [\frac{x^3}{3}]_0^1 = \frac{1}{3}$
|
| 497 |
+
|
| 498 |
+
Then:
|
| 499 |
+
$\text{Var}(X) = \frac{1}{3} - (\frac{1}{2})^2 = \frac{1}{3} - \frac{1}{4} = \frac{1}{12}$
|
| 500 |
+
"""
|
| 501 |
+
)
|
| 502 |
+
return (mktext,)
|
| 503 |
+
|
| 504 |
+
|
| 505 |
+
@app.cell(hide_code=True)
|
| 506 |
+
def _(mo):
|
| 507 |
+
mo.md(
|
| 508 |
+
r"""
|
| 509 |
+
## 🤔 Test Your Understanding
|
| 510 |
+
|
| 511 |
+
Pick which of these statements about random variables you think are correct:
|
| 512 |
+
|
| 513 |
+
<details>
|
| 514 |
+
<summary>The probability density function can be greater than 1</summary>
|
| 515 |
+
✅ Correct! Unlike PMFs, PDFs can exceed 1 as long as the total area equals 1.
|
| 516 |
+
</details>
|
| 517 |
+
|
| 518 |
+
<details>
|
| 519 |
+
<summary>The expected value of a random variable must equal one of its possible values</summary>
|
| 520 |
+
❌ Incorrect! For example, the expected value of a fair die is 3.5, which is not a possible outcome.
|
| 521 |
+
</details>
|
| 522 |
+
|
| 523 |
+
<details>
|
| 524 |
+
<summary>Adding a constant to a random variable changes its variance</summary>
|
| 525 |
+
❌ Incorrect! Adding a constant shifts the distribution but doesn't affect its spread.
|
| 526 |
+
</details>
|
| 527 |
+
"""
|
| 528 |
+
)
|
| 529 |
+
return
|
| 530 |
+
|
| 531 |
+
|
| 532 |
+
@app.cell(hide_code=True)
|
| 533 |
+
def _(mo):
|
| 534 |
+
mo.md(
|
| 535 |
+
"""
|
| 536 |
+
## Summary
|
| 537 |
+
|
| 538 |
+
You've learned:
|
| 539 |
+
|
| 540 |
+
- The difference between discrete and continuous random variables
|
| 541 |
+
- How PMFs and PDFs describe probability distributions
|
| 542 |
+
- Methods for calculating expected values and variances
|
| 543 |
+
- Properties of common probability distributions
|
| 544 |
+
|
| 545 |
+
In the next lesson, we'll explore Probability Mass Functions in detail, focusing on their properties and applications.
|
| 546 |
+
"""
|
| 547 |
+
)
|
| 548 |
+
return
|
| 549 |
+
|
| 550 |
+
|
| 551 |
+
if __name__ == "__main__":
|
| 552 |
+
app.run()
|