diff --git a/Statistics/Distribution/Binomial.hs b/Statistics/Distribution/Binomial.hs index b76cb318..d00e88d5 100644 --- a/Statistics/Distribution/Binomial.hs +++ b/Statistics/Distribution/Binomial.hs @@ -101,7 +101,7 @@ instance D.MaybeEntropy BinomialDistribution where -- This could be slow for big n probability :: BinomialDistribution -> Int -> Double -probability (BD n p) k +probability d@(BD n p) k | k < 0 || k > n = 0 | n == 0 = 1 -- choose could overflow Double for n >= 1030 so we switch to @@ -112,17 +112,19 @@ probability (BD n p) k | n < 1000 , pK >= m_tiny , pNK >= m_tiny = choose n k * pK * pNK - | otherwise = exp $ logChoose n k + log p * k' + log1p (-p) * nk' + | otherwise = exp $ logProbability d k where pK = p^k pNK = (1-p)^(n-k) - k' = fromIntegral k - nk' = fromIntegral $ n - k logProbability :: BinomialDistribution -> Int -> Double logProbability (BD n p) k | k < 0 || k > n = (-1)/0 | n == 0 = 0 + -- p = 0 and p = 1 are point masses; the formula below would + -- evaluate log p * 0 or log1p (-p) * 0, which is NaN + | p == 0 = if k == 0 then 0 else (-1)/0 + | p == 1 = if k == n then 0 else (-1)/0 | otherwise = logChoose n k + log p * k' + log1p (-p) * nk' where k' = fromIntegral k diff --git a/Statistics/Distribution/Lognormal.hs b/Statistics/Distribution/Lognormal.hs index 87d917c2..140490ce 100644 --- a/Statistics/Distribution/Lognormal.hs +++ b/Statistics/Distribution/Lognormal.hs @@ -28,7 +28,7 @@ import Data.Aeson (FromJSON, ToJSON) import Data.Binary (Binary (..)) import Data.Data (Data, Typeable) import GHC.Generics (Generic) -import Numeric.MathFunctions.Constants (m_huge, m_sqrt_2_pi) +import Numeric.MathFunctions.Constants (m_huge, m_neg_inf, m_sqrt_2_pi) import Numeric.SpecFunctions (expm1, log1p) import qualified Data.Vector.Generic as G @@ -153,7 +153,7 @@ instance D.FromSample LognormalDistribution Double where logDensity :: LognormalDistribution -> Double -> Double logDensity (LND d) x | x > 0 = let lx = log x in D.logDensity d lx - lx - | otherwise = 0 + | otherwise = m_neg_inf cumulative :: LognormalDistribution -> Double -> Double cumulative (LND d) x diff --git a/Statistics/Distribution/Normal.hs b/Statistics/Distribution/Normal.hs index e509b5bb..62dbbdd5 100644 --- a/Statistics/Distribution/Normal.hs +++ b/Statistics/Distribution/Normal.hs @@ -89,7 +89,7 @@ instance D.Variance NormalDistribution where stdDev = stdDev instance D.Entropy NormalDistribution where - entropy d = 0.5 * log (2 * pi * exp 1 * D.variance d) + entropy d = 0.5 + ndPdfDenom d instance D.MaybeEntropy NormalDistribution where maybeEntropy = Just . D.entropy @@ -152,10 +152,12 @@ instance D.FromSample NormalDistribution Double where where (m,v) = S.meanVariance xs +-- NOTE: Standard deviation could be arbitrarily small or large, so +-- sd * sd may underflow or overflow. Standardising first avoids +-- that. logDensity :: NormalDistribution -> Double -> Double -logDensity d x = (-xm * xm / (2 * sd * sd)) - ndPdfDenom d - where xm = x - mean d - sd = stdDev d +logDensity d x = (-0.5 * z * z) - ndPdfDenom d + where z = (x - mean d) / stdDev d cumulative :: NormalDistribution -> Double -> Double cumulative d x = erfc ((mean d - x) / ndCdfDenom d) / 2 diff --git a/Statistics/Distribution/Poisson.hs b/Statistics/Distribution/Poisson.hs index 389843ac..0e33a443 100644 --- a/Statistics/Distribution/Poisson.hs +++ b/Statistics/Distribution/Poisson.hs @@ -75,8 +75,9 @@ instance D.Distribution PoissonDistribution where instance D.DiscreteDistr PoissonDistribution where probability (PD lambda) x = I.probability lambda (fromIntegral x) logProbability (PD lambda) i - | i < 0 = m_neg_inf - | otherwise = log lambda * fromIntegral i - logFactorial i - lambda + | i < 0 = m_neg_inf + | lambda == 0 = if i == 0 then 0 else m_neg_inf + | otherwise = log lambda * fromIntegral i - logFactorial i - lambda instance D.Variance PoissonDistribution where variance = poissonLambda diff --git a/changelog.md b/changelog.md index 97729ede..0636c2dc 100644 --- a/changelog.md +++ b/changelog.md @@ -1,3 +1,21 @@ +## Unreleased + + * `logProbability` of the Poisson distribution with rate 0 returns `0` + at 0 instead of `NaN` (#235). + + * `logDensity` and `density` of the normal and lognormal distributions + and `entropy` of the normal distribution no longer return `NaN` or an + infinity in place of a finite value when the square of the standard + deviation underflows or overflows, as at `1e-200` or `1e160`. + + * `logDensity` of the lognormal distribution returns `-Infinity` and + `density` returns `0` at non-positive `x` (they used to return `0` + and `1`). + + * `logProbability` and `probability` of the binomial distribution no + longer return `NaN` at `p = 0` or `p = 1`. + + ## Changes in 0.16.5.0 [2026.01.09] * `ContGen` and `DiscreteGen` instances for `Poisson` distributions are added. diff --git a/tests/Tests/Distribution.hs b/tests/Tests/Distribution.hs index 792c78c7..a11012bb 100644 --- a/tests/Tests/Distribution.hs +++ b/tests/Tests/Distribution.hs @@ -10,7 +10,7 @@ import Numeric.MathFunctions.Constants (m_tiny,m_huge,m_epsilon) import Numeric.MathFunctions.Comparison import Statistics.Distribution import Statistics.Distribution.Beta (BetaDistribution) -import Statistics.Distribution.Binomial (BinomialDistribution) +import Statistics.Distribution.Binomial (BinomialDistribution, binomial) import Statistics.Distribution.CauchyLorentz import Statistics.Distribution.ChiSquared (ChiSquared) import Statistics.Distribution.Exponential (ExponentialDistribution) @@ -19,10 +19,10 @@ import Statistics.Distribution.Gamma (GammaDistribution,gammaDistr) import Statistics.Distribution.Geometric import Statistics.Distribution.Hypergeometric import Statistics.Distribution.Laplace (LaplaceDistribution) -import Statistics.Distribution.Lognormal (LognormalDistribution) +import Statistics.Distribution.Lognormal (LognormalDistribution, lognormalDistr) import Statistics.Distribution.NegativeBinomial (NegativeBinomialDistribution) -import Statistics.Distribution.Normal (NormalDistribution) -import Statistics.Distribution.Poisson (PoissonDistribution) +import Statistics.Distribution.Normal (NormalDistribution, normalDistr) +import Statistics.Distribution.Poisson (PoissonDistribution, poisson) import Statistics.Distribution.StudentT import Statistics.Distribution.Transform (LinearTransform) import Statistics.Distribution.Uniform (UniformDistribution) @@ -37,7 +37,7 @@ import Text.Printf (printf) import Tests.ApproxEq (ApproxEq(..)) import Tests.ExactDistribution (exactDistributionTests) -import Tests.Helpers (T(..), Double01(..), testAssertion, typeName) +import Tests.Helpers (T(..), Double01(..), testAssertion, testEquality, typeName) import Tests.Helpers (monotonicallyIncreasesIEEE,isDenorm) import Tests.Orphanage () @@ -412,6 +412,59 @@ unitTests = testGroup "Unit tests" , testFdistrCDF 1 3 3 0.81830988618379067153 -- CDF , testFdistrCDF 2 2 1.2 0.545455 , testFdistrCDF 10 12 8 0.99935509863451408041 + -- Poisson with rate 0 is a point mass at 0 + , testEquality "logProbability (poisson 0) 0" 0 $ + logProbability (poisson 0) 0 + , testEquality "logProbability (poisson 0) 2" (-1/0) $ + logProbability (poisson 0) 2 + , testAssertion "logProbability (poisson 1) 2" $ + eq 1e-14 (-1.6931471805599454) (logProbability (poisson 1) 2) + -- Normal and lognormal at an extreme standard deviation + -- eqRelErr, unlike eq, fails on an infinite result + , testAssertion "logDensity (normalDistr 0 1e-200) 0" $ + eqRelErr 1e-14 459.59808006560446 (logDensity (normalDistr 0 1e-200) 0) + , testAssertion "density (normalDistr 0 1e-200) 0" $ + eqRelErr 1e-12 3.989422804014327e199 (density (normalDistr 0 1e-200) 0) + , testAssertion "logDensity (lognormalDistr 0 1e-200) 1" $ + eqRelErr 1e-14 459.59808006560446 (logDensity (lognormalDistr 0 1e-200) 1) + , testAssertion "density (lognormalDistr 0 1e-200) 1" $ + eqRelErr 1e-12 3.989422804014327e199 (density (lognormalDistr 0 1e-200) 1) + , testAssertion "logDensity (normalDistr 0 1e-200) 1e-150" $ + eqRelErr 1e-14 (-5e99) (logDensity (normalDistr 0 1e-200) 1e-150) + , testAssertion "logDensity (normalDistr 0 1e160) 1e160" $ + eqRelErr 1e-14 (-369.832553412252) (logDensity (normalDistr 0 1e160) 1e160) + , testAssertion "entropy (normalDistr 0 1e-200)" $ + eqRelErr 1e-14 (-459.09808006560446) (entropy (normalDistr 0 1e-200)) + , testAssertion "entropy (normalDistr 0 1e160)" $ + eqRelErr 1e-14 369.832553412252 (entropy (normalDistr 0 1e160)) + , testAssertion "logDensity (normalDistr 1 2) 3" $ + eqRelErr 1e-14 (-2.112085713764618) (logDensity (normalDistr 1 2) 3) + , testAssertion "logDensity (lognormalDistr 0 1) 2" $ + eqRelErr 1e-14 (-1.8523122207237188) (logDensity (lognormalDistr 0 1) 2) + , testAssertion "entropy (normalDistr 0 1)" $ + eqRelErr 1e-14 1.4189385332046727 (entropy (normalDistr 0 1)) + -- Lognormal has no mass at x <= 0 + , testEquality "logDensity (lognormalDistr 0 1) 0" (-1/0) $ + logDensity (lognormalDistr 0 1) 0 + , testEquality "logDensity (lognormalDistr 0 1) (-1)" (-1/0) $ + logDensity (lognormalDistr 0 1) (-1) + , testEquality "density (lognormalDistr 0 1) (-1)" 0 $ + density (lognormalDistr 0 1) (-1) + -- Binomial at p = 0 puts all mass on 0, at p = 1 all mass on n + , testEquality "logProbability (binomial 5 0) 0" 0 $ + logProbability (binomial 5 0) 0 + , testEquality "logProbability (binomial 5 0) 3" (-1/0) $ + logProbability (binomial 5 0) 3 + , testEquality "logProbability (binomial 5 1) 5" 0 $ + logProbability (binomial 5 1) 5 + , testEquality "logProbability (binomial 5 1) 2" (-1/0) $ + logProbability (binomial 5 1) 2 + , testAssertion "logProbability (binomial 5 0.5) 2" $ + eq 1e-14 (-1.1631508098056809) (logProbability (binomial 5 0.5) 2) + , testEquality "probability (binomial 1000 0) 0" 1 $ + probability (binomial 1000 0) 0 + , testEquality "probability (binomial 1000 1) 1000" 1 $ + probability (binomial 1000 1) 1000 ] where -- Student-T