Commits

Anonymous committed 62dfaaa

Entropy instance for GeometricDistribution

Also changed "finite entropy" to "well-defined entropy" in the
description of the contract for the Entropy
typeclass. GeometricDistribution defines Mean and Variance even though
it treats a success probability of 0 as legal, so we should be
consistent by defining Entropy (and not just MaybeEntropy) as well.

  • Participants
  • Parent commits 098714f

Comments (0)

Files changed (2)

File Statistics/Distribution.hs

   -- | Returns the entropy of a distribution, in nats, if such is defined.
   maybeEntropy :: d -> Maybe Double
   
--- | Type class for distributions with entropy, meaning Shannon entropy
---   in the case of a discrete distribution, or differential entropy in the
---   case of a continuous one.  If the distribution has finite entropy for
---   all valid parameter values then it should be an instance of this type
---   class.
+-- | Type class for distributions with entropy, meaning Shannon
+--   entropy in the case of a discrete distribution, or differential
+--   entropy in the case of a continuous one.  If the distribution has
+--   well-defined entropy for all valid parameter values then it
+--   should be an instance of this type class.
 class (MaybeEntropy d) => Entropy d where
   -- | Returns the entropy of a distribution, in nats.
   entropy :: d -> Double

File Statistics/Distribution/Geometric.hs

 import Data.Binary (Binary)
 import Data.Data (Data, Typeable)
 import GHC.Generics (Generic)
+import Numeric.MathFunctions.Constants(m_pos_inf)
 import qualified Statistics.Distribution as D
 
 newtype GeometricDistribution = GD {
     maybeStdDev   = Just . D.stdDev
     maybeVariance = Just . D.variance
 
+instance D.Entropy GeometricDistribution where
+  entropy (GD s)
+    | s == 0 = m_pos_inf
+    | s == 1 = 0
+    | otherwise = negate $ (s * log s + (1-s) * log (1-s)) / s
+
+instance D.MaybeEntropy GeometricDistribution where
+  maybeEntropy = Just . D.entropy
 
 -- | Create geometric distribution.
 geometric :: Double                -- ^ Success rate