{-# LANGUAGE FlexibleContexts #-}
module Statistics.Sample
(
Sample
, WeightedSample
, range
, mean
, welfordMean
, meanWeighted
, harmonicMean
, geometricMean
, centralMoment
, centralMoments
, skewness
, kurtosis
, variance
, varianceUnbiased
, meanVariance
, meanVarianceUnb
, stdDev
, varianceWeighted
, stdErrMean
, fastVariance
, fastVarianceUnbiased
, fastStdDev
, covariance
, correlation
, pair
) where
import Statistics.Function (minMax)
import Statistics.Sample.Internal (robustSumVar, sum)
import Statistics.Types.Internal (Sample,WeightedSample)
import qualified Data.Vector as V
import qualified Data.Vector.Generic as G
import qualified Data.Vector.Unboxed as U
import Prelude hiding ((^), sum)
range :: (G.Vector v Double) => v Double -> Double
range :: forall (v :: * -> *). Vector v Double => v Double -> Double
range v Double
s = Double
hi Double -> Double -> Double
forall a. Num a => a -> a -> a
- Double
lo
where (Double
lo , Double
hi) = v Double -> (Double, Double)
forall (v :: * -> *).
Vector v Double =>
v Double -> (Double, Double)
minMax v Double
s
{-# INLINE range #-}
mean :: (G.Vector v Double) => v Double -> Double
mean :: forall (v :: * -> *). Vector v Double => v Double -> Double
mean v Double
xs = v Double -> Double
forall (v :: * -> *). Vector v Double => v Double -> Double
sum v Double
xs Double -> Double -> Double
forall a. Fractional a => a -> a -> a
/ Int -> Double
forall a b. (Integral a, Num b) => a -> b
fromIntegral (v Double -> Int
forall (v :: * -> *) a. Vector v a => v a -> Int
G.length v Double
xs)
{-# SPECIALIZE mean :: U.Vector Double -> Double #-}
{-# SPECIALIZE mean :: V.Vector Double -> Double #-}
welfordMean :: (G.Vector v Double) => v Double -> Double
welfordMean :: forall (v :: * -> *). Vector v Double => v Double -> Double
welfordMean = T -> Double
fini (T -> Double) -> (v Double -> T) -> v Double -> Double
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (T -> Double -> T) -> T -> v Double -> T
forall (v :: * -> *) b a.
Vector v b =>
(a -> b -> a) -> a -> v b -> a
G.foldl' T -> Double -> T
go (Double -> Int -> T
T Double
0 Int
0)
where
fini :: T -> Double
fini (T Double
a Int
_) = Double
a
go :: T -> Double -> T
go (T Double
m Int
n) Double
x = Double -> Int -> T
T Double
m' Int
n'
where m' :: Double
m' = Double
m Double -> Double -> Double
forall a. Num a => a -> a -> a
+ (Double
x Double -> Double -> Double
forall a. Num a => a -> a -> a
- Double
m) Double -> Double -> Double
forall a. Fractional a => a -> a -> a
/ Int -> Double
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
n'
n' :: Int
n' = Int
n Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1
{-# SPECIALIZE welfordMean :: U.Vector Double -> Double #-}
{-# SPECIALIZE welfordMean :: V.Vector Double -> Double #-}
meanWeighted :: (G.Vector v (Double,Double)) => v (Double,Double) -> Double
meanWeighted :: forall (v :: * -> *).
Vector v (Double, Double) =>
v (Double, Double) -> Double
meanWeighted = V -> Double
fini (V -> Double)
-> (v (Double, Double) -> V) -> v (Double, Double) -> Double
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (V -> (Double, Double) -> V) -> V -> v (Double, Double) -> V
forall (v :: * -> *) b a.
Vector v b =>
(a -> b -> a) -> a -> v b -> a
G.foldl' V -> (Double, Double) -> V
go (Double -> Double -> V
V Double
0 Double
0)
where
fini :: V -> Double
fini (V Double
a Double
_) = Double
a
go :: V -> (Double, Double) -> V
go (V Double
m Double
w) (Double
x,Double
xw) = Double -> Double -> V
V Double
m' Double
w'
where m' :: Double
m' | Double
w' Double -> Double -> Bool
forall a. Eq a => a -> a -> Bool
== Double
0 = Double
0
| Bool
otherwise = Double
m Double -> Double -> Double
forall a. Num a => a -> a -> a
+ Double
xw Double -> Double -> Double
forall a. Num a => a -> a -> a
* (Double
x Double -> Double -> Double
forall a. Num a => a -> a -> a
- Double
m) Double -> Double -> Double
forall a. Fractional a => a -> a -> a
/ Double
w'
w' :: Double
w' = Double
w Double -> Double -> Double
forall a. Num a => a -> a -> a
+ Double
xw
{-# INLINE meanWeighted #-}
harmonicMean :: (G.Vector v Double) => v Double -> Double
harmonicMean :: forall (v :: * -> *). Vector v Double => v Double -> Double
harmonicMean = T -> Double
fini (T -> Double) -> (v Double -> T) -> v Double -> Double
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (T -> Double -> T) -> T -> v Double -> T
forall (v :: * -> *) b a.
Vector v b =>
(a -> b -> a) -> a -> v b -> a
G.foldl' T -> Double -> T
go (Double -> Int -> T
T Double
0 Int
0)
where
fini :: T -> Double
fini (T Double
b Int
a) = Int -> Double
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
a Double -> Double -> Double
forall a. Fractional a => a -> a -> a
/ Double
b
go :: T -> Double -> T
go (T Double
x Int
y) Double
n = Double -> Int -> T
T (Double
x Double -> Double -> Double
forall a. Num a => a -> a -> a
+ (Double
1Double -> Double -> Double
forall a. Fractional a => a -> a -> a
/Double
n)) (Int
yInt -> Int -> Int
forall a. Num a => a -> a -> a
+Int
1)
{-# INLINE harmonicMean #-}
geometricMean :: (G.Vector v Double) => v Double -> Double
geometricMean :: forall (v :: * -> *). Vector v Double => v Double -> Double
geometricMean = Double -> Double
forall a. Floating a => a -> a
exp (Double -> Double) -> (v Double -> Double) -> v Double -> Double
forall b c a. (b -> c) -> (a -> b) -> a -> c
. v Double -> Double
forall (v :: * -> *). Vector v Double => v Double -> Double
mean (v Double -> Double)
-> (v Double -> v Double) -> v Double -> Double
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Double -> Double) -> v Double -> v Double
forall (v :: * -> *) a b.
(Vector v a, Vector v b) =>
(a -> b) -> v a -> v b
G.map Double -> Double
forall a. Floating a => a -> a
log
{-# INLINE geometricMean #-}
centralMoment :: (G.Vector v Double) => Int -> v Double -> Double
centralMoment :: forall (v :: * -> *). Vector v Double => Int -> v Double -> Double
centralMoment Int
a v Double
xs
| Int
a Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
0 = [Char] -> Double
forall a. HasCallStack => [Char] -> a
error [Char]
"Statistics.Sample.centralMoment: negative input"
| Int
a Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
0 = Double
1
| Int
a Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
1 = Double
0
| Bool
otherwise = v Double -> Double
forall (v :: * -> *). Vector v Double => v Double -> Double
sum ((Double -> Double) -> v Double -> v Double
forall (v :: * -> *) a b.
(Vector v a, Vector v b) =>
(a -> b) -> v a -> v b
G.map Double -> Double
go v Double
xs) Double -> Double -> Double
forall a. Fractional a => a -> a -> a
/ Int -> Double
forall a b. (Integral a, Num b) => a -> b
fromIntegral (v Double -> Int
forall (v :: * -> *) a. Vector v a => v a -> Int
G.length v Double
xs)
where
go :: Double -> Double
go Double
x = (Double
xDouble -> Double -> Double
forall a. Num a => a -> a -> a
-Double
m) Double -> Int -> Double
^ Int
a
m :: Double
m = v Double -> Double
forall (v :: * -> *). Vector v Double => v Double -> Double
mean v Double
xs
{-# SPECIALIZE centralMoment :: Int -> U.Vector Double -> Double #-}
{-# SPECIALIZE centralMoment :: Int -> V.Vector Double -> Double #-}
centralMoments :: (G.Vector v Double) => Int -> Int -> v Double -> (Double, Double)
centralMoments :: forall (v :: * -> *).
Vector v Double =>
Int -> Int -> v Double -> (Double, Double)
centralMoments Int
a Int
b v Double
xs
| Int
a Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
2 Bool -> Bool -> Bool
|| Int
b Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
2 = (Int -> v Double -> Double
forall (v :: * -> *). Vector v Double => Int -> v Double -> Double
centralMoment Int
a v Double
xs , Int -> v Double -> Double
forall (v :: * -> *). Vector v Double => Int -> v Double -> Double
centralMoment Int
b v Double
xs)
| Bool
otherwise = V -> (Double, Double)
fini (V -> (Double, Double))
-> (v Double -> V) -> v Double -> (Double, Double)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (V -> Double -> V) -> V -> v Double -> V
forall (v :: * -> *) b a.
Vector v b =>
(a -> b -> a) -> a -> v b -> a
G.foldl' V -> Double -> V
go (Double -> Double -> V
V Double
0 Double
0) (v Double -> (Double, Double)) -> v Double -> (Double, Double)
forall a b. (a -> b) -> a -> b
$ v Double
xs
where go :: V -> Double -> V
go (V Double
i Double
j) Double
x = Double -> Double -> V
V (Double
i Double -> Double -> Double
forall a. Num a => a -> a -> a
+ Double
dDouble -> Int -> Double
^Int
a) (Double
j Double -> Double -> Double
forall a. Num a => a -> a -> a
+ Double
dDouble -> Int -> Double
^Int
b)
where d :: Double
d = Double
x Double -> Double -> Double
forall a. Num a => a -> a -> a
- Double
m
fini :: V -> (Double, Double)
fini (V Double
i Double
j) = (Double
i Double -> Double -> Double
forall a. Fractional a => a -> a -> a
/ Double
n , Double
j Double -> Double -> Double
forall a. Fractional a => a -> a -> a
/ Double
n)
m :: Double
m = v Double -> Double
forall (v :: * -> *). Vector v Double => v Double -> Double
mean v Double
xs
n :: Double
n = Int -> Double
forall a b. (Integral a, Num b) => a -> b
fromIntegral (v Double -> Int
forall (v :: * -> *) a. Vector v a => v a -> Int
G.length v Double
xs)
{-# SPECIALIZE
centralMoments :: Int -> Int -> U.Vector Double -> (Double, Double) #-}
{-# SPECIALIZE
centralMoments :: Int -> Int -> V.Vector Double -> (Double, Double) #-}
skewness :: (G.Vector v Double) => v Double -> Double
skewness :: forall (v :: * -> *). Vector v Double => v Double -> Double
skewness v Double
xs = Double
c3 Double -> Double -> Double
forall a. Num a => a -> a -> a
* Double
c2 Double -> Double -> Double
forall a. Floating a => a -> a -> a
** (-Double
1.5)
where (Double
c3 , Double
c2) = Int -> Int -> v Double -> (Double, Double)
forall (v :: * -> *).
Vector v Double =>
Int -> Int -> v Double -> (Double, Double)
centralMoments Int
3 Int
2 v Double
xs
{-# SPECIALIZE skewness :: U.Vector Double -> Double #-}
{-# SPECIALIZE skewness :: V.Vector Double -> Double #-}
kurtosis :: (G.Vector v Double) => v Double -> Double
kurtosis :: forall (v :: * -> *). Vector v Double => v Double -> Double
kurtosis v Double
xs = Double
c4 Double -> Double -> Double
forall a. Fractional a => a -> a -> a
/ (Double
c2 Double -> Double -> Double
forall a. Num a => a -> a -> a
* Double
c2) Double -> Double -> Double
forall a. Num a => a -> a -> a
- Double
3
where (Double
c4 , Double
c2) = Int -> Int -> v Double -> (Double, Double)
forall (v :: * -> *).
Vector v Double =>
Int -> Int -> v Double -> (Double, Double)
centralMoments Int
4 Int
2 v Double
xs
{-# SPECIALIZE kurtosis :: U.Vector Double -> Double #-}
{-# SPECIALIZE kurtosis :: V.Vector Double -> Double #-}
data V = V {-# UNPACK #-} !Double {-# UNPACK #-} !Double
variance :: (G.Vector v Double) => v Double -> Double
variance :: forall (v :: * -> *). Vector v Double => v Double -> Double
variance v Double
samp
| Int
n Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
1 = Double -> v Double -> Double
forall (v :: * -> *).
Vector v Double =>
Double -> v Double -> Double
robustSumVar (v Double -> Double
forall (v :: * -> *). Vector v Double => v Double -> Double
mean v Double
samp) v Double
samp Double -> Double -> Double
forall a. Fractional a => a -> a -> a
/ Int -> Double
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
n
| Bool
otherwise = Double
0
where
n :: Int
n = v Double -> Int
forall (v :: * -> *) a. Vector v a => v a -> Int
G.length v Double
samp
{-# SPECIALIZE variance :: U.Vector Double -> Double #-}
{-# SPECIALIZE variance :: V.Vector Double -> Double #-}
varianceUnbiased :: (G.Vector v Double) => v Double -> Double
varianceUnbiased :: forall (v :: * -> *). Vector v Double => v Double -> Double
varianceUnbiased v Double
samp
| Int
n Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
1 = Double -> v Double -> Double
forall (v :: * -> *).
Vector v Double =>
Double -> v Double -> Double
robustSumVar (v Double -> Double
forall (v :: * -> *). Vector v Double => v Double -> Double
mean v Double
samp) v Double
samp Double -> Double -> Double
forall a. Fractional a => a -> a -> a
/ Int -> Double
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Int
nInt -> Int -> Int
forall a. Num a => a -> a -> a
-Int
1)
| Bool
otherwise = Double
0
where
n :: Int
n = v Double -> Int
forall (v :: * -> *) a. Vector v a => v a -> Int
G.length v Double
samp
{-# SPECIALIZE varianceUnbiased :: U.Vector Double -> Double #-}
{-# SPECIALIZE varianceUnbiased :: V.Vector Double -> Double #-}
meanVariance :: (G.Vector v Double) => v Double -> (Double,Double)
meanVariance :: forall (v :: * -> *).
Vector v Double =>
v Double -> (Double, Double)
meanVariance v Double
samp
| Int
n Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
1 = (Double
m, Double -> v Double -> Double
forall (v :: * -> *).
Vector v Double =>
Double -> v Double -> Double
robustSumVar Double
m v Double
samp Double -> Double -> Double
forall a. Fractional a => a -> a -> a
/ Int -> Double
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
n)
| Bool
otherwise = (Double
m, Double
0)
where
n :: Int
n = v Double -> Int
forall (v :: * -> *) a. Vector v a => v a -> Int
G.length v Double
samp
m :: Double
m = v Double -> Double
forall (v :: * -> *). Vector v Double => v Double -> Double
mean v Double
samp
{-# SPECIALIZE meanVariance :: U.Vector Double -> (Double,Double) #-}
{-# SPECIALIZE meanVariance :: V.Vector Double -> (Double,Double) #-}
meanVarianceUnb :: (G.Vector v Double) => v Double -> (Double,Double)
meanVarianceUnb :: forall (v :: * -> *).
Vector v Double =>
v Double -> (Double, Double)
meanVarianceUnb v Double
samp
| Int
n Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
1 = (Double
m, Double -> v Double -> Double
forall (v :: * -> *).
Vector v Double =>
Double -> v Double -> Double
robustSumVar Double
m v Double
samp Double -> Double -> Double
forall a. Fractional a => a -> a -> a
/ Int -> Double
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Int
nInt -> Int -> Int
forall a. Num a => a -> a -> a
-Int
1))
| Bool
otherwise = (Double
m, Double
0)
where
n :: Int
n = v Double -> Int
forall (v :: * -> *) a. Vector v a => v a -> Int
G.length v Double
samp
m :: Double
m = v Double -> Double
forall (v :: * -> *). Vector v Double => v Double -> Double
mean v Double
samp
{-# SPECIALIZE meanVarianceUnb :: U.Vector Double -> (Double,Double) #-}
{-# SPECIALIZE meanVarianceUnb :: V.Vector Double -> (Double,Double) #-}
stdDev :: (G.Vector v Double) => v Double -> Double
stdDev :: forall (v :: * -> *). Vector v Double => v Double -> Double
stdDev = Double -> Double
forall a. Floating a => a -> a
sqrt (Double -> Double) -> (v Double -> Double) -> v Double -> Double
forall b c a. (b -> c) -> (a -> b) -> a -> c
. v Double -> Double
forall (v :: * -> *). Vector v Double => v Double -> Double
varianceUnbiased
{-# SPECIALIZE stdDev :: U.Vector Double -> Double #-}
{-# SPECIALIZE stdDev :: V.Vector Double -> Double #-}
stdErrMean :: (G.Vector v Double) => v Double -> Double
stdErrMean :: forall (v :: * -> *). Vector v Double => v Double -> Double
stdErrMean v Double
samp = v Double -> Double
forall (v :: * -> *). Vector v Double => v Double -> Double
stdDev v Double
samp Double -> Double -> Double
forall a. Fractional a => a -> a -> a
/ (Double -> Double
forall a. Floating a => a -> a
sqrt (Double -> Double) -> (v Double -> Double) -> v Double -> Double
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> Double
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Int -> Double) -> (v Double -> Int) -> v Double -> Double
forall b c a. (b -> c) -> (a -> b) -> a -> c
. v Double -> Int
forall (v :: * -> *) a. Vector v a => v a -> Int
G.length) v Double
samp
{-# SPECIALIZE stdErrMean :: U.Vector Double -> Double #-}
{-# SPECIALIZE stdErrMean :: V.Vector Double -> Double #-}
robustSumVarWeighted :: (G.Vector v (Double,Double)) => v (Double,Double) -> V
robustSumVarWeighted :: forall (v :: * -> *).
Vector v (Double, Double) =>
v (Double, Double) -> V
robustSumVarWeighted v (Double, Double)
samp = (V -> (Double, Double) -> V) -> V -> v (Double, Double) -> V
forall (v :: * -> *) b a.
Vector v b =>
(a -> b -> a) -> a -> v b -> a
G.foldl' V -> (Double, Double) -> V
go (Double -> Double -> V
V Double
0 Double
0) v (Double, Double)
samp
where
go :: V -> (Double, Double) -> V
go (V Double
s Double
w) (Double
x,Double
xw) = Double -> Double -> V
V (Double
s Double -> Double -> Double
forall a. Num a => a -> a -> a
+ Double
xwDouble -> Double -> Double
forall a. Num a => a -> a -> a
*Double
dDouble -> Double -> Double
forall a. Num a => a -> a -> a
*Double
d) (Double
w Double -> Double -> Double
forall a. Num a => a -> a -> a
+ Double
xw)
where d :: Double
d = Double
x Double -> Double -> Double
forall a. Num a => a -> a -> a
- Double
m
m :: Double
m = v (Double, Double) -> Double
forall (v :: * -> *).
Vector v (Double, Double) =>
v (Double, Double) -> Double
meanWeighted v (Double, Double)
samp
{-# INLINE robustSumVarWeighted #-}
varianceWeighted :: (G.Vector v (Double,Double)) => v (Double,Double) -> Double
varianceWeighted :: forall (v :: * -> *).
Vector v (Double, Double) =>
v (Double, Double) -> Double
varianceWeighted v (Double, Double)
samp
| v (Double, Double) -> Int
forall (v :: * -> *) a. Vector v a => v a -> Int
G.length v (Double, Double)
samp Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
1 = V -> Double
fini (V -> Double) -> V -> Double
forall a b. (a -> b) -> a -> b
$ v (Double, Double) -> V
forall (v :: * -> *).
Vector v (Double, Double) =>
v (Double, Double) -> V
robustSumVarWeighted v (Double, Double)
samp
| Bool
otherwise = Double
0
where
fini :: V -> Double
fini (V Double
s Double
w) = Double
s Double -> Double -> Double
forall a. Fractional a => a -> a -> a
/ Double
w
{-# SPECIALIZE varianceWeighted :: U.Vector (Double,Double) -> Double #-}
{-# SPECIALIZE varianceWeighted :: V.Vector (Double,Double) -> Double #-}
fastVar :: (G.Vector v Double) => v Double -> T1
fastVar :: forall (v :: * -> *). Vector v Double => v Double -> T1
fastVar = (T1 -> Double -> T1) -> T1 -> v Double -> T1
forall (v :: * -> *) b a.
Vector v b =>
(a -> b -> a) -> a -> v b -> a
G.foldl' T1 -> Double -> T1
go (Int -> Double -> Double -> T1
T1 Int
0 Double
0 Double
0)
where
go :: T1 -> Double -> T1
go (T1 Int
n Double
m Double
s) Double
x = Int -> Double -> Double -> T1
T1 Int
n' Double
m' Double
s'
where n' :: Int
n' = Int
n Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1
m' :: Double
m' = Double
m Double -> Double -> Double
forall a. Num a => a -> a -> a
+ Double
d Double -> Double -> Double
forall a. Fractional a => a -> a -> a
/ Int -> Double
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
n'
s' :: Double
s' = Double
s Double -> Double -> Double
forall a. Num a => a -> a -> a
+ Double
d Double -> Double -> Double
forall a. Num a => a -> a -> a
* (Double
x Double -> Double -> Double
forall a. Num a => a -> a -> a
- Double
m')
d :: Double
d = Double
x Double -> Double -> Double
forall a. Num a => a -> a -> a
- Double
m
fastVariance :: (G.Vector v Double) => v Double -> Double
fastVariance :: forall (v :: * -> *). Vector v Double => v Double -> Double
fastVariance = T1 -> Double
fini (T1 -> Double) -> (v Double -> T1) -> v Double -> Double
forall b c a. (b -> c) -> (a -> b) -> a -> c
. v Double -> T1
forall (v :: * -> *). Vector v Double => v Double -> T1
fastVar
where fini :: T1 -> Double
fini (T1 Int
n Double
_m Double
s)
| Int
n Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
1 = Double
s Double -> Double -> Double
forall a. Fractional a => a -> a -> a
/ Int -> Double
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
n
| Bool
otherwise = Double
0
{-# INLINE fastVariance #-}
fastVarianceUnbiased :: (G.Vector v Double) => v Double -> Double
fastVarianceUnbiased :: forall (v :: * -> *). Vector v Double => v Double -> Double
fastVarianceUnbiased = T1 -> Double
fini (T1 -> Double) -> (v Double -> T1) -> v Double -> Double
forall b c a. (b -> c) -> (a -> b) -> a -> c
. v Double -> T1
forall (v :: * -> *). Vector v Double => v Double -> T1
fastVar
where fini :: T1 -> Double
fini (T1 Int
n Double
_m Double
s)
| Int
n Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
1 = Double
s Double -> Double -> Double
forall a. Fractional a => a -> a -> a
/ Int -> Double
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Int
n Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1)
| Bool
otherwise = Double
0
{-# INLINE fastVarianceUnbiased #-}
fastStdDev :: (G.Vector v Double) => v Double -> Double
fastStdDev :: forall (v :: * -> *). Vector v Double => v Double -> Double
fastStdDev = Double -> Double
forall a. Floating a => a -> a
sqrt (Double -> Double) -> (v Double -> Double) -> v Double -> Double
forall b c a. (b -> c) -> (a -> b) -> a -> c
. v Double -> Double
forall (v :: * -> *). Vector v Double => v Double -> Double
fastVariance
{-# INLINE fastStdDev #-}
covariance :: (G.Vector v (Double,Double), G.Vector v Double)
=> v (Double,Double)
-> Double
covariance :: forall (v :: * -> *).
(Vector v (Double, Double), Vector v Double) =>
v (Double, Double) -> Double
covariance v (Double, Double)
xy
| Int
n Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
0 = Double
0
| Bool
otherwise = v Double -> Double
forall (v :: * -> *). Vector v Double => v Double -> Double
mean (v Double -> Double) -> v Double -> Double
forall a b. (a -> b) -> a -> b
$ (Double -> Double -> Double) -> v Double -> v Double -> v Double
forall (v :: * -> *) a b c.
(Vector v a, Vector v b, Vector v c) =>
(a -> b -> c) -> v a -> v b -> v c
G.zipWith Double -> Double -> Double
forall a. Num a => a -> a -> a
(*)
((Double -> Double) -> v Double -> v Double
forall (v :: * -> *) a b.
(Vector v a, Vector v b) =>
(a -> b) -> v a -> v b
G.map (\Double
x -> Double
x Double -> Double -> Double
forall a. Num a => a -> a -> a
- Double
muX) v Double
xs)
((Double -> Double) -> v Double -> v Double
forall (v :: * -> *) a b.
(Vector v a, Vector v b) =>
(a -> b) -> v a -> v b
G.map (\Double
y -> Double
y Double -> Double -> Double
forall a. Num a => a -> a -> a
- Double
muY) v Double
ys)
where
n :: Int
n = v (Double, Double) -> Int
forall (v :: * -> *) a. Vector v a => v a -> Int
G.length v (Double, Double)
xy
(v Double
xs,v Double
ys) = v (Double, Double) -> (v Double, v Double)
forall (v :: * -> *) a b.
(Vector v a, Vector v b, Vector v (a, b)) =>
v (a, b) -> (v a, v b)
G.unzip v (Double, Double)
xy
muX :: Double
muX = v Double -> Double
forall (v :: * -> *). Vector v Double => v Double -> Double
mean v Double
xs
muY :: Double
muY = v Double -> Double
forall (v :: * -> *). Vector v Double => v Double -> Double
mean v Double
ys
{-# SPECIALIZE covariance :: U.Vector (Double,Double) -> Double #-}
{-# SPECIALIZE covariance :: V.Vector (Double,Double) -> Double #-}
correlation :: (G.Vector v (Double,Double), G.Vector v Double)
=> v (Double,Double)
-> Double
correlation :: forall (v :: * -> *).
(Vector v (Double, Double), Vector v Double) =>
v (Double, Double) -> Double
correlation v (Double, Double)
xy
| Int
n Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
0 = Double
0
| Bool
otherwise = Double
cov Double -> Double -> Double
forall a. Fractional a => a -> a -> a
/ Double -> Double
forall a. Floating a => a -> a
sqrt (Double
varX Double -> Double -> Double
forall a. Num a => a -> a -> a
* Double
varY)
where
n :: Int
n = v (Double, Double) -> Int
forall (v :: * -> *) a. Vector v a => v a -> Int
G.length v (Double, Double)
xy
(v Double
xs,v Double
ys) = v (Double, Double) -> (v Double, v Double)
forall (v :: * -> *) a b.
(Vector v a, Vector v b, Vector v (a, b)) =>
v (a, b) -> (v a, v b)
G.unzip v (Double, Double)
xy
(Double
muX,Double
varX) = v Double -> (Double, Double)
forall (v :: * -> *).
Vector v Double =>
v Double -> (Double, Double)
meanVariance v Double
xs
(Double
muY,Double
varY) = v Double -> (Double, Double)
forall (v :: * -> *).
Vector v Double =>
v Double -> (Double, Double)
meanVariance v Double
ys
cov :: Double
cov = v Double -> Double
forall (v :: * -> *). Vector v Double => v Double -> Double
mean (v Double -> Double) -> v Double -> Double
forall a b. (a -> b) -> a -> b
$ (Double -> Double -> Double) -> v Double -> v Double -> v Double
forall (v :: * -> *) a b c.
(Vector v a, Vector v b, Vector v c) =>
(a -> b -> c) -> v a -> v b -> v c
G.zipWith Double -> Double -> Double
forall a. Num a => a -> a -> a
(*)
((Double -> Double) -> v Double -> v Double
forall (v :: * -> *) a b.
(Vector v a, Vector v b) =>
(a -> b) -> v a -> v b
G.map (\Double
x -> Double
x Double -> Double -> Double
forall a. Num a => a -> a -> a
- Double
muX) v Double
xs)
((Double -> Double) -> v Double -> v Double
forall (v :: * -> *) a b.
(Vector v a, Vector v b) =>
(a -> b) -> v a -> v b
G.map (\Double
y -> Double
y Double -> Double -> Double
forall a. Num a => a -> a -> a
- Double
muY) v Double
ys)
{-# SPECIALIZE correlation :: U.Vector (Double,Double) -> Double #-}
{-# SPECIALIZE correlation :: V.Vector (Double,Double) -> Double #-}
pair :: (G.Vector v a, G.Vector v b, G.Vector v (a,b)) => v a -> v b -> v (a,b)
pair :: forall (v :: * -> *) a b.
(Vector v a, Vector v b, Vector v (a, b)) =>
v a -> v b -> v (a, b)
pair v a
va v b
vb
| v a -> Int
forall (v :: * -> *) a. Vector v a => v a -> Int
G.length v a
va Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== v b -> Int
forall (v :: * -> *) a. Vector v a => v a -> Int
G.length v b
vb = v a -> v b -> v (a, b)
forall (v :: * -> *) a b.
(Vector v a, Vector v b, Vector v (a, b)) =>
v a -> v b -> v (a, b)
G.zip v a
va v b
vb
| Bool
otherwise = [Char] -> v (a, b)
forall a. HasCallStack => [Char] -> a
error [Char]
"Statistics.Sample.pair: vector must have same length"
{-# INLINE pair #-}
(^) :: Double -> Int -> Double
Double
x ^ :: Double -> Int -> Double
^ Int
1 = Double
x
Double
x ^ Int
n = Double
x Double -> Double -> Double
forall a. Num a => a -> a -> a
* (Double
x Double -> Int -> Double
^ (Int
nInt -> Int -> Int
forall a. Num a => a -> a -> a
-Int
1))
{-# INLINE (^) #-}
data T = T {-# UNPACK #-}!Double {-# UNPACK #-}!Int
data T1 = T1 {-# UNPACK #-}!Int {-# UNPACK #-}!Double {-# UNPACK #-}!Double