!?Q      !"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPNone"&',.47;<=>?DHPSUVXbdeg-#onlineoA rough Median. The average absolute value of the stat is used to callibrate estimate drift towards the medianonline>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} online}Most common statistics are averages, which are some sort of aggregation of values (sum) and some sort of sample size (count).onlineA  is a triple of functions (c -> b) extract" Convert state to the output type.(c -> a -> c) step0 Update state given prior state and (new) input. (a -> c) inject& Convert an input into the state type.%The type is a newtype wrapper around Q in .inject% is necessary to kick off state on a  or U, rather than a state existing prior to the fold or scan (this is a Moore machine or R in ). 1scan (M e s i) (x : xs) = e <$> scanl' s (i x) xsonlinePattern for an .  A sum countonlinePattern for a . M extract step injectonlineFold a list through a . cosieve == foldonlineRun a list through a + and return a list of values for every step length (scan _ xs) == length xsonlineextract the average from an av gives NaN on zero divideonline)substitute a default value on zero-divide av_ (Averager (0,0)) x == xonline online f g is a l where f is a transformation of the data and g is a decay function (convergent tozero) applied at each step. online id id == avonlineqA 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 chartonlineabsolute averagefold (absma 1) xs00.7894201075535578onlineaverage square )fold (ma r) . fmap (**2) == fold (sqma r)onlinestandard deviationLThe construction of standard deviation, using the Applicative instance of a : 9(\s ss -> sqrt (ss - s ** (one+one))) <$> ma r <*> sqma rJThe average deviation of the numbers 1..1000 is about 1 / sqrt 12 * 1000 Phttps://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 chartonlineDThe covariance of a tuple given an underlying central tendency fold.fold (cov (ma 1)) xsp0.8011368250045314 online/correlation of a tuple, specialised to Guassianfold (corrGauss 1) xsp0.8020637696465039!online/a generalised version of correlation of a tuplefold (corr (ma 1) (std 1)) xsp0.8020637696465039 "corr (ma r) (std r) == corrGauss r"onlineThe 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#onlineThe alpha in a simple linear regression of an (independent variable, single dependent variable) tuple given an underlying central tendency fold.9 \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} Kfold (alpha1 (ma 1)) $ zipWith (\x y -> ((3+y), x + 0.5 * (3 + y))) xs0 xs11.1880996822796197e-2$onlineThe (alpha, beta) tuple in a simple linear regression of an (independent variable, single dependent variable) tuple given an underlying central tendency fold.Ifold (reg1 (ma 1)) $ zipWith (\x y -> ((3+y), x + 0.5 * (3 + y))) xs0 xs1+(1.1880996822796197e-2,0.49538752630956845)%onlinemultiple regression \begin{align} {\hat {{\mathbf {B}}}}=({\mathbf {X}}^{{{\rm {T}}}}{\mathbf {X}})^{{ -1}}{\mathbf {X}}^{{{\rm {T}}}}{\mathbf {Y}} \end{align} G \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&onlinealpha in a multiple regression'onlinemultiple 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)(onlineaccumulated sum)onlineconstant Mealy*onlinedelay input values by 1+onlinedelays 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,online#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.zFor time series analytics, a major preoccupation is estimation of the current moments given what has happened in the past.IID:a \begin{align} x_{t+1} & = alpha_t^x + s_{t+1}\\ s_{t+1} & = alpha_t^s * N(0,1) \end{align} AExample: 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-2uThis simple model of relationship between a series and it's historical average shows how fragile the evidence can be. other/ex-madep.svgmadepbIn 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.online8Apply a model1 relationship using a single decay factor.:set -XOverloadedLabels9fold (depModel1 0.01 (zeroModel1 & #betaMa2X .~ 0.1)) xs0-0.472285371232182062online/onlineL1' takes a function and turns it into a M where the step is an incremental update of an (isomorphic) median statistic.3online.onlineL1 takes a function and turns it into a M where the step is an incremental update of an (isomorphic) median statistic.4online@moving median > L.fold (maL1 inc d r) [1..n] 93.928223127421085onlinemoving absolute deviation6online av mempty == nan+online/initial statistical values, delay equals length5  !"#$%&'()*+,-./0123455 !"#$%&'()*+, -./013245None.DbQGonlinea raw non-online tdigest foldHonlinenon-online versionIonlinenon-online versionJonline/decaying quantiles based on the tdigest libraryMonline/decaying histogram based on the tdigest library BCDEFGHIJKLM GHIBCDEFJKLMNone .7>DSX_b#OonlineRrvs creates a list of standard normal random variates. >>> t <- rvs gen n >>> t eq'D [-0.8077385934202513,-1.3423948150518445,-0.4900206084002882] Truers <- rvs gen 10000fold (ma 1) rs-1.735737734197327e-3fold (std 1) rs0.9923615647768976Ponline`rvsPair 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.7933213647252008OPOPS       !"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGGHIJKLMNOPQRSTUVWUXYZ#online-0.6.0-HLmMvMxp7IYLqRFxok4q8s Data.Simulate Data.MealyData.QuantilesDataFold Control.Foldl)mwc-random-0.14.0.0-LOavn6hXcKvRT4YhOjLwASystem.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 fromFoldlfoldBmaB onlineL1'onlineL1maL1absmaL1$fMonoidAverager$fSemigroupAverager$fProfunctorMealy$fCategoryMealy$fFunctorMealy$fApplicativeMealy $fEqAverager$fShowAverager$fFunctorRegressionState $fEqModel1 $fShowModel1$fGenericModel1 OnlineTDigesttdtdNtdRatetDigesttDigestQuantiles tDigestHistonlineQuantilesmedianonlineDigitizeonlineDigestHist$fShowOnlineTDigestrvsrvsp"folds-0.7.5-HgU0TKdfH9p8t2HMoPolls Data.Fold.L1L1 Data.Fold.M1M1