Simple Algorithm
Deep Learning · Emergence · 26 min

Predicting Distributions, Not Points

Heteroscedastic regression, mixture density networks and proper scoring rules.

A point prediction hides what a decision needs: how noisy the target is here, whether two very different outcomes are both likely, how far away the 95th percentile lies. Train the network to output a whole distribution and you gain all of that, along with three new problems: the variance can cheat, a mixture can collapse, and you need a score that cannot be gamed.

What you’ll learn

  • Derive the heteroscedastic Gaussian negative log-likelihood, its gradients and the optimal variance, and explain the inverse-variance pitfall and the β-NLL remedy.
  • Build a mixture density network for multimodal targets and write its loss stably with log-sum-exp.
  • Define proper and strictly proper scoring rules, compare the log score, the CRPS and the pinball loss, and prove that the pinball loss is minimized by a quantile.
  • Separate aleatoric from epistemic uncertainty, and explain why recasting regression as classification over bins can train better.

Learning the noise level

In heteroscedastic regression the noise level depends on the input. Give the network two outputs, a mean μ(x)\mu(\mathbf x) and a variance σ2(x)\sigma^2(\mathbf x), kept positive by predicting s=log⁡σ2s=\log\sigma^2 or applying a softplus, and minimize the Gaussian negative log-likelihood (Nix & Weigend, 1994):

ℓ(μ,σ2;y)=12log⁡σ2+(y−μ)22σ2(+12log⁡2π)\ell(\mu,\sigma^2;y)=\frac12\log\sigma^2+\frac{(y-\mu)^2}{2\sigma^2}\qquad(+\tfrac12\log2\pi)
A large predicted variance discounts the squared error but pays 12log⁡σ2\tfrac12\log\sigma^2 for it. Kendall and Gal (2017) call this "learned loss attenuation": the network can declare points noisy, at a price.
DerivationGradients and the optimal variance
  1. ∂ℓ∂μ=μ−yσ2,∂ℓ∂σ2=12σ2−(y−μ)22σ4\frac{\partial\ell}{\partial\mu}=\frac{\mu-y}{\sigma^2},\qquad \frac{\partial\ell}{\partial\sigma^2}=\frac1{2\sigma^2}-\frac{(y-\mu)^2}{2\sigma^4}

    Differentiate with respect to both outputs:

  2. σ2⋆(x)=E[(y−μ(x))2∣x] → μ=E[y∣x]  Var⁡(y∣x)\sigma^{2\star}(\mathbf x)=\E\big[(y-\mu(\mathbf x))^2\mid\mathbf x\big]\ \xrightarrow{\ \mu=\E[y\mid\mathbf x]\ }\ \Var(y\mid\mathbf x)

    Averaging over yy given x\mathbf x and setting the derivative with respect to σ2\sigma^2 to zero gives the optimal variance: the expected squared error of the mean head. With the mean at E[y∣x]\E[y\mid\mathbf x], it is the true conditional variance.

  3. The mean's gradient is the squared-error gradient divided by σ2\sigma^2: every point's pull on the mean is weighted by its inverse predicted variance. That is the source of the pitfall below.

Predict before you look

A network predicts σ2=4\sigma^2=4 in a region it fits badly and σ2=0.25\sigma^2=0.25 in a region it fits well. Under the Gaussian NLL, how many times more does a unit of squared error pull on the mean in the well-fit region?

Show the answer

16 ×

The weights are 1/σ21/\sigma^2: 1/0.25=41/0.25=4 against 1/4=0.251/4=0.25, a ratio of 1616. The badly fit region, where the mean most needs to move, gets the weakest gradient.

The pitfall. Seitzer et al. (2022) showed that this weighting can trap training. If some region is hard to fit early on, the network raises σ2\sigma^2 there, which shrinks that region's gradient, so it stays badly fit, so σ2\sigma^2 stays high: a self-reinforcing loop that they describe as effectively undersampling the badly predicted points. The result can be a stable but poor fit of the mean, even on a simple sinusoid that squared error fits easily. Their remedy, β-NLL, multiplies each point's loss by its predicted variance raised to the power β\beta, with the gradient stopped through that factor so it acts as a per-point learning rate:

ℓβ=sg⁡ ⁣[σ2β]⋅(12log⁡σ2+(y−μ)22σ2),∂ℓβ∂μ=μ−yσ2(1−β)\ell_\beta=\operatorname{sg}\!\big[\sigma^{2\beta}\big]\cdot\Big(\frac12\log\sigma^2+\frac{(y-\mu)^2}{2\sigma^2}\Big),\qquad \frac{\partial\ell_\beta}{\partial\mu}=\frac{\mu-y}{\sigma^{2(1-\beta)}}
sg⁡\operatorname{sg} is the stop-gradient (.detach() in PyTorch). β=0\beta=0 is the plain NLL; β=1\beta=1 gives the mean exactly the squared-error gradient while still learning a variance; β=12\beta=\tfrac12 weights points by 1/σ1/\sigma instead of 1/σ21/\sigma^2, which Seitzer et al. found the best trade-off between accuracy and log-likelihood.
python
import torch.nn.functional as F

mu, log_var = model(x).unbind(-1)        # two outputs per example
var = log_var.exp()
nll = F.gaussian_nll_loss(mu, y, var)    # mean of 0.5*(log var + (y-mu)^2/var)

beta = 0.5                               # beta-NLL: reweight by detached var**beta
per_point = 0.5 * (log_var + (y - mu) ** 2 / var)
loss = (var.detach() ** beta * per_point).mean()

Multimodal targets: mixture density networks

A Gaussian, however wide, has one mode. Many problems have several right answers for the same input: the joint angles that put a robot arm's hand at a given point (elbow up or elbow down), the next pen stroke in handwriting, the future positions of a car at a junction. A squared-error network predicts the conditional mean, which can be an impossible average of the valid answers. Bishop's (1994) mixture density network outputs a whole mixture:

p(y∣x)=∑k=1Mπk(x) N(y;μk(x),σk2(x)),π=softmax⁡(a),σk=eskp(y\mid\mathbf x)=\sum_{k=1}^{M}\pi_k(\mathbf x)\,\mathcal N\big(y;\mu_k(\mathbf x),\sigma_k^2(\mathbf x)\big),\qquad \boldsymbol\pi=\softmax(\mathbf a),\quad \sigma_k=e^{s_k}
One network outputs 3M3M numbers per input (for scalar yy): mixing logits a\mathbf a, means and log standard deviations. The loss is −log⁡p(y∣x)-\log p(y\mid\mathbf x), a log of a sum, computed as a log-sum-exp of log⁡πk+log⁡N(y;μk,σk2)\log\pi_k+\log\mathcal N(y;\mu_k,\sigma_k^2) so that tiny component densities do not underflow.

Mixture outputs need care: a component can shrink its variance onto a single training point and drive the likelihood to infinity (put a floor on σk\sigma_k), components can collapse onto one mode or go unused, and the gradient of the mixing weights is weak early in training. The idea travels well. Graves (2013) generated handwriting with a recurrent network whose outputs were a mixture over pen offsets, and PixelCNN++ (Salimans et al., 2017) modeled 8-bit pixel values with a mixture of discretized logistic distributions.

Check your understanding

For one input, the target is +1+1 or −1-1 with equal probability. A network trained with squared error and a two-component mixture density network both see plenty of such data. What do they predict?

  1. Both predict +1+1 or −1-1, whichever they saw last
  2. The squared-error network predicts 00, a value never observed; the MDN puts weight 12\tfrac12 on components near +1+1 and −1-1
  3. Both predict 00
  4. The squared-error network predicts ±1\pm1; the MDN predicts 00
Show the answer

B. The squared-error network predicts 00, a value never observed; the MDN puts weight 12\tfrac12 on components near +1+1 and −1-1

Squared error is minimized by the conditional mean, 00. The mixture likelihood is maximized by matching the actual distribution: two narrow components at ±1\pm1 with equal weights.

Scoring a predictive distribution

ScoreDefinitionProperties
log score−log⁡f(y)-\log f(y), with ff the predicted densitystrictly proper; looks only at the density at the outcome; infinite if f(y)=0f(y)=0, so it punishes overconfident tails hard
CRPS∫R(F(z)−1[y≤z])2dz=E∣Y−y∣−12E∣Y−Y′∣\int_{\R}\big(F(z)-\mathbf 1[y\le z]\big)^2dz=\E\abs{Y-y}-\tfrac12\E\abs{Y-Y'}, with Y,Y′∼FY,Y'\sim F independentstrictly proper for distributions with a finite mean; in the units of yy; reduces to absolute error for a point forecast; closed forms for Gaussians and mixtures
pinball (quantile) lossρτ(y−q)\rho_\tau(y-q) with ρτ(u)=u (τ−1[u<0])\rho_\tau(u)=u\,(\tau-\mathbf 1[u<0])scores a single quantile qq; minimized by the τ\tau-quantile; CRPS⁡=2∫01ρτ(y−F−1(τ)) dτ\operatorname{CRPS}=2\int_0^1\rho_\tau\big(y-F^{-1}(\tau)\big)\,d\tau
"linear" score−f(y)-f(y)improper: its expected value rewards concentrating mass where ff is highest, not reporting ff honestly
The Brier score (p−y)2(p-y)^2 for binary outcomes is the CRPS of a two-point distribution. Neural forecasters trained on CRPS or pinball losses need no density at all: they can output samples or quantiles.
DerivationThe pinball loss is minimized by the τ-quantile (Koenker & Bassett, 1978)
  1. R(q)=E ρτ(Y−q)=τ∫y>q(y−q) dF(y)+(1−τ)∫y<q(q−y) dF(y)R(q)=\E\,\rho_\tau(Y-q)=\tau\int_{y>q}(y-q)\,dF(y)+(1-\tau)\int_{y<q}(q-y)\,dF(y)

    Let YY have a continuous distribution function FF. The expected loss of reporting qq is

  2. R′(q)=−τ (1−F(q))+(1−τ) F(q)=F(q)−τR'(q)=-\tau\,\big(1-F(q)\big)+(1-\tau)\,F(q)=F(q)-\tau

    Differentiate with respect to qq: moving qq up by dqdq saves τ dq\tau\,dq on every outcome above it and costs (1−τ) dq(1-\tau)\,dq on every outcome below:

  3. q⋆=F−1(τ)q^\star=F^{-1}(\tau)

    RR is convex with R′(q)=0R'(q)=0 exactly where F(q)=τF(q)=\tau. So a network trained with ρτ\rho_\tau learns the conditional τ\tau-quantile, τ=12\tau=\tfrac12 being the median (absolute error), and several heads with different τ\tau give prediction intervals.

Predict before you look

For some input x\mathbf x the target is y∼N(0,1)y\sim\mathcal N(0,1). A network is trained with the pinball loss at τ=0.9\tau=0.9. What does it predict for this x\mathbf x?

Show the answer

1.28

The 0.9-quantile of a standard normal, Φ−1(0.9)≈1.28\Phi^{-1}(0.9)\approx1.28. Expected pinball loss there is about 0.1750.175, and it rises on either side (about 0.1830.183 at q=1q=1).

Two kinds of uncertainty, and regression by classification

Everything so far models aleatoric uncertainty, the noise in the data itself, which no amount of data removes. Epistemic uncertainty is uncertainty about the model, which shrinks with more data; a single network's predicted σ2\sigma^2 says nothing about it, and can be confidently wrong far from the training data. Kendall and Gal (2017) model both in one Bayesian framework. A simple and strong practical recipe is the deep ensemble (Lakshminarayanan, Pritzel & Blundell, 2017): train MM networks from different seeds with a proper scoring rule and average their predictive distributions. By the law of total variance, the mixture's variance splits into the two kinds:

Var⁡(y)=1M∑mσm2⏟aleatoric+1M∑m(μm−μˉ)2⏟epistemic: disagreement.\Var(y)=\underbrace{\tfrac1M\textstyle\sum_m\sigma_m^2}_{\text{aleatoric}}+\underbrace{\tfrac1M\textstyle\sum_m(\mu_m-\bar\mu)^2}_{\text{epistemic: disagreement}}.

Regression as classification. Instead of a parametric density, cut the range of yy into bins and predict a softmax over them, trained with cross-entropy. WaveNet (van den Oord et al., 2016) did this for audio: it compressed 16-bit samples with the μ\mu-law f(x)=sign⁡(x)ln⁡(1+μ∣x∣)ln⁡(1+μ)f(x)=\operatorname{sign}(x)\frac{\ln(1+\mu\abs x)}{\ln(1+\mu)}, μ=255\mu=255, into 256 levels and predicted a categorical distribution over them, which can take any shape. For a continuous target, the scalar is spread over bins: two-hot splits it between the two neighboring bins, and HL-Gauss integrates a Gaussian centered on the target over each bin, a form of label smoothing in target space; the prediction is the expected bin center, ∑ipici\sum_ip_ic_i for bin centers cic_i.

Farebrother et al. (2024) replaced the squared-error loss used to train deep reinforcement learning value functions with HL-Gauss cross-entropy and found better performance and scaling across Atari, robotic manipulation, chess and a language-agent task, attributing the gains mainly to robustness to noisy and non-stationary targets. Plausible reasons: the gradient p−y\mathbf p-\mathbf y is bounded whatever the target's magnitude, a categorical output can be multimodal or skewed, and the smoothed targets regularize like label smoothing.

Key takeaways

  • Heteroscedastic regression predicts μ(x)\mu(\mathbf x) and σ2(x)\sigma^2(\mathbf x) with the Gaussian NLL; the optimal variance is the expected squared residual, and the mean's gradient is weighted by 1/σ21/\sigma^2.
  • That weighting can lock in poor fits of hard regions; β-NLL reweights each point by a detached σ2β\sigma^{2\beta}, with β=12\beta=\tfrac12 a good trade-off and β=1\beta=1 giving squared-error gradients for the mean.
  • Mixture density networks output πk,μk,σk\pi_k,\mu_k,\sigma_k for multimodal targets, with a log-sum-exp loss and a floor on the variances.
  • Strictly proper scoring rules (log score, CRPS, Brier) are uniquely minimized by the true distribution; the pinball loss is minimized by the τ\tau-quantile, and the CRPS integrates it over all quantiles.
  • Variance heads capture aleatoric uncertainty; ensembles add epistemic uncertainty through disagreement. Regression recast as classification over bins (WaveNet, HL-Gauss) can train more robustly.

Check yourself

In the interactive lesson these are scored with your confidence, and what you miss comes back for review.

Question 1

A network learns μ(x)\mu(\mathbf x) and σ2(x)\sigma^2(\mathbf x) with the Gaussian NLL. Holding the mean head fixed, which variance minimizes the expected loss at x\mathbf x?

  1. σ2=1\sigma^2=1, whatever the data
  2. σ2=E[(y−μ(x))2∣x]\sigma^2=\E[(y-\mu(\mathbf x))^2\mid\mathbf x], the expected squared error of the mean head
  3. σ2→0\sigma^2\to0, since a smaller variance always gives a higher likelihood
  4. σ2=∣E[y∣x]−μ(x)∣\sigma^2=\abs{\E[y\mid\mathbf x]-\mu(\mathbf x)}
Show the answer

B. σ2=E[(y−μ(x))2∣x]\sigma^2=\E[(y-\mu(\mathbf x))^2\mid\mathbf x], the expected squared error of the mean head

Setting ∂Eℓ/∂σ2=12σ2−E(y−μ)22σ4=0\partial\E\ell/\partial\sigma^2=\frac1{2\sigma^2}-\frac{\E(y-\mu)^2}{2\sigma^4}=0 gives the expected squared residual. If the mean head is right, this is the true conditional variance.

Question 2

In β-NLL, what does β=1\beta=1 do to the gradient that reaches the mean head?

  1. It becomes the plain squared-error gradient μ−y\mu-y, while a variance is still learned
  2. It is divided by σ4\sigma^4
  3. It becomes zero, so only the variance is learned
  4. It is unchanged from the ordinary NLL
Show the answer

A. It becomes the plain squared-error gradient μ−y\mu-y, while a variance is still learned

The detached factor σ2β\sigma^{2\beta} multiplies (μ−y)/σ2(\mu-y)/\sigma^2, giving (μ−y)/σ2(1−β)(\mu-y)/\sigma^{2(1-\beta)}; at β=1\beta=1 the inverse-variance weighting disappears.

Question 3

A model is trained with the pinball loss ρτ(y−q)=(y−q)(τ−1[y<q])\rho_\tau(y-q)=(y-q)(\tau-\mathbf 1[y<q]) at τ=0.1\tau=0.1. What does it estimate?

  1. The conditional mean minus one tenth of a standard deviation
  2. The probability that yy exceeds its median
  3. The conditional 0.1-quantile of yy
  4. The conditional 0.9-quantile of yy
Show the answer

C. The conditional 0.1-quantile of yy

The expected loss has derivative F(q)−τF(q)-\tau, which is zero at F(q)=0.1F(q)=0.1: the 0.1-quantile. Underestimates (positive residuals) cost only 0.10.1 per unit, overestimates 0.90.9, so the prediction sits low.

Question 4

Which of these is not a proper scoring rule for a predictive density ff and outcome yy?

  1. The log score −log⁡f(y)-\log f(y)
  2. The CRPS
  3. The linear score −f(y)-f(y)
  4. The Brier score, for binary outcomes
Show the answer

C. The linear score −f(y)-f(y)

The expected linear score −∫f(y)g(y) dy-\int f(y)g(y)\,dy is minimized by concentrating ff where the true density gg is largest, not by f=gf=g. The other three are strictly proper.

Further reading

  • D. A. Nix & A. S. Weigend, "Estimating the mean and variance of the target probability distribution," IEEE International Conference on Neural Networks (1994).
  • C. M. Bishop, "Mixture density networks," Technical Report NCRG/94/004, Aston University (1994).
  • T. Gneiting & A. E. Raftery, "Strictly proper scoring rules, prediction, and estimation," Journal of the American Statistical Association 102 (2007).
  • A. Kendall & Y. Gal, "What uncertainties do we need in Bayesian deep learning for computer vision?," NeurIPS (2017).
  • M. Seitzer, A. Tavakoli, D. Antic & G. Martius, "On the pitfalls of heteroscedastic uncertainty estimation with probabilistic neural networks," ICLR (2022).
  • J. Farebrother et al., "Stop regressing: Training value functions via classification for scalable deep RL," ICML (2024); arXiv:2403.03950.