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.
search.jsonrecords only the kind ("none","diagonal","dense","array"or"result"), so a search loaded from it receives that string:"none"behaves asNone, while"array"/"result"raise at fit time because the seed was not saved.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.
fitness_overridesFitnesskeyword arguments arun(ctx)search adds tomake_fitnessfor this analysis (e.g.batched=Truefor a batched backend).info_fromThe
samples_infoentries of arun(ctx)search's internal state, theinfoitsraw_samples_fromputs on theRawSamples(autofit.non_linear.samples.adapter).load_search_internalThe search's internal state as stored in its output folder, or
Nonewhen its paths store nothing (NullPaths,DatabasePaths).make_fitnessThe Fitness this search evaluates its likelihood through, the one construction site every search shares.
make_poolMake the pool instance used to parallelize a NonLinearSearch alongside a set of unique ids for every process in the pool.
make_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.
output_search_internalArchive
search_internalthrough the search'scheckpointerand, once a fit has completed, discard its resume state (there is nothing left to resume).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.
The BlackJAX chains pickled under
search_internal/as raw samples.result_via_completed_fitReturns the result of the non-linear search of a completed model-fit.
runThe backend loop of a search on the
run(ctx)hook: run the backend throughctxand return its internal state (search_internal).samples_fromLoads the samples of a non-linear search from its output files.
samples_via_internal_fromThe samples of the search, converted from its internal state.
start_pointsThe search's
ninitial points, drawn by its initializer, with the start-point plot made once, here, from the first of them.start_resume_fitAttributes
The search-internal dict archived by
_fit(search_internal.pickle).where the backend's final state is stored when a fit completes, for
Result.search_internalandsamples_via_internal_from.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.
requires_fp64resume_stateThe file an interrupted run resumes from, for searches that genuinely resume (
Noneotherwise).retains_search_internalalways when the archive declares
retain_after_completion(its results cannot be rebuilt without it), otherwise when theoutput.search_internalconfig is on.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.
- jax_use = 'required'#
- gradient = 'uses'#
- batched = True#
- honours_gradient_mode = False#
- posterior_kind = 'chain'#
- produces_evidence = False#
- resumable = False#
- warm_start = 'consumer'#
- install_extra = 'optional'#
- upstream_url = 'https://github.com/blackjax-devs/blackjax'#
- citation_keys = ('blackjax',)#
- status = 'stable'#
- test_mode_budget = {'num_chains': 2, 'num_samples': 20, 'num_warmup': 20}#
- objective_target = (ObjectiveQuantity.LOG_POSTERIOR, CoordinateSpace.PHYSICAL)#
- invalid_value = -inf#
- checkpointer: Checkpointer = PickleCheckpointer('search_internal.pickle', retain_after_completion=True)#
where the backend’s final state is stored when a fit completes, for
Result.search_internalandsamples_via_internal_from.- Type:
The archive strategy
- apply_test_mode()[source]#
Override in subclasses to reduce sampler iterations for test mode.
The base class never calls this method: each search that overrides it calls it at the end of its own
__init__whenis_test_mode()is true (test mode level 1). Overrides directly mutate instance attributes to minimize the number of iterations the sampler performs.
- property backend_filename#