<s>[INST] How to find variance of k+1 elements if variance of k elements is known? I need to find the variance of k+1 elements given the variance of k elements. I can also store other features for k elements like mean ($\mu_n$) etc. So, given the below function's value,  
$$
\frac{1}{n}\sum\limits_{i=1}^n(a_i-\mu_{n})^2
$$
I need to find
$$
\frac{1}{n+1}\sum\limits_{i=1}^{n+1}(a_i-\mu_{n+1})^2
$$
where $\mu_{n}$ is the mean of k elements and $\mu_{n+1}$ is the new mean of k+1 elements.
 [/INST] $$\sigma_{n+1}^2 = \frac{1}{n+1}\sum_{k=1}^{n+1}((a_{k} - \mu_{n}) + (\mu_{n} - \mu_{n+1}))^2 $$
$$ = \frac{1}{n+1}\sum_{k=1}^{n+1}[(a_k-\mu_n)^2 - 2(a_k-\mu_n)(\mu_{n+1} - \mu_n) + (\mu_{n+1} - \mu_n)^2]$$
$$ = \frac{1}{n+1}[\sum_{k=1}^{n}[(a_k-\mu_n)^2] + (a_{n+1} - \mu_n)^2 - 2 (\mu_{n+1} - \mu_n)\sum_{k=1}^{n+1}[(a_k-\mu_n)] + (n+1)(\mu_{n+1}-\mu_n)^2]$$
$$ = \frac{n\sigma_n^2 + (a_{n+1} - \mu_n)^2}{n+1} + \frac{- 2 (\mu_{n+1} - \mu_n)}{n+1}((n+1)(\mu_{n+1}-\mu_n)) + (\mu_{n+1}-\mu_n)^2 $$
$$ = \frac{n\sigma_{n}^2 + (a_{n+1} - \mu_{n})^2}{n+1} - 2(\mu_{n+1}-\mu_n)^2 + (\mu_{n+1}-\mu_n)^2 $$
$$= \frac{n\sigma_{n}^2 + (a_{n+1} - \mu_{n})^2}{n+1} - (\mu_{n+1}-\mu_{n})^2$$
</s>