import MCMC from './_mcmc'; import { Normal } from '../dist'; /** * Class implementing the [Hamiltonian Monte Carlo]{@link https://en.wikipedia.org/wiki/Hamiltonian_Monte_Carlo} * (HMC) sampler. Uses gradient information and a leapfrog integrator to simulate Hamiltonian * dynamics, proposing distant moves along the resulting trajectory and accepting/rejecting via * the Metropolis criterion on the augmented (position, momentum) system. Momenta are resampled * from a standard multivariate Normal at the start of every iteration. During warm-up, the step * size is adapted via Robbins-Monro dual averaging (Hoffman & Gelman 2014, S3.2) toward a target * acceptance probability, and jittered multiplicatively each iteration to avoid periodicity * artifacts. * * **Known limitation**: only `stepSize` is jittered — `pathLength` (the number of leapfrog * steps) stays fixed for the sampler's entire lifetime. A fixed trajectory length * (`pathLength * stepSize`) can still land near a half-period of the target's natural * oscillation at certain target correlations, producing genuine negative low-lag autocorrelation * that [ess()]{@link ran.mc.MCMC#ess} and [ac()]{@link ran.mc.MCMC#ac} faithfully report — this * is a legitimate property of the chain, not a bug in those estimators. If you observe this * (e.g. `ac()` reporting negative autocorrelation at low lags, or `ess()` reporting an effective * sample size well above or below the raw sample count), try a different `pathLength`, or switch * to [NUTS]{@link ran.mc.NUTS}, which adapts trajectory length automatically each iteration * (Hoffman & Gelman 2014) and removes the need to hand-tune `pathLength`. * * @class HMC * @memberof ran.mc * @param {Object} options Sampler options, as a single object. * @param {Function} options.logDensity The logarithm of the (unnormalized) target density. * @param {Function} options.gradLogDensity The gradient of logDensity: maps a state (number[]) to * its gradient (number[]) of the same dimension. The array passed in may be reused and mutated * across subsequent leapfrog steps; read it synchronously and do not retain the reference. * @param {Object=} options.config HMC configuration (see MCMC base class for shared options), plus * `stepSize` (ε, the leapfrog step size, default 0.1), `pathLength` (L, the number of leapfrog * steps per iteration, default 10 — fixed for the sampler's lifetime and not jittered like * `stepSize`; see the class JSDoc's "Known limitation" note for the resonance risk this can * produce), and `metric` (the mass matrix structure adapted during warm-up: `'diag'` (default) * estimates a per-dimension variance; `'dense'` estimates the full covariance matrix, refactored * through Matrix's LDL decomposition). * @param {Object=} options.initialState Initial state of the sampler (see MCMC base class). * @constructor * @throws {Error} If options is not a plain object, or gradLogDensity is not a function, or a * resumed initialState.internal.stepSize) is provided but is not a positive finite number, or a * pathLength (config.pathLength or a resumed initialState.internal.pathLength) is provided but * is not a positive integer or exceeds `HMC._MAX_PATH_LENGTH` (1024), or metric is provided but * is not `'diag'`/`'dense'`, or `'dense'` is * requested with a dimension exceeding the dense-metric cap, or a resumed * initialState.internal.metric does not match the resolved metric type/shape. */ export default class HMC extends MCMC { _metricType: string; _gradLnp: Function; _stepSize: any; _pathLength: any; _q: Normal; _met: { type: any; dim: any; }; /** * @param {Object} options Sampler options, as a single object. * @param {Function} options.logDensity The logarithm of the (unnormalized) target density. * @param {Function} options.gradLogDensity The gradient of logDensity: maps a state (number[]) * to its gradient (number[]) of the same dimension. The array passed in may be reused and * mutated across subsequent leapfrog steps; read it synchronously and do not retain the reference. * @param {Object=} options.config HMC configuration (see MCMC base class for shared options), * plus `stepSize` (ε, the leapfrog step size, default 0.1) and `pathLength` (L, the number of * leapfrog steps per iteration, default 10 — fixed for the sampler's lifetime; see the class * JSDoc's "Known limitation" note for the resonance risk this can produce). * @param {Object=} options.initialState Initial state of the sampler (see MCMC base class). * @throws {Error} If options is not a plain object. */ constructor(options: { logDensity: Function; gradLogDensity: Function; config?: any | undefined; initialState?: any | undefined; }); /** * Sets the seed for the sampler's pseudo random number generator, including the internal * momentum distribution's generator. * * @method seed * @memberof ran.mc.HMC * @param {number|string} value The value of the seed, either a number or a string (for the ease of tracking seeds). * @returns {this} Reference to the current sampler. * @ignore */ seed(value: number | string): this; _iter(x: any, warmUp: any): { x: any; accepted: boolean; alpha: number; }; _adjust(i: any): void; _internal(): { stepSize: any; pathLength: any; metric: { type: string; L: any; D: any; variance?: undefined; } | { L?: undefined; D?: undefined; type: string; variance: any; }; prngQ: number[]; daMu: any; daHbar: any; daLogEpsBar: any; daT: any; metAccumulator: { metN: any; metMean: any; metCovS: any; metM2?: undefined; } | { metCovS?: undefined; metN: any; metMean: any; metM2: any; }; }; static _validateOptions(options: any): void; static _isPlainObject(options: any): boolean; static _validateGradLogDensity(gradLogDensity: any): void; static _validateStepSize(stepSize: any): void; static _validatePathLength(pathLength: any): void; static _isPositiveInteger(value: any): boolean; static get _MAX_PATH_LENGTH(): number; _leapfrog(x: any, r: any, eps: any): { x: any; r: any; }; }