A probability density function (PDF) describes how samples from a continuous distribution are spread across real numbers. It takes a real number and returns a nonnegative weight. Unlike a discrete distribution’s probability mass function, it describes a range of possible values rather than assigning probability to each individual value. section 15.1 section 15.2
double P(double x) =>
0.0 <= x && x <= 1.0 ?
Math.Pow(x, 0.5) * Math.Pow(1.0 - x, 1.5) :
0.0;
This C# density is nonzero only between 0.0 and 1.0. Its curved shape gives some parts of that interval more weight than others. section 15.1
To find the probability of landing in a range, take the area under the curve over that range and divide it by the total area under the curve. For the density above, the area below 0.5 is roughly 0.06 out of roughly 0.2, giving about a 32% chance of a sample below 0.5. A PDF whose total area is exactly 1.0 is normalized; otherwise, its weights can still describe the same relative chances. section 15.1
A density can serve several jobs in sampling:
- Integrate a normalized PDF to obtain its cumulative distribution function (CDF), which gives the probability of a sample at or below a value. If its inverse—the quantile function—is known, apply that function to a standard uniform sample to generate a sample from the target distribution. section 15.4
- When the quantile function is unavailable, use a suitable helper distribution for rejection sampling, or use the Metropolis algorithm with a possibly non-normalized density. section 15.6 section 16.4
Listing 15.4 Implementing the inverse transform method
public class FromQuantile : IContinuousDistribution
{
Func<double, double> pdf;
Func<double, double> quantile;
public FromQuantile(
Func<double, double> pdf, Func<double, double> quantile)
{
this.pdf = pdf;
this.quantile = quantile;
}
public double Sample() =>
quantile(ContinuousUniform.Standard.Sample());
public double Weight(double x) => pdf(x);
}
The distribution keeps both a density and a quantile function: Weight returns the density at a value, while Sample transforms a standard uniform draw. section 15.4
Density is also useful before there is a sampling algorithm. Given a continuous prior and the likelihood of observed evidence, multiplying their weights produces a posterior density up to a constant factor. That missing factor changes the total area, not the relative weights. The likelihood distributions must, however, use consistent weighting across possible prior values. section 16.3
Why normalization can be optional
Metropolis sampling compares the proposed value’s density with the current value’s density. It accepts a higher-weight proposal; a proposal with one-tenth the current weight is accepted 10% of the time. Multiplying every density value by the same constant leaves that ratio unchanged, so the algorithm does not need a normalized PDF. section 16.4





