packages feed

statistics-linreg 0.1 → 0.2

raw patch · 2 files changed

+54/−22 lines, 2 files

Files

Statistics/LinearRegression.hs view
@@ -1,33 +1,64 @@-module Statistics.LinearRegression (linearRegression) where+{-# LANGUAGE BangPatterns #-} +module Statistics.LinearRegression (linearRegression, correl, covar) where+ import qualified Data.Vector.Unboxed as U-import Statistics.Sample+import qualified Statistics.Sample as S  --- * Simple linear regression --- | Covariance-covar :: Sample -> Sample -> (Double, Double, Double)-covar xs ys = (U.sum (U.zipWith (*) (U.map f1 xs) (U.map f2 ys)) / (n-1), m1, m2)+-- | Covariance of two samples+covar :: S.Sample -> S.Sample -> Double+covar xs ys = U.sum (U.zipWith (*) (U.map f1 xs) (U.map f2 ys)) / (n-1)     where-          n = fromIntegral $ U.length xs-          m1 = mean xs-          m2 = mean ys+          !n = fromIntegral $ U.length xs+          !m1 = S.mean xs+          !m2 = S.mean ys           f1 = \x -> (x - m1)           f2 = \x -> (x - m2)+{-# INLINE covar #-} + -- | Pearson's product-moment correlation coefficient-correl :: Sample -> Sample -> (Double, Double, Double, Double, Double)-correl xs ys = let (c, m1, m2) = covar xs ys-                   sx = stdDev xs-                   sy = stdDev ys-               in (c / (stdDev xs * stdDev ys), m1, m2, sx, sy)+correl :: S.Sample -> S.Sample -> Double+correl xs ys = let !c = covar xs ys+                   !sx = S.stdDev xs+                   !sy = S.stdDev ys+               in c / (sx * sy)+{-# INLINE correl #-}  -- | Simple linear regression between 2 samples. --   Takes two vectors Y={yi} and X={xi} and returns---   (alpha, beta) such that Y = alpha + betaX-linearRegression :: Sample -> Sample -> (Double, Double)+--   (alpha, beta, r*r) such that Y = alpha + beta*X+--   and where r is the Pearson product-moment correlation+--   coefficient+linearRegressionRSqr :: S.Sample -> S.Sample -> (Double, Double, Double)+linearRegressionRSqr xs ys = (alpha, beta, r*r)+    where +          !c                   = U.sum (U.zipWith (*) (U.map (subtract m1) xs) (U.map (subtract m2) ys)) / (n-1)+          !r                   = c / (sx * sy)+          !m1                  = S.mean xs +          !m2                  = S.mean ys+          !sx                  = S.stdDev xs+          !sy                  = S.stdDev ys+          !n                   = fromIntegral $ U.length xs+          !beta                = r * sy / sx+          !alpha               = m2 - beta * m1+{-# INLINE linearRegressionRSqr #-}+          +-- | Simple linear regression between 2 samples.+--   Takes two vectors Y={yi} and X={xi} and returns+--   (alpha, beta, r*r) such that Y = alpha + beta*X          +linearRegression :: S.Sample -> S.Sample -> (Double, Double) linearRegression xs ys = (alpha, beta)     where -          (r, m1, m2, sx, sy) = correl xs ys-          beta                = r * sy / sx-          alpha               = m2 - beta * m1+          !c                   = U.sum (U.zipWith (*) (U.map (subtract m1) xs) (U.map (subtract m2) ys)) / (n-1)+          !r                   = c / (sx * sy)+          !m1                  = S.mean xs +          !m2                  = S.mean ys+          !sx                  = S.stdDev xs+          !sy                  = S.stdDev ys+          !n                   = fromIntegral $ U.length xs+          !beta                = r * sy / sx+          !alpha               = m2 - beta * m1+{-# INLINE linearRegression #-}
statistics-linreg.cabal view
@@ -7,7 +7,7 @@ -- The package version. See the Haskell package versioning policy -- (http://www.haskell.org/haskellwiki/Package_versioning_policy) for -- standards guiding when and how versions should be incremented.-Version:             0.1+Version:             0.2  -- A short (one-line) description of the package. Synopsis:            Linear regression between two samples, based on the 'statistics' package@@ -16,7 +16,7 @@ Description:         Provides a function to perform a linear regression between 2 samples, see the documentation of the linearRegression function. This library is based on the 'statistics' package.  -- URL for the project homepage or repository.-Homepage:            http://code.haskell.org/~alpmestan/linreg/+Homepage:            http://github.com/alpmestan/statistics-linreg -- The license under which the package is released. License:             MIT @@ -31,7 +31,7 @@ Maintainer:          Alp Mestanogullari <alpmestan@gmail.com>  -- A copyright notice.-Copyright:           2010 Alp Mestanogullari+Copyright:           2010-2011 Alp Mestanogullari  -- Stability of the pakcage (experimental, provisional, stable...) Stability:           Experimental@@ -54,7 +54,8 @@      -- Packages needed in order to build this package.   Build-depends: statistics >= 0.5, vector >= 0.5, base >= 4 && < 5-  ++  Ghc-options: -funbox-strict-fields     -- Modules not exported by this package.   -- Other-modules: