{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE BangPatterns #-}
module Statistics.Correlation
(
pearson
, pearsonMatByRow
, 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 :: (G.Vector v (Double, Double), G.Vector v Double)
=> v (Double, Double) -> Double
pearson = correlation
{-# INLINE pearson #-}
pearsonMatByRow :: Matrix -> Matrix
pearsonMatByRow m
= generateSym (rows m)
(\i j -> pearson $ row m i `U.zip` row m j)
{-# INLINE pearsonMatByRow #-}
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 #-}
spearmanMatByRow :: Matrix -> Matrix
spearmanMatByRow
= pearsonMatByRow . fromRows . fmap rankUnsorted . toRows
{-# INLINE spearmanMatByRow #-}