Predicting a distribution: Bayesian GLM, distributional boosting and BART
Three learners that report how sure they are, row by row. Read their intervals, check coverage on held-out rows, and know when a Normal assumption lets you down.
What runs in CodeSplash
- Bayesian GLM Verified in Python and R
- Distributional boosting Verified in Python and R
- Bayesian additive regression trees Verified in Python and R
You will be able to
- Say what a predictive distribution adds to a point prediction
- Compare Bayesian GLM, distributional boosting and BART
- Check predictive intervals with held-out coverage and proper scores
Read first
More than a number
ReadA point prediction of 40 ppb might come with a band of 35 to 45, or of 5 to 110. The decision can be different, but a point prediction looks the same.
Three learners in the gallery predict a whole distribution for each row.
| Learner | Idea | Fit |
|---|---|---|
| Bayesian GLM | A Normal prior on coefficients | Exact for regression; no sampling |
| Distributional boosting | Small trees for both the mean and the log SD | Natural gradients |
| Bayesian additive regression trees | A sum of weak trees with a prior | MCMC |
How much does it cost in accuracy?
PredictPredict first
Ozone RMSE: a linear model gets 22.9 and XGBoost 18.2. What do you expect for these three?
Reveal
About the same as their cousins. Bayesian GLM: 22.8, like the linear model. Distributional boosting: 19.4. BART: 20.3. Those last two are close to boosted trees. So you can usually have the uncertainty without giving up the fit.
Prior and data
ExploreA conjugate priorConjugate priorA prior chosen so the posterior has an exact formula; no sampling, so the answer is the same in Python and R. has the data update the prior in closed form, with no sampling, so Python and R agree exactly.
A prior is a head start for one answer. Raise Prior successes (α) and Prior failures (β), and watch how many Trials it takes to pull the posterior away.
EExplorable · From prior to posterior
How does evidence update what you believed, and how much does the prior matter?
What is the posterior mean?
Dashed: prior. Second line: what the data alone say. Solid: posterior, a compromise weighted 83% toward the data.
Commit your prediction above to see the result
Make your prediction to see the result.
In a Bayesian GLM, Prior scale (τ) plays that role, with a smaller value shrinking coefficients harder. On Pima, a very tight with three features has an AUC of 0.848, and a wider one on all seven has 0.855.
Different rows, different widths
ReadChick weights spread out as chicks grow, so a single error band won't suit all ages. Distributional boostingNatural gradient boostingBoosting that fits a whole distribution, one small tree per round for the mean and one for the spread. fits one tree per round to the mean and one to the log SD. Rows it finds harder get wider intervals.
Grouped by chick, its RMSE is 35.3. A run with a width per row is worth more than a slightly better RMSE when decisions depend on the width.
It assumes the target is roughly Normal around each prediction. For skewed targets, model the logarithm.
Check the promise
ReadA 90% interval should contain the truth on about 90% of held-out rows. That's coverage, and the run's summary reports it. Two proper scores judge the whole distribution:
- Negative log-likelihood: how probable the outcome was under the predicted distribution.
- CRPS: a distance between the predicted distribution and the outcome, in the target's units.
Too-narrow intervals with a high fit score are the classic failure. If coverage is off, Uncertainty can wrap the point predictions in conformal intervals, which don't need a distributional assumption.
Which to pick?
Reflect- Bayesian GLM: simple effects, a credible interval per coefficient, fast and exact.
- Distributional boosting: non-linear means, and spread that varies by row.
- BART: little tuning, and a posterior for every prediction. It agrees statistically across engines, since each runs its own random stream. On Pima: AUC 0.866 with four features.
Think of a prediction you'd act on differently if you knew it was uncertain. Which of these fits it?
Check your understanding
CheckpointCheck yourself
A model's 90% predictive intervals cover 70% of held-out rows. What's the matter?
Show the answer
Answer: a
Coverage below the stated level means understated uncertainty. Use a different distribution, or calibrate with conformal prediction.
Check yourself
You shrink Prior scale (τ) in a Bayesian GLM towards zero. What happens to the coefficients?
Show the answer
Answer: a
A Normal prior on coefficients is the Bayesian face of ridge regression. With few rows the prior matters more.
Check yourself
Why does distributional boosting give wider intervals on some rows?
Show the answer
Answer: b
The spread is modeled like the mean. That's its main advantage over a constant error band.
Check yourself
Why doesn't BART's cross-engine comparison match digit for digit?
Show the answer
Answer: a
Same core, different preparation and draws, so the scores agree statistically. That's parity tier P3.
Train them
In the appIn the Learners step, select Bayesian GLM, Distributional boosting or Bayesian additive regression trees. Check that Overview states the interval coverage, and compare them with your usual learners on the Leaderboard. Distributional boosting predicts numbers only. The other two also handle two classes.
import numpy as np
import pandas as pd
from sklearn.linear_model import BayesianRidge
from sklearn.model_selection import KFold
from sklearn.preprocessing import StandardScaler
df = pd.read_csv("airquality.csv").dropna()
X = StandardScaler().fit_transform(df[["Solar.R", "Wind", "Temp", "Month"]])
y = df["Ozone"].to_numpy()
hit = []
for tr, va in KFold(5, shuffle=True, random_state=1).split(X):
mean, sd = BayesianRidge().fit(X[tr], y[tr]).predict(X[va], return_std=True)
hit.append(np.abs(y[va] - mean) <= 1.645 * sd)
print("90% interval coverage", round(np.concatenate(hit).mean(), 3))df <- na.omit(airquality)
X <- cbind(1, scale(df[, c("Solar.R", "Wind", "Temp", "Month")]))
y <- df$Ozone
tau <- 10
set.seed(1)
fold <- sample(rep(1:5, length.out = length(y)))
hit <- unlist(lapply(1:5, function(k) {
Xt <- X[fold != k, ]; yt <- y[fold != k]; Xv <- X[fold == k, ]
s2 <- summary(lm(yt ~ Xt - 1))$sigma^2
V <- solve(crossprod(Xt) / s2 + diag(ncol(X)) / tau^2)
b <- V %*% crossprod(Xt, yt) / s2
sd <- sqrt(s2 + rowSums((Xv %*% V) * Xv))
abs(y[fold == k] - Xv %*% b) <= qnorm(0.95) * sd
}))
round(mean(hit), 3)Recap
Recap- A predictive distribution gives a width per row, usually at little cost in accuracy.
- Bayesian GLM is exact, distributional boosting models the spread, and BART needs little tuning.
- Check held-out coverage and a proper score. Fall back on conformal wrappers if coverage fails.
Concepts in this lesson