Skip to main content

Amortized inference for Bayesian GLMMs

Project description

metabeta

Python 3.12 PyTorch uv PyPI Release checks Hugging Face License: CC BY-NC-SA 4.0

metabeta is an amortized model for Bayesian generalized linear mixed-effects models (GLMMs). Given a grouped dataset, a model formula, and an optional prior specification, it returns posterior samples for fixed effects, random effects, variance components, and correlations with the speed and batching capabilities of a PyTorch forward pass.

  • Supports Normal, Bernoulli, and Poisson outcomes.
  • Accepts convenient lme4-style formulas.
  • Conditions on prior family and hyperparameters at inference time.
  • Supports batched prior-sensitivity analysis in one sample() call.
  • Includes diagnostics for posterior contraction and prediction accuracy.
  • Ships the pretrained inference API, plotting helpers, diagnostics, and demo notebooks.
  • Keeps simulation, training, reference-method evaluation, and experiment scripts in the source repository for research workflows.

Pretrained checkpoints are hosted on Hugging Face and are downloaded automatically on first use.

Quick start

Install the pretrained-model inference package from PyPI:

pip install metabeta

For local development, install from source with uv:

git clone https://github.com/adkipnis/metabeta.git
cd metabeta
uv sync
uv pip install -e .

The default PyPI install is for pretrained-model inference. To reproduce training runs, generate synthetic datasets, benchmark against reference methods, or extend the package, clone the repository and install the optional research dependencies:

git clone https://github.com/adkipnis/metabeta.git
cd metabeta
uv sync --extra research --group simulation --dev
uv pip install -e ".[research]"

Run posterior inference on a grouped dataframe:

import statsmodels.api as sm
from metabeta.models.api import Api

mb = Api.from_pretrained("normal")
df = sm.datasets.get_rdataset("sleepstudy", "lme4").data
result = mb.sample(df, formula="Reaction ~ Days + (Days | Subject)", n_samples=1000)
print(mb.posteriorSummary(result))

See demos/intro.ipynb for the full sleepstudy walkthrough and demos/priors.ipynb for an exemplary prior-sensitivity analysis.

From simulation to deployment

Each pretrained model is built through the same pipeline, from a dataset simulator to a checkpoint that can be loaded by the public API.

  1. Define the model family. Choose the likelihood, GLMM dimensions, group structure, covariate styles, and prior families covered by a checkpoint.
  2. Generate training data. Simulate hierarchical datasets and posterior reference targets across the configured design space.
  3. Format inputs. Convert grouped tabular data into padded tensors, masks, and prior encodings shared by training and inference.
  4. Train amortized posteriors. Use set-transformers (to learn low-dimensional permutation-invariant summaries of the datasets) and conditional coupling flows to approximate posteriors for fixed effects, variance parameters, correlations, and group-wise random effects.
  5. Validate behavior. Check parameter recovery, credible interval coverage, simulation-based calibration, posterior predictive accuracy and run comparisons against reference methods.
  6. Package checkpoints. Bundle weights, configs, routing metadata, and preprocessing expectations into a joint checkpoint.
  7. Deploy through the API. Load joint checkpoints with Api.from_pretrained(...) for convenient posterior estimation and diagnostics.

Repository map

Path Contents
metabeta/analytical/ GLMM analytical fits and helpers
metabeta/configs/ model and preset configuration files
metabeta/datasets/ preprocessing and source-specific dataset fetchers
metabeta/evaluation/ parameter recovery, coverage, SBC, posterior predictive checks and summary metrics
metabeta/models/ model API, set transformers, normalizing flows
metabeta/plotting/ plot functions for posterior samples, recovery, calibration, and runtime
metabeta/posthoc/ experimental post-hoc posterior refinement helpers
metabeta/simulation/ synthetic hierarchical data generation and reference fitting with PyMC
metabeta/training/ training entry point and checkpoint loop
metabeta/utils/ config, dataloading, routing, IO, and shared helper code
experiments/ reproducible experiment scripts grouped by package area
tests/ pytest suite for models, simulation, evaluation, datasets, and utils
demos/ demo notebooks

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

metabeta-0.4.3.tar.gz (3.4 MB view details)

Uploaded Source

Built Distribution

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

metabeta-0.4.3-py3-none-any.whl (271.6 kB view details)

Uploaded Python 3

File details

Details for the file metabeta-0.4.3.tar.gz.

File metadata

  • Download URL: metabeta-0.4.3.tar.gz
  • Upload date:
  • Size: 3.4 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for metabeta-0.4.3.tar.gz
Algorithm Hash digest
SHA256 8f049d42e4df055cd6c306df18f04c5f2748f1a6e4f969cebcf88d84e7705c38
MD5 8b865553289988a2c0ac1b84c3e03c54
BLAKE2b-256 ec5bb06ce021348e60dd74163cc8d2aaccc2d548eeb8e89a9ab4c0491f50232b

See more details on using hashes here.

Provenance

The following attestation bundles were made for metabeta-0.4.3.tar.gz:

Publisher: publish-pypi.yml on adkipnis/metabeta

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file metabeta-0.4.3-py3-none-any.whl.

File metadata

  • Download URL: metabeta-0.4.3-py3-none-any.whl
  • Upload date:
  • Size: 271.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for metabeta-0.4.3-py3-none-any.whl
Algorithm Hash digest
SHA256 e4f90a153013aa696a47c33de5614017b1b984cb0ff063b0caa03a10d4d5a11d
MD5 1059c32809d70559816f88fc61665a4b
BLAKE2b-256 261a04e77bf88da9bb8bd250a4b5a204da5ca1f4e5264281a4eb6d1d06d4d521

See more details on using hashes here.

Provenance

The following attestation bundles were made for metabeta-0.4.3-py3-none-any.whl:

Publisher: publish-pypi.yml on adkipnis/metabeta

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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