packages feed

statistics 0.8.0.1 → 0.8.0.2

raw patch · 4 files changed

+206/−2 lines, 4 files

Files

+ Statistics/Distribution/Beta.hs view
@@ -0,0 +1,51 @@+{-# LANGUAGE DeriveDataTypeable #-}+-- |+-- Module    : Statistics.Distribution.Beta+-- Copyright : (c) 2010 Karamaan Group+--+-- The beta distribution.++module Statistics.Distribution.Beta+    (+      BetaDistribution+    -- * Constructors+    , fromParams+    -- * Accessors+    , tridistA+    , tridistB+    , tridistC+    ) where++import Control.Exception+import Data.Generics+import Foreign.C.Math.Double (gamma)+import qualified Statistics.Distribution as D++data BetaDistribution = BetaDist {+  alpha :: Double,+  beta :: Double+} deriving (Eq, Read, Show, Typeable, Data)++instance D.Distribution BetaDistribution where+  density (BetaDist a b) x = (gamma (a+b) / (gamma a * gamma b)) *+    (x**(a-1)) * ((1-x)**(b-1))+  {-# INLINE density #-}+  cumulative (BetaDist a b) x = undefined+  {-# INLINE cumulative #-}+  quantile (BetaDist a b) p = undefined+  {-# INLINE quantile #-}++instance D.Variance BetaDistribution where+    variance (BetaDist a b) = (a * b) /+      ((a+b)^2 * (a + b + 1))+    {-# INLINE variance #-}++instance D.Mean BetaDistribution where+    mean (BetaDist a b) = a / (a + b)+    {-# INLINE mean #-}++fromParams :: Double -> Double -> BetaDistribution+fromParams a b = assert (a > 0 && b > 0) (BetaDist a b)+{-# INLINE fromParams #-}++
+ Statistics/Distribution/LogNormal.hs view
@@ -0,0 +1,81 @@+{-# LANGUAGE DeriveDataTypeable #-}+-- |+-- Module    : Statistics.Distribution.LogNormal+-- Copyright : (c) 2009 Karamaan Group+--+-- The lognormal distribution. This is the distribution of a random +-- variable whose logarithm is normally distributed.++module Statistics.Distribution.LogNormal+    (+      LogNormalDistribution+    -- * Constructors+    , fromParams+    , standard+    ) where++import Control.Exception (assert)+import Data.Number.Erf (erf)+import Data.Generics+import Statistics.Constants (m_sqrt_2, m_sqrt_2_pi)+import qualified Statistics.Distribution as D++-- | The lognormal distribution.+data LogNormalDistribution = ND {+      mean     :: {-# UNPACK #-} !Double+    , variance :: {-# UNPACK #-} !Double+    , ndPdfDenom :: {-# UNPACK #-} !Double+    , ndCdfDenom :: {-# UNPACK #-} !Double+    } deriving (Eq, Read, Show, Typeable, Data)++instance D.Distribution LogNormalDistribution where+    density    = density+    cumulative = cumulative+    quantile   = quantile++instance D.Variance LogNormalDistribution where+    variance = variance++instance D.Mean LogNormalDistribution where+    mean = mean++standard :: LogNormalDistribution+standard = ND {+             mean = 0.0+           , variance = 1.0+           , ndPdfDenom = m_sqrt_2_pi+           , ndCdfDenom = m_sqrt_2+           }++fromParams :: Double -> Double -> LogNormalDistribution+fromParams m v = assert (v > 0)+                 ND {+                   mean = m+                 , variance = v+                 , ndPdfDenom = m_sqrt_2_pi * sv+                 , ndCdfDenom = m_sqrt_2 * sv+                 }+    where sv = sqrt v++density :: LogNormalDistribution -> Double -> Double+density d x = exp (-xm * xm / (2 * variance d)) / (x * ndPdfDenom d)+    where xm = log x - mean d++cumulative :: LogNormalDistribution -> Double -> Double+cumulative d x = (1 + erf ((log x-mean d) / ndCdfDenom d)) / 2++-- | This is the quantile function for the LogNormalDistribution.+quantile :: LogNormalDistribution -> Double -> Double+quantile d p = exp $ quantile' d p++-- | This is the quantile function for NormalDistribution.+quantile' :: LogNormalDistribution -> Double -> Double+quantile' d p+  | p < 0 || p > 1 = inf/inf+  | p == 0         = -inf+  | p == 1         = inf+  | p == 0.5       = mean d+  | otherwise      = x * sqrt (variance d) + mean d+  where x          = D.findRoot standard p 0 (-100) 100+        inf        = 1/0+
+ Statistics/Distribution/Triangular.hs view
@@ -0,0 +1,68 @@+{-# LANGUAGE DeriveDataTypeable #-}+-- |+-- Module    : Statistics.Distribution.Triangular+-- Copyright : (c) 2010 Karamaan Group+--+-- The triangular distribution. This is the distribution of a random +-- variable with lower limit a, mode c and upper limit b.++module Statistics.Distribution.Triangular+    (+      TriangularDistribution+    -- * Constructors+    , fromParams+    -- * Accessors+    , tridistA+    , tridistB+    , tridistC+    ) where++import Data.Generics+import qualified Statistics.Distribution as D++data TriangularDistribution = TriDist {+  tridistA :: Double, -- min+  tridistB :: Double, -- max+  tridistC :: Double  -- mode+} deriving (Eq, Read, Show, Typeable, Data)++instance D.Distribution TriangularDistribution where+  density (TriDist a b c) x+    | (a <= x) && (x <= c) = (2 * (x - a)) / ((b - a) * (c - a))+    | (c <= x) && (x <= b) = (2 * (b - x)) / ((b - a) * (b - c))+    | otherwise         = 0+  {-# INLINE density #-}++  cumulative (TriDist a b c) x+    | a > x             = 0+    | (a <= x) && (x <= c) = ((x - a) ^ 2) / ((b - a) * (c - a))+    | (c <= x) && (x <= b) = 1 - ((b - x) ^ 2) / ((b - a) * (b - c))+    | otherwise         = 1+  {-# INLINE cumulative #-}++  quantile (TriDist a b c) p   = calc ((c - a) / (b - a))+    where calc p0+            | p < p0    = sqrt ((b-a) * (c-a) * p) + a+            | p == p0   = c+            | otherwise = b - sqrt ((b-a) * (b-c) * (1-p))+  {-# INLINE quantile #-}++instance D.Variance TriangularDistribution where+    variance (TriDist a b c) =+      (a^2 + b^2 + c^2 - (a*b) - (a*c) - (b*c)) / 18+    {-# INLINE variance #-}++instance D.Mean TriangularDistribution where+    mean (TriDist a b c) = (a + b + c) / 3+    {-# INLINE mean #-}++fromParams :: Double -> Double -> Double -> TriangularDistribution+fromParams a b c +    | (c > b) || (c < a) = error $ "Triangular Distribution: Parameter " ++ (show c)+                          ++ " is expected to be between the parameters " +                          ++ (show a) ++ " and " ++ (show b) ++ "."+    | b < a             = error $ "Triangular Distribution: Parameter " ++ (show b)+                          ++ " is expected to be greater than parameter " ++ (show a) ++ "."+    | otherwise         = TriDist a b c+{-# INLINE fromParams #-}+
statistics.cabal view
@@ -1,5 +1,5 @@ name:           statistics-version:        0.8.0.1+version:        0.8.0.2 synopsis:       A library of statistical types, data, and functions description:   This library provides a number of common functions and types useful@@ -23,6 +23,7 @@ license:        BSD3 license-file:   LICENSE homepage:       http://bitbucket.org/bos/statistics+bug-reports:    http://bitbucket.org/bos/statistics/issues author:         Bryan O'Sullivan <bos@serpentine.com> maintainer:     Bryan O'Sullivan <bos@serpentine.com> copyright:      2009, 2010 Bryan O'Sullivan@@ -36,14 +37,17 @@     Statistics.Autocorrelation     Statistics.Constants     Statistics.Distribution+    Statistics.Distribution.Beta     Statistics.Distribution.Binomial     Statistics.Distribution.ChiSquared+    Statistics.Distribution.Exponential     Statistics.Distribution.Gamma     Statistics.Distribution.Geometric-    Statistics.Distribution.Exponential     Statistics.Distribution.Hypergeometric+    Statistics.Distribution.LogNormal     Statistics.Distribution.Normal     Statistics.Distribution.Poisson+    Statistics.Distribution.Triangular     Statistics.Function     Statistics.KernelDensity     Statistics.Math