Skip to main content

GPU-accelerated statistical methods with sklearn-compatible API

Project description

statgpu

PyPI version Python versions License GitHub stars Downloads

GPU-accelerated statistical methods with sklearn-compatible API.

Why statgpu?

Feature statgpu sklearn glmnet (R)
GPU acceleration ✅ CuPy + PyTorch
7 GLM families Partial
10 penalty types ✅ (SCAD, MCP, Group) L1/L2 only L1/L2 only
sklearn API
Debiased Lasso inference
Cross-backend ✅ NumPy/CuPy/Torch NumPy only R only

Quick Start

import numpy as np
from statgpu.linear_model import Lasso, LassoCV, PenalizedGLM_CV

# Lasso with cross-validation
X, y = np.random.randn(1000, 50), np.random.randn(1000)
model = LassoCV(cv=5).fit(X, y)
print(f"Optimal alpha: {model.alpha_:.4f}")

# Penalized GLM: Poisson + SCAD on GPU
model = PenalizedGLM_CV(
    loss="poisson", penalty="scad", cv=5, device="cuda"
).fit(X, y_counts)

Documentation

Features

  • 🚀 3 Backends: NumPy (CPU), CuPy (CUDA), PyTorch (CUDA) — automatic device selection
  • 🔧 sklearn-compatible: fit/predict/score API, sklearn.base.clone() supported
  • 📊 7 GLM Families: squared_error, logistic, poisson, gamma, inverse_gaussian, negative_binomial, tweedie
  • 🔥 10 Penalties: l1, l2, elasticnet, scad, mcp, adaptive_l1, group_lasso, group_mcp, group_scad
  • 6 Solvers: exact, newton, lbfgs, irls, fista, fista_bb — solver="auto" selects optimal
  • 🧮 Inference: HC0-HC3/HAC robust SE, debiased Lasso, bootstrap, simultaneous CI
  • 📈 Nonparametric: KDE, kernel regression, B-splines, GAM
  • 🧬 Unsupervised: PCA, KMeans, DBSCAN, GMM, UMAP, t-SNE (12 classes)
  • 📐 Distributions: 15 distributions across 3 backends via get_distribution()API docs
  • 🧪 Multiple Testing: adjust_pvalues + combine_pvalues + permutation_test
  • 🔥 Cross-Validation: PenalizedGLM_CV (all 7 losses × 10 penalties), RidgeCV, LassoCV, ElasticNetCV

Implemented Methods

Full method list with solvers, penalties, link functions →

Category Classes Highlights
Regression & GLM 12 classes LinearRegression, Ridge, Lasso, ElasticNet, Logistic, Poisson, Gamma, InvGauss, NB, Tweedie, Ordered models
Penalized GLM 8 classes PenalizedGLM + 7 family wrappers (Linear, Logistic, Poisson, Gamma, InvGauss, NB, Tweedie) × 10 penalties × 6 solvers
Cross-Validation 6 classes RidgeCV, LassoCV, ElasticNetCV, LogisticCV, PenalizedGLM_CV, CoxPHCV
ANOVA 1 function f_oneway — GPU-accelerated
Covariance 3 classes EmpiricalCovariance, LedoitWolf, OAS
Panel Data 2 classes PanelOLS, RandomEffects
Nonparametric 5 classes KernelRidge, KernelRidgeCV, pairwise_kernels, bspline_basis, natural_cubic_spline_basis
Semiparametric 1 class GAM (penalized B-splines + GCV)
Unsupervised 12 classes PCA, SVD, NMF, UMAP, t-SNE, KMeans, DBSCAN, GMM, AgglomerativeClustering
Survival 1 class CoxPH (Breslow/Efron ties, robust SE)
Feature Selection 2 functions fixed-X / model-X knockoff filters
Multiple Testing 3 functions adjust_pvalues, combine_pvalues, permutation_test

Installation

# CPU only
pip install statgpu

# With GPU support (choose by CUDA major version)
# CUDA 11.x runtime:
pip install statgpu[gpu11]

# CUDA 12.x runtime:
pip install statgpu[gpu12]

# With PyTorch backend (CUDA 11.x)
pip install statgpu[torch]

# Development
pip install statgpu[dev]

# Formula interface
pip install statgpu[formula]

Quick Start

import numpy as np
from statgpu.inference import norm, poisson
from statgpu.linear_model import LinearRegression, PenalizedGLM_CV
from statgpu import adjust_pvalues, combine_pvalues

# Generate data using statgpu distributions (scipy-compatible API)
X = norm.rvs(size=(10000, 100))
y = X @ norm.rvs(size=100) + norm.rvs(size=10000) * 0.5

# Linear regression with GPU
model = LinearRegression(device='cuda')
model.fit(X, y)
print(f"R²: {model.score(X, y):.4f}")

# Penalized GLM with cross-validation
y_pois = poisson.rvs(mu=np.exp(X[:, :5] @ np.ones(5) * 0.1), size=X.shape[0])
cv_model = PenalizedGLM_CV(
    loss="poisson", penalty="elasticnet", l1_ratio=0.5,
    cv=5, device="cpu",
)
cv_model.fit(X[:, :5], y_pois)
print(f"Best alpha: {cv_model.alpha_:.4f}")

# Multiple-testing correction
reject, pvals_adj = adjust_pvalues(np.array([0.003, 0.02, 0.5]), method='bh')

# Global p-value combination
stat, p_global = combine_pvalues(np.array([0.01, 0.07, 0.03, 0.40]), method='fisher')

Device Control

import statgpu as sg

# Global setting
sg.set_device('cuda')  # Force GPU
sg.set_device('cpu')   # Force CPU
sg.set_device('auto')  # Auto-detect (default)

# Per-model setting
from statgpu.linear_model import LinearRegression
model = LinearRegression(device='cuda', n_jobs=4)

Benchmark Results (RTX 4090)

Full report: dev/tests/_bench_realdata_report.md

Test environment: RTX 4090 (24GB), CuPy 14.1.0, PyTorch 2.8.0+cu128, scikit-learn 1.8.0, statsmodels 0.14.6, lifelines 0.30.3

Real-Data Performance

Module Dataset n p Best Speedup Precision
Poisson GLM freMTPL2 678K 42 196.9x vs sklearn coef_corr=1.000000
Gamma GLM synthetic 678K 42 97.9x vs sklearn coef_corr=0.9995
CoxPH synthetic 1.9K 500 1.2x vs CPU coef_corr=1.000
adjust_pvalues (BH) synthetic 1M 0.55x 100% agreement
PenalizedPoisson(L1) freMTPL2 678K 42 OK
PenalizedCoxPH(L2) synthetic 1.9K 500 C-index match

Precision Summary

Module Metric Result
Poisson GLM coef correlation vs sklearn 1.000000 (full freMTPL2)
Gamma GLM coef correlation vs sklearn 0.9995
CoxPH coef correlation vs lifelines 1.000
adjust_pvalues (BH) reject agreement vs statsmodels 100% (100K to 5M p-values)
Penalized (L1/L2) self-consistency C-index match across penalties

Requirements

  • Python >= 3.8
  • NumPy >= 1.20
  • CuPy (optional, for GPU; choose wheel matching CUDA major version)
    • CUDA 11.x: cupy-cuda11x
    • CUDA 12.x: cupy-cuda12x
  • CUDA runtime compatible with selected CuPy wheel

License

Apache License 2.0 — see LICENSE for details.

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

statgpu-0.1.0.tar.gz (486.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

statgpu-0.1.0-py3-none-any.whl (586.0 kB view details)

Uploaded Python 3

File details

Details for the file statgpu-0.1.0.tar.gz.

File metadata

  • Download URL: statgpu-0.1.0.tar.gz
  • Upload date:
  • Size: 486.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.9

File hashes

Hashes for statgpu-0.1.0.tar.gz
Algorithm Hash digest
SHA256 9dc6391e6b2eef0f084b314abd4b8ce68ce8cdaab70171dc9d33b2bda2b94179
MD5 8826780744411933cad7cdb751567ff2
BLAKE2b-256 4a73dbe689fd0688f2177639d1372a9489ac72b05998a31993b98e27b9a25583

See more details on using hashes here.

File details

Details for the file statgpu-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: statgpu-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 586.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.9

File hashes

Hashes for statgpu-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 570c60d71ec75dc8c3804a1f127c8af58b551a9cd420bd44a9670551eca0ee79
MD5 c88b7dafab94e9ca023f49621c5e83a2
BLAKE2b-256 9c15462cd20fe60b6d533a51510f3d07af298289e2580694bef9719c694e6cee

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