Skip to content

Commit

Permalink
Add weighted variance
Browse files Browse the repository at this point in the history
  • Loading branch information
Shimuuar committed Mar 29, 2010
1 parent 3b01f7e commit 02c5b3a
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions Statistics/Sample.hs
Expand Up @@ -38,6 +38,7 @@ module Statistics.Sample
, variance
, varianceUnbiased
, stdDev
, varianceWeighted

-- ** Single-pass functions (faster, less safe)
-- $cancellation
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 02c5b3a

Please sign in to comment.