Repository navigation
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.
logProbability (poisson 0) 0NaN0logDensity (normalDistr 0 1e-200) 0NaN459.59808006560446logDensity (lognormalDistr 0 1e-200) 1NaN459.59808006560446logDensity (normalDistr 0 1e-200) 1e-150-Infinity-5e99logDensity (normalDistr 0 1e160) 1e160NaN-369.832553412252entropy (normalDistr 0 1e-200)-Infinity-459.09808006560446entropy (normalDistr 0 1e160)Infinity369.832553412252logDensity (lognormalDistr 0 1) 00.0-Infinitydensity (lognormalDistr 0 1) (-1)1.00.0logProbability (binomial 5 0) 0NaN0probability (binomial 1000 0) 0NaN1probability (binomial 1000 1) 1000NaN11. Poisson with rate 0
A Poisson distribution with rate 0 is a point mass at 0 (and
probabilityalready returns1there). The problem comes from the formulaAt
lambda = 0andi = 0, its first term is(-Infinity) * 0. So the first commit adds a guard forlambda == 0, which returns0ati == 0andm_neg_inffor any otheri.2. Normal and lognormal with a tiny or huge σ
In the normal
logDensity,2 * sd * sdunderflows to0, and at the mean the numerator is0too, so it computes0/0(the lognormallogDensitycalls the normal one). Away from the mean the numerator is not0, so the result is-Infinity. With a hugeσit's the other way round:2 * sd * sdoverflows.So the second commit computes
z = (x - mean d) / stdDev dfirst, andlogDensityis nowwhich never squares
sd. I compared the old and new normallogDensityon a grid of 180 ordinary inputs (meanin-10, -1, 0, 1, 10,sdin0.1, 0.5, 1, 2, 10, 100andxin-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, since0.5 * log (2 * pi * exp 1 * D.variance d)takes the log of a variance that underflows to0(and that overflows atσ = 1e160). The second commit replaces it with0.5 + ndPdfDenom d, which is the same quantity (ndPdfDenomislog $ m_sqrt_2_pi * sd) without computing the variance. Neither distribution definesdensity, so with the class defaultexp . logDensity dboth densities atσ = 1e-200are 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 density0). The third commit makeslogDensityreturnm_neg_infforx <= 0instead of0, anddensityfollows through the class default, sinceexp (-Infinity)is0.4. Binomial with p = 0 or p = 1
With
p = 0, all the mass is on0. In the formulathe term
log p * k'is(-Infinity) * 0there, and so islog1p (-p) * nk'atp = 1andk = n.probabilityuses the same formula whenn >= 1000. The fourth commit adds two guards tologProbability: it now returns0atk = 0whenp = 0and atk = nwhenp = 1, and-Infinityfor the other values ofk. The log-domain branch ofprobabilitynow computesexp $ logProbability d k(withd@(BD n p)), which is the same expression as before when0 < p < 1.Tests
logProbability (poisson 1) 2,logDensity (normalDistr 1 2) 3,logDensity (lognormalDistr 0 1) 2,entropy (normalDistr 0 1)andlogProbability (binomial 5 0.5) 2. The binomial tests also checklogProbabilityon and off the mass for both values ofp.eqRelErrand noteq, becauseeqalso accepts an infinite result.statistics-testson 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
## Unreleasedsection at the top ofchangelog.md, with one entry per fix.