Commits

Anonymous committed 416ee06

Reorder data

Comments (0)

Files changed (2)

Data/Monoid/Statistics.hs

 --
 --   > pappend x (pappend y mempty) == pappend x mempty `mappend` pappend y mempty
 --   > mappend x y == mappend y x
+--
+--   It is very similar to Reducer type class from monoids package but
+--   require commutative monoids
 class Monoid m => StatMonoid m a where
   -- | Add one element to monoid accumulator. P stands for point in
   --   analogy for Pointed.

Data/Monoid/Statistics/Numeric.hs

 {-# LANGUAGE FlexibleContexts #-}
 {-# LANGUAGE FlexibleInstances #-}
 {-# LANGUAGE MultiParamTypeClasses #-}
-module Data.Monoid.Statistics.Numeric ( Mean(..)
+module Data.Monoid.Statistics.Numeric ( Count(..)
+                                        -- * Mean
+                                      , Mean(..)
+                                        -- * Variance
                                       , Variance()
                                       , calcCountVar
                                       , calcMeanVar
 
 
 -- | Intermediate quantities to calculate the standard deviation.
-data Variance = Variance { varianceSum   :: {-# UNPACK #-} !Double
-                     -- ^ Current sum of elements of sample
-                   , varianceSumSq :: {-# UNPACK #-} !Double
-                     -- ^ Current sum of squares of deviations from current mean
-                   , calcCountVar  :: {-# UNPACK #-} !Int
-                     -- ^ Number of elements in the sample
-                   }
-           deriving Show
+data Variance = Variance { calcCountVar  :: {-# UNPACK #-} !Int
+                         -- ^ Number of elements in the sample
+                         , varianceSum   :: {-# UNPACK #-} !Double
+                         -- ^ Current sum of elements of sample
+                         , varianceSumSq :: {-# UNPACK #-} !Double
+                         -- ^ Current sum of squares of deviations from current mean
+                         }
+                deriving Show
 
 -- | Calculate mean of the sample (use 'Mean' if you need only it).
 calcMeanVar :: Variance -> Double
 --
 instance Monoid Variance where
   mempty = Variance 0 0 0
-  mappend !(Variance ta sa n1) !(Variance tb sb n2) = Variance (ta+tb) sumsq (n1+n2)
+  mappend !(Variance n1 ta sa) !(Variance n2 tb sb) = Variance (n1+n2) (ta+tb) sumsq
     where
       na = fromIntegral n1
       nb = fromIntegral n2
 
 instance ConvertibleToDouble a => StatMonoid Variance a where
   -- Can be implemented directly as in Welford-Knuth algorithm.
-  pappend !x !s = s `mappend` (Variance (toDouble x) 0 1)
+  pappend !x !s = s `mappend` (Variance 1 (toDouble x) 0)
   {-# INLINE pappend #-}