Skip to main content

Spike-and-Slab Variational Bayes for Linear and Logistic Regression

Project description

sparsevb

A Python package for Spike-and-Slab Variational Bayes for Linear and Logistic Regression.

Description

This package provides variational Bayesian algorithms to perform scalable variable selection for sparse, high-dimensional linear and logistic regression models. The algorithms are implemented in C++ for performance and exposed to Python using Cython.

It is a direct port of the R sparsevb package, using the same underlying C++ logic but with a Pythonic API.

Installation

To install the package from source:

pip install .

Usage

Here is a basic example of how to use the package:

import numpy as np
from sparsevb import svb_fit_linear, svb_fit_logit

# Generate some synthetic data
np.random.seed(42)
n_samples, n_features = 200, 20
X = np.random.randn(n_samples, n_features)
beta_true = np.zeros(n_features)
beta_true[:3] = [1.5, -2.0, 3.0]
intercept_true = 5.0
y = X.dot(beta_true) + intercept_true + 0.1 * np.random.randn(n_samples)

# Fit a sparse linear regression model
res = svb_fit_linear(X, y, intercept=True)
print("Estimated coefficients (mu * gamma):", res['mu'] * res['gamma'])
print("Estimated intercept:", res['intercept'])
print("Estimated noise variance (noise_sd):", res['noise_sd'])

# For logistic regression (binary classification)
logits = X.dot(beta_true) + intercept_true
probs = 1 / (1 + np.exp(-logits))
y_binary = (np.random.rand(n_samples) < probs).astype(float)

res_logit = svb_fit_logit(X, y_binary, intercept=True)
print("Estimated logistic coefficients:", res_logit['mu'] * res_logit['gamma'])
print("Estimated logistic intercept:", res_logit['intercept'])

Features

  • Prioritized Initialization: Uses Ridge and Lasso (via scikit-learn) to provide robust starting points.
  • Intercept Handling: Built-in support for model intercepts.
  • Noise Estimation: Automatic estimation of residual noise for linear models.
  • Same Core Logic: Uses the exact same C++ algorithms as the original R package for reliable results.

Dependencies

  • Python >= 3.8
  • NumPy >= 1.20.0
  • SciPy >= 1.6.0
  • scikit-learn >= 1.0.0
  • Cython >= 0.29.0

License

This project is licensed under the GNU General Public License v3.0.

References

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

sparsevb-0.1.1.tar.gz (1.7 MB view details)

Uploaded Source

File details

Details for the file sparsevb-0.1.1.tar.gz.

File metadata

  • Download URL: sparsevb-0.1.1.tar.gz
  • Upload date:
  • Size: 1.7 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.3

File hashes

Hashes for sparsevb-0.1.1.tar.gz
Algorithm Hash digest
SHA256 325ad1a7925a146267918222af94c3a0ba189107bfd83eef47f2506d036dd622
MD5 1e591b00f88a6cc9dfbbefc659d6db1f
BLAKE2b-256 137c9da7ea5601db28fd1edc6331df3730b687fef08a98e8c97ac67c1f9e6d6c

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page