API Reference
ReactiveHMC.PartialFunction Type
partial(f, args...; kwargs...)
partial(f, :, args...; kwargs...)
partial(f, l1, :, args...; kwargs...)Create a partially applied function. Keyword arguments are accessible as properties (e.g. partial(leapfrog!; stepsize=0.5).stepsize).
Examples
step_f = partial(leapfrog!; stepsize=0.5)
step_f(phasepoint) # calls leapfrog!(phasepoint; stepsize=0.5)
step_f.stepsize # 0.5ReactiveHMC.dual_averaging_state Method
dual_averaging_state(init; target=0.8, ...)Step size adaptation via Nesterov-style dual averaging. Call fit!(da_state, acceptance_rate) after each NUTS step during warmup. Use da_state.current during adaptation and da_state.final after warmup ends. When changing the metric, create a fresh da_state seeded with the current step size.
ReactiveHMC.euclidean_phasepoint Method
euclidean_phasepoint(pot_f, grad_f, metric, pos, mom)Create a reactive phasepoint with Euclidean (diagonal or dense) metric.
pot_f(pos)— potential energy (negated log density)grad_f(pos)— returns(pot, dpot_dpos)(negated log density and gradient)metric— mass matrix, typicallyDiagonal(ones(dim))
Reactive fields: pos, mom, metric (mutable via @invalidatedependants!), pot, dpot_dpos (negated score), ham, dham_dpos, dham_dmom.
ReactiveHMC.generalized_leapfrog! Method
generalized_leapfrog!(phasepoint; stepsize, n_fi_steps)Generalized leapfrog for position-dependent (Riemannian) metrics. Uses n_fi_steps fixed-point iterations per half-step to solve the implicit equations.
ReactiveHMC.hmc_state Method
hmc_state(init; rng, n_steps=1, step_f, stats_f=nothing, min_dham=-1000.)Create an HMC sampler state with fixed trajectory length.
init— a phasepoint (e.g. fromeuclidean_phasepoint)n_steps— number of leapfrog steps per iterationstep_f— integrator, e.g.partial(leapfrog!; stepsize=0.5)stats_f— optional trajectory recorder (e.g.trajectory_stats(dim))
After ReactiveHMC.step!(state), the accepted sample is in state.init.pos.
ReactiveHMC.implicit_midpoint! Method
implicit_midpoint!(phasepoint; stepsize, n_fi_steps)Implicit midpoint rule integrator. Uses n_fi_steps fixed-point iterations to solve for the midpoint, then extrapolates to the full step.
ReactiveHMC.leapfrog! Method
leapfrog!(phasepoint; stepsize)Perform one Störmer-Verlet (leapfrog) integration step in-place: half-step momentum, full-step position, half-step momentum. For Euclidean metrics this is symplectic and time-reversible.
sourceReactiveHMC.multistep Method
multistep(f, args...; n_steps, stepsize, kwargs...)
multistep(f; n_steps)Subdivide a single integration step into n_steps sub-steps, each with stepsize/n_steps. The two-argument form returns a curried integrator.
ReactiveHMC.nuts_state Method
nuts_state(init; rng, step_f, stats_f=nothing, max_depth=10, min_dham=-1000.)Create a NUTS sampler state.
init— a phasepoint (e.g. fromeuclidean_phasepoint)step_f— integrator, e.g.StepFn(leapfrog!, stepsize)stats_f— optional trajectory recorder (e.g.trajectory_stats(dim))
Before each step:
reset!(stats_f, state.init)@invalidatedependants! state.init.mom = ...ReactiveHMC.step!(state)
After the step, the accepted sample is in state.init.pos.
ReactiveHMC.relativistic_euclidean_phasepoint Method
relativistic_euclidean_phasepoint(pot_f, grad_f, metric, pos, mom; c, m)Create a reactive phasepoint with relativistic kinetic energy and Euclidean metric.
c— speed of light parameter (bounds momentum contribution)m— mass parameter
ReactiveHMC.relativistic_riemannian_phasepoint Method
relativistic_riemannian_phasepoint(pot_f, grad_f, metric_f, metric_grad_f, pos, mom; c, m)Create a reactive phasepoint with relativistic kinetic energy and position-dependent Riemannian metric. Combines the metric callbacks of riemannian_phasepoint with the relativistic parameters of relativistic_euclidean_phasepoint.
ReactiveHMC.relativistic_riemannian_softabs_phasepoint Method
relativistic_riemannian_softabs_phasepoint(pot_f, grad_f, premetric_f, premetric_grad_f, pos, mom; alpha=20., c, m)Create a reactive phasepoint with relativistic kinetic energy and SoftAbs-transformed Hessian metric. Combines riemannian_softabs_phasepoint with relativistic parameters c (speed of light) and m (mass).
ReactiveHMC.riemannian_phasepoint Method
riemannian_phasepoint(pot_f, grad_f, metric_f, metric_grad_f, pos, mom)Create a reactive phasepoint with position-dependent Riemannian metric.
metric_f(pos)— returns(pot, dpot_dpos, metric)metric_grad_f(pos)— returns(pot, dpot_dpos, metric, metric_grad)wheremetric_gradis a 3D tensor (dim × dim × dim)
Use with generalized_leapfrog! or implicit_midpoint!.
ReactiveHMC.riemannian_softabs_phasepoint Method
riemannian_softabs_phasepoint(pot_f, grad_f, premetric_f, premetric_grad_f, pos, mom; alpha=20.)Create a reactive phasepoint with SoftAbs-transformed Hessian metric. The metric is computed as eigvals .* coth(alpha .* eigvals) of the pre-metric (typically the negative Hessian), ensuring positive-definiteness.
premetric_f(pos)— returns(pot, dpot_dpos, premetric)premetric_grad_f(pos)— returns(pot, dpot_dpos, premetric, premetric_grad)alpha— softabs sharpness parameter (higher = closer to absolute value)
ReactiveHMC.sampling_stats Method
sampling_stats(tstats)Accumulate per-iteration statistics across a full NUTS sampling run. Wraps a trajectory_stats instance.
Call the resulting object as sstats(state, da_state) after each step to record:
draws— accepted positions (dim × n_draws)n_steps— leapfrog steps per iterationstepsizes— step size used per iterationacc_rate— acceptance rate per iterationdiverged— divergence flag per iterationfull_history— trajectory positions per iteration (for visualization)full_idxs— tree-building order per iteration (for animation)
ReactiveHMC.trajectory_stats Method
trajectory_stats(dim)Record all leapfrog positions, gradients, potentials, and Hamiltonian errors during one NUTS tree expansion. Used as stats_f in nuts_state.
Call reset!(tstats, phasepoint) before each NUTS step. After the step, access: positions, gradients, dhams, pots, idxs.
The idxs field records tree-building order for animation (use invperm(idxs .+ 1) .- 1 for reveal order).
ReactiveHMC.welford_var Method
welford_var(dim)Online variance estimation using Welford's algorithm. Feed samples via step!(wv, x). Fields: .n (count), .mean (running mean), .var (running variance). Accepts vectors or matrices (columns as samples).
For metric adaptation:
Stan-style:
Diagonal(max.(1e-6, wv.var))from position samplesNutpie-style:
Diagonal(max.(1e-6, sqrt.(wvp.var ./ wvg.var)))from pos + grad