liveBook · Manning

Probability density

As explained in Fabulous Adventures in Data Structures and Algorithms

A probability density function is a nonnegative function whose integral over a range determines the probability of a continuous random variable falling in that range.

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

How Fabulous Adventures in Data Structures and Algorithms covers it

How deep it goes
Defines density through area ratios and normalization, then develops sampling and posterior uses; does not derive a general sampler from every PDF.
Examples
Uses a beta-shaped density, a Cauchy distribution, C# distribution objects, and Metropolis acceptance ratios.
What it assumes
Assumes familiarity with real-valued functions, integrals, and C# while defining PDFs and normalization explicitly.
What you can do after
Apply it via C# distribution objects to inspect weights, calculate interval probabilities, and select sampling methods for known densities.
How it connects
Connects density to cumulative distribution functions, quantile functions, inverse-transform sampling, Bayesian posteriors, and Metropolis sampling.

Also explained in

Compare how 6 books explain probability density →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Fabulous Adventures in Data Structures and Algorithms. The section links point to the parts of the book it draws on.