Skip to main content

Amortized inference for Bayesian GLMMs

Project description

metabeta

Python 3.12 PyTorch uv 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

For local development, install from source with uv:

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

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.

Development and research workflows

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]"

This enables the repository-only paths for synthetic data generation, model training, benchmarking against reference methods, simulation-based calibration studies, and experiment scripts. The simulation dependency group installs the GitHub-only scamd dependency used by SCM dataset generation. The metabeta.posthoc package is included for experimental posterior refinement, but it is not part of the production pretrained API and may change without a deprecation window.

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.2.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.2-py3-none-any.whl (271.8 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: metabeta-0.4.2.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.2.tar.gz
Algorithm Hash digest
SHA256 8ad742846082a751c42bdd6449a33658f1cf48f5255c375f30c01df47d5add65
MD5 ca446b158e11965a248112d8327c7718
BLAKE2b-256 2f7659c32a1759e861a2edfdc2a554199df2d07d5a9d46810357d9c043d83924

See more details on using hashes here.

Provenance

The following attestation bundles were made for metabeta-0.4.2.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.2-py3-none-any.whl.

File metadata

  • Download URL: metabeta-0.4.2-py3-none-any.whl
  • Upload date:
  • Size: 271.8 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.2-py3-none-any.whl
Algorithm Hash digest
SHA256 948a9f3ae23e168908120515f433a0e6d24a504e00364d200645d480d36a8cbe
MD5 df022e54c8e8c93453bd6f08945fd899
BLAKE2b-256 59d07c544f4dbdef42e9ded5f519ffa36fd0dc2280941e22c8f07d633bb60017

See more details on using hashes here.

Provenance

The following attestation bundles were made for metabeta-0.4.2-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