Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Statistical distribution functions

ProblemEvaluate densities, cumulative distribution functions and quantiles of the normal, Student’s , chi-square, gamma, beta, , Poisson and binomial distributions
Devised byThe normal distribution: Abraham de Moivre (1733), Pierre-Simon Laplace and Carl Friedrich Gauss (1809); the binomial: Jacob Bernoulli (1713); Poisson: Siméon Denis Poisson (1837); chi-square: Karl Pearson (1900); Student’s : William Sealy Gosset (1908); : Ronald Fisher (1924), named by George Snedecor (1934). The normal quantile approximation: Peter Acklam (2003)
AccuracyAbout relative for the CDFs, through the incomplete gamma and beta functions; quantiles to or better
CostA CDF: one incomplete gamma or beta evaluation. A quantile: a rational approximation and a Halley step (normal), or bisection on the CDF (about 60 CDF evaluations)
Read firstPreliminaries: probability, Incomplete gamma and beta functions and the error function
APInormal_pdf, normal_cdf, normal_quantile, student_t_cdf, student_t_quantile, chi_square_cdf, chi_square_survival, chi_square_quantile, gamma_cdf, beta_cdf, f_cdf, poisson_pmf, poisson_cdf, binomial_pmf, binomial_cdf

Overview

Every statistical test and confidence interval needs the distribution function of a test statistic, or its inverse, the quantile: the -value of a test, the critical value of a chi-square goodness-of-fit test, the probability of at most defects. These functions are the incomplete gamma and beta functions in statistical clothing. This entry collects the identities that connect them and the numerical care that quantiles need, especially in the tails, where small probabilities must keep their relative accuracy.

Where it is used

These are the distribution functions behind classical statistics.

  • Hypothesis tests and confidence intervals. Quantiles of the normal, Student’s t, chi-square and F distributions give the critical values for a test or the width of an interval.
  • Quality control and reliability. Binomial, Poisson and exponential probabilities for defect counts and lifetimes.
  • Signal detection. Probabilities of false alarm and detection in noise.
  • Inverse functions. Quantile functions turn uniform random numbers into samples from each distribution.

History

Abraham de Moivre found the normal curve in 1733 as the limit of the binomial distribution; Laplace and Gauss made it the law of errors. Siméon Denis Poisson derived his distribution as a limit of the binomial in 1837. Karl Pearson introduced the chi-square test in 1900, founding modern statistical testing. William Sealy Gosset, a chemist at the Guinness brewery who published as “Student”, derived the distribution in 1908 for small samples of barley; Ronald Fisher put it on a rigorous footing and introduced the variance ratio, which George Snedecor named in Fisher’s honour. For a century statisticians used printed tables of these functions; the table below is computed by the library.

The idea

Distribution functions as incomplete integrals

A continuous random variable is described by its probability density function (PDF) ; the probability that it falls in an interval is the area under over that interval. Its cumulative distribution function (CDF) is the probability of falling at or below , , which rises from 0 to 1. The complement , the probability of exceeding , is called the survival function or upper tail.

The densities of the common distributions are powers and exponentials, so their CDFs are exactly the incomplete integrals of the incomplete gamma and beta entry. No new numerical method is needed, only the right change of variable:

distributionCDFthrough
normal , the error function
gamma (shape , scale )incomplete gamma
chi-square, degrees of freedomincomplete gamma
Poisson, rate incomplete gamma
beta incomplete beta
Student’s , degrees of freedom for incomplete beta
incomplete beta
binomial incomplete beta

Two numerical points deserve attention.

  • Tails. Small tail probabilities must be computed directly, never as 1 minus a number close to 1, which would cancel their leading digits (see the error function). Computing the normal CDF through instead of gives full relative accuracy in the lower tail, and chi_square_survival computes the upper tail directly, for the tiny -values of large test statistics.
  • Overflow. Probability mass functions of discrete distributions are products of very large and very small factors. They are assembled from logarithms of gamma functions, so that is formed without overflow or underflow, and exponentiated only at the end.

As the degrees of freedom grow, Student’s approaches the normal distribution, from heavier tails:

Figure 6.16. Student's t distribution.

Quantiles

The quantile function is the inverse of the CDF: is the value below which the variable falls with probability , the solution of . The median is , and the critical values of statistical tests are quantiles. Since CDFs have no closed-form inverse, a quantile is found by solving that equation numerically. For the normal distribution, Acklam’s rational approximation gives to about , and one Halley step on brings it to full precision. For the other continuous distributions the library brackets the solution by doubling and then bisects on the CDF to a relative width of a few units of , about 60 evaluations: slow but certain, since the CDF is monotone.

In the tails, the relative accuracy of matters. A quantile for should satisfy to about relative. Computing it as , by symmetry, would first form , which retains only six digits of ; the library’s Student’s quantile did exactly that, and now inverts the lower tail directly.

Familiar critical values, computed by the library:

Table 6.11. Quantiles of common distributions.

distribution90%95%97.5%99%99.9%
standard normal1.28161.64491.96002.32633.0902
Student’s , 3.07776.313812.706231.8205318.3088
Student’s , 1.47592.01502.57063.36495.8934
Student’s , 1.37221.81252.22812.76384.1437
Student’s , 1.31041.69732.04232.45733.3852
, 2.70553.84155.02396.634910.8276
, 4.60525.99157.37789.210313.8155
, 9.236411.070512.832515.086320.5150
, 15.987218.307020.483223.209329.5883

Accuracy

Table 6.12. Accuracy of the statistical functions.

functiontest pointsmedian relative errorlargest relative errorat arguments
normal_cdf601e-15(-3)
normal_quantile56e-162e-12(0.999999)
student_t_cdf302e-165e-12(-1, 10000)
student_t_quantile201e-152e-14(0.3, 100)
chi_square_quantile241e-154e-12(0.999999, 1)
chi_square_survival407e-16(30, 4)
f_cdf121e-152e-14(1, 20, 50)
poisson_pmf52e-151e-13(1000, 1000)
poisson_cdf56e-166e-14(1000, 1000)
binomial_pmf51e-153e-13(400, 1000, 0.42)
binomial_cdf53e-152e-13(400, 1000, 0.42)

The quantile errors near (for example ) reflect the conditioning: is known only to about relative when is a double close to 1.

The largest student_t_cdf error, at , has a different cause. The function evaluates the incomplete beta function at . For large this argument is squeezed against 1, where it is stored with an absolute error of about while the quantity that matters, , is smaller by four orders of magnitude. The relative error of the result therefore grows to about , which matches the table. It is the same loss of digits as in above, now in the argument rather than the output.

Algorithm

A cumulative distribution function.

  1. Check the parameters, and return 0 or 1 for arguments outside the support.
  2. Map the argument and parameters to those of the incomplete gamma or beta function, as in the table.
  3. Evaluate that function. Where the distribution has two tails, compute the smaller tail directly and obtain the other by subtraction: for Student’s , for instance, the incomplete beta function gives the tail beyond , and the CDF is that tail or one minus it according to the sign of .

A probability mass or density function: assemble the logarithm of the value from logarithms of gamma functions and of the parameters, and exponentiate once at the end.

The normal quantile .

  1. Return or for or 1.
  2. Evaluate Acklam’s rational approximation, which has one formula for the central region and another, in the variable , for each tail.
  3. Apply one step of Halley’s method to , using the density as the derivative.

Other quantiles.

  1. Find an interval containing the quantile: start from a guess and double the width until the CDF values at the ends lie on either side of .
  2. Bisect on the CDF until the interval is a few units of wide, relative to its position.

For Student’s , which is symmetric, the search is always carried out in the smaller tail: with , find with , and return if and otherwise. A small is thus inverted as it stands, with its full relative precision.

Implementation

/// The CDF of Student's t distribution with `nu` degrees of freedom.
pub fn student_t_cdf(t: f64, nu: f64) -> f64 {
    if !(nu > 0.0) || t.is_nan() {
        return f64::NAN;
    }
    if t.is_infinite() {
        return if t > 0.0 { 1.0 } else { 0.0 };
    }
    let tail = 0.5 * incomplete_beta(0.5 * nu, 0.5, nu / (nu + t * t));
    if t > 0.0 { 1.0 - tail } else { tail }
}
  • Invalid parameters (non-positive scales or degrees of freedom, probabilities outside ) give NaN.
  • Random sampling from these distributions is in lib_jsl_random; the statistics crate builds tests and intervals on these functions.

In practice

//! Distribution functions and quantiles for statistics.
//!
//! Run with `cargo run -p lib_jsl_special_functions --example special_statistical`.

use lib_jsl_special_functions::{binomial_cdf, chi_square_quantile, chi_square_survival, normal_cdf, normal_quantile, poisson_cdf, student_t_cdf, student_t_quantile};

fn main() {
    // A 95% confidence interval for a mean from 8 measurements uses the t quantile with 7
    // degrees of freedom instead of the normal 1.96.
    println!("z(0.975) = {:.6}, t₇(0.975) = {:.6}", normal_quantile(0.975, 0.0, 1.0), student_t_quantile(0.975, 7.0));
    // A two-sided p-value for t = 2.5 with 7 degrees of freedom.
    println!("p = {:.6}", 2.0 * (1.0 - student_t_cdf(2.5, 7.0)));
    // A chi-square goodness-of-fit statistic of 15.2 on 6 degrees of freedom.
    println!("χ² = 15.2, k = 6: p = {:.6}; critical value at 5%: {:.6}", chi_square_survival(15.2, 6.0), chi_square_quantile(0.95, 6.0));
    // Discrete distributions: at most 2 defects among 100 items at rate 1%; at most 3 events at rate 1.5.
    println!("binomial P(X ≤ 2) = {:.6}, Poisson P(N ≤ 3) = {:.6}", binomial_cdf(2, 100, 0.01), poisson_cdf(3, 1.5));
    println!("Φ(1.645) = {:.6}", normal_cdf(1.645, 0.0, 1.0));
}

Strengths

  • One accurate core (incomplete gamma and beta) behind every CDF.
  • Tail probabilities with full relative accuracy.

Weaknesses

  • Quantiles other than the normal use bisection, about 60 CDF evaluations each.
  • No non-central distributions.

Exercises

  1. Show that the chi-square distribution with degrees of freedom is a gamma distribution with shape and scale 2.
  2. Derive the Poisson CDF identity by differentiating both sides with respect to .
  3. For , Student’s is the Cauchy distribution. Show that its CDF is and check student_t_cdf.
  4. Replace bisection in the chi-square quantile by Newton’s method on the CDF, using the density as derivative. How many iterations are needed, and when does Newton fail?
  5. Compute the probability that a fair coin shows at least 600 heads in 1000 tosses, exactly with binomial_cdf and by the normal approximation with continuity correction.
Hints and answers
  1. The chi-square density is , which is the gamma density with shape and scale .
  2. Differentiating with respect to telescopes to , and the same is true of ; both are 1 at .
  3. The Cauchy density is ; its integral is . Compare student_t_cdf(t, 1.0) at several to rounding.
  4. Newton on the CDF converges in a handful of iterations from a good start, but it fails when the density is nearly zero (far tails) or when a step leaves the support; bisection never fails but needs about 50 iterations.
  5. The exact tail is ; the normal approximation with continuity correction gives , a relative error of about 14%, because the normal approximation is poor so far out in the tail.

Further reading

  • N. L. Johnson, S. Kotz and N. Balakrishnan, Continuous Univariate Distributions, 2nd ed., Wiley, 1994–1995 (2 vols.).
  • P. J. Acklam, “An algorithm for computing the inverse normal cumulative distribution function”, 2003.
  • M. J. Wichura, “Algorithm AS 241: the percentage points of the normal distribution”, Applied Statistics 37 (1988) 477–484.
  • NIST Digital Library of Mathematical Functions, §8.11 and chapter 7.