Skip to content

API Reference

ReactiveHMC.PartialFunction Type
julia
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

julia
step_f = partial(leapfrog!; stepsize=0.5)
step_f(phasepoint)  # calls leapfrog!(phasepoint; stepsize=0.5)
step_f.stepsize      # 0.5
source
ReactiveHMC.dual_averaging_state Method
julia
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.

source
ReactiveHMC.euclidean_phasepoint Method
julia
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, typically Diagonal(ones(dim))

Reactive fields: pos, mom, metric (mutable via @invalidatedependants!), pot, dpot_dpos (negated score), ham, dham_dpos, dham_dmom.

source
ReactiveHMC.generalized_leapfrog! Method
julia
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.

source
ReactiveHMC.hmc_state Method
julia
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. from euclidean_phasepoint)

  • n_steps — number of leapfrog steps per iteration

  • step_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.

source
ReactiveHMC.implicit_midpoint! Method
julia
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.

source
ReactiveHMC.leapfrog! Method
julia
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.

source
ReactiveHMC.multistep Method
julia
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.

source
ReactiveHMC.nuts_state Method
julia
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. from euclidean_phasepoint)

  • step_f — integrator, e.g. StepFn(leapfrog!, stepsize)

  • stats_f — optional trajectory recorder (e.g. trajectory_stats(dim))

Before each step:

  1. reset!(stats_f, state.init)

  2. @invalidatedependants! state.init.mom = ...

  3. ReactiveHMC.step!(state)

After the step, the accepted sample is in state.init.pos.

source
ReactiveHMC.relativistic_euclidean_phasepoint Method
julia
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

source
ReactiveHMC.relativistic_riemannian_phasepoint Method
julia
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.

source
ReactiveHMC.relativistic_riemannian_softabs_phasepoint Method
julia
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).

source
ReactiveHMC.riemannian_phasepoint Method
julia
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) where metric_grad is a 3D tensor (dim × dim × dim)

Use with generalized_leapfrog! or implicit_midpoint!.

source
ReactiveHMC.riemannian_softabs_phasepoint Method
julia
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)

source
ReactiveHMC.sampling_stats Method
julia
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 iteration

  • stepsizes — step size used per iteration

  • acc_rate — acceptance rate per iteration

  • diverged — divergence flag per iteration

  • full_history — trajectory positions per iteration (for visualization)

  • full_idxs — tree-building order per iteration (for animation)

source
ReactiveHMC.trajectory_stats Method
julia
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).

source
ReactiveHMC.welford_var Method
julia
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 samples

  • Nutpie-style: Diagonal(max.(1e-6, sqrt.(wvp.var ./ wvg.var))) from pos + grad

source
You are viewing the dev branch. This branch may include code written with Claude Code with less human supervision. Only human-approved code is merged into main.