Overview

15 Sampling continuous distributions

This chapter introduces the challenge of generating random samples from one-dimensional continuous distributions, extending ideas that were already used for categorical distributions. It explains that continuous distributions describe real-valued random processes with probability density functions, where probabilities are represented by areas under curves rather than by discrete weights. The chapter sets up the main goal: learning practical ways to simulate continuous random phenomena, especially when the distribution is known, partly known, or only available as a non-normalized density.

It first covers special-purpose sampling methods for common distributions, beginning with the continuous uniform distribution and then the normal distribution. The uniform case is straightforward because values can be drawn directly from a range, while the normal distribution is generated efficiently with the Box-Muller transform, producing bell-shaped samples centered around a mean with a chosen spread. The chapter also shows how histograms can be used to visualize sampled data and confirm that the generated values match the expected distribution.

Next, the chapter explains the inverse transform method, which samples from any continuous distribution as long as its cumulative distribution function or quantile function is known. This is illustrated with the Cauchy distribution, showing how a PDF, CDF, and inverse CDF work together to enable compact sampling code. Finally, it presents rejection sampling as a more general technique for sampling from distributions that are hard to sample directly, including clamping unwanted tails to a limited range; it also emphasizes the method’s drawbacks, especially inefficiency and the need for manual choice of a suitable helper distribution and scaling factor.

A PDF is like a probability mass function but defined over a range of real numbers rather than over discrete items. To sample, uniformly choose a point in the shaded area; the x coordinate of that point is the sample value.
The probability that a sample from this distribution is less than 0.5 is proportional to the shaded area and equal to that shaded area divided by the shaded area in figure 15.1.
The PDF of a normal distribution with mean = 60.0 mm and standard deviation = 0.1 mm. This graph shows the normalized PDF; the area below the curve is 1.0.
The normalized PDF and CDF of the standard Cauchy distribution. This distribution is useful when samples from the tails are more common than they are in a normal distribution. Almost all CDFs have a stretched S shape, called a sigmoid.
The two-hump function is the PDF of the desired distribution, and the one-hump function is the non-normalized standard normal distribution from section 15.3 that we’ll use as a helper. The helper’s PDF clearly isn’t larger than the target’s PDF everywhere, but we can fix that.
The target PDF is always smaller than the scaled-up helper PDF, so we can use rejection sampling. If we throw a dart, and it lands in the dark gray area, the x coordinate of the dart is the sample. If the dart lands in the light-gray area, we reject the throw and try again.

Summary

  • A continuous probability distribution can be characterized by its PDF. A sample from a distribution is like throwing a dart uniformly at the area below the PDF and using its x coordinate as the sample. If the area below the PDF is 1.0, it’s said to be normalized. We frequently encounter non-normalized PDFs.
  • The continuous uniform distribution generates random samples that are equally likely over a given range. If the range is 0.0 to 1.0, that’s the standard uniform distribution.
  • The normal distribution describes a random process in which values deviate from a central value, the mean; this is the classic bell-shaped curve.
  • The integral of a PDF is the CDF, the function which gives the probability that a sample will be less than a given value.
  • The inverse of the CDF is the quantile function. We can sample from any distribution if we know its quantile function. Calling which function with a standard uniform sample generates a sample from the target distribution.
  • If we don’t know the quantile function but have the PDF of a target distribution, sometimes we can find a helper distribution that allows us to use the rejection-­sampling algorithm. Finding a suitable helper distribution isn’t easy to automate, and arbitrarily many samples can be rejected before one is accepted.

FAQ

What is a continuous probability distribution?A continuous probability distribution describes a random process that can produce any real number within a continuous range. It is usually represented by a probability density function (PDF), and probabilities for ranges are given by areas under the curve.
What is a probability density function (PDF)?A PDF is a function that takes a real number and returns a non-negative value. The total area under the curve must be finite, and when normalized, the total area equals 1.0.
How do you interpret probability in a continuous distribution?The probability of getting a value in a range is proportional to the area under the PDF over that range. For a very small interval, the probability is approximately the PDF value at that point multiplied by the interval width.
What is the continuous uniform distribution?The continuous uniform distribution generates any value between a minimum and maximum with equal likelihood. The standard continuous uniform distribution ranges from 0.0 to 1.0.
How does the standard continuous uniform distribution work in code?It can be implemented by scaling a random double from 0.0 to 1.0 into the desired range. Its weight function returns 1.0 inside the range and 0.0 outside it.
What is the normal distribution?The normal distribution, also called the Gaussian distribution, is a bell-shaped continuous distribution characterized by a mean and a standard deviation. Most values lie close to the mean, with thin tails extending outward.
How can you sample from a normal distribution?The chapter uses the Box-Muller transform. It generates a random point using uniform random numbers in polar coordinates, then converts it so the x-coordinate follows a standard normal distribution. Arbitrary normal distributions are obtained by rescaling and shifting.
What is the inverse transform method?It is a general sampling technique for continuous distributions. You compute the CDF from the PDF, invert it to get the quantile function, sample a uniform random probability, and apply the quantile function to get a sample.
What is rejection sampling used for?Rejection sampling generates samples from a target distribution by sampling from a helper distribution and rejecting points that fall above the target PDF. It is useful when the target PDF is known up to a constant factor or when clamping a distribution to a range.
What are the main problems with rejection sampling?Two major problems are that the number of rejected samples can be arbitrarily large, making it inefficient, and that choosing a good helper distribution and scale factor is often difficult and requires manual tuning.

pro $24.99 per month

  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose one free eBook per month to keep
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime

lite $19.99 per month

  • access to all Manning books, including MEAPs!

team

5, 10 or 20 seats+ for your team - learn more


choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • Fabulous Adventures in Data Structures and Algorithms ebook for free
choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • Fabulous Adventures in Data Structures and Algorithms ebook for free
choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • Fabulous Adventures in Data Structures and Algorithms ebook for free