Sampling a Distribution using Slice Sampling

Slice sampling is a Markov chain Monte Carlo method used to sample points from a given distribution. The algorithm may be summarized as follows: given the previously sampled point, indicated by a purple dashed line, the corresponding ordinate is evaluated. A random number is drawn from a uniform distribution from zero to the ordinate value, indicated by a green circle. The intersections of a horizontal line (slice) at this value with the distribution curve is calculated. From the regions where the curve lies above the horizontal line, a second uniformly distributed random value is drawn, indicated by a red circle. This value is taken as the new sample point, and the algorithm repeats using this as the new starting point. A histogram of the sampled points is shown at the bottom. Adjust the slider to view the next or previous sample points. The distribution shapes may be chosen as Gaussian, gamma, or a linear combination of multiple Gaussian or gamma distributions. To generate new random variables for the current sample point, press the "generate random sample" button.
  • Contributed by: Oliver K. Ernst



  • [Snapshot]
  • [Snapshot]
  • [Snapshot]


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.