Slice sampling

In mathematics and physics, Slice sampling is a type of Markov chain Monte Carlo sampling algorithm based on the observation that to sample a random variable one can sample uniformly from the region under the graph of its density function.

Implementation
To sample a random variable X with density $$f(x)$$ we introduce an auxiliary variable $$Y$$ and iterate as follows: Given a sample x we choose y uniformly at random from the interval $$[0, f(x)]$$; given y we choose x uniformly at random from the set $$f^{-1}[0, y]$$. The sample of x is obtained by ignoring the y values.

Example
To sample from the normal distribution $$N(0,1)$$ we first choose an initial x -- say 0. After each sample of x we choose y uniformly at random from $$[0, e^{-x^2/2}/\sqrt{2\pi}]$$; after each y sample we choose x uniformly at random from $$[-\alpha, \alpha]$$ where $$\alpha = \sqrt{-2\log(y\sqrt{2\pi})}$$.

An implementation in the Macsyma language is: