Skip to main content

Utility functions for extracting log-probabilities, parameter transforms, and Fisher information from NumPyro models.

Project description

numpyro-inferutils

Small utility functions for inference with NumPyro models.

This package provides lightweight helpers for:

  • extracting log-prior and log-likelihood from NumPyro models,
  • working with constrained / unconstrained parameter spaces,
  • computing Fisher information matrices from NumPyro models with independent Gaussian likelihoods.

Installation

pip install numpyro-inferutils

Quick examples

Log-prior and log-likelihood

from numpyro_inferutils import build_logprob_functions

logprior, loglik = build_logprob_functions(model)

theta = {
    "x": 0.0,
    "y": 1.2,
}

lp = logprior(theta)
ll = loglik(theta)
  • logprior(theta) sums log-probabilities from non-observed sample sites.
  • loglik(theta) sums log-probabilities from observed sample sites.
  • Contributions added via numpyro.factor are treated as part of the log-likelihood.

Constrained ↔ unconstrained parameters

from numpyro_inferutils.transforms import to_unconstrained_dict

params_constrained = {"sigma": 2.0}
params_unconstrained = to_unconstrained_dict(
    model,
    params_constrained,
    keys=["sigma"],
)

This inspects the model’s sample-site supports and applies the appropriate inverse transforms using

biject_to(site["fn"].support)

Seeding and substituting parameters

from jax import random
from numpyro_inferutils.transforms import seed_and_substitute

rng_key = random.PRNGKey(0)

model_sub = seed_and_substitute(
    model,
    params_dict={"sigma": 0.5},
    param_space="unconstrained",
    rng_key=rng_key,
)
  • If param_space="unconstrained", parameters are interpreted as living in unconstrained space and mapped to constrained space using NumPyro’s internal unconstraining reparameterization.
  • If param_space="constrained", values are substituted directly.

Fisher information (independent Gaussian likelihood)

from numpyro_inferutils.fisher import information_from_model_independent_normal

info = information_from_model_independent_normal(
    model=model,
    pdic={"w": 1.0, "b": 0.0},
    mu_name="mu",
    observed=y_obs,
    keys=["w", "b"],
    sigma_sd=sigma,
)

F = info["fisher"]

The Fisher matrix is approximated as

F ≈ Jᵀ J,

where J_ij = ∂r_i / ∂θ_j and

r = (y − μ(θ)) / σ.

Both constrained and unconstrained parameterizations are supported.


License

MIT License.

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

numpyro_inferutils-0.1.1.tar.gz (7.9 kB view details)

Uploaded Source

Built Distribution

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

numpyro_inferutils-0.1.1-py3-none-any.whl (8.1 kB view details)

Uploaded Python 3

File details

Details for the file numpyro_inferutils-0.1.1.tar.gz.

File metadata

  • Download URL: numpyro_inferutils-0.1.1.tar.gz
  • Upload date:
  • Size: 7.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.11

File hashes

Hashes for numpyro_inferutils-0.1.1.tar.gz
Algorithm Hash digest
SHA256 6b87f571955aabb7e4795cf142ccedc0203090d3f0210147f78c0a557d8cd769
MD5 4eb5763b25d92931b87f334ad4ea5c4b
BLAKE2b-256 85bb51fb5ba423c3d7ab97e54c2fb1d288536fe44bb1055d5c61e5047d92706f

See more details on using hashes here.

File details

Details for the file numpyro_inferutils-0.1.1-py3-none-any.whl.

File metadata

File hashes

Hashes for numpyro_inferutils-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 7b174e292760e760485bc019b73f96304a2700f35b4770559c9ff447feff7f9a
MD5 7e59f85bf50cd5b4db1054c061b2a4b6
BLAKE2b-256 be7c43bff7e3e7d7b10014981e38eb438e80fc38be859079f994202ac7085e82

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