Statistics · density function
Normal Probability Density (normpdf)
The normal density function, often called normpdf, returns the height of the bell curve at a point x for a given mean μ and standard deviation σ. The formula is f(x) = exp(−(x − μ)² / (2σ²)) / (σ√(2π)). With μ = 0 and σ = 1 it becomes the standard form φ(x) = exp(−x²/2) / √(2π), and every other normal density is that curve shifted and stretched.
A height is not a probability. For a continuous variable the probability of landing exactly on x is zero, and the density tells how thickly probability is spread around it. Probabilities come from areas, which is the job of the cumulative function and the tail functions such as the Student t tail.
Standardizing
Two parameters move the curve. The mean μ slides it along the axis, and σ widens or narrows it. Both are handled by the substitution z = (x − μ)/σ, followed by a division by σ so the total area stays equal to one:
f(x; μ, σ) = φ((x − μ)/σ) / σ
The division matters. A common bug is to evaluate φ at the standardized value and forget the 1/σ, which gives a curve whose area is σ instead of 1. With σ = 2 the peak height should be half that of the standard curve, 0.19947, not 0.39894.
Values worth memorizing
Three numbers cover most sanity checks: φ(0) = 1/√(2π) = 0.398942, φ(1) = exp(−½)/√(2π) = 0.241971, and φ(2) = exp(−2)/√(2π) = 0.053991. The ratio between consecutive entries shows how fast the Gaussian falls: from 0 to 1 the height drops by a factor of 1.65, from 1 to 2 by 4.48.
A short worked case: for μ = 10, σ = 2 and x = 12, the standardized value is z = 1, so f(12) = φ(1)/2 = 0.241971 / 2 = 0.120985. For x = 10 the result is the peak, 0.398942 / 2 = 0.199471.
| z | φ(z) |
|---|---|
| 0 | 0.398942 |
| 0.5 | 0.352065 |
| 1 | 0.241971 |
| 1.5 | 0.129518 |
| 2 | 0.053991 |
| 3 | 0.004432 |
The curve is symmetric, so φ(−z) = φ(z). It has its maximum at μ and inflection points at μ ± σ, where it changes from bending downward to bending upward. Those two landmarks make a plot easy to inspect by eye.
The log density
Likelihood work multiplies many densities, and the product underflows quickly. Summing logarithms avoids that. The log of the density is ln f(x) = −½ ln(2π) − ln σ − z²/2, a quadratic in x with no exponential at all.
As an example, take observations 9, 11 and 13 with μ = 10 and σ = 2. The constant part is −0.918939 − 0.693147 = −1.612086. The standardized values are −0.5, 0.5 and 1.5, so the quadratic parts are −0.125, −0.125 and −1.125. The three log densities are −1.737086, −1.737086 and −2.737086, and their sum is the log-likelihood, −6.211258. Taking the exponential at the end gives a likelihood of about 0.002, a number that is awkward to hold but harmless as a logarithm.
Vectors and edge cases
Applied to a vector of x values, the function should return a vector of the same shape, evaluating the formula element by element. The same pattern appears in element-by-element multiplication, which is how the expression is written without a loop.
Reject σ ≤ 0. For |z| beyond about 38 the exponential underflows to zero in double precision; if that is a problem, work with the log density. Since the density is the derivative of the cumulative distribution, differentiating a cumulative table numerically, as described under finite differences, recovers it approximately.
Going the other way, from a probability back to a point on the axis, is the subject of the inverse normal distribution. More functions of this kind are listed under statistics and special functions.