HMC

class telescopic.kernels.HMC(
step_size=None,
n_steps=None,
*,
cov=None,
jitter=0.5,
meeting_weight=0.05,
meeting_scale=0.001,
)

Hamiltonian Monte Carlo with a random-walk meeting step.

With probability \(1 - \gamma\), a leapfrog trajectory: from a whitened momentum \(\nu \sim \mathcal N(0, I)\), n_steps updates

\[\nu \leftarrow \nu + \tfrac{\epsilon_u}2 C^\top\nabla\log\pi(x), \quad x \leftarrow x + \epsilon_u C\nu, \quad \nu \leftarrow \nu + \tfrac{\epsilon_u}2 C^\top\nabla\log\pi(x),\]

with \(CC^\top = \Sigma\) (cov) and the step \(\epsilon_u = \epsilon(1 + j(2u - 1))\), \(u \sim \mathcal U(0, 1)\) drawn afresh for each trajectory (\(j\) = jitter), accepted on the change in \(\log\pi(x) - \tfrac12\lVert\nu\rVert^2\). A trajectory that reaches a non-finite position or a log-density of -inf stops there and is rejected. With probability \(\gamma\) (meeting_weight), a random-walk step with proposal covariance \(\sigma^2\Sigma\) (meeting_scale \(= \sigma\)).

Coupled chains take the same component. Their trajectories share \(u\), the momentum and the uniform, so the chains contract; the random-walk step couples them by the reflection-maximal coupling, so chains that have come close meet (Heng & Jacob 2019, with a whitened meeting step). Without the jitter, a given n_steps, or a given cov that does not whiten the target, can put an automatic step on a resonance of the leapfrog, where the trajectories do not contract; the jitter varies the step from one trajectory to the next, so that no step stays on it (When the automatic step fails, in Check and set a step size).

Parameters:
step_sizefloat, optional

\(\epsilon\), the nominal step. None leaves it to tune(): dual averaging of the jittered trajectories alone toward an acceptance rate of 0.8, from \(\epsilon_0 = d^{-1/4}\). The scaling \(d^{-1/4}\) is that of Beskos et al. (2013); the rate 0.8 is this package’s choice, where they find 0.651 optimal for product targets as \(d\) grows.

n_stepsint, optional

Leapfrog steps per trajectory; \(\lceil 1/\epsilon\rceil\) by default, a trajectory of length about 1 in whitened coordinates at the nominal step, and at most 1,000. The jitter leaves it fixed, so it varies the length of a trajectory between \(1 - j\) and \(1 + j\) times its nominal value.

covarray_like, shape (d, d), optional

\(\Sigma\); None leaves it to tune(), which takes the covariance of adaptive Metropolis pilot chains.

jitterfloat

\(j\), in [0, 1): the step of a trajectory is uniform on \([(1 - j)\epsilon, (1 + j)\epsilon)\). 0 gives a fixed step.

meeting_weightfloat

\(\gamma\), in (0, 1).

meeting_scalefloat

\(\sigma\), positive.

Notes

A step uses 1 uniform to choose its component, then, for a trajectory, 1 uniform for \(u\) when jitter is not 0, d normals and 1 uniform; a coupled step uses the same, plus 1 uniform for a meeting step. A trajectory at a fixed step leaves \(\pi\) invariant, and \(u\) does not depend on the state, so the jittered trajectory, a mixture of them, does too. A trajectory evaluates the log-density and its gradient n_steps times per chain.

Examples

Once the step size is tuned, the default number of leapfrog steps gives a trajectory of length about 1:

>>> import math
>>> from telescopic import examples, kernels, sample
>>> example = examples.gaussian(2)
>>> result = sample(
...     example.log_prob, example.initial, grad=example.grad,
...     kernel=kernels.HMC(), n_replicates=50, vectorize=True, rng=0,
... )
>>> result.kernel.n_steps == math.ceil(1 / result.kernel.step_size)
True
property cov

\(\Sigma\), or None while unset.

Returns:
ndarray, shape (d, d), or None
property jitter

The relative width \(j\) of the jitter of the step.

Returns:
float
property n_steps

Leapfrog steps per trajectory, or None while the step size is unknown.

Returns:
int or None
property step_size

The nominal leapfrog step size \(\epsilon\), or None while unset.

Returns:
float or None