Skip to main content

CI PyPI DOI Hugging Face Open in Colab arXiv

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 the FAQ.
  • 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 was built

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.

Simulation (figure, top)

  • Sizes: the numbers of predictors d, random effects q, groups m and rows per group ni are drawn first.
  • Priors: the prior families and their scales are drawn next.
  • Parameters: the fixed effects β, random-effect SDs σ and correlations R are drawn from these priors.
  • Predictors: X is sampled from synthetic tables, built from random structural causal models as in TabPFN/TabICL, or subsampled from hundreds of real tabular datasets.
  • Outcomes: y is drawn from the GLMM, given X and the parameters.

Training (figure, bottom)

  • Summaries: Σl condenses all rows per group into a vector, and Σg condenses these vectors into one summary s. Both ignore the order of rows and groups.
  • Augmentation: efficient analytical estimates and the prior join the summaries.
  • Posterior: normalizing flows turn random noise u into posterior draws. Πg draws β, σ and R from s; Πl draws each group's αi from its summary and a draw of Πg.
  • Objective: all networks are trained end-to-end to optimize the probability of the true parameters under the proposed posterior.

A GLMM fit is then a forward pass through the trained networks. Metropolis-Hastings refines its draws to grant the estimated posterior asymptotic exactness.

FAQ

What can MetaBeta fit?
  • Models: Gaussian, Bernoulli (0/1) and Poisson GLMMs with one grouping factor.
  • Data: at least 5 groups and 5 rows per group. The networks were trained on up to 200 groups, 150 rows per group and 3,000 rows; larger datasets are fitted with a warning.
  • 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.
Do I need a GPU?

No. A fit takes 0.2 to 1 second on a laptop CPU, and about 0.2 seconds on an H100 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.

What do the diagnostics mean?

They are in the summary's last line and in result.safeguards:

  • acceptance: the share of the networks' draws that Metropolis-Hastings accepts.
  • min ESS: the smallest effective sample size (ESS).
  • PSIS k̂: how well the networks' 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 the acceptance, the ESS or MAP z crosses its threshold. It also warns when a prior lies outside the range the networks were trained on.

When should I fall back to NUTS?

When sample still warns after more draws (n_samples), or warns about the prior:

  • Random-walk moves (random_walk=True): usually repair the intervals, but the warning stays, so check the fit against NUTS.
  • A prior outside the trained range: fit with NUTS, or choose a prior inside the range.

nuts refits a result with NUTS, warm-started from its draws:

# pip install "metabeta[nuts]"
refit = mb.nuts(result)
print(mb.posteriorSummary(refit))

02-families compares a Poisson fit with NUTS.

Contributing

Issues and pull requests are welcome; see CONTRIBUTING.md.

Citing

@misc{kipnis.2026,
  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}
}

All versions share the DOI 10.5281/zenodo.23458113.

License

Metadata

Release files for metabeta 0.8.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.8.0
File Size Uploaded Provenance Actions
metabeta-0.8.0.tar.gz 813.7 kB With provenance Details for metabeta-0.8.0.tar.gz

Built distribution (wheel)

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

Total release size: 1.2 MB

Release files / metabeta-0.8.0.tar.gz

Download URL metabeta-0.8.0.tar.gz
Size 813.7 kB
Tags Source
SHA-256 checksum
How to use checksums
606629764150669faf02519e829aec3400f2f3fa479e422e664cc751d2e6a80d
BLAKE2b-256 checksum
How to use checksums
3f4a9c9f0ee06059252d7866bd8bc95028635b562c571cfa3872773ec8696afa
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

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 Nov 22, 2026.

Transparency log

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

Download URL metabeta-0.8.0-py3-none-any.whl
Size 370.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
a65bbfbb352279755fe98970b85e3058af617f01b24df9cc835cc6f1d9cf379b
BLAKE2b-256 checksum
How to use checksums
7f0e7054b258018a65620d659fa3a34a761ba3cd8b5a608882309f677e3758d1
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

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 Nov 22, 2026.

Transparency log

Security for metabeta 0.8.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.8.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 Nov 22, 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