Snapshot 1: sampling from a Gaussian distribution
Snapshot 2: sampling from a gamma distribution
Snapshot 3: sampling from a linear combination of gamma distributions; 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 that generates pseudorandom 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 that it requires no external parameters to be set.
Given a starting point
, usually drawn from a uniform distribution over the abscissas, the
iteration of the algorithm proceeds as follows :
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 step 3.
5. For iteration
, repeat from step 1 using
as the new starting value.
In this Demonstration, step 3 is performed by interpolating between values in a lookup 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 that contracts and expands exist to address these issues [1, 2].
 D. J. C. MacKay, Information Theory, Inference, and Learning Algorithms
New York: Cambridge University Press, 2003.
 R. M. Neal, "Slice Sampling," The Annals of Statistics, 31
(3), 2003 pp. 705–767.