You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
29 KiB
29 KiB
None
<html lang="en">
<head>
</head>
</html>
In [25]:
# Week 3 — Probability Basics (Navidi Ch. 2) — Jupyter/Python Lab
# --------------------------------------------------------------
# Suggested run order: top to bottom.
# This notebook focuses on: sample spaces, events, relative frequency,
# conditional probability, independence, Bayes (via simulation), and Monte Carlo.
import numpy as np
import math
import matplotlib.pyplot as plt
rng = np.random.default_rng(12345)
def relfreq(successes, trials):
return successes / trials
def simulate_repeated(trials, sim_fn):
"""Run sim_fn() 'trials' times; sim_fn returns True/False for success."""
x = np.fromiter((sim_fn() for _ in range(trials)), dtype=bool, count=trials)
return x.mean()
def running_estimate(trials, sim_fn):
"""Return running estimate of P(success) over time."""
x = np.fromiter((sim_fn() for _ in range(trials)), dtype=np.int8, count=trials)
return np.cumsum(x) / np.arange(1, trials + 1)
def plot_running(est, p_true=None, title="Running estimate"):
plt.figure()
plt.plot(est, '.k')
if p_true is not None:
plt.axhline(p_true)
plt.xlabel("n")
plt.ylabel("estimate")
plt.title(title)
plt.show()
def bernoulli(p, rng=rng):
return rng.random() < p
In [43]:
# Cell 1 — Basic: coin flips as Bernoulli trials
p = 0.5
N = 500
est = running_estimate(N, lambda: bernoulli(p))
plot_running(est, p_true=p, title="Coin: running estimate of P(Heads)=0.5")
print("Final estimate:", est[-1])
In [3]:
# Cell 2 — Law of Total Probability via simulation (urn example)
# Urn A chosen with probability 0.3; Urn B with probability 0.7
# Urn A: 70% red; Urn B: 20% red
pA = 0.3
p_red_A = 0.7
p_red_B = 0.2
N = 200_000
def draw_red():
# choose urn then draw color
choose_A = bernoulli(pA)
if choose_A:
return bernoulli(p_red_A)
else:
return bernoulli(p_red_B)
p_hat = simulate_repeated(N, draw_red)
p_exact = pA * p_red_A + (1 - pA) * p_red_B
print("Simulated P(Red):", p_hat)
print("Exact P(Red): ", p_exact)
In [12]:
trials = 10
sim_fn = lambda: rng.random() < 0.5
x = np.fromiter((sim_fn() for _ in range(trials)), dtype=np.int8, count=trials)
# x might be: array([1, 0, 1, 1, 0], dtype=int8)
x
Out[12]:
In [18]:
x = np.fromiter((sim_fn() for _ in range(trials)), dtype=np.int8, count=trials)
x
Out[18]: