- Introduced by
- Yao, Y.; Vehtari, A.; Simpson, D.; Gelman, A.
- Year
- 2018
- Type
- Bayesian ensemble combination
- Family
- Machine learning
What does it do?
Bayesian stacking combines the predictive distributions of several base models by choosing non-negative weights that sum to one and maximise the leave-one-out log predictive score of the resulting mixture.
Formalised by Yao, Vehtari, Simpson, and Gelman (2018), it yields a single combined predictive distribution. The weights are point estimates found by optimising a cross-validated score, not posterior probabilities of the models and not a posterior distribution over weights. Whether the combined distribution is well calibrated must be checked separately, because a strong leave-one-out score does not by itself guarantee calibrated intervals.
The intuition behind the method
Bayesian model averaging (BMA) usually weights models by their marginal likelihoods. Bayesian stacking instead asks which weights on the models' predictive distributions would have predicted each observation best if that observation were left out in turn. Yao et al. (2018) argue that BMA is flawed when the true data-generating process is not among the candidate models, the setting they call M-open, and they recommend stacking of predictive distributions on the basis of simulations and real-data applications. The advantage is therefore an empirical argument from that work and is not guaranteed for any particular dataset.
History
Stacked generalisation was introduced by Wolpert (1992), who used a meta-learner to combine the predictions of base models. The Bayesian version, which combines predictive distributions and optimises leave-one-out log scores, was formalised by Yao, Vehtari, Simpson, and Gelman in their 2018 Bayesian Analysis paper. That paper compares stacking with Bayesian model averaging, Pseudo-BMA, and related alternatives in simulations and real-data applications, and recommends stacking of predictive distributions.
When to use it
Use Bayesian stacking when you have two or more probabilistic models for the same outcome and want a combined predictive distribution rather than a choice of one model. It fits best when the candidate models are all plausible but imperfect, and when the predictive distribution itself, such as an interval for a clinical or risk forecast, is the output that matters. Do not use it when the candidate models are nearly identical, because there is little to combine, or when computing LOO quantities is too costly for the dataset at hand. It does not replace model checking: if the base models are poorly specified, the mixture inherits that weakness.
Limitations and considerations
- Requires each base model to output a full predictive distribution, not only point predictions.
- Leave-one-out computation is costly for large datasets unless it is approximated, for example with the importance sampling approach used by Yao et al. (2018).
- With few observations, the leave-one-out estimates of predictive accuracy are noisy, so the estimated weights can be unstable.
- Does not repair poorly specified base models; a mixture of misspecified models remains misspecified.
- The combined model is a mixture, so its parameters are harder to interpret than those of any single base model.
Source conditions do not replace independent scientific review or validation by an analysis engine.
Strengths
- Produces a combined predictive distribution rather than a point estimate, so the spread from each component model is carried into the mixture.
- Chooses weights by leave-one-out predictive accuracy, so it does not depend on marginal likelihood approximations, which Yao et al. (2018) argue is a weakness of BMA when the true process is not among the candidates.
- The optimisation is concave in the weights, so a standard convex solver finds the global optimum, although the optimum is not always unique.
- Can combine any base models that yield predictive densities, whatever their internal structure.
- When two base models make nearly identical predictions, the optimisation may split weight between them arbitrarily, but the mixture's predictive density is essentially unchanged by that split.
Applications
- Combining competing Bayesian regression or hierarchical models in clinical and epidemiological research when several specifications are defensible.
- Probabilistic forecasting when several candidate models are plausible and a single combined predictive distribution is required.
- Constructing ensembles from base models built in Stan, brms, or PyMC, provided each model can produce predictive densities.
- Risk and uncertainty quantification where a single combined predictive interval is the target output.
How it works
How it works
A set of K base models is trained on the full dataset. The models may differ in architecture, prior specification, feature set, or likelihood. Diversity is desirable: highly correlated base models provide little additional information when combined.
For each base model and every training observation, a leave-one-out (LOO) predictive density is computed, that is, the density of y_i given x_i under a model fitted without observation i. The stacking objective is defined in terms of these out-of-sample quantities. Yao et al. (2018) use Pareto-smoothed importance sampling (PSIS) to approximate the leave-one-out posterior predictive distributions without refitting each model once per observation.
The weights w_1, ..., w_K are chosen, subject to being non-negative and summing to one, to maximise the sum over observations of the log of the weighted mixture of the models' leave-one-out predictive densities, p(y_i | x_i, M_k, D_-i). The objective is concave in the weights, so this is a convex optimisation problem that standard solvers handle, and any local optimum is global.
Formula
\hat{w} = \arg\max_{w \ge 0,\, \sum w_k=1} \sum_{i=1}^n \log \sum_{k=1}^K w_k \, p(y_i \mid x_i, M_k, D_{-i})For new observations, the final predictive distribution is the weighted mixture of the K base model predictive distributions using the optimised stacking weights. Uncertainty from all component models is preserved in the mixture.
Formula
p(y^* \mid x^*) = \sum_{k=1}^K \hat{w}_k \, p(y^* \mid x^*, M_k)After combining, evaluate the ensemble predictive distribution on a held-out test set or with additional cross-validation diagnostics. Calibration checks, such as probability integral transform histograms or empirical coverage of predictive intervals, show whether the combined uncertainty is well calibrated. When PSIS is used, inspect the Pareto-k diagnostic for each observation: values above the threshold recommended in the PSIS-LOO literature indicate unreliable leave-one-out estimates. In that case, refit the affected observations with exact leave-one-out, use k-fold cross-validation instead, or treat the resulting weights with caution.
Common pitfalls
- Using too few diverse base models, so that stacking collapses to a single model with weight near one.
- Applying stacking weights estimated on training data to a new domain where the relative model quality shifts.
- Confusing Bayesian stacking with Bayesian model averaging: they differ in the optimisation criterion and robustness properties.
- Ignoring calibration checks after combining; a good LOO score does not automatically guarantee well-calibrated intervals.
- Using importance-sampling LOO without checking Pareto-k diagnostics, which flag unreliable estimates; refit or use k-fold cross-validation for flagged observations.
- Estimating weights from in-sample fitted predictions instead of out-of-sample (leave-one-out or cross-validated) predictive densities, which overstates fit and biases the weights.
Frequently asked questions
How is Bayesian stacking different from Bayesian model averaging (BMA)?
BMA weights models by their marginal likelihoods (posterior model probabilities), which can place most of the weight on one model. Bayesian stacking instead chooses weights to maximise leave-one-out predictive accuracy, so it can give weight to several complementary models. Yao et al. (2018) argue this is more appropriate when the true process is not among the candidate models (the M-open setting).
How many base models do I need?
Even two or three diverse models can benefit from stacking. The key is diversity: models that make different errors on different regions of the data. Adding nearly identical models wastes computation without improving the mixture.
Is LOO always required, or can I use k-fold cross-validation?
LOO is often approximated efficiently via PSIS-LOO. For large datasets, or when Pareto-k diagnostics flag unreliable estimates, k-fold cross-validation (e.g., 10-fold) can be used to obtain the out-of-sample log predictive densities needed for weight optimisation, at the cost of some approximation.
Can I use Bayesian stacking with non-Bayesian base models?
Yes, if the models can produce full predictive densities or probabilities (e.g., a probabilistic forecast from a random forest or a Gaussian process). Each model must supply out-of-sample predictive densities for every observation, obtained by leave-one-out or k-fold cross-validation. Predictions from models fitted on all the data must not be used to estimate the weights, because they overstate fit. A full posterior is not required.
What software supports Bayesian stacking?
The loo R package provides loo_model_weights(), which computes stacking weights among other options. In Python, ArviZ offers LOO and model-weight computation. Stan and PyMC models can serve as base models.
Related methods
Sources
Yao, Y., Vehtari, A., Simpson, D., & Gelman, A. (2018). Using stacking to average Bayesian predictive distributions. Bayesian Analysis, 13(3), 917–1007.
Open DOI referenceWolpert, D. H. (1992). Stacked generalization. Neural Networks, 5(2), 241–259.
Open DOI reference