Commits

Aleksey Khudyakov committed 57f2911

Add logarithm of probablity density

Both density and logDensity could be expressed terms of each other
so default implementations are added.

Currently none of the distributions have good implementations of
logDensity since they use default implementation.

Affects #41

Comments (0)

Files changed (1)

Statistics/Distribution.hs

     probability :: d -> Int -> Double
 
 
--- | Continuous probability distributuion
+-- | Continuous probability distributuion.
+--
+--   Minimal complete definition is 'quantile' and either 'density' or
+--   'logDensity'.
 class Distribution d => ContDistr d where
     -- | Probability density function. Probability that random
     -- variable /X/ lies in the infinitesimal interval
     -- [/x/,/x+/δ/x/) equal to /density(x)/⋅δ/x/
     density :: d -> Double -> Double
+    density d = exp . logDensity d
+    {-# INLINE density #-}
 
     -- | Inverse of the cumulative distribution function. The value
     -- /x/ for which P(/X/≤/x/) = /p/. If probability is outside
     -- of [0,1] range function should call 'error'
     quantile :: d -> Double -> Double
 
+    -- | Natural logarithm of density.
+    logDensity :: d -> Double -> Double
+    logDensity d = log . density d
+    {-# INLINE logDensity #-}
 
 
 -- | Type class for distributions with mean. 'maybeMean' should return