autofit.BlackJAXNUTS#
- class BlackJAXNUTS[source]#
Bases:
AbstractMCMCA BlackJAX No-U-Turn Sampler (NUTS) non-linear search.
BlackJAX is a sampling library implemented on top of JAX. NUTS is its gradient-based MCMC kernel — an extension of HMC that adapts trajectory length on the fly. The autofit
Analysistherefore must be constructed withuse_jax=Trueso the log-likelihood is JAX-traceable end-to-end (andjax.gradof it can be taken). A clear error is raised at fit time otherwise.For a full description of BlackJAX, see:
https://github.com/blackjax-devs/blackjax
The fit runs in two phases, both vmapped over
num_chainsindependent chains:blackjax.window_adaptationwarmup, which tunes the leapfrog step size (dual averaging) and an inverse mass matrix (diagonal or dense, seeinverse_mass_matrixbelow) per chain.NUTS sampling with each chain’s tuned kernel, run inside a
jax.lax.scanso the inner step is fully JIT-compiled. The scan is broken intoiterations_per_full_update-sized chunks so partial state is persisted andperform_updateruns periodically (samples.csv flush, plotting), mirroring the Emcee chunking pattern.
Warm starting. Both the starting point(s) and the mass matrix can be seeded from a previous Result, without ever modifying that result’s model’s priors (contrast with
Result.model_centred*, which rewrite priors to new GaussianPrior`s). For example, to warm-start a 4-chain run from an earlier (e.g. single-chain, or MLE) fit’s ``result`:search = af.BlackJAXNUTS( num_chains=4, initializer=result.start_point_from(n_points=4, jitter=0.05), inverse_mass_matrix=result, )
result.start_point_from(...)(equivalentlyInitializerParamStartPoints.from_result(result, n_points=4, jitter=0.05)) maps the previous result’s best-fit point onto the (possibly different) target model by path, then jittersn_pointsphysical-space starting vectors around it — one per chain. Passinginverse_mass_matrix=result(orresult.samples) seeds warmup’s dense inverse mass matrix from that result’ssamples.covariance_matrix; this raises a clear error if the result looks MLE-only (too few samples for a reliable covariance), in which case an explicit array (e.g. from a Laplace approximation) should be passed instead.- Parameters:
name (
Optional[str]) – The name of the search, controlling the last folder results are output to.path_prefix (
Optional[str]) – The path of folders prefixing the name folder where results are output.unique_tag (
Optional[str]) – A unique tag for this model-fit, used as a folder between the path prefix and the search name and as the SQLite identifier.num_warmup (
int) – Number of warmup steps used byblackjax.window_adaptationto tune the leapfrog step size and inverse mass matrix.num_samples (
int) – Number of post-warmup samples drawn from the tuned NUTS kernel, per chain.num_chains (
int) – Number of independent chains sampled in parallel viavmap.target_accept (
float) – Target Metropolis acceptance rate for window adaptation (default 0.8 — the standard Stan setting).max_num_doublings (
int) – NUTS doubling cap. 10 means at most 1024 leapfrog steps per sample, which is the standard ceiling.seed (
int) – Integer seed passed tojax.random.PRNGKey.initializer (
Optional[Initializer]) – Generates the starting point(s), one per chain. Defaults toInitializerBall(0.49, 0.51)fromAbstractMCMC— small ball around the prior median in unit-cube coordinates. Passresult.start_point_from(n_points=num_chains, jitter=...)(or anInitializerParamStartPoints.from_result(...)) to warm-start from a previous result instead.inverse_mass_matrix (
Union[None,str,ndarray,object]) –Controls the metric adapted by
blackjax.window_adaptation:None(default): adapt a fresh diagonal inverse mass matrix, seeded at the identity (standard behaviour)."diagonal"/"dense": adapt a diagonal / dense inverse mass matrix, still seeded at the identity.a
numpyarray: seed the adaptation with this matrix – a 1-D array of shape(n_dim,)seeds a diagonal metric, a 2-D array of shape(n_dim, n_dim)seeds a dense metric.a Result or Samples object: seed a dense metric from its
samples.covariance_matrix. RaisesValueErrorif that covariance looks MLE-only (too few samples, or non-finite / identity) – pass an explicit array in that case.
mass_matrix_shrinkage (
float) – Forwarded toblackjax.window_adaptation’simm_shrinkage_to_previous: shrinkage of each warmup window’s adapted inverse mass matrix toward the previous window’s (in addition to Stan’s existing shrinkage toward the identity).0.0(default) reproduces Stan’s standard per-window-reset behaviour; a positive value lets a high-confidenceinverse_mass_matrixseed persist further into warmup.share_adaptation (
bool) – IfTrue, after per-chain warmup thenum_chainstuned inverse mass matrices are averaged and the tuned step sizes combined via their median, and every chain’s sampling phase uses this single shared, shared kernel. IfFalse(default) each chain samples with its own independently-tuned kernel.auto_correlation_settings (
AutoCorrelationsSettings) – Configures the per-parameter ESS-derived integrated auto-correlation diagnostics.check_for_convergencedefaults toFalsehere because NUTS is run to a fixed sample budget after warmup.iterations_per_full_update (
Optional[int]) – Sample chunk size betweenperform_updatecalls. Inherited from the autonerves config whenNone.number_of_cores (
int) – Currently unused — chains run vmapped on a single device. Kept for API parity with the other MCMC searches.silence (
bool) – If True, the default print output of the non-linear search is silenced.session (
Optional[Session]) – An SQLalchemy session instance.
Methods
Override in subclasses to reduce sampler iterations for test mode.
Synthesise the standard
AutoCorrelationsfrom BlackJAX's per-param ESS via the canonical identityτ_int = N / ESS.check_modelcopy_with_pathsexact_fitfitFit a model, M with some function f that takes instances of the class represented by model M and gives a score for their fitness.
make_poolMake the pool instance used to parallelize a NonLinearSearch alongside a set of unique ids for every process in the pool.
make_sneakier_poolmake_sneaky_poolCreate a pool for multiprocessing that uses slight-of-hand to avoid copying the fitness function between processes multiple times.
optimisePerform optimisation for expectation propagation.
Pickle the search-internal dict.
perform_updatePerform an update of the non-linear search's model-fitting results.
perform_visualizationPerform visualization of the non-linear search's model-fitting results.
plot_resultsplot_start_pointVisualize the starting point of the non-linear search, using an instance of the model at the starting point of the maximum likelihood estimator.
post_fit_outputCleans up the output folderds after a completed non-linear search.
pre_fit_outputOutputs attributes of fit before the non-linear search begins.
result_via_completed_fitReturns the result of the non-linear search of a completed model-fit.
samples_fromLoads the samples of a non-linear search from its output files.
Convert the BlackJAX chain pickled under
search_internal/into a standardSamplesMCMC.start_resume_fitAttributes
Load the pickled search-internal dict written by
_fit.loggerLog 'msg % args' with severity 'DEBUG'.
namepathsquick_update_messageOne line, logged at the start of every search, telling the user the real cadence of the on-the-fly maximum-likelihood updates.
should_plot_start_pointtimerReturns the timer of the search, which is used to output informaiton such as how long the search took and how much parallelization sped up the search time.
- apply_test_mode()[source]#
Override in subclasses to reduce sampler iterations for test mode.
Called during __init__ when test mode is active (level 1). Subclasses should directly mutate instance attributes to minimize the number of iterations the sampler performs.
- property backend_filename#
- output_search_internal(search_internal)[source]#
Pickle the search-internal dict.
BlackJAX has no native on-disk format (cf. emcee’s HDFBackend), so we round-trip the chain + diagnostics via pickle. We bypass
self.paths.save_search_internalbecause the autofit dill path chokes on a few numpy/jax-backed members; a direct pickle of already-numpy data is robust.NullPaths(noname/path_prefix) setssearch_internal_pathtoNoneto suppress disk output — skip silently in that case.