mixle.inference.mcmc.parameter_bridge module¶
High-level parameter-posterior sampling for mixle.stats families.
Given a prototype distribution that fixes the model family, a dataset, and a
prior over parameters, this samples
p(theta | data) proportional to exp(sum_i log p(x_i | theta)) * prior(theta)
by running Metropolis-Hastings or Hamiltonian Monte Carlo in an unconstrained
reparameterization (log for positive scales, stick-breaking for probability
simplices) and mapping the retained samples back to parameter space (or to
rebuilt distribution objects).
- class ParameterBridge(dim, to_unconstrained, from_unconstrained, log_abs_det_jacobian, build, param_names, initial_theta=None)[source]
Bases:
objectMap between a distribution’s parameters and an unconstrained vector.
- Parameters:
- dim
Length of the unconstrained vector
phi.- Type:
- to_unconstrained
theta -> phifor the prototype’s parameters.- Type:
- from_unconstrained
phi -> theta(parameter-space value).- Type:
- log_abs_det_jacobian
phi -> log|det dtheta/dphi|so a flat prior in theta-space maps to the right density in phi-space.- Type:
- build
theta -> distributionrebuilds a model from parameters.- Type:
collections.abc.Callable[[Any], Any]
- initial_theta
The prototype’s parameters in theta-space (chain start).
- Type:
Any
- dim: int
- initial_theta: Any = None
- build_parameter_bridge(prototype)[source]
Build a
ParameterBridgefor a prototype distribution.Supported
mixle.statsfamilies:Gaussian
(mu, sigma2)->(mu, log sigma2)Gamma
(k, theta)->(log k, log theta)Exponential
beta/lam(positive scalar) ->logPoisson
lam->log lamBernoulli
p->logit pBeta
(a, b)->(log a, log b)Categorical probability map -> stick-breaking over the simplex
- Raises:
NotImplementedError – if the family or a parameter shape is unsupported.
- Parameters:
prototype (Any)
- Return type:
ParameterBridge
- sample_parameter_posterior(prototype_dist, data, prior=None, sampler='mh', steps=2000, burn_in=500, thin=1, seed=None, proposal=None, initial=None, step_size=0.05, num_steps=20, grad_log_target=None, return_distributions=False)[source]
Sample the parameter posterior
p(theta | data)of a distribution.The model family is fixed by
prototype_dist; its parameters define the sampled space. The unnormalized log target is the data log-likelihood (rebuilding the distribution per proposedthetaand summingseq_log_density) plus a prior log-density and the reparameterization Jacobian. Sampling runs in an unconstrained space (seebuild_parameter_bridge()) so proposals stay in-domain, and retained samples are mapped back to parameter space.- Parameters:
prototype_dist (Any) – A distribution instance fixing the model family.
data (Any) – Observations accepted by the family’s encoder.
prior (Any) –
None(flat), a callabletheta -> log p(theta), or a distribution withlog_densityover the parameter representation.sampler (str) –
'mh'(Metropolis-Hastings),'hmc'(Hamiltonian Monte Carlo), or'nuts'(No-U-Turn Sampler, self-tuning). HMC/NUTS usegrad_log_targetif given, else a finite-difference gradient.steps (int) – Number of retained posterior samples.
burn_in (int) – Number of initial transitions to discard.
thin (int) – Keep one sample every
thintransitions.seed (int | None) – Seed for the RandomState.
proposal (Proposal | None) – Optional MH proposal in the unconstrained space; defaults to a random walk (MH only).
initial (Any) – Optional starting
theta; defaults to the prototype’s parameters.step_size (float) – HMC leapfrog controls (NUTS self-tunes step size).
num_steps (int) – HMC leapfrog controls (NUTS self-tunes step size).
grad_log_target (Callable[[Any], Any] | None) – Optional exact gradient of
log_targetin the unconstrained space (e.g. frommixle.inference.mcmc.gradients.torch_gradient()). Replaces the finite-difference gradient for HMC/NUTS – one backward pass per step instead ofO(dim)target evaluations.return_distributions (bool) – If True,
samplesare rebuilt distribution objects instead of parameter-space values.
- Returns:
MCMCResult whose
samplesare parameter-space values (or rebuilt distributions) and whose diagnostics come from the underlying driver.- Return type:
MCMCResult