Is Gibbs sampling faster than Hamiltonian Monte Carlo on GLMs?

Steven Luu, Zuheng Xu, Nikola Surjanovic, Miguel Biron-Lattes, Trevor Campbell, Alexandre Bouchard‐Côté · DOI

Abstract

The Hamiltonian Monte Carlo (HMC) algorithm is often lauded for its ability to effectively sample from high-dimensional distributions. In this paper we challenge the presumed domination of HMC for the Bayesian analysis of GLMs. By utilizing the structure of the compute graph rather than the graphical model, we show a reduction of the time per sweep of a full-scan Gibbs sampler from $O(d^2)$ to $O(d)$, where $d$ is the number of GLM parameters. A simple change to the implementation of the Gibbs sampler allows us to perform Bayesian inference on high-dimensional GLMs that are practically infeasible with traditional Gibbs sampler implementations. We empirically demonstrate a substantial increase in effective sample size per time when comparing our Gibbs algorithms to state-of-the-art HMC algorithms. While Gibbs is superior in terms of dimension scaling, neither Gibbs nor HMC dominate the other: we provide numerical and theoretical evidence that HMC retains an edge in certain circumstances thanks to its advantageous condition number scaling. Interestingly, for GLMs of fixed data size, we observe that increasing dimensionality can stabilize or even decrease condition number, shedding light on the empirical advantage of our efficient Gibbs sampler.

The High-Dimensional Challenge in GLMs

In Bayesian statistics, Generalized Linear Models (GLMs) serve as a fundamental tool for data analysis. However, as datasets grow in complexity, the computational cost of sampling from posterior distributions becomes a significant bottleneck. For years, the common wisdom has been that Gibbs sampling scales poorly as the number of parameters ($d$) increases. Traditional Gibbs implementations recompute the entire likelihood for every coordinate update, leading to a full sweep complexity of $O(d^2)$.

Consequently, the field largely migrated toward Hamiltonian Monte Carlo (HMC) and sophisticated Probabilistic Programming Languages (PPLs) like Stan. HMC utilizes gradient information to navigate high-dimensional spaces more efficiently than simple random-walk Metropolis or traditional Gibbs. However, HMC is not a silver bullet; it requires intensive manual tuning and expensive "warm-up" phases to learn the geometry of the posterior.

From Graphical Models to Compute Graphs

This research introduces Compute Graph Gibbs (CGGibbs), which reframes the implementation of the Gibbs sampler. The core innovation lies in moving beyond the "coarse-grained" dependencies of traditional graphical models (used by tools like BUGS or JAGS) to the fine-grained operations of a "compute graph."

In a GLM, the likelihood depends on a linear predictor: $x_i^\top \theta$. In a standard implementation, updating a single parameter $ heta_j$ requires re-multiplying the entire vector, an $O(d)$ operation. By caching the value of the linear predictor, CGGibbs can update $\theta_j$ by simply subtracting the old contribution ($x_{ij}\theta_j$) and adding the new one ($x_{ij}\theta_j'$). This reduces the cost of a single parameter update to $O(1)$, and a full sweep across all $d$ parameters to $O(d)$.

Empirical Scaling Analysis

The theoretical $O(d)$ speedup is clearly visible when comparing CGGibbs to traditional PPLs. While popular tools like JAGS and MultiBUGS exhibit the expected quadratic slowdown as dimensionality increases, CGGibbs maintains a linear scaling profile. This efficiency allows for Bayesian inference on high-dimensional models that were previously considered the exclusive domain of HMC.

Scaling of CGGibbs vs. traditional Gibbs implementations.

Performance on Real-World Datasets

To evaluate the practical impact of this optimization, the researchers benchmarked CGGibbs against Stan’s No-U-Turn Sampler (NUTS) across 12 diverse high-dimensional datasets, including gene expression data (e.g., ALLAML, Prostate_GE) and text classification (e.g., BASEHOCK).

In 11 out of 12 datasets, CGGibbs outperformed NUTS. In the most favorable scenarios, CGGibbs achieved an Effective Sample Size (ESS) per second up to 300 times higher than state-of-the-art HMC. This advantage is partly due to Gibbs sampling's natural invariance to axis-aligned stretching; it essentially performs "optimal diagonal preconditioning" automatically, whereas HMC must spend significant computational resources learning these scales during warm-up.

Comparative performance of CGGibbs and NUTS across benchmark datasets.

The Dimension vs. Condition Number Trade-off

Despite the success of CGGibbs, the paper provides a nuanced view: neither algorithm strictly dominates. The performance of MCMC is governed by a tug-of-war between dimensionality ($d$) and the condition number ($\kappa$), which measures the correlation or "stretch" of the distribution.

While Gibbs scales better with $d$, HMC scales better with $\kappa$. In datasets where features are highly correlated, HMC’s ability to slice through the geometry of the posterior remains superior. However, the authors observed a "blessing of dimensionality" in many real-world GLMs: as more features are added ($d \gg n$), the condition number often stabilizes or even improves. This creates a sweet spot where the $O(d)$ scaling of CGGibbs makes it vastly superior to HMC.

Conclusion

By optimizing the underlying compute graph, Gibbs sampling can be transformed from an $O(d^2)$ process into a highly efficient $O(d)$ algorithm for GLMs. While HMC remains powerful for highly correlated distributions, CGGibbs offers a robust and often much faster alternative for high-dimensional Bayesian inference. Future work may lie in hybrid "block HMC-within-Gibbs" algorithms that leverage compute-graph optimizations to handle both high dimensionality and complex correlations simultaneously.

Summary updated

BibTeX
@inproceedings{luu2025is,
  author = {Steven Luu and Zuheng Xu and Nikola Surjanovic and Miguel Biron-Lattes and Trevor Campbell and Alexandre Bouchard‐Côté},
  title = {{Is Gibbs sampling faster than Hamiltonian Monte Carlo on GLMs?}},
  booktitle = {International Conference on Artificial Intelligence and Statistics},
  year = {2025},
  url = {https://openreview.net/forum?id=s8qmRdRpWR},
}
Figure