Quant Memo
Advanced

Heteroscedastic Regression With Learned Variance

A network that predicts its own error bar alongside its point estimate — wide on inputs where the truth is genuinely noisy, narrow where it is not — is rewarded by a loss function that penalizes overconfidence directly.

Prerequisites: The Multilayer Perceptron, Maximum A Posteriori Estimation

Ordinary regression trained with mean-squared error implicitly assumes every prediction is wrong by roughly the same amount — one error bar, applied everywhere, regardless of whether the input is a calm, predictable region of the data or a genuinely chaotic one. That assumption is usually false: a volatility forecast on a quiet blue-chip stock is naturally more precise than the same forecast on an earnings day for a small-cap. Heteroscedastic regression fixes this by having the network predict its own error bar, input by input, rather than living with one fixed number for every prediction.

The analogy: a forecaster who states their own confidence

A weak weather forecaster always says "expect 20°C" with the same implied confidence, whether it's a calm summer day or the middle of an unpredictable storm system. A better forecaster says "20°C, plus or minus 1 degree" on the calm day, and "20°C, plus or minus 8 degrees" during the storm — the same point forecast, but honest about how much less trustworthy it is under the second set of conditions. A heteroscedastic network is trained to be that second forecaster: it outputs both a mean μ(x)\mu(x) and a spread σ(x)\sigma(x) for every input xx, and σ(x)\sigma(x) is allowed to be large or small depending on how genuinely noisy that region of the input space is.

The loss that rewards honesty

Training uses the Gaussian negative log-likelihood instead of plain squared error:

L=12logσ(x)2+(yμ(x))22σ(x)2\mathcal{L} = \frac{1}{2}\log\sigma(x)^2 + \frac{(y - \mu(x))^2}{2\sigma(x)^2}

The second term is a squared-error term, but divided by the predicted variance — a prediction that's off by a lot costs less if the network honestly predicted a large σ\sigma there, and costs a lot if the network claimed high confidence (small σ\sigma) and was wrong. The first term, 12logσ2\frac12\log\sigma^2, stops the network from gaming this by simply predicting enormous σ\sigma everywhere to make every error look cheap — that first term grows whenever σ\sigma grows, so inflating variance without genuine cause is itself penalized. In plain English: the loss rewards a network for being both accurate and correctly calibrated about how accurate it is.

Worked example 1: comparing a calibrated guess to an overconfident one

Two predictions for the same true value y=10y = 10. Prediction A: μ=9.8\mu=9.8, σ=0.5\sigma=0.5 (small, honest error, modest confidence). Prediction B: μ=9.8\mu=9.8, σ=0.05\sigma=0.05 (same error, but wildly overconfident). Loss A: 12log(0.25)+0.042(0.25)=0.693+0.08=0.613\frac12\log(0.25) + \frac{0.04}{2(0.25)} = -0.693 + 0.08 = -0.613. Loss B: 12log(0.0025)+0.042(0.0025)=3.00+8.0=5.00\frac12\log(0.0025) + \frac{0.04}{2(0.0025)} = -3.00 + 8.0 = 5.00. Prediction B's tiny error is amplified into a huge loss because it claimed near-certainty; prediction A, admitting more uncertainty for the same error, is penalized far less. This is exactly the incentive structure that trains the network to widen σ\sigma honestly rather than bluff confidence.

Worked example 2: two regions with different noise levels

Region A (calm) has residuals typically near ±0.2\pm0.2; region B (volatile) has residuals typically near ±2.0\pm2.0. A network forced to use one shared σ\sigma for both regions must compromise — say σ=1.1\sigma=1.1 — which is too wide for region A (unnecessarily inflating its loss via the log term relative to its tiny true errors) and too narrow for region B (heavily penalizing it via the squared-error term for genuinely large, unavoidable errors). Letting σ(x)\sigma(x) vary lets the network settle near σA0.2\sigma_A \approx 0.2 and σB2.0\sigma_B \approx 2.0 instead, and the total loss summed across both regions is lower than any single shared value could achieve — the model is rewarded for admitting region B is inherently noisier.

calm region: narrow σ(x) volatile region: wide σ(x)
The predicted mean (line) barely changes in width of uncertainty, but the band around it — the learned σ(x) — widens sharply once the input enters a genuinely noisier region.

Function explorer
-2222.0
x = 1.00f(x) = 2.000

The squared-error term in the loss has exactly this shape before being divided by σ2\sigma^2 — heteroscedastic regression's whole trick is letting that division rescale the curve differently in different regions of the input.

Heteroscedastic regression trains a network to output an input-dependent variance σ(x)2\sigma(x)^2 alongside its mean, using a loss (Gaussian negative log-likelihood) that penalizes both inaccuracy and overconfidence, so the network learns to widen its own error bars exactly where the data is genuinely noisier.

What this means in practice

This is the standard way to get input-dependent (aleatoric) uncertainty out of a single deterministic forward pass — no repeated sampling needed, unlike MC dropout — making it cheap enough for real-time risk sizing: a position-sizing model can shrink size automatically on inputs where its own predicted σ(x)\sigma(x) is wide, without any separate uncertainty-estimation pipeline.

The common mistake is training logσ2\log\sigma^2 directly as a raw, unconstrained network output without checking it stays numerically sane — a network can find a shortcut early in training where driving σ2\sigma^2 toward zero on a few easy points makes the log term plunge toward -\infty, destabilizing the whole loss. In practice logσ2\log\sigma^2 is predicted (rather than σ2\sigma^2 itself) and often clipped, precisely to prevent the loss from being dominated by this degenerate shortcut instead of genuine calibration.

Related concepts

Practice in interviews

Further reading

  • Nix & Weigend, Estimating the Mean and Variance of the Target Probability Distribution (1994)
  • Kendall & Gal, What Uncertainties Do We Need in Bayesian Deep Learning for Computer Vision? (2017)
ShareTwitterLinkedIn