Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Commit which were missing after fixing problems with HG<->git mirroring Conflicts: .hgtags statistics.cabal
- Loading branch information
Showing
16 changed files
with
617 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
{-# LANGUAGE FlexibleContexts #-} | ||
{-# LANGUAGE BangPatterns #-} | ||
-- | | ||
-- Module : Statistics.Correlation.Pearson | ||
-- | ||
module Statistics.Correlation | ||
( -- * Pearson correlation | ||
pearson | ||
, pearsonMatByRow | ||
-- * Spearman correlation | ||
, spearman | ||
, spearmanMatByRow | ||
) where | ||
|
||
import qualified Data.Vector.Generic as G | ||
import qualified Data.Vector.Unboxed as U | ||
import Statistics.Matrix | ||
import Statistics.Sample | ||
import Statistics.Test.Internal (rankUnsorted) | ||
|
||
|
||
---------------------------------------------------------------- | ||
-- Pearson | ||
---------------------------------------------------------------- | ||
|
||
-- | Pearson correlation for sample of pairs. | ||
pearson :: (G.Vector v (Double, Double), G.Vector v Double) | ||
=> v (Double, Double) -> Double | ||
pearson = correlation | ||
{-# INLINE pearson #-} | ||
|
||
-- | Compute pairwise pearson correlation between rows of a matrix | ||
pearsonMatByRow :: Matrix -> Matrix | ||
pearsonMatByRow m | ||
= generateSym (rows m) | ||
(\i j -> pearson $ row m i `U.zip` row m j) | ||
{-# INLINE pearsonMatByRow #-} | ||
|
||
|
||
|
||
---------------------------------------------------------------- | ||
-- Spearman | ||
---------------------------------------------------------------- | ||
|
||
-- | compute spearman correlation between two samples | ||
spearman :: ( Ord a | ||
, Ord b | ||
, G.Vector v a | ||
, G.Vector v b | ||
, G.Vector v (a, b) | ||
, G.Vector v Int | ||
, G.Vector v Double | ||
, G.Vector v (Double, Double) | ||
, G.Vector v (Int, a) | ||
, G.Vector v (Int, b) | ||
) | ||
=> v (a, b) | ||
-> Double | ||
spearman xy | ||
= pearson | ||
$ G.zip (rankUnsorted x) (rankUnsorted y) | ||
where | ||
(x, y) = G.unzip xy | ||
{-# INLINE spearman #-} | ||
|
||
-- | compute pairwise spearman correlation between rows of a matrix | ||
spearmanMatByRow :: Matrix -> Matrix | ||
spearmanMatByRow | ||
= pearsonMatByRow . fromRows . fmap rankUnsorted . toRows | ||
{-# INLINE spearmanMatByRow #-} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
{-# LANGUAGE DeriveDataTypeable, DeriveGeneric #-} | ||
-- | | ||
-- Module : Statistics.Distribution.Laplace | ||
-- Copyright : (c) 2015 Mihai Maruseac | ||
-- License : BSD3 | ||
-- | ||
-- Maintainer : mihai.maruseac@maruseac.com | ||
-- Stability : experimental | ||
-- Portability : portable | ||
-- | ||
-- The Laplace distribution. This is the continuous probability | ||
-- defined as the difference of two iid exponential random variables | ||
-- or a Brownian motion evaluated as exponentially distributed times. | ||
-- It is used in differential privacy (Laplace Method), speech | ||
-- recognition and least absolute deviations method (Laplace's first | ||
-- law of errors, giving a robust regression method) | ||
-- | ||
|
||
module Statistics.Distribution.Laplace | ||
( | ||
LaplaceDistribution | ||
-- * Constructors | ||
, laplace | ||
-- * Accessors | ||
, ldLocation | ||
, ldScale | ||
) where | ||
|
||
import Data.Aeson (FromJSON, ToJSON) | ||
import Data.Binary (Binary(..)) | ||
import Data.Data (Data, Typeable) | ||
import GHC.Generics (Generic) | ||
import qualified Statistics.Distribution as D | ||
import Control.Applicative ((<$>), (<*>)) | ||
|
||
|
||
data LaplaceDistribution = LD { | ||
ldLocation :: {-# UNPACK #-} !Double | ||
-- ^ Location. | ||
, ldScale :: {-# UNPACK #-} !Double | ||
-- ^ Scale. | ||
} deriving (Eq, Read, Show, Typeable, Data, Generic) | ||
|
||
instance FromJSON LaplaceDistribution | ||
instance ToJSON LaplaceDistribution | ||
|
||
instance Binary LaplaceDistribution where | ||
put (LD l s) = put l >> put s | ||
get = LD <$> get <*> get | ||
|
||
instance D.Distribution LaplaceDistribution where | ||
cumulative = cumulative | ||
complCumulative = complCumulative | ||
|
||
instance D.ContDistr LaplaceDistribution where | ||
density (LD l s) x = exp (- abs (x - l) / s) / (2 * s) | ||
logDensity (LD l s) x = - abs (x - l) / s - log 2 - log s | ||
quantile = quantile | ||
|
||
instance D.Mean LaplaceDistribution where | ||
mean (LD l _) = l | ||
|
||
instance D.Variance LaplaceDistribution where | ||
variance (LD _ s) = 2 * s * s | ||
|
||
instance D.MaybeMean LaplaceDistribution where | ||
maybeMean = Just . D.mean | ||
|
||
instance D.MaybeVariance LaplaceDistribution where | ||
maybeStdDev = Just . D.stdDev | ||
maybeVariance = Just . D.variance | ||
|
||
instance D.Entropy LaplaceDistribution where | ||
entropy (LD _ s) = 1 + log (2 * s) | ||
|
||
instance D.MaybeEntropy LaplaceDistribution where | ||
maybeEntropy = Just . D.entropy | ||
|
||
instance D.ContGen LaplaceDistribution where | ||
genContVar = D.genContinous | ||
|
||
cumulative :: LaplaceDistribution -> Double -> Double | ||
cumulative (LD l s) x | ||
| x <= l = 0.5 * exp ( (x - l) / s) | ||
| otherwise = 1 - 0.5 * exp ( - (x - l) / s ) | ||
|
||
complCumulative :: LaplaceDistribution -> Double -> Double | ||
complCumulative (LD l s) x | ||
| x <= l = 1 - 0.5 * exp ( (x - l) / s) | ||
| otherwise = 0.5 * exp ( - (x - l) / s ) | ||
|
||
quantile :: LaplaceDistribution -> Double -> Double | ||
quantile (LD l s) p | ||
| p == 0 = -inf | ||
| p == 1 = inf | ||
| p == 0.5 = l | ||
| p > 0 && p < 0.5 = l + s * log (2 * p) | ||
| p > 0.5 && p < 1 = l - s * log (2 - 2 * p) | ||
| otherwise = | ||
error $ "Statistics.Distribution.Laplace.quantile: p must be in [0,1] range. Got: "++show p | ||
where | ||
inf = 1 / 0 | ||
|
||
-- | Create an Laplace distribution. | ||
laplace :: Double -- ^ Location | ||
-> Double -- ^ Scale | ||
-> LaplaceDistribution | ||
laplace l s | ||
| s <= 0 = | ||
error $ "Statistics.Distribution.Laplace.laplace: scale parameter must be positive. Got " ++ show s | ||
| otherwise = LD l s |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.