# HG changeset patch # User Alexey Khudyakov # Date 1378067367 -14400 # Node ID d01f878361721a90ff44f49fac1e66deaacd203d # Parent 8fd519b7da30a41a06e8a26dc6d4b1e26231b11f Add logProbability to geometric distribution diff --git a/Statistics/Distribution/Geometric.hs b/Statistics/Distribution/Geometric.hs --- a/Statistics/Distribution/Geometric.hs +++ b/Statistics/Distribution/Geometric.hs @@ -29,7 +29,7 @@ import Data.Binary (Binary) import Data.Data (Data, Typeable) import GHC.Generics (Generic) -import Numeric.MathFunctions.Constants(m_pos_inf) +import Numeric.MathFunctions.Constants(m_pos_inf,m_neg_inf) import qualified Statistics.Distribution as D newtype GeometricDistribution = GD { @@ -42,7 +42,13 @@ cumulative = cumulative instance D.DiscreteDistr GeometricDistribution where - probability = probability + probability (GD s) n + | n < 1 = 0 + | otherwise = s * (1-s) ** (fromIntegral n - 1) + logProbability (GD s) n + | n < 1 = m_neg_inf + | otherwise = log s + log (1-s) * (fromIntegral n - 1) + instance D.Mean GeometricDistribution where mean (GD s) = 1 / s @@ -77,11 +83,6 @@ error \$ "Statistics.Distribution.Geometric.geometric: probability must be in [0,1] range. Got " ++ show x {-# INLINE geometric #-} -probability :: GeometricDistribution -> Int -> Double -probability (GD s) n | n < 1 = 0 - | otherwise = s * (1-s) ** (fromIntegral n - 1) -{-# INLINE probability #-} - cumulative :: GeometricDistribution -> Double -> Double cumulative (GD s) x | x < 1 = 0