Level 0: Foundation
★☆☆☆☆ Introductory⏱ 5 minGeneral audience. No mathematics required.Rearranging to stay on the ruler
Suppose you need to compare two huge numbers like a billion billion billion and a billion billion billion and one. Your calculator only has 8 digits, so you can’t even type them. But you could subtract a billion billion billion from both first, and then compare 0 and 1. That’s easy. Stable computation is the art of rewriting a formula so the computer never has to hold numbers that are too big, too small or too close together.
| logit | naive | stable |
|---|---|---|
| 50 | 0.7310 | 0.7310 |
| 49 | 0.2689 | 0.2689 |
| 40 | 0.00003319 | 0.00003319 |
| true variance | 0.3048 |
| one-pass E[x²] − E[x]² | 0.1270 |
| Welford | 0.3048 |
A navigation filter that computes particle weights or HMM likelihoods by multiplying probabilities will underflow to zero within a few hundred steps. A classifier with large logits will produce NaN losses. Stable formulations are the difference between a system that works for a minute and one that works for a mission.
- 1. Stable computation means…
- 2. Multiplying many probabilities together tends to…
- 3. Working with logarithms turns products into…
Level 1: Beginner
★★☆☆☆ Basic⏱ 20 minSecondary-school mathematics: arithmetic and elementary algebra.The log-sum-exp trick
Every exponent is now ≤ 0, so nothing overflows, and at least one term equals 1, so the sum can’t underflow to 0.
Stable softmax and log-softmax
Compute log-probabilities directly. Never take log of a softmax output that may have underflowed to 0.
Softplus and BCE from logits
. Binary cross-entropy with logit and label is . That’s what BCEWithLogitsLoss computes.
- 1. In the LSE trick, m is…
- 2. log softmax(z)ᵢ =
- 3. softplus(1000) ≈
Level 2: Intermediate
★★★☆☆ Intermediate⏱ 40 minUndergraduate students and practising engineers.More stable rewrites
- Variance: the one-pass cancels catastrophically when the mean is large. Welford’s update keeps a running mean and sum of squared deviations: , then and .
- Quadratic roots: , giving and . This avoids subtracting nearly equal and .
- Norms:
hypot(x, y)scales to avoid overflow in . - Log-space probability: store log-probabilities. Replace products with sums and sums with LSE. This is used in HMMs, particle-filter weights, Bayesian evidence and beam search.
- 1. The one-pass variance formula fails when…
- 2. The stable quadratic formula avoids…
- 3. Particle-filter weights should be normalized using…
Level 3: Advanced
★★★★☆ Advanced⏱ 40 minGraduate level, with full derivations and proofs.Why LSE is accurate
Let . Each term is computed with relative error and the sum of positive terms is well-conditioned. then has absolute error , and adding is one more rounding. So LSE is accurate to a few ulps relative to (Blanchard, Higham and Higham, 2021). ∎
Softmax gradient stays stable
, a difference of bounded quantities. Fusing log-softmax with NLL avoids ever forming .
Welford is backward stable
Each update works with deviations from the current mean, whose size is set by the spread, not the offset. The error bound is , compared with for the one-pass formula (Chan, Golub and LeVeque, 1983).
Exercise 3.1: LSE of (−1000, −1000)
, , so the result is . The naive version gives .
- 1. In LSE, the shifted sum s lies in…
- 2. LSE(−1000, −1000) ≈
- 3. The gradient of log-softmax with respect to the logits is…
Level 4: Expert
★★★★★ Expert⏱ 35 minResearchers: original papers and open problems.Terrain HMM over a long traverse
A rover infers terrain type (sand, rock, ice) from wheel-slip readings with a hidden Markov model. The forward algorithm multiplies thousands of probabilities below 1. After 2000 steps the true likelihood is about , far below the smallest double (), so the naive recursion returns exactly 0. There are two standard fixes, and both give the same log-likelihood:
- log-space forward, using LSE over predecessor states;
- scaled forward (Rabiner, 1989), which normalizes every step and accumulates .
Stable computation in AI, robotics and aerospace
- Deep learning frameworks: fused
log_softmax+ NLL,BCEWithLogitsLoss,logsumexpand FlashAttention’s online softmax, which uses running-max rescaling (Milakov and Gimelshein, 2018; Dao et al., 2022). - Particle filters: log-weights normalized by LSE, plus effective sample size from normalized weights.
- Kalman filters: Joseph form and square-root or UD factorizations keep covariances symmetric positive definite (Bierman, 1977).
- Bayesian model comparison: log marginal likelihoods and log Bayes factors are always computed in log space.
References
- N. Higham (2002). Accuracy and Stability of Numerical Algorithms, 2nd ed. SIAM.
- P. Blanchard, D. Higham, N. Higham (2021). “Accurately computing the log-sum-exp and softmax functions.” IMA Journal of Numerical Analysis, 41(4).
- B. P. Welford (1962). “Note on a method for calculating corrected sums of squares and products.” Technometrics, 4(3).
- T. Chan, G. Golub, R. LeVeque (1983). “Algorithms for computing the sample variance: analysis and recommendations.” The American Statistician, 37(3).
- L. Rabiner (1989). “A tutorial on hidden Markov models and selected applications in speech recognition.” Proceedings of the IEEE, 77(2).
- M. Milakov, N. Gimelshein (2018). “Online normalizer calculation for softmax.” arXiv:1805.02867.
- T. Dao et al. (2022). “FlashAttention: fast and memory-efficient exact attention with IO-awareness.” NeurIPS.
- G. Bierman (1977). Factorization Methods for Discrete Sequential Estimation. Academic Press.