commit 0818743b94bd958646d48cd1fdbd0dd87f4e8135
parent 8c9d07f20deaaf98ddf5c9b834bf82d2f57a9680
Author: Jared Tobin <jared@jtobin.io>
Date: Thu, 21 May 2020 20:59:50 +0400
Pull in foldl dependency for tests.
Diffstat:
2 files changed, 7 insertions(+), 5 deletions(-)
diff --git a/mighty-metropolis.cabal b/mighty-metropolis.cabal
@@ -81,6 +81,7 @@ Test-suite tests
build-depends:
base >= 4 && < 6
, containers >= 0.5 && < 0.7
+ , foldl
, mighty-metropolis
, mwc-probability >= 1.0.1
, hspec
diff --git a/test/Spec.hs b/test/Spec.hs
@@ -1,6 +1,7 @@
{-# OPTIONS_GHC -Wall #-}
{-# LANGUAGE RecordWildCards #-}
+import qualified Control.Foldl as L
import Data.Functor.Identity
import Data.Maybe (mapMaybe)
import Data.Sampling.Types
@@ -16,13 +17,13 @@ withinPercent b n a
d = abs (a - b)
mean :: [Double] -> Double
-mean xs = sum xs / n where
- n = fromIntegral (length xs)
+mean = L.fold L.mean
variance :: [Double] -> Double
-variance xs = sum [(x - m) ** 2.0 | x <- xs] / (n - 1) where
- m = mean xs
- n = fromIntegral (length xs)
+variance xs = L.fold alg xs where
+ alg = (/) <$> L.premap csq L.sum <*> L.genericLength - 1
+ csq = (** 2.0) . subtract m
+ m = mean xs
stdDev :: [Double] -> Double
stdDev = sqrt . variance