Skip to main content

PyPI CI arXiv Hugging Face Open in Colab License

metabeta

Bayesian inference for GLMMs (generalized linear mixed-effects models) in under a second:

  • Full posterior in one call for Gaussian, Bernoulli and Poisson outcomes: fixed effects, random effects, and their standard deviations (SDs) and correlations.
  • Priors as input: you can change them without retraining, fit several at once, and compare models using Bayes factors.
  • Asymptotically exact: Metropolis–Hastings refines the networks' posterior samples against the exact unnormalized posterior.
  • Diagnostics included: every fit reports diagnostics and suggests a remedy.
  • A familiar workflow: lme4-style tables, per-group effects, pair plots and posterior predictive checks.

Installation

pip install metabeta

The pretrained checkpoints download from Hugging Face on first use.

For development:

git clone https://github.com/adkipnis/metabeta && cd metabeta && uv sync

Quick start

Fit lme4's sleepstudy: reaction times of 18 subjects over 10 days of sleep restriction.

import pandas as pd
from metabeta import Api

df = pd.read_csv('https://vincentarelbundock.github.io/Rdatasets/csv/lme4/sleepstudy.csv')
mb = Api.from_pretrained('gaussian')
result = mb.sample(df, formula='Reaction ~ Days + (Days | Subject)')
print(mb.posteriorSummary(result))
Formula:  Reaction ~ Days + (Days | Subject)
n = 180   m = 18
...
Fixed Effects:
|           |    Mean |    SD |    2.5% |   50.0% |   97.5% |   P(>0) |   Contr. |
|-----------|---------|-------|---------|---------|---------|---------|----------|
| Intercept | 251.276 | 8.046 | 235.865 | 251.166 | 267.935 |   1.000 |    0.999 |
| Days      |  10.434 | 1.653 |   7.267 |  10.485 |  13.676 |   1.000 |    0.999 |

Standard Deviations:
|           |   Mean |    SD |   2.5% |   50.0% |   97.5% |
|-----------|--------|-------|--------|---------|---------|
| Intercept | 29.938 | 8.059 | 17.744 |  29.099 |  47.685 |
| Days      |  6.368 | 1.472 |  3.936 |   6.194 |   9.584 |
| Residual  | 25.752 | 1.535 | 22.652 |  25.714 |  28.829 |
...
draws = 1000   acceptance = 0.78   min ESS = 456   PSIS k̂ = 0.240   MAP z = 0.5   R² = 0.827
  • Units: ms, and ms per day for Days (lme4: 251.4 and 10.47).
  • Last line: the diagnostics are explained in How it works.
  • More: mb.rfxSummary(result) prints each subject's random effects, and mb.plotParameters(result) visualizes posterior draws. The tutorials cover posteriorDraws, groupDraws, posteriorPredictive and bayesFactor.

Tutorials

Notebook Content
01-quickstart First fit: summaries, plots and diagnostics Open in Colab
02-families Bernoulli and Poisson outcomes Open in Colab
03-priors Custom priors and Bayes factors Open in Colab

How it works

Top: training datasets are simulated from priors over the fixed effects, the random-effect SDs and their correlations. Bottom: summary networks condense each group, then all groups; flows map the summary and the prior to posterior draws.
  1. Training: the networks learn from simulated GLMM datasets with random sizes, priors and parameters (figure, top).
  2. Summary: Σl summarizes each group, and Σg summarizes across groups (figure, bottom).
  3. Posterior: normalizing flows turn the summary, analytical estimates and the prior into draws. Πg draws the fixed effects β, random-effect SDs σ and correlations R; Πl draws each group's random effects αi.
  4. Refinement: independence Metropolis–Hastings (IMH) uses the flow's draws as proposals.

Diagnostics, in result.safeguards and the summary's last line:

  • acceptance: the IMH acceptance rate.
  • min ESS: the smallest effective sample size (ESS).
  • PSIS k̂: how well the flow's draws match the posterior, by Pareto-smoothed importance sampling (PSIS); lower is better.
  • MAP z: the distance between the posterior mean and the maximum a posteriori (MAP) estimate, in posterior SDs.
  • R², AUC or Deviance, by family: how well the posterior predicts the data (in result.diagnostics).

sample warns when IMH acceptance, the ESS or MAP z crosses its threshold.

Research workflow

The repository also holds the code that builds the checkpoints. After uv sync, run these scripts in order (each takes --help):

  1. metabeta/simulation/generate.py simulates datasets.
  2. metabeta/training/train.py trains the networks.
  3. metabeta/evaluation/evaluate.py evaluates them, also against NUTS.
  4. scripts/build_ckpt.py bundles them into checkpoints.

FAQ

Do I need a GPU?

No. A fit takes 0.2 to 1 second on a laptop CPU, and about 0.1 seconds on a fast GPU. Colab's free runtimes are slower: one to a few seconds on CPU or T4. To use a GPU, pass device='cuda' to Api.from_pretrained.

When should I fall back to NUTS?

When sample still warns after you apply its suggested remedy, e.g. more draws or random-walk moves (random_walk=True). 02-families compares a Poisson fit with NUTS.

What can metabeta fit?
  • Models: Gaussian, Bernoulli (0/1) and Poisson GLMMs with one grouping factor.
  • Data: 5 to 200 groups, 5 to 150 rows per group, at most 3,000 rows.
  • Effects: at most 16 fixed and 5 random effects, intercepts included.
  • Random slopes: each must also be a fixed effect.
  • Enough groups: at least 4 more groups than fixed effects, and than random-effect variances and covariances.
  • Enough rows: at least 2 more rows per group than random effects.
  • Formulas: additive lme4 syntax, such as y ~ x1 + x2 + (1 + x1 | g).
  • Categories: string and categorical columns are dummy-coded, with the most frequent level as the reference (R uses the first).
  • Currently not supported: ||, /, interactions (:, *), functions (log(), factor(), C()), offsets and binomial trials. Transform the columns in the DataFrame instead; the error message says how.
On which scale are the estimates?
  • Summaries and plots use the data's scale: slopes per unit of each predictor, in the response's units (Gaussian), log-odds (Bernoulli) or log rates (Poisson).
  • x_scale='standardized' reports slopes per SD of the predictor instead.
  • Priors and raw draws (result.proposal) use a standardized scale, with predictors (and a Gaussian response) at mean 0 and SD 1.

Citing

@misc{kipnis2026prioramortized,
  title         = {Prior-Amortized In-Context {Bayesian} Inference for Generalized Linear
                   Mixed-Effects Models},
  author        = {Kipnis, Alex and Binz, Marcel and Schulz, Eric},
  year          = {2026},
  eprint        = {2609.24422},
  archivePrefix = {arXiv},
  primaryClass  = {cs.LG},
  url           = {https://arxiv.org/abs/2609.24422}
}

License

  • Code: Apache-2.0.
  • Pretrained weights, downloaded by from_pretrained: CC BY-NC-SA 4.0, non-commercial.

Metadata

Release files for metabeta 0.6.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for metabeta 0.6.0
File Size Uploaded Provenance Actions
metabeta-0.6.0.tar.gz 795.7 kB With provenance Details for metabeta-0.6.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for metabeta 0.6.0
File Interpreter ABI Platform Actions
metabeta-0.6.0-py3-none-any.whl Python 3 none any Details for metabeta-0.6.0-py3-none-any.whl

Total release size: 1.1 MB

Release files / metabeta-0.6.0.tar.gz

Download URL metabeta-0.6.0.tar.gz
Size 795.7 kB
Tags Source
SHA-256 checksum
How to use checksums
8863a51602ecb134c256627a173e976fa7a5f9ff3092e971eb1a281ad32a59cd
BLAKE2b-256 checksum
How to use checksums
6528291ca33641279e5fa57d90618d22947429fb87d32ade4c5f2e9b6ec8ef0c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 18, 2026.

Transparency log

Release files / metabeta-0.6.0-py3-none-any.whl

Download URL metabeta-0.6.0-py3-none-any.whl
Size 352.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
fcb81ee2db6ce26bc9ab13ef9dfbae9886e43746bf3fe3b43ae4b43e05a08ea1
BLAKE2b-256 checksum
How to use checksums
fed1a97851664092076b9b97511617dfdbd71533bfeaefe8f31ca365b868906e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 18, 2026.

Transparency log

Security for metabeta 0.6.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

Both files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for metabeta-0.6.0.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 18, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page