hstatistics 0.2.0.4 → 0.2.0.5
raw patch · 5 files changed
+123/−51 lines, 5 filesdep ~hmatrixdep ~hmatrix-gsl-stats
Dependency ranges changed: hmatrix, hmatrix-gsl-stats
Files
- CHANGES +3/−0
- hstatistics.cabal +4/−3
- lib/Numeric/Statistics/Histogram.hs +55/−0
- lib/Numeric/Statistics/Information.hs +61/−0
- lib/Numeric/Statistics/Shannon.hs +0/−48
CHANGES view
@@ -28,3 +28,6 @@ 0.2.0.4: fixed definition of mutual_information++0.2.0.5:+ added Histogram
hstatistics.cabal view
@@ -1,5 +1,5 @@ Name: hstatistics-Version: 0.2.0.4+Version: 0.2.0.5 License: GPL License-file: LICENSE Copyright: (c) A.V.H. McPhail 2010@@ -22,12 +22,13 @@ library Build-Depends: base >= 3 && < 5,- hmatrix >= 0.9.3, hmatrix-gsl-stats >= 0.1.1.3+ hmatrix >= 0.10.0, hmatrix-gsl-stats >= 0.1.1.4 Extensions: hs-source-dirs: lib- Exposed-modules: Numeric.Statistics.Shannon+ Exposed-modules: Numeric.Statistics.Information+ Numeric.Statistics.Histogram other-modules: C-sources:
+ lib/Numeric/Statistics/Histogram.hs view
@@ -0,0 +1,55 @@+{-# OPTIONS_GHC -fglasgow-exts #-}+-----------------------------------------------------------------------------+-- |+-- Module : Numeric.Statistics.Histogram+-- Copyright : (c) Alexander Vivian Hugh McPhail 2010+-- License : GPL-style+--+-- Maintainer : haskell.vivian.mcphail <at> gmail <dot> com+-- Stability : provisional+-- Portability : portable+--+-- create histograms from density functions+--+-----------------------------------------------------------------------------++module Numeric.Statistics.Histogram (+ cumulativeToHistogram+ , gaussianHistogram+ ) where+++-----------------------------------------------------------------------------++import Data.Packed.Vector++import qualified Numeric.GSL.Histogram as H+--import qualified Numeric.GSL.Histogram2D as H2++import qualified Numeric.GSL.Distribution.Continuous as C++--import Numeric.LinearAlgebra.Algorithms+--import Numeric.LinearAlgebra.Interface()++-----------------------------------------------------------------------------++vectorToTuples = toTuples . toList+ where toTuples [] = error "need a minimum of two elements"+ toTuples [_] = error "need a minimum of two elements"+ toTuples [x1,x2] = [(x1,x2)]+ toTuples (x1:x2:xs) = (x1,x2) : (toTuples (x2:xs))++-----------------------------------------------------------------------------++cumulativeToHistogram :: (Double -> Double) -- ^ the cumulative distribution function D(x <= X)+ -> Vector Double -- ^ the bins+ -> H.Histogram -- ^ the resulting histogram+cumulativeToHistogram f v = H.addListWeighted (H.emptyRanges v) $ map (\(x1,x2) -> ((x1 + x2) / 2.0,f x2 - f x1)) (vectorToTuples v)++gaussianHistogram :: Double -- ^ mean+ -> Double -- ^ standard deviation+ -> Vector Double -- ^ the bins+ -> H.Histogram -- ^ the resulting histogram+gaussianHistogram u s = cumulativeToHistogram (\x -> C.density_1p C.Gaussian C.Lower s (x-u))++-----------------------------------------------------------------------------
+ lib/Numeric/Statistics/Information.hs view
@@ -0,0 +1,61 @@+{-# OPTIONS_GHC -fglasgow-exts #-}+-----------------------------------------------------------------------------+-- |+-- Module : Numeric.Statistics.Information+-- 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.Information (+ entropy+ , mutual_information+ ) where+++-----------------------------------------------------------------------------++import Data.Packed.Vector++import qualified Numeric.GSL.Histogram as H+import qualified Numeric.GSL.Histogram2D as H2++import Numeric.LinearAlgebra.Algorithms+import Numeric.LinearAlgebra.Interface()++-----------------------------------------------------------------------------++zeroToOne x+ | x == 0.0 = 1.0+ | otherwise = x++logE = mapVector (log . zeroToOne)+++-----------------------------------------------------------------------------++-- | the entropy \sum p_i l\ln{p_i} of a sequence+entropy :: H.Histogram -- ^ the underlying distribution+ -> Vector Double -- ^ the sequence (expected to fall within bounds of Histogram)+ -> Double -- ^ the entropy+entropy p x = let ps = H.prob p x+ in dot ps (logE ps)++-- | the mutual information \sum_x \sum_y \ln{\frac{p(x,y)}{p(x)p(y)}}+mutual_information :: H2.Histogram2D -- ^ the underlying distribution+ -> H.Histogram -- ^ the first dimension distribution+ -> H.Histogram -- ^ the second dimension distribution+ -> (Vector Double, Vector Double) -- ^ the sequence (expected to fall within bounds of Histogram)+ -> Double -- ^ the mutual information+mutual_information p px py z@(x,y) = let ps = H2.prob p z+ xs = H.prob px x+ ys = H.prob py y+ in dot ps (logE ps - logE (xs*ys)) ++-----------------------------------------------------------------------------
− lib/Numeric/Statistics/Shannon.hs
@@ -1,48 +0,0 @@-{-# 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- , mutual_information- ) where---import Data.Packed.Vector--import qualified Numeric.GSL.Histogram as H-import qualified Numeric.GSL.Histogram2D as H2--import Numeric.LinearAlgebra.Algorithms-import Numeric.LinearAlgebra.Interface()----import Prelude hiding (sum)---- | the entropy \sum p_i l\ln{p_i} of a sequence-entropy :: H.Histogram -- the underlying distribution- -> Vector Double -- the sequence (expected to fall within bounds of Histogram)- -> Double -- the entropy-entropy p x = let ps = H.prob p x- in dot ps (log ps)---- | the mutuaal information \sum_x \sum_y \ln{\frac{p(x,y)}{p(x)p(y)}}-mutual_information :: H2.Histogram2D -- the underlying distribution- -> H.Histogram -- the first dimension distribution- -> H.Histogram -- the second dimension distribution- -> (Vector Double, Vector Double) -- the sequence (expected to fall within bounds of Histogram)- -> Double -- the mutual information-mutual_information p px py z@(x,y) = let ps = H2.prob p z- xs = H.prob px x- ys = H.prob py y- in dot ps (log (ps/(xs*ys)))