Slice sampling is a type of
Markov chain Monte Carlo
In statistics, Markov chain Monte Carlo (MCMC) methods comprise a class of algorithms for sampling from a probability distribution. By constructing a Markov chain that has the desired distribution as its equilibrium distribution, one can obtain ...
algorithm
In mathematics and computer science, an algorithm () is a finite sequence of rigorous instructions, typically used to solve a class of specific problems or to perform a computation. Algorithms are used as specifications for performing ...
for
pseudo-random number sampling
Non-uniform random variate generation or pseudo-random number sampling is the numerical practice of generating pseudo-random numbers (PRN) that follow a given probability distribution.
Methods are typically based on the availability of a unifo ...
, i.e. for drawing random samples from a statistical distribution. The method is based on the observation that to sample a
random variable
A random variable (also called random quantity, aleatory variable, or stochastic variable) is a mathematical formalization of a quantity or object which depends on random events. It is a mapping or a function from possible outcomes (e.g., the p ...
one can sample uniformly from the region under the graph of its density function.
Motivation
Suppose you want to sample some random variable ''X'' with distribution ''f''(''x''). Suppose that the following is the graph of ''f''(''x''). The height of ''f''(''x'') corresponds to the likelihood at that point.

If you were to uniformly sample ''X'', each value would have the same likelihood of being sampled, and your distribution would be of the form ''f''(''x'') = ''y'' for some ''y'' value instead of some non-uniform function ''f''(''x''). Instead of the original black line, your new distribution would look more like the blue line.

In order to sample ''X'' in a manner which will retain the distribution ''f''(''x''), some sampling technique must be used which takes into account the varied likelihoods for each range of ''f''(''x'').
Method
Slice sampling, in its simplest form, samples uniformly from underneath the curve ''f''(''x'') without the need to reject any points, as follows:
# Choose a starting value x
0 for which ''f''(''x''
0) > 0.
# Sample a value uniformly between 0 and ''f''(''x''
0).
# Draw a horizontal line across the curve at this position.
# Sample a point (, ) from the line segments within the curve.
# Repeat from step 2 using the new value.
The motivation here is that one way to sample a point uniformly from within an arbitrary curve is first to draw thin uniform-height horizontal slices across the whole curve. Then, we can sample a point within the curve by randomly selecting a slice that falls at or below the curve at the x-position from the previous iteration, then randomly picking an x-position somewhere along the slice. By using the x-position from the previous iteration of the algorithm, in the long run we select slices with probabilities proportional to the lengths of their segments within the curve.
The most difficult part of this algorithm is finding the bounds of the horizontal slice, which involves inverting the function describing the distribution being sampled from. This is especially problematic for multi-modal distributions, where the slice may consist of multiple discontinuous parts. It is often possible to use a form of rejection sampling to overcome this, where we sample from a larger slice that is known to include the desired slice in question, and then discard points outside of the desired slice.
This algorithm can be used to sample from the area under ''any'' curve, regardless of whether the function integrates to 1. In fact, scaling a function by a constant has no effect on the sampled x-positions. This means that the algorithm can be used to sample from a distribution whose
probability density function
In probability theory, a probability density function (PDF), or density of a continuous random variable, is a function whose value at any given sample (or point) in the sample space (the set of possible values taken by the random variable) c ...
is only known up to a constant (i.e. whose
normalizing constant
The concept of a normalizing constant arises in probability theory and a variety of other areas of mathematics. The normalizing constant is used to reduce any probability function to a probability density function with total probability of one.
...
is unknown), which is common in
computational statistics
Computational statistics, or statistical computing, is the bond between statistics and computer science. It means statistical methods that are enabled by using computational methods. It is the area of computational science (or scientific comput ...
.
Implementation
Slice sampling gets its name from the first step: defining a ''slice'' by sampling from an auxiliary variable
. This variable is sampled from