Normal Distribution Algorithm

This page was generated with the assistance of AI. Please report any inaccuracies.

NormalDist implements the normal (Gaussian) distribution as described in section 6.14.1 of Numerical Recipes, 3rd Edition (see Reference). For usage-oriented examples, see Statistical Distributions; this page focuses on the underlying formulas and how they map onto the error function described in Error Function.

Probability density function

If is drawn from a normal distribution with mean and standard deviation , written , its probability density function is

NormalDist.p(x) evaluates this expression directly, and the static overload NormalDist.p(x, mu, sig) does the same without requiring an instance.

Cumulative distribution function

The cumulative distribution function is the probability of observing a value at most . For the normal distribution it is written in terms of the complementary error function rather than being integrated numerically:

NormalDist.cdf(x) computes exactly this, delegating to Erf.erfc.

Plot of the standard normal p.d.f. and c.d.f.
Figure 1. Standard normal distribution: probability density function p(x) and cumulative distribution function cdf(x), for μ = 0 and σ = 1.

Inverse cumulative distribution function

Because the c.d.f. is monotonically increasing between 0 and 1, it can be inverted in closed form using the inverse complementary error function:

NormalDist.invcdf(p) calls Erf.inverfc(2.0 * p) and rescales the result, which is why it is only valid for probabilities strictly between 0 and 1.

flowchart LR subgraph NormalDist P["p(x)"] CDF["cdf(x)"] INVCDF["invcdf(p)"] end subgraph Erf ERFC["erfc(x)"] INVERFC["inverfc(p)"] end CDF -->|"0.5 * erfc(...)"| ERFC INVCDF -->|"-sqrt(2) * sig * inverfc(2p) + mu"| INVERFC
import com.irurueta.statistics.NormalDist;

NormalDist normalDist = new NormalDist(10.0, 2.5);

double density = normalDist.p(12.0);
double probabilityBelow = normalDist.cdf(12.0);
double valueAt95Percent = normalDist.invcdf(0.95);

Mahalanobis distance

Beyond what the book’s Normaldist struct provides, this library also exposes the Mahalanobis distance of a point to the distribution’s mean, expressed in units of standard deviation:

double mahalanobisDistance = NormalDist.mahalanobisDistance(12.0, 10.0, 2.5);

Gaussian uncertainty propagation

NormalDist.propagate is another extension beyond the book: given a differentiable one-dimensional function and an input distribution , it linearizes around (a first-order, or "delta method," approximation) to estimate the distribution of :

import com.irurueta.statistics.NormalDist;

NormalDist.DerivativeEvaluator squareEvaluator = new NormalDist.DerivativeEvaluator() {
    @Override
    public double evaluate(final double x) {
        return x * x;
    }

    @Override
    public double evaluateDerivative(final double x) {
        return 2.0 * x;
    }
};

NormalDist input = new NormalDist(3.0, 0.1);
NormalDist propagated = NormalDist.propagate(squareEvaluator, input);