Permalink
Browse files

Add weighted variance

  • Loading branch information...
1 parent 3b01f7e commit 02c5b3a8d852d48e95d0d0c0aa7737f220144b69 @Shimuuar Shimuuar committed Mar 29, 2010
Showing with 18 additions and 0 deletions.
  1. +18 −0 Statistics/Sample.hs
View
@@ -38,6 +38,7 @@ module Statistics.Sample
, variance
, varianceUnbiased
, stdDev
+ , varianceWeighted
-- ** Single-pass functions (faster, less safe)
-- $cancellation
@@ -229,6 +230,23 @@ varianceUnbiased samp
stdDev :: Sample -> Double
stdDev = sqrt . varianceUnbiased
+
+robustSumVarWeighted :: WeightedSample -> V
+robustSumVarWeighted samp = U.foldl go (V 0 0) samp
+ where
+ go (V s w) (x,xw) = V (s + xw*d*d) (w + xw)
+ where d = x - m
+ m = meanWeighted samp
+
+-- | Weighted variance. This is biased estimation.
+varianceWeighted :: WeightedSample -> Double
+varianceWeighted samp
+ | U.length samp > 1 = fini $ robustSumVarWeighted samp
+ | otherwise = 0
+ where
+ fini (V s w) = s / w
+{-# INLINE varianceWeighted #-}
+
-- $cancellation
--
-- The functions prefixed with the name @fast@ below perform a single

0 comments on commit 02c5b3a

Please sign in to comment.