# Distributions Using Slice Sampling

Initializing live version Requires a Wolfram Notebook System

Interact on desktop, mobile and cloud with the free Wolfram Player or other Wolfram Language products.

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, click the "generate random sample" button.

Contributed by: Oliver K. Ernst (July 2015)
Open content licensed under CC BY-NC-SA

## Snapshots ## Details

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.

## Permanent Citation

Oliver K. Ernst

 Feedback (field required) Email (field required) Name Occupation Organization Note: Your message & contact information may be shared with the author of any specific Demonstration for which you give feedback. Send
{"ft_lang_label":"__label__en","ft_lang_prob":0.8762249,"math_prob":0.94954026,"size":3386,"snap":"2021-31-2021-39","text_gpt3_token_len":683,"char_repetition_ratio":0.15109403,"word_repetition_ratio":0.046904314,"special_character_ratio":0.20023626,"punctuation_ratio":0.13290113,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.989571,"pos_list":[0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32],"im_url_duplicate_count":[null,null,null,null,null,5,null,5,null,5,null,5,null,5,null,5,null,10,null,5,null,10,null,5,null,10,null,5,null,10,null,5,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2021-09-26T07:28:58Z\",\"WARC-Record-ID\":\"<urn:uuid:5fad7e4f-fd8e-4c93-8081-9940b901473e>\",\"Content-Length\":\"90641\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:663baf18-63bc-4acb-8354-d8648ff5e1be>\",\"WARC-Concurrent-To\":\"<urn:uuid:4e9c180d-2cb1-444f-8637-202c6ba068fb>\",\"WARC-IP-Address\":\"140.177.205.90\",\"WARC-Target-URI\":\"https://demonstrations.wolfram.com/DistributionsUsingSliceSampling/\",\"WARC-Payload-Digest\":\"sha1:V7NMT6MSB3VP5YK3UPK7PT4ESORWSFSY\",\"WARC-Block-Digest\":\"sha1:MUXH42RA3LJAWAMDUKRQURQKYZM4HCOX\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2021/CC-MAIN-2021-39/CC-MAIN-2021-39_segments_1631780057830.70_warc_CC-MAIN-20210926053229-20210926083229-00565.warc.gz\"}"}