Why Use Hamiltonian Monte Carlo?
A joint-model posterior can contain many correlated continuous quantities: longitudinal fixed effects, association parameters, and a latent random effect for each subject. A random-walk proposal may move only a short distance before its acceptance rate becomes poor. Hamiltonian Monte Carlo (HMC) uses the gradient of the log posterior to guide proposals across the posterior, potentially making larger moves while retaining a Metropolis acceptance step to correct for numerical error.
HMC is a computation method, not a different joint model. It targets the same posterior distribution as other valid MCMC updates. Its main requirements are a continuously differentiable log posterior over the parameters being updated, a way to evaluate its gradient, and a proper target distribution. Discrete quantities cannot be included in an ordinary HMC trajectory. Parameters constrained to a positive range or another restricted space require a suitable transformation or a method that handles the constraint.
A Joint-Model Target and Its Gradient
For a concrete derivation, consider a shared-random-intercept model. Subject \(i\) has longitudinal measurements \(y_{ij}\) at times \(t_{ij}\), and an observed event or censoring time \(T_i\) with event indicator \(\delta_i\). The longitudinal mean is \(\eta_i=\beta+b_i\), where \(\beta\) is a fixed intercept and \(b_i\) is a subject-specific random intercept. Suppose \(y_{ij}\mid b_i,\beta\sim N(\eta_i,\sigma^2)\), \(b_i\sim N(0,\tau^2)\), and \(\beta\sim N(0,s_\beta^2)\). For illustration, let the event hazard be constant over time for subject \(i\), conditional on their shared level: \(h_i(t)=\lambda\exp(\alpha\eta_i)\). Here \(\alpha\) is the association parameter, and \(\sigma,\tau,s_\beta,\lambda\) are treated as known positive constants. Take \(\alpha\sim N(0,s_\alpha^2)\). This deliberately simple specification makes the gradient visible without changing the central HMC construction.
The event contribution is \(\delta_i\log h_i-T_i h_i\): an observed event contributes the log hazard as well as the cumulative hazard term, while a censored observation contributes only the survival term. Up to constants not depending on \((\beta,\alpha,b_1,\ldots,b_n)\), the log posterior is
The normalizing constants omitted here do not depend on the HMC parameters, so they do not affect the gradient or acceptance ratio. Differentiating gives
These expressions show how the sources of information combine. For example, each \(b_i\) receives a longitudinal contribution from that subject's residuals, a shrinkage contribution from its normal prior, and an event contribution through the shared level \(\eta_i\). The fixed effect \(\beta\) appears in every subject's level, so its gradient collects contributions across subjects as well as its prior term.
Worked Example: Calculating a Subject-Specific Gradient
Suppose one hypothetical subject has measurements \(y_{i1}=1\) and \(y_{i2}=2\), and \(T_i=2,\ \delta_i=1\). Set \(\beta=0.5,\ b_i=0.2,\ \alpha=0.4\), with \(\sigma=\tau=\lambda=1,\ s_\beta=2,\ s_\alpha=1\). Then \(\eta_i=0.5+0.2=0.7\), and the residuals are \(1-0.7=0.3\) and \(2-0.7=1.3\). Also, \(\exp(\alpha\eta_i)=\exp(0.28)\approx1.3231\).
The gradient with respect to this subject's random effect is \(\partial\ell/\partial b_i=(0.3+1.3)-0.2+0.4-2(0.4)(1.3231)\). The arithmetic gives \(1.6-0.2+0.4-1.0585\approx0.7415\). At this parameter value, increasing \(b_i\) slightly increases the log posterior to first order.
For comparison, the fixed-effect gradient in this one-subject illustration is \(1.6-0.5/4+0.4-1.0585\approx0.8165\). The association-parameter gradient is \(-0.4+0.7-2(0.7)(1.3231)\approx-1.5524\). These are local derivatives, not posterior summaries or evidence that any parameter should be increased without limit.
Hamiltonian Dynamics and Leapfrog Updates
Let \(\mathbf q\) denote the vector of parameters being updated, such as \((\beta,\alpha,b_1,\ldots,b_n)\). Write \(\ell(\mathbf q)\) for its log posterior. HMC introduces an auxiliary momentum \(\mathbf p\sim N(\mathbf 0,M)\), where \(M\) is a positive-definite mass matrix chosen by the implementation. The Hamiltonian, a negative log density on the augmented space up to an additive constant, is
The corresponding dynamics are \(d\mathbf q/dt=M^{-1}\mathbf p\) and \(d\mathbf p/dt=\nabla\ell(\mathbf q)\). If these equations could be followed exactly, they would conserve \(H\). In practice, HMC uses a numerical integrator. With step size \(\epsilon\), one leapfrog step is
Repeating these steps for a chosen number of steps produces a proposed endpoint. The Metropolis acceptance probability is \(\min\{1,\exp[H(\mathbf q_0,\mathbf p_0)-H(\mathbf q_1,\mathbf p_1)]\}\). Implementations commonly reverse the endpoint momentum when forming the proposal; this makes the numerical trajectory reversible. Resampling momentum for each trajectory and applying the acceptance correction are essential parts of the update, not optional refinements.
Worked Example: One Leapfrog Step and Its Acceptance Probability
To check the mechanics numerically, take a one-dimensional standard normal target, so \(\ell(q)=-q^2/2\), with \(M=1\). Start at \(q_0=1,\ p_0=0.5\), and use step size \(\epsilon=0.2\). Since \(\nabla\ell(q)=-q\), the first half-step is \(p_{1/2}=0.5+(0.2/2)(-1)=0.4\). The position update gives \(q_1=1+0.2(0.4)=1.08\), followed by \(p_1=0.4+(0.2/2)(-1.08)=0.292\).
The initial Hamiltonian is \(H_0=(1^2+0.5^2)/2=0.625\). At the endpoint, \(H_1=(1.08^2+0.292^2)/2=(1.1664+0.085264)/2=0.625832\). Therefore the acceptance probability is \(\min\{1,\exp(0.625-0.625832)\}\approx0.9992\). The proposal is accepted with this probability; it is not guaranteed to be accepted. This example illustrates the numerical correction, rather than a joint-model-specific calculation.
Implementation Choices for Joint Models
For continuous fixed effects and latent random effects, one possible HMC state is the entire vector \((\beta,\alpha,\mathbf b)\), with the joint log-posterior gradient evaluated at every leapfrog position. The subject-level structure in the gradient can make evaluation efficient: longitudinal and event contributions can be accumulated subject by subject. The latent effects are part of the state, not integrated out in this update. Which parameters are updated together is an implementation decision; the gradient formula itself does not prescribe a particular block strategy.
Scaling matters because HMC advances all coordinates using a common step size. If one coordinate has much higher posterior curvature than another, a step size large enough to move effectively in the flatter direction may be too large for the sharply curved direction. The mass matrix changes the momentum-to-position scaling. In the convention above, for a one-dimensional Gaussian target with variance \(s^2\), choosing \(M=1/s^2\) makes the local oscillation frequency \(\sqrt{(1/s^2)/M}=1\). In practice, a diagonal or more general estimated mass matrix can help account for different scales and correlations. Software may describe its tuning quantity using an inverse-mass convention, so verify the convention rather than transferring matrix values by name alone.
If a parameter must be positive, such as a standard deviation or hazard rate, one option is to sample an unconstrained log parameter \(z\) and set \(\theta=\exp(z)\). The target density in \(z\) must include the change-of-variables Jacobian: \(\log|d\theta/dz|=z\). Omitting this term targets the wrong posterior. More generally, gradients must be taken with respect to the coordinates actually used by the sampler.
Worked Example: Diagnosing a Scaling Mismatch
Suppose, as a hypothetical local approximation to two posterior coordinates, one coordinate has standard deviation \(0.1\) and the other has standard deviation \(10\), with negligible correlation. Their Gaussian negative-log-posterior curvatures are \(1/0.1^2=100\) and \(1/10^2=0.01\). With identity mass matrix, the corresponding oscillation frequencies are \(\sqrt{100}=10\) and \(\sqrt{0.01}=0.1\), a factor of 100 apart.
A mass matrix with diagonal entries \(100\) and \(0.01\) gives frequencies \(\sqrt{100/100}=1\) and \(\sqrt{0.01/0.01}=1\) under the stated convention. This calculation motivates scaling adaptation; it does not establish that those entries are suitable for a real posterior. Joint-model posteriors need not be Gaussian, and correlations or changing curvature can remain even after scaling.
Diagnostics, Alternatives, and Limitations
A high rejection rate or frequent large changes in Hamiltonian energy can signal that the step size is too large, gradients are inaccurate, or the posterior has difficult curvature. Very small steps may avoid numerical failure but require many gradient evaluations to travel a useful distance. Check analytic gradients against finite-difference checks at representative parameter values, and inspect whether the log posterior and gradient remain finite over the trajectory. Extreme exponentials in an event likelihood, coding errors in the censoring contribution, and an omitted transformation Jacobian are particularly consequential failure modes.
HMC is most useful when the target is continuous and gradients are available and reliable. Random-walk Metropolis requires no gradients and can accommodate a broader range of proposal designs, but often explores strongly correlated continuous targets inefficiently. Gibbs updates can be effective when full conditional distributions are available in a tractable form; HMC is not a substitute for an exact conditional update that is already convenient. In a joint model, HMC can also be less effective when the posterior geometry is poorly scaled, contains sharp or narrow regions, or has nondifferentiable boundaries. Discrete model indicators require other update mechanisms.
A common mistake is to interpret a large HMC proposal as evidence that the chain is mixing well. A long numerical trajectory can instead accumulate large integration error and be rejected, while an accepted trajectory can still explore the posterior poorly if the parameterization or scaling is unfavorable. Acceptance alone does not establish convergence. Evaluate chain behavior with appropriate convergence and Monte Carlo diagnostics, and investigate divergences or unstable energy behavior rather than treating them as cosmetic warnings.
Check Your Understanding
Answer each question, then compare with the concise solutions below.
- In the example model, which three sources contribute to the gradient with respect to a subject's random intercept?
- For a trajectory with initial and final Hamiltonians \(H_0=3.2\) and \(H_1=3.5\), what is the Metropolis acceptance probability?
- Why must a log-transformed positive parameter include a Jacobian contribution in the transformed log target?
- What does a high rate of HMC rejection suggest, and why does a high acceptance rate alone not prove good mixing?
Solutions. (1) The subject's longitudinal residuals, the normal prior shrinkage term \(-b_i/\tau^2\), and the event contribution through the shared level. (2) It is \(\exp(3.2-3.5)=\exp(-0.3)\approx0.7408\), since this is less than one. (3) A change of variables changes density by the absolute derivative of the transformation; for \(\theta=\exp(z)\), its log is \(z\). Omitting it gives the wrong target density in \(z\). (4) It can indicate excessive integration error, an overly large step size, or unreliable gradients. High acceptance does not show that trajectories travel far enough or that chains explore all relevant posterior regions.