Goal: generate samples X1, ⋯, Xn ∈ ℝd approximately distributed from Π(x) ∝ e−Φ(x) by evaluating a potential function Φ and its gradient.
Example 1: Gaussian $$ \begin{aligned} \Phi(x)&=\frac{1}{2}(x-\mu)^\top\Sigma^{-1}(x-\mu)\\ \nabla\Phi(x)&=\Sigma^{-1}(x-\mu) \end{aligned} $$ Suppose we want to sample from a Gaussian distribution with heterogeneous scales, such that $$ \mu=0_d,\qquad \Sigma=\underset{1\le i\le d}{\rm diag}(\sigma_i^2),\qquad \sigma_i^2=i/d,\qquad d=50. $$ We specify the corresponding potential function and its gradient, as well as the starting values.
d=50
sigma=((d:1)/d)^(1/2)
U=function(x){sum(0.5*x^2/sigma^2)}
grad=function(x){x/sigma^2}
init=rep(5,d)
We choose a friction, time step and integration time. We run malt from a deterministic initialisation and draw n = 10000 samples. We also run hmc (friction: g=0) to compare. Both outputs are stored.
We obtain the acceptance rates of malt and hmc.
See also: Assessing the sampling efficiency.