Repository navigation
Module 03
⬅️ Module 02 · 🗺️ Curriculum · 🏠 Home · Module 04 ➡️
Level: Foundational · Prerequisites: Module 01, Module 02 · Time: about 8–10 hours
On this page: Overview and learning goals · Beginner: the big picture · Core concepts and derivations · Worked example · Implementation · Advanced topics · Pitfalls and best practices · Conceptual questions and answers · Sources and further reading
Machine learning is reasoning under uncertainty. Probability describes uncertainty about data and parameters; statistics turns finite samples into estimates; information theory quantifies how much a distribution tells us and gives us the loss functions (cross-entropy, KL divergence) used throughout the rest of this wiki. In particular, minimising cross-entropy is maximum-likelihood estimation, and the VAE, GAN and diffusion objectives are all statements about divergences between distributions.
After this module you should be able to:
- apply conditional probability and Bayes' theorem;
- compute expectations, variances and covariances; recognise common distributions;
- derive maximum-likelihood estimators and connect them to loss functions;
- define entropy, cross-entropy, KL divergence and mutual information and prove
$D_{KL}\ge0$ ; - derive the gradient of the softmax cross-entropy loss.
A random variable assigns numbers to uncertain outcomes. Its distribution says how probable each value is. We usually cannot see the true distribution, only samples, so we pick a family of models
flowchart LR
D["Data samples x1..xN"] --> L["Likelihood p_theta(data)"]
M["Model family p_theta"] --> L
L --> N["Negative log-likelihood = cross-entropy"]
N --> O["Minimise over theta (gradient descent)"]
O --> E["Trained model / MLE"]
In Bayesian language: posterior
Discrete variables have a PMF
| Distribution | Support | Parameters | Mean / variance | Used for |
|---|---|---|---|---|
| Bernoulli |
|
binary labels | ||
| Categorical | – | multiclass labels, tokens | ||
| Binomial |
|
counts of successes | ||
| Uniform |
|
initialisation, noise | ||
| Gaussian |
|
noise, latents, weights |
Monte Carlo:
Maximum likelihood (MLE). For i.i.d. data,
Loss functions are likelihoods. If
Entropy is the average surprise; cross-entropy the average surprise when you believe

Figure 3.1 — For figures/make_figs_1.py).
Closed form for 1-D Gaussians:
For softmax outputs
The gradient "prediction minus target" is remarkably simple and does not vanish when the model is confidently wrong — unlike MSE on probabilities. For a Bernoulli/sigmoid output the same form holds:
Bayes. A disease has prevalence 1%; a test has sensitivity 99% and false-positive rate 5%.
Softmax cross-entropy. Logits
import numpy as np
def softmax(z):
z = z - z.max(axis=-1, keepdims=True) # numerical stability
e = np.exp(z); return e / e.sum(axis=-1, keepdims=True)
def cross_entropy(logits, y_idx):
logp = np.log(softmax(logits)) # in practice: log-softmax directly
return -logp[np.arange(len(y_idx)), y_idx].mean()
def kl(p, q): return np.sum(p * (np.log(p) - np.log(q)))
z = np.array([[2., 1., 0.]]); print(cross_entropy(z, np.array([0]))) # 0.4076
p = softmax(z)[0]; print(p - np.eye(3)[0]) # gradient p - y
assert kl(p, p) == 0 and kl(p, np.ones(3) / 3) > 0PyTorch: torch.nn.functional.cross_entropy(logits, target) takes raw logits and applies log-softmax internally; never feed it softmax outputs.
-
MLE, MAP and Bayesian inference. MAP adds a log-prior: a Gaussian prior gives
$\ell_2$ regularisation, a Laplace prior$\ell_1$ (Module 10). -
Fisher information and natural gradient use the local curvature of
$D_{KL}$ ; Adam's second moment is a crude diagonal proxy. -
Variational inference minimises
$D_{KL}(q\Vert p(\cdot\mid x))$ over a tractable family (Module 18);$f$ -divergences and Wasserstein distance underlie GAN variants (Module 19). - Estimators: bias–variance of estimators, the Cramér–Rao bound, and importance sampling.
- Concentration inequalities (Hoeffding, Chebyshev) underpin generalisation bounds (Module 10).
- Calibration measures whether predicted probabilities match frequencies (Module 24).
- Feeding probabilities into a loss that expects logits (double softmax).
- Computing
$\ln(\text{softmax})$ naively → overflow/underflow; use log-sum-exp. - Treating KL as symmetric or as a metric; the direction determines mode-seeking vs mass-covering behaviour.
- Confusing a PDF value with a probability.
- Ignoring base rates (Bayes example above) when interpreting classifier outputs.
Q1. Why is minimising cross-entropy equivalent to maximum likelihood?
Show answer
Cross-entropy between the empirical distribution and the model is
Q2. Why does MSE correspond to Gaussian noise?
Show answer
With
Q3. Prove that KL divergence is non-negative.
Show answer
Apply Jensen to the convex function
Q4. Why is KL asymmetric, and what is the practical effect?
Show answer
The expectation is taken under the first argument.
Q5. What is the gradient of softmax cross-entropy with respect to the logits?
Show answer
Q6. Are uncorrelated variables independent?
Show answer
No. Independence implies zero covariance, but
Q7. Why does Monte Carlo estimation work in very high dimensions?
Show answer
The standard error
Q8. Why is the unbiased variance estimator divided by
Show answer
The sample mean is fitted to the same data, making deviations smaller than deviations from the true mean; dividing by
Q9. What does mutual information measure and why is it
Show answer
How much knowing one variable reduces uncertainty about the other. It is a KL divergence between the joint and the product of marginals, hence non-negative and zero iff independent.
Q10. A model assigns probability 0.01 to the true class. Compare MSE and cross-entropy gradients.
Show answer
With sigmoid output, MSE's gradient contains the factor
- Cover, T. M., & Thomas, J. A. (2006). Elements of Information Theory (2nd ed.). Wiley.
- Bishop, C. M. (2006). Pattern Recognition and Machine Learning, Ch. 1–2. Springer.
- Murphy, K. P. (2022). Probabilistic Machine Learning: An Introduction. MIT Press.
- Goodfellow, Bengio & Courville (2016). Deep Learning, Ch. 3.
- Blitzstein, J., & Hwang, J. (2019). Introduction to Probability (2nd ed.). CRC Press.
⬅️ Module 02 · 🗺️ Curriculum · 🏠 Home · Module 04 ➡️
🏠 Home · 🗺️ Curriculum · 🧭 Learning paths · ❓ Question bank · 📖 Glossary · 📚 References · ✏️ Contributing
🏠 Home · 🗺️ Curriculum · 🧭 Learning paths
❓ Question bank · 📖 Glossary · 📚 References
Jump to module:
01 02 03 04 05 06 07 08 09 10 11 12
13 14 15 16 17 18 19 20 21 22 23 24