import MCMC from './_mcmc'; import { Normal } from '../dist'; /** * Class implementing the [No-U-Turn Sampler]{@link https://jmlr.org/papers/v15/hoffman14a.html} * (NUTS). Extends Hamiltonian Monte Carlo by replacing a fixed-length leapfrog trajectory with a * recursive doubling-tree procedure that extends forward or backward in a randomly chosen * direction until the trajectory's outer endpoints start turning back toward each other (the * "U-turn" stopping criterion) or a maximum tree depth is reached. The transition is selected via * slice sampling over the tree's valid states, eliminating the need to hand-tune a trajectory * length. Momenta are resampled from `N(0, M)` at the start of every iteration, where `M` is the * Euclidean mass matrix adapted during warm-up (`config.metric`); the no-U-turn criterion is * evaluated on the velocity `M⁻¹r`. During warm-up, the step size is adapted via Robbins-Monro * dual averaging (Hoffman & Gelman 2014, S3.2) toward a target acceptance probability, driven by * the tree-averaged acceptance statistic accumulated across every leapfrog evaluation in that * iteration's tree. * * Sampler-health diagnostics are exposed both per-transition and in aggregate: every * [iterate]{@link ran.mc.MCMC#iterate} result carries `divergent` and `maxDepthHit` booleans, and * [divergenceCount]{@link ran.mc.NUTS#divergenceCount} / [maxDepthCount]{@link ran.mc.NUTS#maxDepthCount} * report the per-sampling-phase totals (reset like [ar]{@link ran.mc.MCMC#ar}). A well-behaved run * reports both counts as 0; nonzero divergences flag a step size that is too large or a target too * extreme, nonzero max-depth hits a step size that is too small. See * decisions/0048-nuts-sampler-health-diagnostics.md. * * @class NUTS * @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. * @param {Object=} options.config NUTS configuration (see MCMC base class for shared options), * plus `stepSize` (ε, the leapfrog step size, default 0.1) 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 * stepSize (config.stepSize or a resumed initialState.internal.stepSize) is provided but is not * a positive finite number, 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 NUTS extends MCMC { _metricType: string; _gradLnp: Function; _stepSize: any; _q: Normal; _met: { type: any; dim: any; }; _divergenceCount: number; _maxDepthCount: number; /** * @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. * @param {Object=} options.config NUTS configuration (see MCMC base class for shared options), * plus `stepSize` (ε, the leapfrog step size, default 0.1) and `metric` (the mass matrix * structure adapted during warm-up: `'diag'` (default) or `'dense'`). * @param {Object=} options.initialState Initial state of the sampler (see MCMC base class). */ 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.NUTS * @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; /** * Returns the number of divergent transitions recorded during the current sampling phase. A * divergent transition is one whose leapfrog trajectory contained a leaf whose Hamiltonian drifted * more than the energy-divergence threshold from the trajectory's start, indicating the step size * is too large or the target geometry too extreme (biased exploration). Reset at construction and * at the start of each [sample]{@link ran.mc.MCMC#sample} call, so a read afterwards reflects the * sampling phase only — the same lifecycle as [ar]{@link ran.mc.MCMC#ar}. A well-behaved run reports 0. * * @method divergenceCount * @memberof ran.mc.NUTS * @returns {number} Count of divergent transitions since the last reset. */ divergenceCount(): number; /** * Returns the number of transitions that reached the maximum tree depth during the current sampling * phase. A max-depth hit is a doubling that exhausted the tree-depth cap without the trajectory * U-turning, indicating the step size is too small (inefficient sampling). Reset at construction and * at the start of each [sample]{@link ran.mc.MCMC#sample} call, so a read afterwards reflects the * sampling phase only — the same lifecycle as [ar]{@link ran.mc.MCMC#ar}. A well-behaved run reports 0. * * @method maxDepthCount * @memberof ran.mc.NUTS * @returns {number} Count of max-tree-depth-saturated transitions since the last reset. */ maxDepthCount(): number; _iter(x: any, warmUp: any): { x: any; accepted: boolean; alpha: number; divergent: boolean; maxDepthHit: boolean; }; _adjust(i: any): void; _internal(): { stepSize: 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; }; }; _initAccumulators(): void; _maybeFreezeStepSize(warmUp: any): void; _growTree(x: any, r0: any, ctx: any): { xNew: any; logPNew: any; alphaSum: number; nAlpha: number; divergent: boolean; maxDepthHit: boolean; }; _selectCandidate(subtree: any, n: any, candidate: any): any; _buildTree(node: any, v: any, j: any, ctx: any): any; _buildTreeLeaf(node: any, v: any, ctx: any): { xMinus: any; rMinus: any; velMinus: any; xPlus: any; rPlus: any; velPlus: any; xPrime: any; logPPrime: any; nPrime: number; sPrime: number; divergent: boolean; alphaSum: number; nAlpha: number; }; _buildTreeBranch(node: any, v: any, j: any, ctx: any): any; _combineSubtrees(first: any, second: any, v: any): { xMinus: any; rMinus: any; velMinus: any; xPlus: any; rPlus: any; velPlus: any; xPrime: any; logPPrime: any; nPrime: any; sPrime: number; divergent: any; alphaSum: any; nAlpha: any; }; _leapfrog(x: any, r: any, eps: any): { x: any; r: any; vel: any; }; static _validateOptions(options: any): void; static _isPlainObject(options: any): boolean; static _validateGradLogDensity(gradLogDensity: any): void; static _validateStepSize(stepSize: any): void; static _noUTurn(xMinus: any, velMinus: any, xPlus: any, velPlus: any): 0 | 1; }