Risk Managed

Thoughts on finance and risk management

Online updating of a covariance matrix

The sample covariance matrix $\Sigma$ of a sample $\mathbf{X}_{i,j}$, $i = 1,\ldots,n$, $j=1,\ldots,p$ is given by $\Sigma_n = \frac{1}{n} (X-\bar{X})^T(X-\bar{X})$.

In some applications this covariance matrix must be kept updated as each observation comes in, which is called online updating. The formulas below provide an efficient way of calculating $\Sigma_{n+1}$, the covariance matrix of the sample of size $n + 1$, from the previous covariance matrix and the new observation $X_{n+1}$.

$$\Sigma_{n+1} = \frac{n}{n+1} \Sigma_n + \frac{1}{n} \left(X_{n+1} - \bar{X}_{n+1}\right)^T\left(X_{n+1} - \bar{X}_{n+1}\right)$$

$$\bar{X}_{n+1} = \bar{X}_n + \frac{1}{n+1} \left(X_n - \bar{X}_n\right)$$