Safe Haskell | Safe |
---|---|
Language | Haskell2010 |
online statistics based on a moving average
Synopsis
- data Averager a b
- online :: Fractional b => (a -> b) -> (b -> b) -> Fold a b
- ma :: Fractional a => a -> Fold a a
- absma :: Fractional a => a -> Fold a a
- sqma :: Fractional a => a -> Fold a a
- std :: (Fractional a, Floating a) => a -> Fold a a
- cov :: Num a => Fold a a -> Fold (a, a) a
- corr :: Floating a => Fold a a -> Fold a a -> Fold (a, a) a
- corrGauss :: Floating a => a -> Fold (a, a) a
- beta :: Fractional a => Fold a a -> Fold (a, a) a
- alpha :: Fractional a => Fold a a -> Fold (a, a) a
- autocorr :: RealFloat a => Fold a a -> Fold (a, a) a -> Fold a a
- mconst :: a -> Fold a a
Documentation
Most common statistics are averages.
online :: Fractional b => (a -> b) -> (b -> b) -> Fold a b Source #
online takes a function and turns it into a Fold
where the step is an incremental update of the (isomorphic) statistic.
online statistics
ma :: Fractional a => a -> Fold a a Source #
moving average with a decay rate
so 'ma 1' is the simple average (no decay in the statistic), and 'ma 0.00001' is the last value (insta-decay)
>>>
L.fold (ma 1) [0..100]
50.0
>>>
L.fold (ma 1e-12) [0..100]
99.999999999999
>>>
L.fold (ma 0.9) [0..100]
91.00241448887785
absma :: Fractional a => a -> Fold a a Source #
absolute average
sqma :: Fractional a => a -> Fold a a Source #
average square
std :: (Fractional a, Floating a) => a -> Fold a a Source #
standard deviation
The formulae for standard deviation, expressed in online terminology, highlights how this statistic is composed of averages:
(\s ss -> sqrt (ss - s ** (one+one))) <$> ma r <*> sqma r
The average deviation of the numbers 1..1000 is about 1 sqrt 12 * 1000 (see <<https:en.wikipedia.orgwiki/Uniform_distribution_(continuous)#Standard_uniform wiki>>)
>>>
L.fold (std 1) [0..1000]
288.9636655359978
The average deviation with a decay of 0.99
>>>
L.fold (std 0.99) [0..1000]
99.28328803164005
cov :: Num a => Fold a a -> Fold (a, a) a Source #
the covariance of a tuple given an underlying central tendency fold
corr :: Floating a => Fold a a -> Fold a a -> Fold (a, a) a Source #
a generalised version of correlation of a tuple
corrGauss :: Floating a => a -> Fold (a, a) a Source #
correlation of a tuple, specialised to Guassian
beta :: Fractional a => Fold a a -> Fold (a, a) a Source #
the beta in a simple linear regression of a tuple given an underlying central tendency fold
autocorr :: RealFloat a => Fold a a -> Fold (a, a) a -> Fold a a Source #
autocorrelation is a slippery concept. This method starts with the concept that there is an underlying random error process (e), and autocorrelation is a process on top of that ie for a one-step correlation relationship.
valuet = e
t + k * e@t-1
where k is the autocorrelation.
There are thus two online rates needed: one for the average being considered to be the dependent variable, and one for the online of the correlation calculation between the most recent value and the moving average. For example,
L.fold (autocorr zero one)
would estimate the one-step autocorrelation relationship of the previous value and the current value over the entire sample set.