h$'3%      !"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHNone #'(-/58<=>?"a#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 (convergent tozero) applied at each step. online id id == avmealyA moving average using a decay rate of r. r=1 represents the simple average, and r=0 represents the latest value.fold (ma 0) (fromList [1..100])100.0fold (ma 1) (fromList [1..100])50.5fold (ma 0.99) xs0-4.292501077490672e-2A change in the underlying mean at n=10000 in the chart below highlights the trade-off between stability of the statistic and response to non-stationarity. other/ex-ma.svgma chartmealyabsolute averagefold (absma 1) xs00.7894201075535578mealyaverage 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) xs00.9923523681261158 other/ex-std.svg std chartmealyThe covariance of a tuple given an underlying central tendency fold.fold (cov (ma 1)) xsp0.8011368250045314 mealy/correlation of a tuple, specialised to Guassianfold (corrGauss 1) xsp0.8020637696465039!mealy/a generalised version of correlation of a tuplefold (corr (ma 1) (std 1)) xsp0.8020637696465039 "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.9953875263096014#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.1880996822796197e-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.1880996822796197e-2,0.49538752630956845)%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 - xsb09.999999999999976e-2This simple model of relationship between a series and it's historical average shows how fragile the evidence can be. other/ex-madep.svgmadepIn unravelling the drivers of this result, the standard deviation of a moving average scan seems well behaved for r > 0.01, but increases substantively for values less than this. This result seems to occur for wide beta values. For high r, the standard deviation of the moving average seems to be proprtional to r**0.5, and equal to around (0.5*r)**0.5. 'fold (std 1) (scan (ma (1 - 0.01)) xs0) other/ex-stdma.svgstdma.mealy8Apply a model1 relationship using a single decay factor.:set -XOverloadedLabels9fold (depModel1 0.01 (zeroModel1 & #betaMa2X .~ 0.1)) xs0-0.472285371232182061mealy/onlineL1' takes a function and turns it into a  where the step is an incremental update of an (isomorphic) median statistic.2mealy.onlineL1 takes a function and turns it into a  where the step is an incremental update of an (isomorphic) median statistic.3mealymoving median > L.fold (maL1 inc d r) [1..n] 93.928223127421084mealymoving absolute deviation5mealy av mempty == nan+mealy/initial statistical values, delay equals length4  !"#$%&'()*+,-./012344 !"#$%&'()*+, -./02134None ,/#*Cmealy/decaying quantiles based on the tdigest libraryCDEDCENone /?%Gmealyrvs creates a list of standard normal random variates. >>> t <- rvs gen n >>> t eq` [-0.8077385934202513,-1.3423948150518445,-0.4900206084002882] Truers <- rvs gen 10000fold (ma 1) rs-1.735737734197327e-3fold (std 1) rs0.9923615647768976HmealyrvsPair generates a list of correlated random variate tuples | >>> t <- rvsp gen 3 0.8 >>> t eq'p [(-0.8077385934202513,-1.4591410449385904),(-1.3423948150518445,-0.6046212701237168),(-0.4900206084002882,0.923007518547542)] Truersp <- rvsp gen 10000 0.8fold (corr (ma 1) (std 1)) rsp0.7933213647252008GHGH        !"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKL"mealy-0.0.2-Cef9UNGjcx3IJ9aLPDdzV3Data.Mealy.Simulate Data.MealyData.Mealy.Quantiles Control.FoldlFold)mwc-random-0.15.0.1-A8cXf5tU8qSHwPsadA6McSystem.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 depModel1foldBmaB onlineL1'onlineL1maL1absmaL1$fMonoidAverager$fSemigroupAverager$fExceptionMatrixException $fEqModel1 $fShowModel1$fGenericModel1$fShowMatrixException$fFunctorRegressionState $fEqAverager$fShowAverager$fProfunctorMealy$fCategoryTYPEMealy$fFunctorMealy$fApplicativeMealy quantilesmediandigitize$fShowOnlineTDigestrvsrvsp