packages feed

hstatistics-0.2.0.2: lib/Numeric/Statistics/Shannon.hs

{-# OPTIONS_GHC -fglasgow-exts #-}
-----------------------------------------------------------------------------
-- |
-- Module      :  Numeric.Statistics.Shannon
-- Copyright   :  (c) Alexander Vivian Hugh McPhail 2010
-- License     :  GPL-style
--
-- Maintainer  :  haskell.vivian.mcphail <at> gmail <dot> com
-- Stability   :  provisional
-- Portability :  portable
--
-- Shannon entropy
--
-----------------------------------------------------------------------------

module Numeric.Statistics.Shannon (
                         entropy
                         ) where


import Data.Packed.Vector

import Numeric.GSL.Histogram hiding(sum)

import Numeric.LinearAlgebra.Algorithms
import Numeric.LinearAlgebra.Interface()

import Prelude hiding (sum)

sum x = dot x (constant  1 (dim x))

prob p y = let Just y' = find p y 
           in getBin p y'

-- | the entropy \sum p_i l\ln{p_i} of a sequence
entropy :: Histogram             -- the underlying distribution
        -> Vector Double         -- the sequence (expected to fall within bounds of Histogram)
        -> Double                -- the entropy
entropy p x = let ps = mapVector (prob p) x
              in sum (ps * log ps)