Skip to content

Fix edge cases in Poisson, normal, lognormal and binomial distributions - #239

Open
youqad wants to merge 5 commits into
haskell:masterfrom
youqad:fix/log-density-edge-cases
Open

youqad wants to merge 5 commits into
haskell:masterfrom
youqad:fix/log-density-edge-cases

Conversation

@youqad

@youqad youqad commented Nov 8, 2026 •

Copy link
Copy Markdown

Hi! This fixes four edge cases where a distribution returns a wrong value. There is one commit per fix, and a fifth one for the changelog. Fixes #235.

Expression On master With this PR
logProbability (poisson 0) 0 NaN 0
logDensity (normalDistr 0 1e-200) 0 NaN 459.59808006560446
logDensity (lognormalDistr 0 1e-200) 1 NaN 459.59808006560446
logDensity (normalDistr 0 1e-200) 1e-150 -Infinity about -5e99
logDensity (normalDistr 0 1e160) 1e160 NaN -369.832553412252
entropy (normalDistr 0 1e-200) -Infinity -459.09808006560446
entropy (normalDistr 0 1e160) Infinity 369.832553412252
logDensity (lognormalDistr 0 1) 0 0.0 -Infinity
density (lognormalDistr 0 1) (-1) 1.0 0.0
logProbability (binomial 5 0) 0 NaN 0
probability (binomial 1000 0) 0 NaN 1
probability (binomial 1000 1) 1000 NaN 1

1. Poisson with rate 0

A Poisson distribution with rate 0 is a point mass at 0 (and probability already returns 1 there). The problem comes from the formula

log lambda * fromIntegral i - logFactorial i - lambda

At lambda = 0 and i = 0, its first term is (-Infinity) * 0. So the first commit adds a guard for lambda == 0, which returns 0 at i == 0 and m_neg_inf for any other i.

2. Normal and lognormal with a tiny or huge σ

In the normal logDensity, 2 * sd * sd underflows to 0, and at the mean the numerator is 0 too, so it computes 0/0 (the lognormal logDensity calls the normal one). Away from the mean the numerator is not 0, so the result is -Infinity. With a huge σ it's the other way round: 2 * sd * sd overflows.

So the second commit computes z = (x - mean d) / stdDev d first, and logDensity is now

(-0.5 * z * z) - ndPdfDenom d

which never squares sd. I compared the old and new normal logDensity on a grid of 180 ordinary inputs (mean in -10, -1, 0, 1, 10, sd in 0.1, 0.5, 1, 2, 10, 100 and x in -5, -1, 0, 1, 3, 10): 154 values are bit for bit identical, 17 differ by 1 ulp and 9 by 2 ulps.

The same underflow breaks entropy, since 0.5 * log (2 * pi * exp 1 * D.variance d) takes the log of a variance that underflows to 0 (and that overflows at σ = 1e160). The second commit replaces it with 0.5 + ndPdfDenom d, which is the same quantity (ndPdfDenom is log $ m_sqrt_2_pi * sd) without computing the variance. Neither distribution defines density, so with the class default exp . logDensity d both densities at σ = 1e-200 are now right as well (3.989422804014327e199).

3. Lognormal at x ≤ 0

A lognormal distribution has no mass at x <= 0, so the log density there should be -Infinity (and the density 0). The third commit makes logDensity return m_neg_inf for x <= 0 instead of 0, and density follows through the class default, since exp (-Infinity) is 0.

4. Binomial with p = 0 or p = 1

With p = 0, all the mass is on 0. In the formula

logChoose n k + log p * k' + log1p (-p) * nk'

the term log p * k' is (-Infinity) * 0 there, and so is log1p (-p) * nk' at p = 1 and k = n. probability uses the same formula when n >= 1000. The fourth commit adds two guards to logProbability: it now returns 0 at k = 0 when p = 0 and at k = n when p = 1, and -Infinity for the other values of k. The log-domain branch of probability now computes exp $ logProbability d k (with d@(BD n p)), which is the same expression as before when 0 < p < 1.

Tests

  • Each wrong value in the table has a new test, which fails on master (with GHC 9.6.7) and passes with these commits.
  • Each fix also has a test on an ordinary value: logProbability (poisson 1) 2, logDensity (normalDistr 1 2) 3, logDensity (lognormalDistr 0 1) 2, entropy (normalDistr 0 1) and logProbability (binomial 5 0.5) 2. The binomial tests also check logProbability on and off the mass for both values of p.
  • The normal and lognormal tests use eqRelErr and not eq, because eq also accepts an infinite result.
  • I ran statistics-tests on GHC 9.6.7, 9.12.4 and 9.14.1, and all 475 tests pass (the 451 already on master and the 24 new ones).

The last commit adds an ## Unreleased section at the top of changelog.md, with one entry per fix.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

logProbability returns NaN for the Poisson distribution with λ = 0

1 participant