A convenient object-oriented wrapper for working with numpyro models.
Project description
An object-oriented interface to numpyro
This package provides a wrapper for working with numpyro models. It aims to remain model-agnostic, but package up a lot of the model fitting code to reduce repetition.
It is intended to make life a bit easier for people who are already familiar with Numpyro and Bayesian modelling. It is not intended to fulfil the same high-level wrapper role as packages such as brms. The user is still required to write the model from scratch. This is an intentional choice: writing the model from scratch takes longer and is less convenient for standard models like GLMs, but has the advantage that one gains a deeper insight into what is happening under the hood, and also it is more transparent to implement custom models that don't fit a standard mould.
Getting started
pip install numpyro-oop
The basic idea is that the user defines a new class that inherits from BaseNumpyroModel,
and defines (minimally) the model to be fit by overwriting the model method:
from numpyro_oop import BaseNumpyroModel
class DemoModel(BaseNumpyroModel):
def model(self, data=None, ...):
...
m1 = DemoModel(data=df, seed=42)
Then all other sampling and prediction steps are handled by numpyro-oop, or related libraries (e.g. arviz):
# sample from the model:
m1.sample()
# generate model predictions for the dataset given at initialization:
preds = m1.predict(...)
# generate an Arviz InferenceData object stored in self.arviz_data:
m1.generate_arviz_data()
A complete demo can be found in /scripts/demo_1.ipynb.
Requirements of the model method
Consider the following model method:
class DemoModel(BaseNumpyroModel):
def model(self, data=None, sample_conditional=True, ...):
...
if sample_conditional:
obs = data["y"].values
else:
obs = None
numpyro.sample("obs", dist.Normal(mu, sigma), obs=obs)
m1 = DemoModel(data=df, seed=42)
First, note that we can pass data as an optional kwarg that defaults to None.
If data is not passed to the model object directly then
model will automatically fall back to self.data, defined when the class instance is initialised.
Second, note the sample_conditional argument and subsequent pattern.
To use Numpyro's Predictive method, we need the ability to set any observed data
that a sampling distribution is conditioned upon (typically the likelihood) to be None.
See the Numpyro docs for examples.
Currently, numpyro-oop requires this to be implemented by the user in the model definition
in some way; a suggested pattern is shown above.
After the model is sampled, we can then generate posterior predictive distributions by
passing sample_conditional=False as a model_kwarg:
m1.predict(model_kwargs={"sample_conditional": False})
Using reparameterizations
One of the really neat features of Numpyro is the ability to define reparameterizations
of variables that can be applied to the model object
(see docs).
To use these with numpyro-oop, the user must overwrite the generate_reparam_config
method of BaseNumpyroModel to return a reparameterization dictionary:
def generate_reparam_config(self) -> dict:
reparam_config = {
"theta": LocScaleReparam(0),
}
return reparam_config
In this example, the node theta in the model will be reparameterized with a location/scale reparam
if use_reparam=True when the class instance is created.
This is handy, because you can then test the effect of your reparameterization by simply setting
use_reparam=False and re-fitting the model.
See examples/demo.ipynb for a
working example.
Roadmap after initial release
- include doctest, improved examples
- demo and tests for multiple group variables
- export docs to some static page (readthedocs or similar); detail info on class methods and attributes
- Contributor guidelines
- Fix type hints via linter checks
Development notes
Install the library with development dependencies via pip install -e ".[dev]".
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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file numpyro_oop-0.1.1.tar.gz.
File metadata
- Download URL: numpyro_oop-0.1.1.tar.gz
- Upload date:
- Size: 13.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.8
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
15eb8cdd036204fbb577b98370b6c8aefb98cc34c2c56972a477e077a684635b
|
|
| MD5 |
6eafb63788623cc2abc296c0e7381d6b
|
|
| BLAKE2b-256 |
41c0b22410ebae67fc491f674232ebf53c749b89dcf96d35bf1966b26f825b49
|
Provenance
The following attestation bundles were made for numpyro_oop-0.1.1.tar.gz:
Publisher:
python-publish.yml on ag-perception-wallis-lab/numpyro-oop
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
numpyro_oop-0.1.1.tar.gz -
Subject digest:
15eb8cdd036204fbb577b98370b6c8aefb98cc34c2c56972a477e077a684635b - Sigstore transparency entry: 169860428
- Sigstore integration time:
-
Permalink:
ag-perception-wallis-lab/numpyro-oop@ce1abc9f8303a8f243dc969ca5f390a3d77d28e1 -
Branch / Tag:
refs/tags/0.1.1 - Owner: https://github.com/ag-perception-wallis-lab
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
python-publish.yml@ce1abc9f8303a8f243dc969ca5f390a3d77d28e1 -
Trigger Event:
release
-
Statement type:
File details
Details for the file numpyro_oop-0.1.1-py3-none-any.whl.
File metadata
- Download URL: numpyro_oop-0.1.1-py3-none-any.whl
- Upload date:
- Size: 9.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.8
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c2c32bab021ef316a4ce1d1f3f99676cb7eb388eb1792bb6846e758b06165a02
|
|
| MD5 |
156e131ce6a6b8fdd080a2534ddd5dca
|
|
| BLAKE2b-256 |
0f2d78378b6101f9980f7f0c09d830d95e90c82b97897278eb50e81d73acc812
|
Provenance
The following attestation bundles were made for numpyro_oop-0.1.1-py3-none-any.whl:
Publisher:
python-publish.yml on ag-perception-wallis-lab/numpyro-oop
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
numpyro_oop-0.1.1-py3-none-any.whl -
Subject digest:
c2c32bab021ef316a4ce1d1f3f99676cb7eb388eb1792bb6846e758b06165a02 - Sigstore transparency entry: 169860430
- Sigstore integration time:
-
Permalink:
ag-perception-wallis-lab/numpyro-oop@ce1abc9f8303a8f243dc969ca5f390a3d77d28e1 -
Branch / Tag:
refs/tags/0.1.1 - Owner: https://github.com/ag-perception-wallis-lab
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
python-publish.yml@ce1abc9f8303a8f243dc969ca5f390a3d77d28e1 -
Trigger Event:
release
-
Statement type: