Running n-lag correlation matrix? Working in python, I get data at regular interval. The data contains some features, $X_1,\dots,X_p$. I am trying to get an online algorithm to build correlation matrixes. The naive approach of keeping track of the last $n$ instances of $X$ is working but rather slow.
I have found some way to get variance and covariance of the whole sample in one or two  passes, but it doesn't seems to be what I am looking for. I am looking for a way to update some current calculation with mostly the last value (and removing the oldest values) in a running manner. Example: for a running sum, you can keep track of a cumulative sum, only adding / removing one value at each time step.
Any idea how to perform that? any idea how to perform that in a vectorized way so as to get matrixes as outputs?

If I understand correctly, you have a sequence of observations $\{(X^t_1,\dots, X^t_p)\}_t$ and you want to compute the matrix
$$Corr(t)_{ij}=\sum_{t'=t-n+1}^t (X^{t'}_i-\mu_i^{t})(X^{t'}_j-\mu_j^{t})\left(\sum_{t'=t-n+1}^t (X_i^{t'}-\mu_i^t)^2\sum_{t'=t-n+1}^t (X^{t'}_j-\mu_j^t)^2\right)^{-1/2}$$
where $\mu_i^t=n^{-1}\sum_{t'=t-n+1}^t X^{t'}_i$ is the n-lag mean. Like I mentioned above, you can write this in terms of the lag-n covariance matrix in the usual way, so it is enough to compute the covariance $Cov(t)_{ij}=n^{-1}\sum_{t'=t-n+1}^t (X^{t'}_i-\mu_i^{t})(X^{t'}_j-\mu_j^{t})$, from which you can read off the correlations.
I think the easiest way to do it is to keep track of both the mean vector $\mu^t_i$ as well as the "uncentered covariances" $CovU(t)_{ij}=n^{-1}\sum_{t'=t-n+1}^t X^{t'}_iX^{t'}_j$. Each of these can be updated in responses to a new observation simply by adding and removing one value at a time.
Then once you have updated these values, you can read off the covariance by the formula $Cov(t)_{ij}=CovU(t)_{ij}-\mu^t_i\mu^t_j$ and likewise the correlations by $Corr(t)_{ij}=Cov(t)_{ij}/\sqrt{Cov(t)_{ii}Cov(t)_{jj}}$.
Also, depending on what you ultimately want to do, it may be worth trying an exponentially weighted moving average, rather than a hard cutoff. This has the advantage that you only have to store the previous estimate (rather than the previous estimate, as well as the previous n observations), and the update equation is considerably simpler.
