Search

Search papers and lessons

Slice Sampling

Level 3 · 11 min read

Slice sampling draws from a density by adding a random height under the curve and then sampling uniformly from the horizontal slice at that height, with no rejections and little tuning.

On this page

  • Colour = topic mix
  • Probability
  • Discrete math
  • Calculus
  • Linear algebra
  • Sampling
  • Stochastic processes
  • Sign in to see your progress

TL;DR

Slice sampling is a way to draw samples from a density that you can only evaluate up to a constant. Each step first picks a random height below the curve, then draws the next point uniformly from the horizontal slice of the curve at that height. Because every point under the curve is equally likely, the points you keep follow the density. The method has no accept-or-reject step on a proposed move, and it needs only one scale setting to work well. (Quantities known "up to a constant" are known only in proportion: you can evaluate f(x)f(x), but not the total area under it.)

It is a Markov chain method, so it fits in the same family as the Metropolis algorithm, but it avoids the accept-or-reject step that makes Metropolis tuning fiddly.

Where you'll see it: in Bayesian software that needs a sampler with no tuning, in one-dimensional steps inside larger samplers, and in the trajectory-length step of the No-U-Turn Sampler.

Intuition

Sampling points under a curve

Suppose you want samples from a bell-shaped density, such as a Gaussian. Draw the curve on a sheet of paper. Now imagine scattering darts uniformly over the whole region under the curve. Each dart lands somewhere under the curve. If you keep only the horizontal positions of the darts, the density of those positions is exactly the shape of the curve, because tall parts of the curve have more area under them.

Sampling darts uniformly under a curve is hard in general. Slice sampling breaks the problem into two easier moves that a computer can repeat.

  1. Given your current point xx, drop a dart at a random height uu between 00 and the curve's height at xx. This makes a point (x,u)(x, u) that is still under the curve.
  2. Draw the next xx uniformly from the set of horizontal positions where the curve is higher than uu. That set is called the slice.

Each move keeps the joint point uniform under the curve, so the chain's xx values keep the right distribution.

Why it helps

Metropolis-style methods propose a jump and then accept or reject it, which wastes work when the proposal is badly sized. Slice sampling never throws away a move, unlike the accept-or-reject step of Rejection Sampling or Metropolis. Its only choice is the initial width of the search interval, and a wrong width costs a few extra function evaluations, not a wrong answer. That robustness is why it is popular for quick samplers and for sampling inside other algorithms.

−3−2−1012300.20.40.60.81xf(x)
  • target f(x)
  • height u = 0.3
The slice at height u = 0.3 is the set of x where the curve is above the dashed line, here between the two marked points.

The math

The auxiliary variable

Let f(x)≥0f(x) \ge 0 be the target, known up to a constant. Introduce an auxiliary variable uu, called the height. Define the joint density

p(x,u)∝{1if 0<u<f(x),0otherwise.p(x, u) \propto \begin{cases} 1 & \text{if } 0 \lt u \lt f(x), \\ 0 & \text{otherwise.} \end{cases}

Here p(x,u)p(x,u) is uniform on the region under the graph of ff. Summing over uu gives the marginal of xx. The height of the region at xx is f(x)f(x), so the marginal is proportional to f(x)f(x), which is the target.

The slice

For a fixed height uu, the slice is

S(u)={x:f(x)>u}.S(u) = \{ x : f(x) \gt u \}.

The slice is the set of horizontal positions where the curve sits above uu.

One update

Given the current x0x_0:

  1. Draw u∼Uniform(0,f(x0))u \sim \mathrm{Uniform}(0, f(x_0)), a uniform random number between 0 and f(x0)f(x_0). This is the random height under the curve.
  2. Draw x1x_1 uniformly from S(u)S(u).

Step 1 keeps the pair uniform under the graph. Step 2 is the conditional move that keeps the pair uniform. Together they leave the marginal of xx unchanged, so the chain has ff as its stationary distribution (the distribution that, once reached, further steps do not change).

Stepping out and shrinking

The slice is usually not known in closed form, so Neal's procedure finds it with an interval. Start with a width ww placed randomly around x0x_0, and repeat:

  1. Step out. Extend the left end by ww while ff is still above uu there, and do the same on the right.
  2. Shrink. Draw a point uniformly from the interval. If ff at that point is above uu, accept it. If not, shrink the interval toward x0x_0 and draw again.

The interval only needs to cover the slice, so ww can be off by a factor of several and the procedure still works. Each shrink step reduces the interval, and the process always ends because x0x_0 itself lies in the slice.

Log scale

For densities so small that a computer rounds them to zero (underflow), work with log⁡f\log f. Draw the height as log⁡u=log⁡f(x0)−E\log u = \log f(x_0) - E, where E∼Exponential(1)E \sim \mathrm{Exponential}(1) (an exponential random number with mean 1) and u=f(x0)e−Eu = f(x_0) e^{-E} is uniform below the curve. The slice test becomes log⁡f(x)>log⁡u\log f(x) \gt \log u.

Worked examples

Example
Example 1: an arch-shaped target (tiny)

Let f(x)=x(2−x)f(x) = x(2 - x) on [0,2][0, 2], an arch (a downward parabola) with its peak at x=1x = 1. Start at x0=0.5x_0 = 0.5, where f(0.5)=0.75f(0.5) = 0.75.

  1. Draw the height uu uniformly from (0,0.75)(0, 0.75). Suppose it comes out as u=0.4u = 0.4.
  2. The slice is where x(2−x)>0.4x(2 - x) \gt 0.4, or x2−2x+0.4<0x^2 - 2x + 0.4 \lt 0. The roots are x=1±0.6≈1±0.775x = 1 \pm \sqrt{0.6} \approx 1 \pm 0.775, so the slice is (0.225,1.775)(0.225, 1.775).
  3. Draw x1x_1 uniformly from that interval. Suppose it lands at x1=1.2x_1 = 1.2.
  4. Check: f(1.2)=1.2×0.8=0.96>0.4f(1.2) = 1.2 \times 0.8 = 0.96 \gt 0.4, so the point is in the slice.

Interpretation: the slice has length 1.551.55, about 78% of the support. Most of the time the chain would move to a point of similar height, and the shape of ff decided the answer without any rejection.

Example
Example 2: a standard normal target (standard case)

Let f(x)=e−x2/2f(x) = e^{-x^2/2}, the standard normal up to its constant. Start at x0=1x_0 = 1, where f(1)=e−0.5≈0.6065f(1) = e^{-0.5} \approx 0.6065.

  1. Draw the height uu from (0,0.6065)(0, 0.6065). Suppose u=0.3u = 0.3.
  2. The slice is where e−x2/2>0.3e^{-x^2/2} \gt 0.3, which means x2<−2ln⁡0.3≈2.408x^2 \lt -2\ln 0.3 \approx 2.408. So the slice is (−1.552,1.552)(-1.552, 1.552).
  3. Use stepping out with w=1w = 1. Place the initial interval at [0.4,1.4][0.4, 1.4]. The right end, f(1.4)≈0.375>0.3f(1.4) \approx 0.375 \gt 0.3, so extend to 2.42.4. Now f(2.4)≈0.056<0.3f(2.4) \approx 0.056 \lt 0.3, so stop on the right.
  4. The left end, f(0.4)≈0.923>0.3f(0.4) \approx 0.923 \gt 0.3, so extend to −0.6-0.6. Then f(−0.6)≈0.835>0.3f(-0.6) \approx 0.835 \gt 0.3, so extend to −1.6-1.6. Now f(−1.6)≈0.278<0.3f(-1.6) \approx 0.278 \lt 0.3, so stop on the left.
  5. The interval is [−1.6,2.4][-1.6, 2.4]. Draw x1x_1 uniformly from it. Suppose x1=−0.9x_1 = -0.9.
  6. Check: f(−0.9)=e−0.405≈0.667>0.3f(-0.9) = e^{-0.405} \approx 0.667 \gt 0.3. Accept.

Interpretation: the interval [−1.6,2.4][-1.6, 2.4] covers the slice (−1.552,1.552)(-1.552, 1.552) with some extra room. The shrink step would have pulled in any draw outside the slice, so the accepted point is always valid.

Example
Example 3: a log-density in software (realistic)

A Bayesian model gives log-density log⁡f(x)=−12(x−2)2\log f(x) = -\tfrac{1}{2}(x - 2)^2 up to a constant. Software returns only log values, so we work on the log scale. Start at x0=0x_0 = 0, where log⁡f(0)=−2\log f(0) = -2.

  1. Draw E∼Exponential(1)E \sim \mathrm{Exponential}(1). Suppose E=0.7E = 0.7.
  2. The log height is log⁡u=log⁡f(x0)−E=−2−0.7=−2.7\log u = \log f(x_0) - E = -2 - 0.7 = -2.7.
  3. The slice is where −12(x−2)2>−2.7-\tfrac{1}{2}(x-2)^2 \gt -2.7, which means (x−2)2<5.4(x - 2)^2 \lt 5.4. So ∣x−2∣<2.324|x - 2| \lt 2.324, and the slice is (−0.324,4.324)(-0.324, 4.324).
  4. Stepping out with w=2w = 2 places the interval at [−1,1][-1, 1]. The right end at 11 has log density −0.5>−2.7-0.5 \gt -2.7, so extend to 33. There the log density is still −0.5-0.5, so extend to 55, where it is −4.5<−2.7-4.5 \lt -2.7, and stop. The left end at −1-1 has log density −4.5-4.5, already outside the slice, so it does not move. The interval is [−1,5][-1, 5], which covers (−0.324,4.324)(-0.324, 4.324).
  5. Draw x1x_1 uniformly from [−1,5][-1, 5]. Suppose it lands at x1=2.5x_1 = 2.5. Its log density is −12(0.5)2=−0.125>−2.7-\tfrac{1}{2}(0.5)^2 = -0.125 \gt -2.7, so accept.

Interpretation: the whole computation used log densities, so it stays stable when the density itself would be far too small to represent. The only tuning choice was the width w=2w = 2, and an error of a factor of two there only changed how many expansion steps were needed.

Tradeoffs and failure modes

SituationWhat goes wrongBetter choice
Univariate or a few coordinatesWorks well, with few tuning choicesSlice sampling, one coordinate at a time
Many correlated dimensionsCoordinate-wise moves mix slowly along diagonal ridges[[hamiltonian-monte-carlo
Width too smallInterval shrinks many times, extra evaluations per stepPick a width near the typical scale, or let it adapt
Width too largeStepping out takes many expansionsCap the number of expansions, a standard safeguard
Target with a single narrow spikeThe slice may be tiny and hard to find from a poor startStart near the mode, and choose a width that matches the spike
Need samples from a known densityA direct sampler is cheaperUse the direct method

Each function evaluation costs time, and slice sampling typically uses more evaluations per step than a Metropolis proposal. What it saves is tuning and wasted rejections, which often makes it faster overall when tuning is hard.

History

Radford Neal circulated slice sampling as a technical report in 1997 and published it, with discussion, in the Annals of Statistics in 2003. The paper introduced the stepping-out and shrinking procedures that make the one-dimensional update practical, and analysed how the width setting affects cost. The auxiliary-height idea later reappeared inside the No-U-Turn Sampler of Hoffman and Gelman (2011), which uses a slice variable to choose which points along a trajectory are eligible, and that sampler is the engine of widely used Bayesian software.

Recap

Slice sampling adds a random height under a curve, then samples uniformly from the horizontal slice at that height. The auxiliary variable makes the update leave the target invariant with no rejections. The procedure works from unnormalised values, and the stepping-out and shrinking steps find the slice from a width that only needs to be roughly right. The next idea to study is how these one-dimensional moves combine into the larger samplers used in MCMC practice.

Check yourself

QuestionWhy does drawing the height uu uniformly below f(x0)f(x_0) matter?Show answer

It makes the pair (x0,u)(x_0, u) uniform on the region under the curve of ff. Then drawing xx uniformly from the slice keeps the pair uniform on that region. The marginal of xx is then proportional to f(x)f(x), so the chain targets the right distribution. Without the height step, the slice would have no link to the target.

QuestionFor f(x)=4−x2f(x) = 4 - x^2 on [−2,2][-2, 2], the current point is x0=0x_0 = 0, so f(0)=4f(0) = 4, and the height is u=3u = 3. What is the slice, and what is its length?Show answer

The slice is where 4−x2>34 - x^2 \gt 3, so x2<1x^2 \lt 1, which gives (−1,1)(-1, 1). Its length is 2. The slice covers half of the support [−2,2][-2, 2] in length, and the draw x1x_1 would be uniform on (−1,1)(-1, 1).

QuestionWhy does every slice sampling step end in a valid move, while a Metropolis step sometimes does not?Show answer

Slice sampling draws xx uniformly from the slice, and the shrink step keeps shrinking the search interval toward the current point until a draw lands inside the slice. Every point it returns satisfies f(x)>uf(x) \gt u, so the chain always moves to a valid point. (Draws that miss the slice are discarded inside the shrink loop, but they cost only a function evaluation, not a lost step.) Metropolis proposes a move from a fixed distribution and then decides whether to keep it, so a whole step can be wasted.

QuestionIn Example 3, the log height was −2.7-2.7. Why is computing the slice on the log scale safer than on the raw scale?Show answer

Raw densities can underflow to zero or overflow when a model has many factors or large dimensions, so the comparison f(x)>uf(x) \gt u may fail to represent the values. On the log scale, the test becomes log⁡f(x)>log⁡u\log f(x) \gt \log u, and sums of logs stay in a comfortable range. The same slice results, with fewer numerical problems.

QuestionA user sets the initial width to a value 10 times too large. What happens, and what does it cost?Show answer

The stepping-out procedure has to extend the interval many times before both ends fall outside the slice, which costs extra function evaluations. The shrink step then works on a wide interval, which can take several draws. The output stays correct, because only the cost changes. A common safeguard is to cap the number of expansions.

Sources

Slice Sampling — explained simply | ungoon.me