⚙️ Setup¶
import numpy as np
from scipy import stats
rng = np.random.default_rng(371)
print("ready")
ready
n, p = 5, 1/6
P3 = stats.binom.pmf(3, n, p)
print(f"P(exactly 3 sixes) = C(5,3)(1/6)^3(5/6)^2 = {P3:.4f}")
print(f"expected sixes = np = {n*p:.4f}")
P(exactly 3 sixes) = C(5,3)(1/6)^3(5/6)^2 = 0.0322 expected sixes = np = 0.8333
Answer: P(exactly 3 sixes) = C(5,3)(1/6)³(5/6)² ≈ 0.0322, about 3%. The expected number of sixes is np = 5/6 ≈ 0.83. Three sixes in five rolls is well above average, hence uncommon.
p = 0.25
print(f"E[attempts] = 1/p = {1/p:.1f}")
P3 = stats.geom.pmf(3, p)
print(f"P(first success on attempt 3) = (0.75)^2 (0.25) = {P3:.4f}")
E[attempts] = 1/p = 4.0 P(first success on attempt 3) = (0.75)^2 (0.25) = 0.1406
Answer: the expected wait is 1/p = 4 attempts. P(first success on attempt 3) = (1−p)² p = (0.75)²(0.25) ≈ 0.1406: you must fail twice, then succeed.
lam = 4
print(f"P(X=2) = e^-4 * 4^2 / 2! = {stats.poisson.pmf(2, lam):.4f}")
print(f"P(X=0) = e^-4 = {stats.poisson.pmf(0, lam):.4f}")
P(X=2) = e^-4 * 4^2 / 2! = 0.1465 P(X=0) = e^-4 = 0.0183
Answer: P(exactly 2) = e⁻⁴(4²/2!) ≈ 0.1465, and P(no calls) = e⁻⁴ ≈ 0.0183. With a mean of 4, both 2 calls and 0 calls are below average, so 0 is quite unlikely.
M, K, N = 50, 5, 10
P1 = stats.hypergeom.pmf(1, M, K, N)
print(f"P(exactly 1 defective) = {P1:.4f}")
print(f"expected defectives = N*K/M = {N*K/M:.2f}")
P(exactly 1 defective) = 0.4313 expected defectives = N*K/M = 1.00
Answer: P(exactly 1 defective) ≈ 0.431. The expected number of defectives is N·K/M = 10(5/50) = 1.0. Because the draws are without replacement, this is hypergeometric, not binomial, though with such a small sample the two are close.
k, n = 8, 20
p_hat = k/n
print(f"MLE p_hat = k/n = {p_hat:.2f}")
# confirm: log-likelihood is maximized at the sample proportion
grid = np.linspace(0.01,0.99,400)
loglik = k*np.log(grid)+(n-k)*np.log(1-grid)
print(f"argmax of log-likelihood on a grid = {grid[np.argmax(loglik)]:.2f}")
MLE p_hat = k/n = 0.40 argmax of log-likelihood on a grid = 0.40
Answer: the MLE is p̂ = k/n = 8/20 = 0.40. For Bernoulli/binomial data the maximum-likelihood estimate of p is simply the observed proportion of successes, which is exactly the quantity a logistic-regression classifier is trained to predict.