Permalink
Browse files

Importing statistics-linreg to github

  • Loading branch information...
0 parents commit 1779f54e298bd1d03f6cc61f79f5eb9d71564cb0 @alpmestan committed Sep 26, 2011
Showing with 131 additions and 0 deletions.
  1. +19 −0 LICENSE
  2. +3 −0 Setup.hs
  3. +33 −0 Statistics/LinearRegression.hs
  4. +63 −0 statistics-linreg.cabal
  5. BIN tests/hstats
  6. +6 −0 tests/hstats.hs
  7. BIN tests/linreg
  8. +7 −0 tests/linreg.hs
19 LICENSE
@@ -0,0 +1,19 @@
+Copyright (c) 2010 Alp Mestanogullari
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in
+all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+THE SOFTWARE.
@@ -0,0 +1,3 @@
+#!/usr/bin/env runhaskell
+import Distribution.Simple
+main = defaultMain
@@ -0,0 +1,33 @@
+module Statistics.LinearRegression (linearRegression) where
+
+import qualified Data.Vector.Unboxed as U
+import Statistics.Sample
+
+--- * 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)
+ where
+ n = fromIntegral $ U.length xs
+ m1 = mean xs
+ m2 = mean ys
+ f1 = \x -> (x - m1)
+ f2 = \x -> (x - m2)
+
+-- | 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)
+
+-- | 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)
+linearRegression xs ys = (alpha, beta)
+ where
+ (r, m1, m2, sx, sy) = correl xs ys
+ beta = r * sy / sx
+ alpha = m2 - beta * m1
@@ -0,0 +1,63 @@
+-- linreg.cabal auto-generated by cabal init. For additional options,
+-- see
+-- http://www.haskell.org/cabal/release/cabal-latest/doc/users-guide/authors.html#pkg-descr.
+-- The name of the package.
+Name: statistics-linreg
+
+-- 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
+
+-- A short (one-line) description of the package.
+Synopsis: Linear regression between two samples, based on the 'statistics' package
+
+-- A longer description of the package.
+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/
+-- The license under which the package is released.
+License: MIT
+
+-- The file containing the license text.
+License-file: LICENSE
+
+-- The package author(s).
+Author: Alp Mestanogullari <alpmestan@gmail.com>
+
+-- An email address to which users can send suggestions, bug reports,
+-- and patches.
+Maintainer: Alp Mestanogullari <alpmestan@gmail.com>
+
+-- A copyright notice.
+Copyright: 2010 Alp Mestanogullari
+
+-- Stability of the pakcage (experimental, provisional, stable...)
+Stability: Experimental
+
+Category: Math, Statistics
+
+Build-type: Simple
+
+-- Extra files to be distributed with the package, such as examples or
+-- a README.
+-- Extra-source-files:
+
+-- Constraint on the version of Cabal needed to build this package.
+Cabal-version: >=1.4
+
+
+Library
+ -- Modules exported by the library.
+ Exposed-modules: Statistics.LinearRegression
+
+ -- Packages needed in order to build this package.
+ Build-depends: statistics >= 0.5, vector >= 0.5, base >= 4 && < 5
+
+ -- Modules not exported by this package.
+ -- Other-modules:
+
+ -- Extra tools (e.g. alex, hsc2hs, ...) needed to build the source.
+ -- Build-tools:
+
Binary file not shown.
@@ -0,0 +1,6 @@
+import Math.Statistics
+
+main = do
+ let xs = [1..100000]
+ let ys = map (\x -> x*100 + 2000) xs
+ putStrLn . show $ linreg $ zip xs ys
Binary file not shown.
@@ -0,0 +1,7 @@
+import Statistics.LinearRegression
+import qualified Data.Vector.Unboxed as U
+
+main = do
+ let xs = U.fromList [1..100000]
+ let ys = U.map (\x -> x*100 + 2000) xs
+ putStrLn . show $ simpleLinReg xs ys

0 comments on commit 1779f54

Please sign in to comment.