NeurIPS 2026Coming to NeurIPS?Explore papers, orals & directions

Bayesian hierarchical stacking

Yuling Yao, Gregor Pirs, Aki Vehtari, Andrew Gelman · DOI

Abstract

Stacking is a widely used model averaging technique that asymptotically yields optimal predictions among linear averages. We show that stacking is most effective when model predictive performance is heterogeneous in inputs, and we can further improve the stacked mixture with a hierarchical model. We generalize stacking to Bayesian hierarchical stacking. The model weights are varying as a function of data, partially-pooled, and inferred using Bayesian inference. We further incorporate discrete and continuous inputs, other structured priors, and time series and longitudinal data. To verify the performance gain of the proposed method, we derive theory bounds, and demonstrate on several applied problems.

Introduction

In machine learning and statistics, model stacking is a standard technique for combining the strengths of multiple candidate models. By assigning a weight to each model and producing a weighted average of their predictions, stacking often achieves better predictive performance than any single model. However, traditional stacking typically employs "complete-pooling," where a single set of weights is applied across the entire dataset.

This approach ignores a fundamental reality: model performance is often heterogeneous. A complex model might be highly accurate in regions where data is dense but fail spectacularly in the "tails" of the distribution. Conversely, a simpler model might be more robust in those same tails. Standard stacking fails to exploit these local strengths. While one could calculate separate weights for every subgroup (no-pooling), this leads to overfitting in data-sparse regions. Bayesian Hierarchical Stacking provides a "middle way" by allowing model weights to vary based on input features while using hierarchical priors to regularize the estimation.

The Hierarchical Stacking Framework

The core innovation of this method is the transition from a fixed weight $w_k$ to an input-dependent weight function $w_k(x)$. This allows the ensemble to adapt its composition depending on where in the feature space a prediction is being made.

Partial Pooling and the Hierarchical Bridge

To avoid the noise and overfitting associated with local weight estimation, we treat the weights as a Bayesian hierarchical problem. We use a softmax transformation to map unconstrained parameters to a simplex, ensuring that weights are non-negative and sum to one. By placing hierarchical priors on these parameters, we achieve "partial pooling." This mechanism allows small subgroups to borrow strength from the global average, while allowing larger groups with strong evidence to deviate and specialize.

Flexible Weight Structures

The framework is highly adaptable to different data types:

  • Discrete Categories: Weights can vary across demographic groups or geographic regions.
  • Additive Models: Using splines or rectified linear units (ReLU), weights can change smoothly over continuous variables.
  • Gaussian Processes: For high-dimensional continuous inputs, Gaussian processes can model complex weight surfaces.

Workflow and Implementation

The authors propose a robust two-stage workflow that maintains computational efficiency:

  1. Individual Model Fitting: Fit the candidate models independently and calculate pointwise leave-one-out (LOO) predictive densities.
  2. Hierarchical Stacking: Use these LOO densities as the "data" to fit the hierarchical stacking model via MCMC. This separation avoids the need to fit a full, complex "mixture of experts" model simultaneously, which is often computationally prohibitive.

Key Insights

  • Locally Useful Models: The method operationalizes the mantra that "all models are wrong, but some are somewhere useful." By acknowledging local performance, the ensemble becomes more than the sum of its parts.
  • Immunity to Covariate Shift: Because hierarchical stacking focuses on conditional fit ($y$ given $x$), it is naturally robust to changes in the distribution of $x$. If the model knows where a specific sub-model is accurate, it remains accurate even if the test set distribution shifts toward those regions.
  • Regularization as a Safeguard: The hierarchical prior acts as a shrinkage mechanism, preventing the model from aggressively picking a single "winner" in small subgroups—a common failure mode in local model selection.

Empirical Results

The method was evaluated across three distinct domains:

  1. Well-switching in Bangladesh: In a study of 3,020 households deciding whether to switch to safer water wells, hierarchical stacking consistently achieved higher log predictive densities and better calibration than standard stacking.
  2. Gaussian Process Regression: On synthetic benchmarks designed to produce multimodal posteriors, the method outperformed others in data-sparse regions (the tails) by correctly shifting weight toward more robust model approximations.
  3. 2016 U.S. Presidential Election Polls: Using a longitudinal dataset of 50 states over 40 weeks, the hierarchical approach provided stable and accurate back-tests. It successfully handled state-level correlations and the scarcity of data early in the campaign cycle.

Limitations and Future Directions

While powerful, the method introduces computational overhead due to the MCMC sampling required for the weights. Additionally, the user must currently specify which features the weights should depend on; automating this feature selection for weights is a promising area for future research. Finally, while the current framework is optimized for log-predictive scores, extending it to other scoring rules like Mean Squared Error remains an open challenge.

Conclusion

Bayesian Hierarchical Stacking represents a significant step forward in model averaging. By allowing for input-dependent weights and utilizing hierarchical priors to manage the bias-variance tradeoff, it provides a robust framework for building ensembles that are accurate, well-calibrated, and resilient to shifts in data distribution.

Summary updated

BibTeX
@inproceedings{yao2021bayesian,
  author = {Yuling Yao and Gregor Pirs and Aki Vehtari and Andrew Gelman},
  title = {{Bayesian hierarchical stacking}},
  booktitle = {Bayesian},
  year = {2021},
  doi = {10.1214/21-ba1287},
}
Figure