Bayesian Deep Learning — Calibrated Uncertainty

Python · PyTorch  ·  capstone of the neural-networks section

Giving a Network an Error Bar

Every network in this section returned a point prediction — one number, no sense of how sure it is. That is a serious gap under risk: a credit model at 51% confidence and one at 99% should be treated very differently, and a volatility forecast is only useful with an error bar. Full Bayesian inference over a network's weights is intractable, so two practical approximations do the work: MC-dropout (leave dropout on at test time and average many stochastic passes) and deep ensembles (train several networks from different initialisations and read their disagreement as uncertainty).

The canonical demonstration is a 1-D regression with training data in two bands, leaving a gap in the middle and nothing in the tails. A plain network fits a confident curve everywhere, including regions it has never seen. Both Bayesian treatments widen where there is no data — but not equally well, and the ratio is what matters. The deep ensemble separates extrapolation from in-data by about 15×; MC-dropout manages only .

The reason shows up on the in-data side. MC-dropout reports a standard deviation of 0.145 even where it has plenty of data — larger than the data's own noise of 0.10. Much of that is the dropout rate showing through rather than a statement about evidence. The ensemble's in-data spread collapses to 0.016, so when it does widen, you have learned something.

predictive std, 1-D demoon datainterpolation gapextrapolationratio
deep ensemble (8 nets)0.0160.0250.23915×
MC-dropout (200 passes)0.145 — above the data noise of 0.100.1650.466

Calibration, and what it costs

On California housing both methods produce near-calibrated intervals: 92.2% coverage against a nominal 90%, slightly conservative — wider than strictly needed, which is the safer direction but worth naming precisely rather than rounding to "calibrated". The uncertainty is also informative: mean absolute error rises monotonically with the predicted standard deviation. The error bars are not free, though. The dropout net scores RMSE 0.5595 where the identical architecture without dropout scores 0.5464 and the plain MLP about 0.525 — roughly 0.03 RMSE is the price of the machinery.

The Selective-Prediction Trap

Then the sharpest lesson in the section, which is a negative result. The standard way to show uncertainty "changes decisions" is selective prediction: auto-decide the most-confident cases, defer the rest to human review. Accuracy on the retained set duly climbs from 0.821 to 0.904 as more is deferred. Read that column alone and it is the textbook result.

Read the next column and it evaporates. Because the data is 22% defaults, the retained subset gets easier at almost exactly the rate the model appears to improve. The lift over simply predicting "no default" falls to zero: at 50% retention the model is no better than a constant, and at 30% retention it catches none of the defaults it exists to find. Deferring by uncertainty discards 73% of all defaults.

% auto-predicted (most certain first)accuracymajority-class baselineliftrecall on defaultsAUC on retained
100%0.8210.779+0.0420.3340.768
75%0.8560.853+0.0030.0490.682
50%0.8820.883−0.0000.0060.667
30%0.9040.904+0.0000.0000.684

The mechanism is not subtle once you look for it: corr(uncertainty, predicted probability) = 0.71. For a sigmoid output the spread across dropout passes is mechanically small when the probability sits near zero, so "most certain" mostly means "confidently predicted non-default" — the easy majority class. This is a general trap rather than a quirk of this dataset: selective prediction scored by accuracy on imbalanced data will almost always look like it works. The honest diagnostics are lift over the majority baseline, recall on the minority class, or AUC on the retained subset — none of which improve here.

What survives is weaker but real. Errors do sit at higher uncertainty (0.0509 against 0.0412), and uncertainty predicts being wrong with AUC 0.645 — a usable triage signal, just a far more modest one than the accuracy curve advertises. And on the regression task, where there is no class balance to be fooled by, the interval story holds up cleanly.

Proportions vs Predictions

Binning the test set by what the model predicted and plotting what actually happened — the collection's standard diagnostic — separates two claims a single coverage number conflates, and both turn out to matter here.

Calibration of the mean. The California decile means bend away from the diagonal at both ends: the model over-predicts the cheapest blocks by 0.077 and under-predicts the dearest by 0.164. Ordinary regression-to-the-mean shrinkage, and the same sag the tree and kernel examples found at the $500k price cap.

Calibration of the interval — and this is the one worth having. Overall coverage was 0.922, comfortably near nominal. Per decile it runs from 0.814 to 0.995: the intervals are far too wide for cheap blocks and too narrow for expensive ones, and the two errors cancel in the average. A single coverage number can look calibrated while the model is miscalibrated across the whole range; only the conditional view exposes it. For a risk application that is the difference between an error bar you can size a position with and one you cannot.

California decilepredictedactualgap90% coverage (nominal 0.90)
1 (cheapest)0.8490.772−0.0770.995 — far too wide
31.3541.325−0.0280.974
51.7351.720−0.0160.950
61.9651.994+0.0290.916 — about right
93.0843.206+0.1220.814 — too narrow
10 (dearest)4.0564.220+0.1640.858

And the probability check, which is a different question from interval coverage. On credit default the reliability curve gives ECE 0.0127 over ten quantile bins, with mean predicted probability 0.217 against a base rate of 0.221 — honest across most of the range, drifting only in the top decile (0.652 predicted against 0.710 observed), where the model is under-confident about its riskiest clients. Note this is a different question from the section above: the probabilities can be well calibrated while an uncertainty score built from them still fails as a triage rule, and here both are true at once.

Where this sits

This closes the loop with the collection's Bayesian core. BART delivers posterior credible intervals from a tree ensemble; Gaussian processes deliver exact posterior variance from a kernel; here neural networks get the same thing by approximation. Exact-but-limited, sampling-based, and scalable-approximate are three routes to one idea that runs through the whole collection — report what you don't know — with the caveat this notebook adds: check that the metric you use to prove it is not doing the work for you.

Notebook

Downloads

References