# Exponential weighted moving skewness/kurtosis

There are well-known on-line formulas for computing exponentially weighted moving averages and standard deviations of a process $(x_n)_{n=0,1,2,\dots}$. For the mean,

$\mu_n = (1-\alpha) \mu_{n-1} + \alpha x_n$

and for the variance

$\sigma_n^2 = (1-\alpha) \sigma_{n-1}^2 + \alpha(x_n - \mu_{n-1})(x_n - \mu_n)$

from which you can compute the standard deviation.

Are there similar formulas for on-line computation of exponential weighted third- and fourth-central moments? My intuition is that they should take the form

$M_{3,n} = (1-\alpha) M_{3,n-1} + \alpha f(x_n,\mu_n,\mu_{n-1},S_n,S_{n-1})$

and

$M_{4,n} = (1-\alpha) M_{4,n-1} + \alpha f(x_n,\mu_n,\mu_{n-1},S_n,S_{n-1},M_{3,n},M_{3,n-1})$

from which you could compute the skewness $\gamma_n = M_{3,n} / \sigma_n^3$ and the kurtosis $k_n = M_{4,n}/\sigma_n^4$ but I've not been able to find simple, closed-form expression for the functions $f$ and $g$.

Edit: Some more information. The updating formula for moving variance is a special case of the formula for the exponential weighted moving covariance, which can be computed via

$C_n(x,y) = (1-\alpha) C_{n-1}(x,y) + \alpha (x_n - \bar{x}_n) (y_n - \bar{y}_{n-1})$

where $\bar{x}_n$ and $\bar{y}_n$ are the exponential moving means of $x$ and $y$. The asymmetry between $x$ and $y$ is illusory, and disappears when you notice that $y-\bar{y}_n = (1-\alpha) (y-\bar{y}_{n-1})$.

Formulas like this can be computed by writing the central moment as an expectation $E_n(\cdot)$, where weights in the expectation are understood to be exponential, and using the fact that for any function $f(x)$ we have

$E_n(f(x)) = \alpha f(x_n) + (1-\alpha) E_{n-1}(f(x))$

It's easy to derive the updating formulas for the mean and variance using this relation, but it's proving to be more tricky for the third and fourth central moments.

The formulas are straightforward but they are not as simple as intimated in the question.

Let $Y$ be the previous EWMA and let $X = x_n$, which is presumed independent of $Y$. By definition, the new weighted average is $Z = \alpha X + (1 - \alpha)Y$ for a constant value $\alpha$. For notational convenience, set $\beta = 1-\alpha$. Let $F$ denote the CDF of a random variable and $\phi$ denote its moment generating function, so that

$$\phi_X(t) = \mathbb{E}_F[\exp(t X)] = \int_\mathbb{R}{\exp(t x) dF_X(x)}.$$

With Kendall and Stuart, let $\mu_k^{'}(Z)$ denote the non-central moment of order $k$ for the random variable $Z$; that is, $\mu_k^{'}(Z) = \mathbb{E}[Z^k]$. The skewness and kurtosis are expressible in terms of the $\mu_k^{'}$ for $k = 1,2,3,4$; for example, the skewness is defined as $\mu_3 / \mu_2^{3/2}$ where

$$\mu_3 = \mu_3^{'} - 3 \mu_2^{'}\mu_1^{'} + 2{\mu_1^{'}}^3 \text{ and }\mu_2 = \mu_2^{'} - {\mu_1^{'}}^2$$

are the third and second central moments, respectively.

By standard elementary results,

\eqalign{ &1 + \mu_1^{'}(Z) t + \frac{1}{2!} \mu_2^{'}(Z) t^2 + \frac{1}{3!} \mu_3^{'}(Z) t^3 + \frac{1}{4!} \mu_4^{'}(Z) t^4 +O(t^5) \cr &= \phi_Z(t) \cr &= \phi_{\alpha X}(t) \phi_{\beta Y}(t) \cr &= \phi_X(\alpha t) \phi_Y(\beta t) \cr &= (1 + \mu_1^{'}(X) \alpha t + \frac{1}{2!} \mu_2^{'}(X) \alpha^2 t^2 + \cdots) (1 + \mu_1^{'}(Y) \beta t + \frac{1}{2!} \mu_2^{'}(Y) \beta^2 t^2 + \cdots). }

To obtain the desired non-central moments, multiply the latter power series through fourth order in $t$ and equate the result term-by-term with the terms in $\phi_Z(t)$.

• I am having some formula visualization problem, possibly whenever a ' is used, with both IE and Firefox, would you please care checking? Thanks! – Quartz Oct 17 '13 at 13:25
• @Quartz Thanks for the heads up. This used to display properly, so evidently there has been some change in the processing of the $\TeX$ markup. I found a workaround by enclosing all single quotes within braces. (This change has probably broken a few dozen posts on this site.) – whuber Oct 17 '13 at 13:30

I think that the following updating formula works for the third moment, although I'd be glad to have someone check it:

$M_{3,n} = (1-\alpha)M_{3,n-1} + \alpha \Big[ x_n(x_n-\mu_n)(x_n-2\mu_n) - x_n\mu_{n-1}(\mu_{n-1}-2\mu_n) - \dots$ $\dots - \mu_{n-1}(\mu_n-\mu_{n-1})^2 - 3(x_n-\mu_n) \sigma_{n-1}^2 \Big]$

Updating formula for the kurtosis still open...

• Why the ... in the above formula? – Chris Sep 11 '16 at 18:55
• Line continuation. – Chris Taylor Sep 12 '16 at 7:25
• Did your equation prove to be correct? I asked a similar question in R. stats.stackexchange.com/q/234460/70282 – Chris Sep 12 '16 at 13:03
• Did you account for the division by N in the third moment? Skewness is the ratio of the 3rd moment and the standard deviation^3 like so: Skew = m3 / sqrt(variance)^3 The third moment is defined as: m3 = sum( (x-mean)^3 )/n – Chris Sep 12 '16 at 13:05