h&!X       !"#$%&'()*+,-./0123456789:;<=>?@ Safe-Inferred"%)*/18?8"mealyA rough Median. The average absolute value of the stat is used to callibrate estimate drift towards the medianmealyMost common statistics are averages, which are some sort of aggregation of values (sum) and some sort of sample size (count).Amealy Strict Pair mealyA   is a triple of functions (a -> b) inject& Convert an input into the state type.(b -> a -> b) step0 Update state given prior state and (new) input. (c -> b) extract" Convert state to the output type./By adopting this order, a Mealy sum looks like:  M id (+) idwhere the first id is the initial injection to a contravariant position, and the second id is the covariant extraction.inject kicks off state on the initial element of the Foldable, but is otherwise be independent of step. 1scan (M i s e) (x : xs) = e <$> scanl' s (i x) xs mealyPattern for an .  A sum count mealyConvenience pattern for a  . M extract step injectmealyFold a list through a  . cosieve == foldmealyRun a list through a  + and return a list of values for every step length (scan _ xs) == length xsmealyextract the average from an av gives NaN on zero dividemealy)substitute a default value on zero-divide av_ (Averager (0,0)) x == xmealy online f g is a   where f is a transformation of the data and g is a decay function (usually convergent to zero) applied at each step. online id id == avonline is best understood by examining usage to produce a moving average and standard deviation:An exponentially-weighted moving average with a decay rate of 0.9 ma r == online id (*r)7An exponentially-weighted moving average of the square. #sqma r = online (\x -> x * x) (* r)Applicative-style exponentially-weighted standard deviation computation: 9std r = (\s ss -> sqrt (ss - s ** 2)) <$> ma r <*> sqma rmealyA moving average using a decay rate of r. r=1 represents the simple average, and r=0 represents the latest value.fold (ma 0) ([1..100])100.0fold (ma 1) ([1..100])50.5fold (ma 0.99) xs09.713356299018187e-2mealyabsolute averagefold (absma 1) xs00.8075705557429647mealyaverage square )fold (ma r) . fmap (**2) == fold (sqma r)mealystandard deviationThe construction of standard deviation, using the Applicative instance of a  : 9(\s ss -> sqrt (ss - s ** (one+one))) <$> ma r <*> sqma rThe average deviation of the numbers 1..1000 is about 1 / sqrt 12 * 1000 https://en.wikipedia.org/wiki/Uniform_distribution_(continuous)#Standard_uniformfold (std 1) [0..1000]288.9636655359978*The average deviation with a decay of 0.99fold (std 0.99) [0..1000]99.28328803163829fold (std 1) xs01.0126438036262801mealyThe covariance of a tuple given an underlying central tendency fold.fold (cov (ma 1)) xsp0.7818936662586868mealy/correlation of a tuple, specialised to Guassianfold (corrGauss 1) xsp0.7978347126677433mealy/a generalised version of correlation of a tuplefold (corr (ma 1) (std 1)) xsp0.7978347126677433 "corr (ma r) (std r) == corrGauss rmealyThe beta in a simple linear regression of an (independent variable, single dependent variable) tuple given an underlying central tendency fold.This is a generalisation of the classical regression formula, where averages are replaced by   statistics. \begin{align} \beta & = \frac{n\sum xy - \sum x \sum y}{n\sum x^2 - (\sum x)^2} \\ & = \frac{n^2 \overline{xy} - n^2 \bar{x} \bar{y}}{n^2 \overline{x^2} - n^2 \bar{x}^2} \\ & = \frac{\overline{xy} - \bar{x} \bar{y}}{\overline{x^2} - \bar{x}^2} \\ \end{align} :fold (beta1 (ma 1)) $ zipWith (\x y -> (y, x + y)) xs0 xs10.999747321294513mealyThe alpha in a simple linear regression of an (independent variable, single dependent variable) tuple given an underlying central tendency fold. \begin{align} \alpha & = \frac{\sum y \sum x^2 - \sum x \sum xy}{n\sum x^2 - (\sum x)^2} \\ & = \frac{n^2 \bar{y} \overline{x^2} - n^2 \bar{x} \overline{xy}}{n^2 \overline{x^2} - n^2 \bar{x}^2} \\ & = \frac{\bar{y} \overline{x^2} - \bar{x} \overline{xy}}{\overline{x^2} - \bar{x}^2} \\ \end{align} fold (alpha1 (ma 1)) $ zipWith (\x y -> ((3+y), x + 0.5 * (3 + y))) xs0 xs11.3680496627365146e-2mealyThe (alpha, beta) tuple in a simple linear regression of an (independent variable, single dependent variable) tuple given an underlying central tendency fold.fold (reg1 (ma 1)) $ zipWith (\x y -> ((3+y), x + 0.5 * (3 + y))) xs0 xs1*(1.3680496627365146e-2,0.4997473212944953)mealymultiple regression \begin{align} {\hat {{\mathbf {B}}}}=({\mathbf {X}}^{{{\rm {T}}}}{\mathbf {X}})^{{ -1}}{\mathbf {X}}^{{{\rm {T}}}}{\mathbf {Y}} \end{align}  \begin{align} {\mathbf {X}}={\begin{bmatrix}{\mathbf {x}}_{1}^{{{\rm {T}}}}\\{\mathbf {x}}_{2}^{{{\rm {T}}}}\\\vdots \\{\mathbf {x}}_{n}^{{{\rm {T}}}}\end{bmatrix}}={\begin{bmatrix}x_{{1,1}}&\cdots &x_{{1,k}}\\x_{{2,1}}&\cdots &x_{{2,k}}\\\vdots &\ddots &\vdots \\x_{{n,1}}&\cdots &x_{{n,k}}\end{bmatrix}} \end{align}  let ys = zipWith3 (\x y z -> 0.1 * x + 0.5 * y + 1 * z) xs0 xs1 xs2 let zs = zip (zipWith (\x y -> fromList [x,y] :: F.Array '[2] Double) xs1 xs2) ys fold (beta 0.99) zs %0.4982692361226971, 1.038192474255091mealyalpha in a multiple regressionmealymultiple regression let ys = zipWith3 (\x y z -> 0.1 * x + 0.5 * y + 1 * z) xs0 xs1 xs2 let zs = zip (zipWith (\x y -> fromList [x,y] :: F.Array '[2] Double) xs1 xs2) ys fold (reg 0.99) zs>([0.4982692361226971, 1.038192474255091],2.087160803386695e-3) mealyaccumulated sum!mealyconstant Mealy"mealymost recent value#mealy9most recent value if it exists, previous value otherwise.$mealydelay input values by 1%mealydelays values by n stepsdelay [0] == delay1 0delay [] == id!delay [1,2] = delay1 2 . delay1 1scan (delay [-2,-1]) [0..3] [-2,-1,0,1]Autocorrelation example: 8scan (((,) <$> id <*> delay [0]) >>> beta (ma 0.99)) xs0&mealya moving window of a's, most recent at the front of the sequence'mealy/onlineL1' takes a function and turns it into a   where the step is an incremental update of an (isomorphic) median statistic.(mealymoving median > L.fold (maL1 inc d r) [1..n] 93.928223127421081mealy av mempty == nan%mealy/initial statistical values, delay equals length(  !"#$%&'((   !"#$%&'( Safe-Inferred.1i;mealy,Mealy quantiles based on the tdigest library<mealyMealy median using BThe tdigest algorithm works best at extremes and can be unreliable in the centre.=mealyA mealy that computes the running quantile bucket. For example, in a scan, digitize 0.9 [0,0.5,1] returns:=0 if the current value is less than the current mealy median.1 if the current value is greater than the current mealy median.;<=<;= Safe-Inferred 1?mealy6rvs creates a list of standard normal random variates.import Data.Mealyimport Data.Mealy.Simulate gen <- create rvs gen 3?[1.8005943761746166e-2,0.36444481359059255,-1.2939898115295387]rs <- rvs gen 10000fold (ma 1) rs1.29805301109162e-2fold (std 1) rs1.0126527176272948@mealy?@ABCDEFG"mealy-0.4.2-EwjpNqYkgloDrMXOEt3FBYData.Mealy.Simulate Data.MealyData.Mealy.Quantiles*mwc-random-0.15.0.2-Fxh37Je2QGPLkAuS7FE4sdSystem.Random.MWCcreateMedianer$sel:medAbsSum:Medianer$sel:medCount:Medianer$sel:medianEst:MedianerAverager$sel:sumCount:AveragerMealyAMdipurefoldscanavav_onlinemaabsmasqmastdcov corrGausscorrbeta1alpha1reg1betaalpharegasumaconstlast maybeLastdelay1delaywindowonlineL1maL1$fExceptionMealyError $fStrongMealy$fProfunctorMealy$fFunctorMealy$fCategoryTYPEMealy$fApplicativeMealy $fMonoidPair'$fSemigroupPair'$fMonoidAverager$fSemigroupAverager$fFunctorRegressionState $fEqAverager$fShowAverager $fEqPair' $fOrdPair' $fShowPair' $fReadPair'$fShowMealyError quantilesmediandigitize$fShowOnlineTDigestrvsrvspPair'&tdigest-0.2.1.1-AWWkKoW8P9C1rvWy2JNG3PData.TDigest.Tree.Internaltdigest