h$'W%      !"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHNone #'(-/58<=>?!$mealyA rough Median. The average absolute value of the stat is used to callibrate estimate drift towards the medianmealy>a linear model of state dependencies for the first two moments \begin{align} x_{t+1} & = (alpha_t^x + beta_t^{x->x} * ma_t^x + beta_t^{s->x} * std_t^x) + s_{t+1}\\ s_{t+1} & = (alpha_t^s + beta_t^{x->s} * ma_t^x + beta_t^{s->s} * std_t^x) * N(0,1) \end{align} mealyMost common statistics are averages, which are some sort of aggregation of values (sum) and some sort of sample size (count).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 covriant extraction.inject kicks off state on the initial element of the Foldable, but is otherwise be independent of step. 1scan (M e s i) (x : xs) = e <$> scanl' s (i x) xsmealyPattern for an .  A sum countmealyPattern 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.7818936662586868 mealy/correlation of a tuple, specialised to Guassianfold (corrGauss 1) xsp0.7978347126677433!mealy/a generalised version of correlation of a tuplefold (corr (ma 1) (std 1)) xsp0.7978347126677433 "corr (ma r) (std r) == corrGauss r"mealyThe 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.999747321294513#mealyThe 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-2$mealyThe (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.038192474255091&mealyalpha in a multiple regression'mealymultiple 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*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,mealy#Add a state dependency to a series.Typical regression analytics tend to assume that moments of a distributional assumption are unconditional with respect to prior instantiations of the stochastics being studied.For time series analytics, a major preoccupation is estimation of the current moments given what has happened in the past.IID: \begin{align} x_{t+1} & = alpha_t^x + s_{t+1}\\ s_{t+1} & = alpha_t^s * N(0,1) \end{align} Example: including a linear dependency on moving average history: \begin{align} x_{t+1} & = (alpha_t^x + beta_t^{x->x} * ma_t^x) + s_{t+1}\\ s_{t+1} & = alpha_t^s * N(0,1) \end{align} =let xs' = scan (depState (\a m -> a + 0.1 * m) (ma 0.99)) xs02let ma' = scan ((ma (1 - 0.01)) >>> delay [0]) xs'>let xsb = fold (beta1 (ma (1 - 0.001))) $ drop 1 $ zip ma' xs'8-- beta measurement if beta of ma was, in reality, zero.?let xsb0 = fold (beta1 (ma (1 - 0.001))) $ drop 1 $ zip ma' xs0 xsb - xsb00.10000000000000009-mealyzeroised Model1.mealy8Apply a model1 relationship using a single decay factor.:set -XOverloadedLabelsimport Optics.Core9fold (depModel1 0.01 (zeroModel1 & #betaMa2X .~ 0.1)) xs0-0.4591515493154126/mealy/onlineL1' takes a function and turns it into a  where the step is an incremental update of an (isomorphic) median statistic.0mealy.onlineL1 takes a function and turns it into a  where the step is an incremental update of an (isomorphic) median statistic.1mealymoving median > L.fold (maL1 inc d r) [1..n] 93.928223127421082mealymoving absolute deviation4mealy av mempty == nan+mealy/initial statistical values, delay equals length2  !"#$%&'()*+,-./0122 !"#$%&'()*+, -.0/12None,/#OCmealy,Mealy quantiles based on the tdigest libraryDmealyMealy median using IThe tdigest algorithm works best at extremes and can be unreliable in the centre.EmealyA 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.CDEDCENone /?%Gmealy6rvs 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.0126527176272948Hmealy?@ABCDEFGHIJKLMNO"mealy-0.2.0-99dKz0RHlu23gKHb5QG7jmData.Mealy.Simulate Data.MealyData.Mealy.Quantiles Control.FoldlFold*mwc-random-0.15.0.2-DZJl4yt1XcA3JJ72fU5kYvSystem.Random.MWCcreateMedianer$sel:medAbsSum:Medianer$sel:medCount:Medianer$sel:medianEst:MedianerModel1$sel:alphaX:Model1$sel:alphaS:Model1$sel:betaMa2X:Model1$sel:betaMa2S:Model1$sel:betaStd2X:Model1$sel:betaStd2S:Model1Averager$sel:sumCount:AveragerMealy $sel:l1:MealyAMfoldscanavav_onlinemaabsmasqmastdcov corrGausscorrbeta1alpha1reg1betaalpharegasumaconstdelay1delaydepState zeroModel1 depModel1 onlineL1'onlineL1maL1absmaL1$fExceptionMealyError$fMonoidAverager$fSemigroupAverager$fExceptionMatrixException $fEqModel1 $fShowModel1$fGenericModel1$fShowMatrixException$fFunctorRegressionState $fEqAverager$fShowAverager$fProfunctorMealy$fCategoryTYPEMealy$fFunctorMealy$fApplicativeMealy$fShowMealyError quantilesmediandigitize$fShowOnlineTDigestrvsrvsp&tdigest-0.2.1.1-4EWMVLvpGPNB2XkFO85oFeData.TDigest.Tree.Internaltdigest