Spaces:
Sleeping
Sleeping
small edits
Browse files
probability/09_random_variables.py
CHANGED
|
@@ -10,7 +10,7 @@
|
|
| 10 |
|
| 11 |
import marimo
|
| 12 |
|
| 13 |
-
__generated_with = "0.11.
|
| 14 |
app = marimo.App(width="medium", app_title="Random Variables")
|
| 15 |
|
| 16 |
|
|
@@ -72,7 +72,7 @@ def _(mo):
|
|
| 72 |
r"""
|
| 73 |
## Properties of Random Variables
|
| 74 |
|
| 75 |
-
Each random variable has several key properties
|
| 76 |
|
| 77 |
| Property | Description | Example |
|
| 78 |
|----------|-------------|---------|
|
|
@@ -81,7 +81,7 @@ def _(mo):
|
|
| 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 |
|
| 85 |
| Standard Deviation | Square root of variance | $\sigma_X$ |
|
| 86 |
| Mode | Most likely value | argmax$_x$ $p_X(x)$ |
|
| 87 |
|
|
@@ -121,14 +121,14 @@ def _(mo):
|
|
| 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,
|
| 132 |
plt.bar(_x, probabilities)
|
| 133 |
plt.title("PMF of Rolling a Fair Die")
|
| 134 |
plt.xlabel("Outcome")
|
|
@@ -166,7 +166,7 @@ def _(np, plt, stats):
|
|
| 166 |
_pdf = stats.norm.pdf(_x, loc=0, scale=1)
|
| 167 |
|
| 168 |
plt.figure(figsize=(8, 4))
|
| 169 |
-
plt.plot(_x, _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")
|
|
@@ -214,7 +214,7 @@ def _(np):
|
|
| 214 |
|
| 215 |
@app.cell
|
| 216 |
def _(E_X):
|
| 217 |
-
|
| 218 |
return
|
| 219 |
|
| 220 |
|
|
@@ -224,17 +224,17 @@ def _(mo):
|
|
| 224 |
r"""
|
| 225 |
## Variance
|
| 226 |
|
| 227 |
-
The variance
|
| 228 |
|
| 229 |
-
|
| 230 |
|
| 231 |
This can be computed as:
|
| 232 |
-
|
| 233 |
|
| 234 |
Properties:
|
| 235 |
|
| 236 |
-
1.
|
| 237 |
-
2.
|
| 238 |
"""
|
| 239 |
)
|
| 240 |
return
|
|
@@ -243,7 +243,7 @@ def _(mo):
|
|
| 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
|
|
@@ -305,12 +305,39 @@ def _(np, variance_discrete):
|
|
| 305 |
@app.cell(hide_code=True)
|
| 306 |
def _(coin_var, mo, normal_var, uniform_var):
|
| 307 |
mo.md(
|
| 308 |
-
|
| 309 |
Let's look at some calculated variances:
|
| 310 |
|
| 311 |
-
- Fair coin (X = 0 or 1): Var(X) = {coin_var:.4f}
|
| 312 |
-
- Standard normal distribution (discretized): Var(X) ≈ {normal_var:.4f}
|
| 313 |
-
- Uniform distribution on [0,1] (discretized): Var(X) ≈ {uniform_var:.4f}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 314 |
"""
|
| 315 |
)
|
| 316 |
return
|
|
@@ -320,7 +347,7 @@ def _(coin_var, mo, normal_var, uniform_var):
|
|
| 320 |
def _(mo):
|
| 321 |
mo.md(
|
| 322 |
r"""
|
| 323 |
-
|
| 324 |
|
| 325 |
This example shows the relationship between a Binomial distribution (discrete) and its Normal approximation (continuous).
|
| 326 |
The parameters control both distributions:
|
|
@@ -334,7 +361,7 @@ def _(mo):
|
|
| 334 |
|
| 335 |
@app.cell
|
| 336 |
def _(mo, n_trials, p_success):
|
| 337 |
-
mo.hstack([n_trials, p_success], justify=
|
| 338 |
return
|
| 339 |
|
| 340 |
|
|
@@ -348,28 +375,28 @@ def _(mo):
|
|
| 348 |
|
| 349 |
@app.cell(hide_code=True)
|
| 350 |
def _(n_trials, np, p_success, plt, stats):
|
| 351 |
-
fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(12,
|
| 352 |
|
| 353 |
# Discrete: Binomial PMF
|
| 354 |
k = np.arange(0, n_trials.value + 1)
|
| 355 |
pmf = stats.binom.pmf(k, n_trials.value, p_success.value)
|
| 356 |
-
ax1.bar(k, pmf, alpha=0.8, color=
|
| 357 |
-
ax1.set_title(f
|
| 358 |
-
ax1.set_xlabel(
|
| 359 |
-
ax1.set_ylabel(
|
| 360 |
ax1.grid(True, alpha=0.3)
|
| 361 |
|
| 362 |
# Continuous: Normal PDF approx.
|
| 363 |
mu = n_trials.value * p_success.value
|
| 364 |
-
sigma = np.sqrt(n_trials.value * p_success.value * (1-p_success.value))
|
| 365 |
-
x = np.linspace(max(0, mu - 4*sigma), min(n_trials.value, mu + 4*sigma), 100)
|
| 366 |
pdf = stats.norm.pdf(x, mu, sigma)
|
| 367 |
|
| 368 |
-
ax2.plot(x, pdf,
|
| 369 |
-
ax2.fill_between(x, pdf, alpha=0.3, color=
|
| 370 |
-
ax2.set_title(f
|
| 371 |
-
ax2.set_xlabel(
|
| 372 |
-
ax2.set_ylabel(
|
| 373 |
ax2.grid(True, alpha=0.3)
|
| 374 |
|
| 375 |
# Set consistent x-axis limits for better comparison
|
|
@@ -387,7 +414,7 @@ def _(mo, n_trials, np, p_success):
|
|
| 387 |
**Current Distribution Properties:**
|
| 388 |
|
| 389 |
- Mean (μ) = {n_trials.value * p_success.value:.2f}
|
| 390 |
-
- Standard Deviation (σ) = {np.sqrt(n_trials.value * p_success.value * (1-p_success.value)):.2f}
|
| 391 |
|
| 392 |
Notice how the Normal distribution (right) approximates the Binomial distribution (left) better when:
|
| 393 |
|
|
@@ -397,33 +424,6 @@ def _(mo, n_trials, np, p_success):
|
|
| 397 |
return
|
| 398 |
|
| 399 |
|
| 400 |
-
@app.cell(hide_code=True)
|
| 401 |
-
def _(mo):
|
| 402 |
-
mo.md(
|
| 403 |
-
r"""
|
| 404 |
-
## Common Distributions
|
| 405 |
-
|
| 406 |
-
1. Bernoulli Distribution
|
| 407 |
-
- Models a single success/failure experiment
|
| 408 |
-
- $P(X = 1) = p$, $P(X = 0) = 1-p$
|
| 409 |
-
- $E[X] = p$, $Var(X) = p(1-p)$
|
| 410 |
-
|
| 411 |
-
2. Binomial Distribution
|
| 412 |
-
|
| 413 |
-
- Models number of successes in $n$ independent trials
|
| 414 |
-
- $P(X = k) = \binom{n}{k}p^k(1-p)^{n-k}$
|
| 415 |
-
- $E[X] = np$, $Var(X) = np(1-p)$
|
| 416 |
-
|
| 417 |
-
3. Normal Distribution
|
| 418 |
-
|
| 419 |
-
- Bell-shaped curve defined by mean $\mu$ and variance $\sigma^2$
|
| 420 |
-
- PDF: $f_X(x) = \frac{1}{\sigma\sqrt{2\pi}}e^{-\frac{(x-\mu)^2}{2\sigma^2}}$
|
| 421 |
-
- $E[X] = \mu$, $Var(X) = \sigma^2$
|
| 422 |
-
"""
|
| 423 |
-
)
|
| 424 |
-
return
|
| 425 |
-
|
| 426 |
-
|
| 427 |
@app.cell(hide_code=True)
|
| 428 |
def _(mo):
|
| 429 |
mo.md(
|
|
@@ -435,7 +435,7 @@ def _(mo):
|
|
| 435 |
|
| 436 |
1. The support of $X$
|
| 437 |
2. The PMF $p_X(x)$
|
| 438 |
-
3. $E[X]$ and
|
| 439 |
|
| 440 |
<details>
|
| 441 |
<summary>Solution</summary>
|
|
@@ -457,7 +457,7 @@ def _(mo):
|
|
| 457 |
|
| 458 |
1. The PDF integrates to 1
|
| 459 |
2. $E[X] = 1/2$
|
| 460 |
-
3.
|
| 461 |
|
| 462 |
Try solving this yourself first, then check the solution below.
|
| 463 |
"""
|
|
@@ -490,13 +490,13 @@ def _(mo):
|
|
| 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 |
-
|
| 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 |
-
|
| 500 |
"""
|
| 501 |
)
|
| 502 |
return (mktext,)
|
|
|
|
| 10 |
|
| 11 |
import marimo
|
| 12 |
|
| 13 |
+
__generated_with = "0.11.10"
|
| 14 |
app = marimo.App(width="medium", app_title="Random Variables")
|
| 15 |
|
| 16 |
|
|
|
|
| 72 |
r"""
|
| 73 |
## Properties of Random Variables
|
| 74 |
|
| 75 |
+
Each random variable has several key properties:
|
| 76 |
|
| 77 |
| Property | Description | Example |
|
| 78 |
|----------|-------------|---------|
|
|
|
|
| 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 |
|
|
|
|
| 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")
|
|
|
|
| 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")
|
|
|
|
| 214 |
|
| 215 |
@app.cell
|
| 216 |
def _(E_X):
|
| 217 |
+
E_X
|
| 218 |
return
|
| 219 |
|
| 220 |
|
|
|
|
| 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
|
|
|
|
| 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
|
|
|
|
| 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
|
|
|
|
| 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:
|
|
|
|
| 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 |
|
|
|
|
| 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
|
|
|
|
| 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 |
|
|
|
|
| 424 |
return
|
| 425 |
|
| 426 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 427 |
@app.cell(hide_code=True)
|
| 428 |
def _(mo):
|
| 429 |
mo.md(
|
|
|
|
| 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>
|
|
|
|
| 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 |
"""
|
|
|
|
| 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,)
|