All formulas

Probability distributions

Normal log density

prob.normal-logpdfAlso called normal logpdf, gaussian log likelihood, normal log likelihood, log normal pdf

ln⁡f(x)=−ln⁡σ−12ln⁡(2π)−(x−μ)22σ2\ln f(x) = -\ln\sigma - \frac{1}{2}\ln(2\pi) - \frac{(x - \mu)^2}{2\sigma^2}

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

xx

the point at which the density is evaluated

x ∈ ℝ

μ\mu

mean (location)

μ ∈ ℝ

σ\sigma

standard deviation

σ > 0

ln⁡f(x)\ln f(x)

Result: the log density at x

scalar

Worked examples

Each result is solved exactly and confirmed by two independent computations.

  1. Given

    x=0,μ=0,σ=1x = 0, \mu = 0, \sigma = 1

    Result

    ln⁡f(x)=−log⁡(2π)2\displaystyle \ln f(x) = - \frac{\log{\left(2 \pi \right)}}{2}

    ≈ -0.918938533205

  2. Given

    x=2,μ=0,σ=1x = 2, \mu = 0, \sigma = 1

    Result

    ln⁡f(x)=−2−log⁡(2π)2\displaystyle \ln f(x) = -2 - \frac{\log{\left(2 \pi \right)}}{2}

    ≈ -2.9189385332

  3. Given

    x=1,μ=3,σ=2x = 1, \mu = 3, \sigma = 2

    Result

    ln⁡f(x)=−log⁡(2π)2−log⁡(2)−12\displaystyle \ln f(x) = - \frac{\log{\left(2 \pi \right)}}{2} - \log{\left(2 \right)} - \frac{1}{2}

    ≈ -2.11208571376

  4. Given

    x=10,μ=0,σ=0.01x = 10, \mu = 0, \sigma = 0.01

    Result

    ln⁡f(x)=−500000−log⁡(2π)2+log⁡(100)\displaystyle \ln f(x) = -500000 - \frac{\log{\left(2 \pi \right)}}{2} + \log{\left(100 \right)}

    ≈ -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

    doi.org