Probability and Statistics for ML
Distributions, Bayes, tests, bootstrap, sampling from logits and Markov chains. From-scratch Python and SciPy.
- Concepts and terms
- Tasks and commands
- Annotated code
- Diagrams
- Defaults and numbers
- Troubleshooting
- Questions and tips
Logits to a token#
Bayesian A/B test#
Tasks in code#
| Task | Code |
|---|---|
| Posterior from one test | bayes(prior= |
| Repeat the test | sequential_bayes(prior, |
| Spam filter with add-one smoothing | NaiveBayes(smoothing=1.0), then classifier.train(train_docs, train_labels) |
| Loss from logits | cross_entropy_loss(logits, |
| Bootstrap confidence interval | bootstrap_statistic(data, |
| Welch t-test, paired t-test | two_sample_ttest(data1, data2), paired_ttest(data1, data2) |
| Effect size | cohens_d(data1, data2), then interpret_cohens_d(d) |
| Correct for many tests | bonferroni_correction(p_values, |
From Lesson 01.07, 01.07 code/bayes.py, 01.15 code/statistics.py, 01.06 code/probability.py
Sample from scratch#
x = sample_exponential_inverse_cdf(lam)-log(u) / lamx = rejection_sample(target_pdf, proposal_sample, proposal_pdf, M)accept rate 1/Mest = importance_sampling_estimate(f, target_pdf, proposal_pdf, proposal_sample, n)idx = temperature_sample(logits, temperature)z / T, then softmaxidx = top_k_sample(logits, k)keep the k largest logitsidx = top_p_sample(logits, p)stop when cumsum >= pz = reparam_sample(mu, sigma)mu + sigma * epsilony = gumbel_softmax_sample(log_probs, temperature)soft one-hotLibrary calls#
normal = stats.norm(loc=0, scale=1)normal.cdf(1.96)P(X < 1.96)normal.ppf(0.975)inverse CDFsamples = normal.rvs(size=10000)probs = softmax(logits)from scipy.specialrng = np.random.default_rng(42)token = rng.choice(len(logits), p=probs)distribution = distribution @ Ppower method, repeat 100 timeseigenvalues = np.linalg.eigvals(P)spectral_gap = 1 - sorted(np.abs(eigenvalues))[-2]mixing time near 1/gapFrom Lesson 01.06, Lesson 01.16, Lesson 01.22
Key formulas#
- Bayes
- P(A \mid B) = \frac{P(B \mid A) \cdot P(A)}{P(B)}. P(B) is the normalizer.
- Variance
- \mathrm{Var}(X) = \mathbb{E}[X^2] - (\mathbb{E}[X])^2. Sample variance divides by n-1.
- Softmax with T
- p_i = \exp(z_i / T) / \sum_j \exp(z_j / T).
- Laplace smoothing
- add 1 to each count and the vocabulary size to the total.
- Beta update
- prior Beta(a, b) with s successes and f failures gives Beta(a + s, b + f).
- Importance weight
- w(x) = p(x) / q(x). Average f(x_i) \, w(x_i) over x_i \sim q.
- Metropolis-Hastings
- accept with \min(1, a). A symmetric proposal gives a = p(x') / p(x).
- Langevin
- x_{t+1} = x_t - dt \, \nabla U(x_t) + \sqrt{2 T \, dt} \, z_t.
From Lesson 01.06, Lesson 01.07, Lesson 01.15, Lesson 01.16, Lesson 01.22
Distributions#
| Command | What it does |
|---|---|
| Bernoulli | Mean p, variance p(1-p). Binary classification |
| Categorical | Probabilities sum to 1. Softmax output |
| Poisson | Mean and variance both lambda. Event counts |
| Uniform(0, 1) | E[U] = 0.5, Var(U) = 1/12. Start of every sampler |
| Normal | 68%, 95%, 99.7% within 1, 2, 3 sigma |
| Beta(a, b) | Mean a/(a+b). Conjugate prior for Bernoulli |
| Exponential | Inverse CDF gives x = -ln(u) / lambda |
| Brownian motion | B(t) is normal with mean 0 and variance t |
Pick a test#
| Command | What it does |
|---|---|
| One-sample t-test | Mean against mu_0. Degrees of freedom n - 1 |
| Welch t-test | Two independent groups. No equal variance assumption |
| Paired t-test | Same folds for both models. Test d_i against 0 |
| Chi-squared | Observed against expected frequencies |
| Mann-Whitney U | Two groups without a normal assumption |
| Wilcoxon signed-rank | Paired data. Good for 5 or 10 folds |
| Spearman rho | Monotonic relation on ranks. Robust to outliers |
| Bootstrap | Any metric. 95% CI from the 2.5th and 97.5th percentiles |
From Lesson 01.15
Numbers and defaults#
| Setting | Default | Note |
|---|---|---|
| Significance level alpha | 0.05 | Reject H0 when p is below it |
| z for a 95% interval | 1.96 | x_bar plus or minus z times s / sqrt(n) |
n_bootstrap | 5000 | The lesson range is 1000 to 10000 |
| CLT sample size | n >= 30 | Use n >= 100 for skewed data |
| Cohen's d, large effect | 0.8 | 0.2 small, 0.5 medium |
| MH acceptance, Gaussian proposal | 0.234 | High dimensions |
Top-k k | 40 | Typical setting |
| Temperature with top-p | 0.7 + top-p 0.9 | General purpose |
From Lesson 01.07, Lesson 01.15, 01.15 code/statistics.py, Lesson 01.16
Troubleshooting#
| Symptom | Cause | Fix |
|---|---|---|
| exp(102) overflows in softmax | Large logits | Subtract the max logit first |
| Sequence probability is 0.0 | Underflow after about 30 terms | Add log probabilities |
| Naive Bayes score is 0 | Unseen word has MLE count 0 | Add-one Laplace smoothing |
| 99% test gives about 1% sick | Rare prior dominates | Use the base rate. Run a second test |
| One of 20 metrics is significant | 64% chance of a false positive | Bonferroni: alpha / m |
| Tiny p-value on a 0.03% gain | Very large n | Report the effect size too |
| MH chain stuck or slow | Proposal scale too large or too small | Tune toward about 23% acceptance |
| A few samples dominate the estimate | Proposal q far from p | Self-normalize the weights |
From Lesson 01.06, Lesson 01.07, Lesson 01.15, Lesson 01.16, Lesson 01.22
Questions#
- Is the p-value the chance that H0 is true?
- No. It is P(data this extreme given H0).
- What does a 95% CI mean?
- 95% of intervals from repeated experiments contain the true mean.
- Why the reparameterization trick?
- Sampling blocks gradients. z = mu + sigma * epsilon is differentiable.
- Why top-p over top-k?
- Top-p adapts the candidate set to the model confidence.
- How fast does a chain mix?
- A spectral gap of 0.2 needs about 5 steps. 0.01 needs about 100.
- How does MAP relate to regularization?
- A Gaussian prior is L2. A Laplace prior is L1.
Tips#
- Evaluate both models on the same test set and the same folds.
- Report the p-value and the effect size together.
- Monte Carlo error is O(1 / sqrt(N)) in any dimension.
- Discard burn-in samples. Run several chains from different starts.
- A random walk drifts about sqrt(n) from the origin after n steps.
- Use T = 0.0 (greedy) for factual answers and deterministic tasks.
From Lesson 01.15, Lesson 01.16, Lesson 01.22