Suppose to have a sequence $X$ of $m$ samples and for each $i^{th}$ sample you want to calculate a local mean $\mu_{X}(i)$ and a local variance $\sigma^2_{X}(i)$ estimation over $n \ll m$ samples of $X$.
Excluding the "boundary" samples of $X$, one can incrementally calculate the local mean estimation as follows:
\begin{equation} S(i+1) = S(i) + X(i+1+\frac{n}{2}) - X(i-\frac{n}{2}) \\ \mu_{X}(i+1) = \frac{1}{n} S(i+1) \end{equation}
where we suppose to have the previous value of $S(i)$ which is
\begin{equation} S(i) = \sum_{k=i-n/2}^{i+n/2}{X(k)} \end{equation}
and therefore without needing to compute the whole $S(i)$ for each $i$.
I'm looking for a similar "fast" way to calculate a local variance $\sigma^2_{X}(i)$ estimation. Notice I could accept an approximate extimation of this measure too, if precise enough. Can you help me? Thank you very much in advance for your attention.
EDIT: now my doubt is the following:
the local variance at $(i+1)^{th}$ sample should be:
\begin{align} \sigma^2_{X}(i+1) &= \frac{1}{n}\sum_{k=i+1-n/2}^{i+1+n/2}{(X(k) - \mu_{X}(i+1))^2} \\ &= \frac{1}{n}\sum_{k=i+1-n/2}^{i+1+n/2}{ \left((X(k))^2 + (\mu_{X}(i+1))^2 - 2\mu_{X}(i+1)X(k) \right)} \\ &= \frac{1}{n}\sum_{k=i+1-n/2}^{i+1+n/2}{(X(k))^2} + \frac{1}{n}\sum_{k=i+1-n/2}^{i+1+n/2}{(\mu_{X}(i+1))^2} - \frac{1}{n}\sum_{k=i+1-n/2}^{i+1+n/2}{2\mu_{X}(i+1)X(k)} \\ &= \mu_{X^2}(i+1) + (\mu_{X}(i+1))^2 - 2\mu_{X}(i+1)\frac{1}{n}\sum_{k=i+1-n/2}^{i+1+n/2}{X(k)} \\ &= \mu_{X^2}(i+1) + (\mu_{X}(i+1))^2 - 2(\mu_{X}(i+1))^2 \\ &= \mu_{X^2}(i+1) - (\mu_{X}(i+1))^2 \end{align}
where $\mu_{X^2}(i+1)$ can be calculated by doing \begin{equation} \mu_{X^2}(i+1) = \frac{1}{n}K(i+1) \end{equation} as proposed by Kartik Audhkhasi, and $(\mu_{X}(i+1))^2$ can be computed by using the already computed $\mu_{X}(i+1)$. Therefore, given \begin{equation} \mu_{X^2}(i+1) = \frac{1}{n}K(i+1) \quad,\quad \mu_{X}(i+1) = \frac{1}{n}S(i+1) \end{equation} the final result is: \begin{align} \sigma^2_{X}(i+1) &= \mu_{X^2}(i+1) - (\mu_{X}(i+1))^2 \\ &= \frac{1}{n}K(i+1) - \frac{1}{n^2}(S(i+1))^2 \\ &= \frac{1}{n}\left(K(i+1) - \frac{1}{n}S(i+1)S(i+1)\right) \end{align}
Could this method be correct and what's the difference with the one proposed below by Kartik Audhkhasi. In other words, why it has been proposed an update formula which requires to store the previous mean value too for variance calculation?
Thank you again for your attention.