PyTorch implementation of Gibbs sampler for Bayesian linear regression with horseshoe prior.
The horseshoe prior is a sparsity-inducing prior useful for high-dimensional linear regression problems where many coefficients are expected to be zero. This package implements efficient Gibbs sampling methods for Bayesian sparse regression with horseshoe priors using PyTorch.
- C. M. Carvalho, N. G. Polson, and J. G. Scott, "The horseshoe estimator for sparse signals," Biometrika 97, 465 (2010).
- A. Bhattacharya, A. Chakraborty, and B. K. Mallick, "Fast sampling with Gaussian scale-mixture priors in high-dimensional regression," Biometrika 103, 985 (2016).
- H. Rue, "Fast sampling of Gaussian Markov random fields," J. R. Stat. Soc. Series B Stat. Methodol. 63, 325 (2001).
- E. Makalic and D. F. Schmidt, "A simple sampler for the horseshoe estimator," IEEE Signal Process. Lett. 23, 179 (2016).
import torch
from horseshoe_gibbs_torch import HorseshoeGibbsSampler
# Create synthetic sparse regression problem
n = 300 # Number of data samples
p = 200 # Number of features
X = torch.randn(n, p)
w = torch.randn(p) * torch.bernoulli(torch.ones(p) * 0.1) # Sparse coefficients
y = X @ w
# Run the horseshoe sampler
w_sample = HorseshoeGibbsSampler(X, y).sample(num_mcs=100)
# Calculate RMSE
rmse = ((w_sample - w)**2).mean().sqrt()HorseshoeGibbsSampler(
train_X: torch.Tensor,
train_Y: torch.Tensor,
weight: Optional[torch.Tensor] = None,
sigma2: Optional[torch.Tensor] = None,
tau2: Optional[torch.Tensor] = None,
lamb2: Optional[torch.Tensor] = None,
a_sigma2: float = 1.0,
b_sigma2: float = 1.0
)Gibbs sampler for Bayesian linear regression with horseshoe prior.
FastMultivariateGaussianMixtureBCM(
X: torch.Tensor,
y: torch.Tensor,
D_diag: torch.Tensor,
sigma2: torch.Tensor,
device: Optional[torch.device] = None
)Fast sampler for Gaussian posterior using the Bhattacharya-Chakraborty-Mallick algorithm. Fast when p >> n.
FastMultivariateGaussianMixtureRue(
XtX: torch.Tensor,
Xty: torch.Tensor,
D_diag: torch.Tensor,
sigma2: torch.Tensor,
device: Optional[torch.device] = None
)Fast sampler for Gaussian posterior using Rue's algorithm.
# Clone the repository
git clone https://github.com/mory22k/horseshoe-gibbs-torch.git
cd horseshoe-gibbs-torch
# If you are using mise, trust and install the dependencies
mise trust
mise install
# Set up development environment
uv syncThis project uses:
misefor development environment managementtaskfor running common development tasksuvfor python package managementrufffor linting and formattingmypyfor type checking
# Format code
task format
# Check code style
task check
# Format code style
task format
# Fix autofixable issues
task fix
# Prepare and commit
task commitThis project is licensed under the MIT License. See the LICENSE file for details.