Snapshot 1: sampling from a Gaussian distribution.
Snapshot 2: sampling from a gamma distribution.
Snapshot 3: sampling from a linear combination of gamma distributions to show an example involving divided slices.
Consider the problem of drawing samples from an arbitrary distribution. If the cumulative distribution function (CDF) and its inverse are known and easy to compute, methods such as inversion sampling may be used. In practice, however, this is rarely the case. Many Monte Carlo methods such as rejection sampling require no knowledge of the CDF but are computationally expensive. The slice sampling algorithm is a Markov chain Monte Carlo method which generates pseudo-random numbers from a distribution by sampling uniformly from horizontal slices through the curve. Advantages of the algorithm include its simplicity, that it involves no rejections and requires no external parameters to be set.
Given a starting point

, usually drawn from a uniform distribution over the abscissas, the

th iteration of the algorithm proceeds as follows [1]:
1. The distribution is evaluated for

.
2. A random variable

is drawn from a uniform distribution over the interval

.
3. Take a horizontal slice of the distribution at height

. Determine the regions where the curve

lies above the slice.
4. Generate a new sample point

by drawing from a uniform distribution over the regions in (3).
5. Repeat for the

st iteration from step (1) using

as the new starting value.
In this demonstration, step (3) is performed by interpolating between values in a look-up table of distribution values

. In practice, however, this method may be inaccurate, and finding exact solutions for the intersections inefficient. Modified algorithms that employ a slice width which contracts and expands exist to address these issues [1,2].
[1] D. J. MacKay,
Information Theory, Inference, and Learning Algorithms, Cambridge: Cambridge University Press, 2003.
[2] R. M. Neal, "Slice Sampling,"
The Annals of Statistics, 31(3), 2003 pp. 705-767.