Assume that we are given a probability distribution on and for simplicity we further assume that is continuous, i.e. somehow indicates, how likely is. To be more precise, the probability, that for some (measurable) set is, is . We do I start like this? There are cases, in which one known some probability distribution, then obtains some and wants to know, if this was particularly representative for this distribution. One example of this situation is a **statistical inverse problem** where the model is with a linear (known) map and some error . From some observed , one wants to know as much as possible about the underlying . Assuming that the noise has a known distribution and that one has *prior information* on (i.e. likely are ones where the *prior distribution* is large) one can model the following probability distributions:

The latter distribution, the posterior, is what one is interested in, i.e. given that one has observed , what is the probability that some (which we may generate in some way) is the true solution? So, in principle, all information we have is contained in the posterior, but how to get on grip on it?

What we know are the distribution and but note that is not know and is usually not simple to compute. Another expression for is since the right hand side in the definition of the posterior has to be a probability distribution. So to calculate we have to evaluate one integral, but note that this is in an integral over and in the context of statistical inverse problems, this is usually not in the ten-thousands, but easily in the millions. So we see, that the calculation of is usually out of reach (unless all distributions are very simple). So where are we: If we get some we can calculate and , but what would it say if , for example? Not much, because we do not know the the normalization constant. While seems pretty small and hence to be somehow unlikely, it may be that and then which would make to be rather likely. On the other hand we would really would like some more information about to judge about that since may have a large variance and a value of may be among the largest possible values, again rendering quite likely, but subject to large variance. To recap:

- We have a probability distribution but we can only generate values for some
*unknown*. - The domain of definition of has a huge dimension.
- We want to know as much as possible about , e.g. its expected value, its variance or its mode…

Note that some questions about can be answered without knowing the normalization constant, e.g. the mode , i.e. the which maximizes . That may one prime reason while MAP (maximum-a-posterior) estimators are so widely used… One approach, to get more information out of is to use *sampling*.

Before we come to methods that can sample from unnormalized distributions, we describe what we actually mean by sampling, and give the main building blocks.

**1. Sampling from simple distribution **

*Sampling from a distribution* means a method to generate values such that the values are distributed according to . Expressed in formula: For every integrable function and sequence of generated samples, we want that

In the following we want to describe a few methods to generate samples. All the methods will build on the ability to sample from some simple basic distributions, and these are

- The uniform distribution on denoted by . On a computer this is possible to a good approximation by pseudorandom number generators like the Mersenne twister (used, e.g., in MATLAB).
- The standard normal distribution denoted by . If you generate pseudorandom normally distributed numbers, then, under the hood, the machine generates uniformly distributed numbers and cleverly transforms these to be normally distributed, e.g. with the Box-Muller method.

Note that simple scaling allows to sample from and .

If we sample from some distribution we will denote this by

so means that is drawn from a uniform distribution over .

**2. Rejection sampling **

For *rejection sampling* from (having access to only) we need a so-called *proposal distribution* from which we can sample (i.e. a uniform one or a normal one) and we need to know some such that

for all . The sampling scheme goes as follows: Draw proposals from the proposal distribution and accept them based on some rule that will ensure the we will end up with samples distributed according to . In more detail:

- generate , i.e. sample from the proposal distribution,
- calculate the
*acceptance rate* - generate ,
- accept if , otherwise reject it.

Proposition 1The samples generated by rejection sampling are distributed according to .

*Proof:* Let us denote by the event that a sample , drawn from has been accepted. Further, we denote by the distribution of , provided it has been accepted. So we aim to show that .

To do so, we employ Bayes’ theorem and write

For the three probabilities on the right hand side we know:

- is the distribution of the sample, i.e the proposal distribution, .
- The probability is the probability of acceptance, provided that has been sampled. Looking at the acceptance step (step 2), we see that this probability is exactly , i.e.
- The probability of the event is the probability of acceptance, and this is given by the integral of the joint distribution over the values . Since the joint distribution fulfills and we get
since is a probability distribution.

Together we get

The crucial steps to apply rejection sampling is to find a good proposal distribution with small constant (and also, to calculate can be tricky). As an example, consider that you want to sample from the tail of a standard normal distribution, e.g. you want to obtain “normally distributed random numbers larger that ”. Rejection sampling is pretty straightforward: You choose as standard normal, get samples from that and only accept if . In this case you have , but is small, namely which means that less than 1 out of 43 samples are accepted…

**3. (Non-adaptive) Metropolis sampling **

Here comes another method. This method is from the class of *Markov-Chain* methods, i.e. we will generate a sequence such that they form a Markov chain with a given transition probability. This means, we have a distribution such that . Again, our goal is that the sequence is distributed according to . A central result in the theory of Markov-chain methods is, that this is the case when the so-called *(detailed) balance equation is fulfilled* , i.e. if

Remember, that we only have access to and we don’t know . Here is the method which is called *Metropolis-Hastings sampling*: To begin, choose a symmetric *proposal distribution* , (i.e. is the distribution for “go from to ” and fulfills ), initialize with some and set . Then:

- generate , i.e. make a trial step from ,
- set ,
- generate
- accept with probability , i.e. set if , increase and repeat, otherwise do reject, i.e. do not increase and repeat.

Note that in step 3 we don’t really need and but only and since the unknown ‘s cancel out.

Proposition 2The samples generated from Metropolis-Hastings sampling are distributed according to .

*Proof:* We first calculate the transition probability of the method. The probability to go from to the proposed is “ multiplied by the acceptance rate ” and the probability to stay in is . In formula: We write the acceptance ratio as

and can write the transition probability as

with

We aim to show the detailed balance equation . First by a simple distinction of cases, we get

This gives and since is symmetric, we get

Then we note that

(which can be seen by integration both sides against some ). Putting things together, we get

as desired.

Note that Metropolis-Hastings sampling is fairly easy to implement as soon as it is simple to get samples from the proposal distribution. A quick way is to use the standard normal distribution as proposal distribution. In this case, Metropolis-Hastings performs a “restricted random walk”, i.e. if we would accept every step, the sequence would be a random walk. However, we allow for the possibility to stop the walk in the cases where it would lead us to a region of lower probability — in the cases where it leads to a point of higher probability, we follow the random walk. Although this approach is simple to implement, it may take a lot of time for the chain to get something reasonable. The reasons are, that the random walk steps may be rejected quite often, that it is a long way to get to where the “most probability is” from the initialization or that the distribution has several modes, separated by valleys and that the random walk has difficulties to get from one mode to the other (again due to frequent rejection).

Also note that one can use Gaussian distributions with any variance as proposal distributions and that the variance acts like some stepsize. As often with stepsizes, there is a trade-off: smaller stepsizes mean, that the sampler will need more time to explore the distribution while larger stepsizes would allow faster exploration but also lead to more frequent rejection.