monoid-statistics-0.1: Monoids for calculation of statistics of sample

Stability experimental Alexey Khudyakov

Data.Monoid.Statistics

Description

Synopsis

# Documentation

class Monoid m => StatMonoid m a whereSource

Monoid which corresponds to some stattics. In order to do so it must be commutative. In many cases it's not practical to construct monoids for each element so `papennd` was added. First parameter of type class is monoidal accumulator. Second is type of element over which statistic is calculated.

Statistic could be calculated with fold over sample. Since accumulator is `Monoid` such fold could be easily parralelized.

Instance must satisfy following law:

``` pappend x (pappend y mempty) == pappend x mempty `mappend` pappend y mempty
mappend x y == mappend y x
```

Methods

pappend :: a -> m -> mSource

Add one element to monoid accumulator. P stands for point in analogy for Pointed.

Instances

 StatMonoid Mean Double StatMonoid Mean Float StatMonoid Mean Int StatMonoid Mean Int8 StatMonoid Mean Int16 StatMonoid Mean Int32 StatMonoid Mean Int64 StatMonoid Mean Integer StatMonoid Mean Word StatMonoid Mean Word8 StatMonoid Mean Word16 StatMonoid Mean Word32 StatMonoid Mean Word64 Integral a => StatMonoid (Count a) b (StatMonoid a x, StatMonoid b x) => StatMonoid (TwoStats a b) x

evalStatistic :: (Foldable d, StatMonoid m a) => d a -> mSource

Calculate statistic over `Foldable`. It's implemented in terms of foldl'.

# Statistic monoids

newtype Count a Source

Simplest statistics. Number of elements in the sample

Constructors

 Count FieldscalcCount :: a

Instances

 Show a => Show (Count a) Integral a => Monoid (Count a) Integral a => StatMonoid (Count a) b

data Mean Source

Mean of sample. Samples of Double,Float and bui;t-in integral types are supported

Numeric stability of `mappend` is not proven.

Constructors

 Mean FieldscalcMean :: DoubleCurrent mean calcCountMean :: IntNumber of entries

Statistic is function of a sample which does not depend on order of elements in a sample. For each statistics corresponding monoid could be constructed:

``` f :: [A] -> B

data F = F [A]

evalF (F xs) = f xs

instance Monoid F here
mempty = F []
(F a) `mappend` (F b) = F (a ++ b)
```

This indeed proves that monoid could be constructed. Monoid above is completely impractical. It runs in O(n) space. However for some statistics monoids which runs in O(1) space could be implemented. For example mean.

On the other hand some statistics could not be implemented in such way. For example calculation of median require O(n) space. Variance could be implemented in O(1) but such implementation won't be numerically stable.