setu.NLE#
- class setu.NLE(flow, x_transform=None, theta_transform=None, condition_transform=None, x_dim=0, condition_dim=0, trained=False)[source]#
Bases:
ModuleNeural Likelihood Estimator using normalizing flows.
Estimates p(x|theta) or p(x|theta, conditions) using a conditional normalizing flow. Use create_nle() to create an instance and fit_nle() to train it.
- flow#
FlowJAX conditional normalizing flow.
- x_transform#
Standardizer for observations (fitted during training).
- theta_transform#
Standardizer for parameters (fitted during training).
- condition_transform#
Standardizer for conditions (fitted during training).
- x_dim#
Dimension of observations.
- condition_dim#
Dimension of experimental conditions (0 if none).
- Parameters:
flow (
Module)x_transform (
Standardizer|None)theta_transform (
Standardizer|None)condition_transform (
Standardizer|None)x_dim (
int)condition_dim (
int)trained (
bool)
- log_prob(x, theta, conditions=None)[source]#
Evaluate log p(x|theta) or log p(x|theta, conditions).
Note the argument order:
log_probtakes x first, theta second, whereasto_pymc(theta, x_observed)andsample(theta, key, ...)take theta first. If x_dim == theta_dim, swapping the two silently returns a wrong (but finite) value rather than erroring, so pass them by keyword when in doubt.- Parameters:
x (
Array) – Observation with shape (x_dim,).theta (
Array) – Parameter with shape (theta_dim,).conditions (
Array|None) – Experimental conditions with shape (condition_dim,). Required if NLE was trained with conditions.
- Return type:
Array- Returns:
Log probability as scalar.
- Raises:
ValueError – If NLE has not been trained yet.
ValueError – If conditions required but not provided, or vice versa.
ValueError – If conditions has wrong dimension.
- sample(theta, key, conditions=None, *, n_samples)[source]#
Sample from p(x|theta) or p(x|theta, conditions).
Draws
n_samplesobservations for a single parameter vector. The signature matchesMixedNLE.sample(theta first, key second, n_samples keyword-only); unlike MixedNLE this does not accept a batched theta.- Parameters:
- Return type:
Array- Returns:
Samples with shape (n_samples, x_dim).
- Raises:
ValueError – If NLE has not been trained yet.
ValueError – If theta is not a single 1D parameter vector.
ValueError – If conditions required but not provided, or vice versa.
ValueError – If conditions has wrong dimension.
TypeError – If key is not a JAX random key (e.g. an integer passed positionally where key is expected).
- to_pymc(theta, x_observed, *, conditions=None, name='nle_likelihood', dims=None, chunk_size=None)[source]#
Convert to PyMC likelihood.
- Parameters:
theta – PyMC parameter variable
x_observed – Observed data (n_obs, x_dim) or (x_dim,)
conditions (
Array|None) – Optional experimental conditions (n_obs, condition_dim)name (
str) – Name for the PyMC potentialdims (
tuple[str,...] |None) – PyMC dimension names (for coords)chunk_size (
int|None) – If set, evaluate log-prob in chunks to reduce memory.
- Returns:
PyMC Potential adding log p(x_observed | theta, conditions) to model
- to_pymc_hierarchical(theta, x_observed, *, conditions=None, trial_dim=None, subject_dim=None, group_dim=None, name='nle_likelihood_hierarchical', theta_shape=None, dims=None, chunk_size=None, mask=None)[source]#
Convert to PyMC hierarchical likelihood.
Automatically handles 2-level or 3-level hierarchies via nested vmap.
- Parameters:
theta – PyMC parameter variable with dims matching hierarchy
x_observed – Observed data ([groups], subjects, trials, x_dim)
conditions (
Array|None) – Optional conditions ([groups], subjects, trials, condition_dim)trial_dim (
str|None) – Name of trial dimension (innermost level)subject_dim (
str|None) – Name of subject dimension (middle level)group_dim (
str|None) – Name of group dimension (outermost level, optional)name (
str) – Name for the PyMC potentialtheta_shape (
tuple[int,...] |None) – Explicit theta shape when theta.type.shape contains None (e.g., when using PyMC dims). Inferred from x_observed if not given.dims (
tuple[str,...] |None) – PyMC dimension names (for coords)chunk_size (
int|None) – If set, evaluate log-prob in chunks to reduce memory.mask (
Array|None) – Boolean array matching x_observed leading dims. Where False, trial logp is zeroed (for ragged/unbalanced data).
Note
When sampling with BlackJAX using
chain_method="vectorized", setprogressbar=Falseinpm.sample(). The BlackJAX progress bar uses IO callbacks that are incompatible with vectorized chains.- Returns:
PyMC Potential adding sum of log p(x_observed | theta, conditions) to model
- Parameters:
See setu.integration.pymc.to_pymc_hierarchical for full documentation.
- to_numpyro(theta, x_observed, *, conditions=None, name='nle_likelihood')[source]#
Convert to NumPyro likelihood factor.
- to_numpyro_hierarchical(theta, x_observed, *, conditions=None, mask=None, name='nle_likelihood_hierarchical')[source]#
Convert to NumPyro hierarchical likelihood factor.
- Parameters:
theta – Parameters from numpyro.sample(). 2-level: shape (n_subjects, theta_dim). 3-level: shape (n_groups, n_subjects, theta_dim).
x_observed – Observed data. 2-level: shape (n_subjects, n_trials, x_dim). 3-level: shape (n_groups, n_subjects, n_trials, x_dim).
conditions (
Array|None) – Optional conditions matching x_observed leading dims.mask (
Array|None) – Boolean mask matching x_observed leading dims.name (
str) – Name for the NumPyro factor site.