Note: This page has been translated by MathWorks. Please click here

To view all translated materals including this page, select Japan from the country navigator on the bottom of this page.

To view all translated materals including this page, select Japan from the country navigator on the bottom of this page.

For more complex probability distributions, you might need more advanced methods for generating samples than the methods described in Common Pseudorandom Number Generation Methods. Such distributions arise, for example, in Bayesian data analysis and in the large combinatorial problems of Markov chain Monte Carlo (MCMC) simulations. An alternative is to construct a Markov chain with a stationary distribution equal to the target sampling distribution, using the states of the chain to generate random numbers after an initial burn-in period in which the state distribution converges to the target.

The Metropolis-Hastings algorithm draws samples from a distribution that is only known up to a constant. Random numbers are generated from a distribution with a probability density function that is equal to or proportional to a proposal function.

To generate random numbers:

Assume an initial value

(*x*).*t*Draw a sample,

(*y*), from a proposal distribution*t*(*q*|*y*(*x*)).*t*Accept

(*y*) as the next sample*t*(*x*+ 1) with probability*t*(*r*(*x*),*t*(*y*)), and keep*t*(*x*) as the next sample*t*(*x*+ 1) with probability 1 –*t*(*r*(*x*),*t*(*y*)), where:*t*$$r(x,y)\text{\hspace{0.17em}}\text{=}\text{}\text{\hspace{0.17em}}min\left\{\frac{f(y)}{f(x)}\frac{q(x|y)}{q(y|x)},\text{}\text{\hspace{0.17em}}1\right\}$$

Increment

→*t*+ 1, and repeat steps 2 and 3 until you get the desired number of samples.*t*

Generate random numbers using the Metropolis-Hastings method
with the `mhsample`

function. To
produce quality samples efficiently with the Metropolis-Hastings algorithm,
it is crucial to select a good proposal distribution. If it is difficult
to find an efficient proposal distribution, use slice sampling (`slicesample`

) or Hamiltonian Monte Carlo
(`hmcSampler`

) instead.

In instances where it is difficult to find an efficient Metropolis-Hastings
proposal distribution, the slice sampling algorithm does not require
an explicit specification. The slice sampling algorithm draws samples
from the region under the density function using a sequence of vertical
and horizontal steps. First, it selects a height at random from 0
to the density function *f *(*x*).
Then, it selects a new *x* value at random by sampling
from the horizontal "slice" of the density above the
selected height. A similar slice sampling algorithm is used for a
multivariate distribution.

If a function *f*(*x*)
proportional to the density function is given, then do the following
to generate random numbers:

Assume an initial value

*x*(*t*) within the domain of*f*(*x*).Draw a real value

*y*uniformly from (0,(*f*(*x*))), thereby defining a horizontal "slice" as*t*= {*S*:*x*<*y*(*f*)}.*x*Find an interval

= (*I*,*L*) around*R*(*x*) that contains all, or much of the "slice"*t*.*S*Draw the new point

(*x*+ 1) within this interval.*t*Increment

→*t*+ 1 and repeat steps 2 through 4 until you get the desired number of samples.*t*

Slice sampling can generate random numbers from a distribution
with an arbitrary form of the density function, provided that an efficient
numerical procedure is available to find the interval * I* =
(

Generate random numbers using the slice sampling method with
the `slicesample`

function.

Metropolis-Hastings and slice sampling can produce MCMC chains that mix slowly and take a long time to converge to the stationary distribution, especially in medium-dimensional and high-dimensional problems. Use the gradient-based Hamiltonian Monte Carlo (HMC) sampler to speed up sampling in these situations.

To use HMC sampling, you must specify log * f(x)* (up
to an additive constant) and its gradient. You can use a numerical
gradient, but this leads to slower sampling. All sampling variables
must be unconstrained, meaning that log

The HMC sampling algorithm introduces a random "momentum
vector" * z* and defines a joint density of

The HMC algorithm assigns a Gaussian density with covariance
matrix * M* (the "mass matrix") to

$$g(z)\propto \mathrm{exp}\left(-\frac{1}{2}{z}^{T}{M}^{-1}z\right)$$

Then, it defines an "energy function" as

$$E(x,z)=-\mathrm{log}f(x)+\frac{1}{2}{z}^{T}{M}^{-1}z=U(x)+K(z)$$

with * U(x)
= * – log

To generate random samples, the HMC algorithm:

Assumes an initial value

of the position vector.*x*Generates a sample of the momentum vector:

.*z ∼ g(z)*Evolves the state

for some amount of fictitious time*(x, z)*to a new state*τ*using the "equations of motion":*(x',z')*$$\frac{dz}{d\tau}=-\frac{\partial U}{\partial x}$$

$$\frac{dx}{d\tau}=\frac{\partial K}{\partial z}$$

If the equations of motion could be solved exactly, the energy (and hence the density) would remain constant:

. In practice, the equations of motions must be solved numerically (usually using so-called leapfrog integration) and the energy is not conserved.*E(x,z) = E(x',z')*Accepts

as the next sample with probability*x'**p*_{acc}= min(1, exp{}), and keeps*E(x,z) – E(x',z')*as the next sample with probability 1 –*x**p*_{acc}.Repeats steps 2 through 4 until it has generated the desired number of samples.

To use HMC sampling, create a sampler using the `hmcSampler`

function. After creating a sampler,
you can compute MAP (maximum-a-posteriori) point estimates, tune the
sampler, draw samples, and check convergence diagnostics. For an example
of this workflow, see Bayesian Linear Regression Using Hamiltonian Monte Carlo.

Was this topic helpful?