Probability distributions
Normal log density
prob.normal-logpdfAlso called normal logpdf, gaussian log likelihood, normal log likelihood, log normal pdf
Natural log of the normal density, computed directly. Taking the log of the density underflows to -∞ once the point is about 38σ from the mean; the direct form does not.
Notation
the point at which the density is evaluated
x ∈ ℝ
mean (location)
μ ∈ ℝ
standard deviation
σ > 0
Result: the log density at x
scalar
Worked examples
Each result is solved exactly and confirmed by two independent computations.
Given
Result
≈ -0.918938533205
Given
Result
≈ -2.9189385332
Given
Result
≈ -2.11208571376
Given
Result
≈ -499996.313768
Common mistakes
Takes the log of the density
The density underflows to zero far from the mean, so its log becomes −inf.
Fix: Compute −ln σ − ½ln(2π) − (x − μ)2/(2σ2) directly.
Treats σ as the variance
The code uses its σ argument where σ2 belongs.
Fix: Pass the standard deviation, or use −½ln σ2 with the variance.
Uses ln σ2 where ln σ belongs
The normalizing term is −ln σ2, double the correct −ln σ (or −½ ln σ2).
Fix: Use −ln σ, or −½ ln σ2.
Source
NIST/SEMATECH e-Handbook, §1.3.6.6.1
NIST/SEMATECH e-Handbook of Statistical Methods. https://www.itl.nist.gov/div898/handbook/ Last updated 2022-04-27. doi:10.18434/M32189