pespace.inference.sampler.flowmc¶
FlowMC sampler interface for Bayesian parameter estimation.
flowMC is a JAX-based normalizing-flow-enhanced MCMC sampler (Wong et al. 2023, Gabrié et al. 2022). A local MCMC kernel (Gaussian random walk / MALA / HMC) walks the chains while a rational-quadratic-spline normalizing flow is trained online on the accumulated chain history and used as an independent-Metropolis global proposal, which dramatically accelerates the mixing of multi-modal or high-dimensional posteriors.
This module provides FlowMCSampler, which follows the same design as
the other samplers in this package: priors are declared through numpyro
distributions (NumpyroPriorSamplerBase),
sampling is performed in the unbounded (unconstrained) space prepared by the
prior bijections, and the output is transformed back to physical parameters.
Six built-in flowMC resource-strategy bundles are available under their flowMC
names (_buildin_bundles), with "RQSpline_MALA" as the default.
Bundle hyperparameters are supplied as a plain bundle_config dictionary and
merged on top of the built-in default configuration (_default_config).
n_chains and checkpoint_interval are sampler-level settings managed
directly by the constructor. Fully custom setups are supported by passing a
pre-built ResourceStrategyBundle instance (bundle_config is then
ignored). flowMC is an optional dependency and is imported lazily.
Notes
The likelihood only needs to provide
log_likelihood(params)and thelikelihood_param_typenamedtuple type — exactly the interfaceWhittleLikelihoodJaxexposes.MCMC provides no evidence estimate;
log_evidenceis reported asNaN.For the parallel-tempering bundles the
logpdfpassed to flowMC is the log-likelihood, and the log-prior (including the Jacobian of the prior bijections, which must not be rescaled by the temperature) is passed as the untemperedlogprior.flowMC has not reached v1.0 yet and its API is unstable; this interface is developed and tested against flowMC 0.6.x.
Classes
|
Normalizing-flow-enhanced MCMC (flowMC) sampler. |
- class pespace.inference.sampler.flowmc.FlowMCSampler(likelihood, prior_dict, label=None, outdir=None, rkey_seed=0, resource_strategy_bundles='RQSpline_MALA', bundle_config={'adapt_step_size': True, 'adapt_step_size_per_dim': True, 'batch_size': 10000, 'chain_batch_size': 0, 'early_stopping': False, 'early_stopping_min_acceptance': 0.1, 'early_stopping_patience': 3, 'early_stopping_tolerance': 0.05, 'global_thinning': 1, 'history_window': 100, 'learning_rate': 0.001, 'local_thinning': 1, 'mala_step_size': 0.1, 'n_NFproposal_batch_size': 10000, 'n_epochs': 100, 'n_global_steps': 100, 'n_local_steps': 100, 'n_max_examples': 10000, 'n_production_loops': 20, 'n_training_loops': 20, 'rq_spline_hidden_units': (32, 32), 'rq_spline_n_bins': 8, 'rq_spline_n_layers': 4, 'verbose': False}, n_chains=64, likelihood_batch_size=0, checkpoint_interval=600.0)[source]¶
Bases:
NumpyroPriorSamplerBaseNormalizing-flow-enhanced MCMC (flowMC) sampler.
The chains evolve in the unbounded space prepared by
NumpyroPriorSamplerBase(the prior bijections and the transform back to physical parameters are handled by the base class). Each training loop runsn_local_stepssteps of the local kernel (GRW / MALA / HMC), trains the RQ-spline normalizing flow forn_epochsepochs on the accumulated chain history, and then proposesn_global_stepsindependent-Metropolis moves from the flow. Aftern_training_loopssuch loops (the warmup, during which the step size is adapted and the flow is fitted), the flow is frozen andn_production_loopsloops of local + global steps are collected as the posterior sample. Flow proposals are corrected by their proposal density, so the frozen-flow production chain satisfies detailed balance.The flowMC sampler (bundle and backend) is constructed in
__init__;run()only executes — and times — thesamplecall.- Parameters:
likelihood (object) – Likelihood instance providing a pure
log_likelihood(params) -> scalarmethod (JAX-transformable) and thelikelihood_param_typenamedtuple type of the likelihood parameters.prior_dict (dict) – Prior declaration, see
NumpyroPriorSamplerBase.label (str, optional) – Label of the run, converted to
str; also the JSON filename (outdir/{label}_results.json). Default is None (->"None").outdir (str, optional) – Output directory for the saved result and the flowMC checkpoint. It must not exist yet: the base class creates it and refuses to reuse an existing path, so that the results of an earlier run are never overwritten. Default is None, which resolves to an
outdir_{label}directory under the current working directory.rkey_seed (int, optional) – Seed for the JAX random number generator key. Default is 0.
resource_strategy_bundles (str or ResourceStrategyBundle, optional) – Either the name of a built-in bundle (
_buildin_bundles) or a pre-constructedResourceStrategyBundleinstance. Default is"RQSpline_MALA". For pre-built instancesbundle_configis ignored and the number of chains is inferred from the bundle’s buffers.bundle_config (dict, optional) – Bundle hyperparameters, merged on top of the built-in default configuration
_default_config. Defaults to_default_config["RQSpline_MALA"]. Unknown keys are discarded with a printed warning; settings that are not explicitly given fall back to their defaults (also with a printed warning). Ignored when a pre-built bundle instance is provided.n_chains (int, optional) – Number of parallel chains. Default is 64. With a pre-built bundle instance, passing
Noneinfers the value from the bundle’s buffers; a concrete value is checked against the bundle’s buffers and a mismatch raises an error.likelihood_batch_size (int, optional) – Evaluate the final log-likelihoods in sequential batches of this size (
jax.lax.map); 0 keeps a singlejax.vmap. A positive value bounds the peak memory of the likelihood intermediates, at the cost of cross-batch parallelism. Default is 0.checkpoint_interval (float, optional) – Minimum wall-clock seconds between flowMC checkpoint writes; set 0 to disable checkpointing. Default is 600.0.
- Raises:
ImportError – If flowMC is not installed.
ValueError –
If
resource_strategy_bundlesnames an unknown bundle or is not a string / bundle instance; - If the number of chains cannot be inferred from a pre-built bundle instance.
- _z_to_array(z)[source]¶
Stack a sampled-parameter namedtuple into an
(..., n_dim)array.Handles both scalar leaves (shape
()) and batched leaves (shape(n_chains,)or(n_samples,)).- Return type:
Array
- _build_logpdfs(is_pt)[source]¶
Return the flowMC
logpdf(andlogpriorfor PT bundles).The flowMC callables take
f(x, data)withxa 1-D array; they rewrapxinto the sampled-parameter namedtuple and evaluate the unbounded-space functions of the base class. PT bundles temper the likelihood, sologpdfis the log-likelihood there and the log-prior (including the bijection Jacobian, which must not be rescaled by the temperature) is returned aslogprior.- Parameters:
is_pt (bool)
- _resolve_bundle(bundle_arg, bundle_config)[source]¶
Resolve the
resource_strategy_bundles/bundle_configinputs into a ready-to-useResourceStrategyBundle.Sets
self.resource_strategy_bundles(the bundle class name),self.bundle_config(the merged configuration, orNonefor a pre-built instance) andself.n_chains.Unknown
bundle_configkeys are discarded with a printed warning; settings that are not explicitly given fall back to their defaults (also with a printed warning).- Return type:
ResourceStrategyBundle
- _build_sampler()[source]¶
Construct the flowMC
Samplerfrom the resolved bundle.Called from
__init__so thatrunonly executes and timessample.- Return type:
Sampler
- _get_production_samples()[source]¶
Collect the production samples from the bundle resources.
Built-in bundles store them in the
"positions_production"buffer; custom bundles are expected to contain aStateresource whose"target_positions"entry points to the output buffer (the same convention the built-in bundles use).- Return type:
Array
- _finalize(flat_samples, runing_time)[source]¶
Assemble the chain output into the run-result dictionary.
The chain positions are restored to the physical space and reported as
sampled_params; thefixed_paramsare broadcast to the sample count, and thederived_paramsare taken from the likelihood-input assembly. MCMC samples are equally weighted, soweightsis left as None andlog_evidenceis NaN.- Return type:
dict
- run()[source]¶
Run the flowMC sampler.
Only the
samplecall is executed (and timed) here; the flowMC sampler and its bundle were constructed in__init__.- Returns:
Run result containing
label,outdir,rkey_seed,prior_dict,likelihood_class,runing_time,sampler_class,sampler_config,posteriors(physical sampled/derived/fixed parameters) andlog_likelihoods; the log-evidencelog_evidenceis NaN andweightsis an empty array (equally weighted MCMC samples). The result is also saved tooutdir/{label}_results.json.- Return type:
dict
- _abc_impl = <_abc._abc_data object>¶