Let's say I have two random variables $X$ and $Y$ which each represents the daily returns of two given stocks. I can easy calculate their (total) correlation by finding their covariance matrix $\Sigma[X, Y]$. However, I would like to graph their correlation throughout time; not just a single point. Thus, I would like to have a vector of their correlation like so: $C = \{ \textrm{corr}_0, \textrm{corr}_1, \cdots, \textrm{corr}_m \}$. Here is my attempt:

Let $X = \{x_1, x_2, x_3, x_4, \cdots, x_n\}$ and $Y=\{y_1, y_2, y_3, y_4, \cdots, y_n\}$ be random variables. Thus, we define its correlation throughout time (as in the comments, its name is rolling correlation) as

\begin{equation} \textrm{corr}[X, Y | k] := \frac{ \textrm{cov}[X, Y | k]}{\sigma_X \cdot \sigma_Y} := \frac{\displaystyle\sum_i^{i+k-1} (x_i-\bar{x})(y_i-\bar{y})}{(k) \cdot \sigma_X \cdot \sigma_Y}, i = \{1, 2, 3, \cdots n-k+1\} \end{equation}

Equivalently, I would separate both $X$ and $Y$ into $j$ subsets such that each satisfy $|X_j|=|Y_j|=k$ and then calculate their correlation $\textrm{corr}[X_j, Y_j]$ and finally include it into the vector $C$.

Is this the best way to find correlation throughout time? Is it even correct?


  • $\begingroup$ Note sure I understood correctly but why not just calculate correlation on a rolling window? $\endgroup$
    – Mayeul sgc
    Commented Dec 17, 2021 at 2:01
  • 1
    $\begingroup$ Shouldn't the denominator contain only $K$ instead of $i+k$? $\endgroup$ Commented Dec 17, 2021 at 10:12
  • 1
    $\begingroup$ I believe the term you are looking for would be rolling correlation $\endgroup$ Commented Dec 17, 2021 at 17:11
  • $\begingroup$ Yes, @Kermittfrog. I'll correct that. And yes, I searched and that's the name, rubikscube09. Thank you guys! $\endgroup$
    – Mr. N
    Commented Dec 17, 2021 at 19:46

2 Answers 2


Your question is indeed on the stationarity of correlations between your two instruments $X$ and $Y$.

To make it clear from a theoretical viewpoint, let's consider their 2D joint distribution $(X,Y)$:

  • first you need to decide if you want to consider that they have autocorrelations or if they are iid, in short: is $(X,Y)$ a stochastic process or a pair of random variables?
  • then do you assume that the underlying distribution of $(X,Y)$ is the same at any point in time (strong stationarity) or simply that few statistics (like volatility, correlation and average) are the same at any point in time (weak stationarity)?
  • note that for Gaussians, weak stationarity imply string stationarity since (vol,cor,mean) is a sufficient statistic.
  • of course you can assume that there is no stationarity of any kind there, but all would be more complicated.

To make it simple, assume that $(X,Y)$ is a weakly stationary random variable, then the only difference between the empirical correlation computed on one time window $[t_1,t_1+w_1]$ and on another one $[t_2,t_2+w_2]$ is the sampling noise. It means that when $w_1$ and $w_2$ jointly go towards infinity, Thanks to the Central Limit Theorem: you will obtain the same correlations and the convergence will happen in $1/\sqrt{\min(w_1,w_2)}$ (for uniformly sampled data).

If now you do not believe in the stationarity of the correlations (note that you can have non stationarity of the marginal volatilities, convince yourself that you nevertheless captured it, for instance thanks to 2 GARCHs, such that the correlations are stationary now), then the empirical correlations computed on $[t_1,t_1+w_1]$ and $[t_2,t_2+w_2]$ will not be the same for two reasons

  1. the estimation noise (like in the stationary case)
  2. the shift in the covariances (that you could name a covariate shift and read this nice book: Machine Learning in Non-Stationary Environments, by Sugiyama and Kawanabe)

And you face a dilemma: the larger the window $w_1$ and $w_2$

  • the less estimation noise
  • but the more you mix estimates from different underlying correlations.

This is subtle and there nothing you can really do without empirical investigation:

  1. how does the times series of empirical correlations changes with time?
  2. what bootstrap can tell you about the estimation noise? (be careful, if you are witnessing a stochastic process and not iid random variables, bootstrapping is subtle, have a look at Lectures On Some Aspects Of The Bootstrap by Evarist Giné).
  • $\begingroup$ Thank you sir for your fully detailed answer. I wish I could understand and take more out of it. I'll try to improve my Statistics/Stochastic knowledge to fully get what you said. Thank again! $\endgroup$
    – Mr. N
    Commented Dec 17, 2021 at 19:49

Column A is the price of asset A

Column B is the price of asset B

C is the return of A

D is the return of B

E is CORREL(OFFSET($D,-t,0,t+1,1),OFFSET($E,-t,0,t+1,1))

F is something cool I want to correlate to my correlation to tell a good story

G is CORREL(OFFSET($E,-t,0,t+1,1),OFFSET($F,-t,0,t+1,1)), ie the correlation of correlation to something real.

Hit F9.

Where "t" is a lookback, visually optimised for the coolest-looking chart. The only constraints being 1d, 1w, 1m, 3m, 6m, 200d=9m etc. because deviations from the classic market demarcations of time would be an obvious "tell" for over-fitting.

Apologies for the lack of appropriate statistical notation. And, yes, I exaggerate a little. But the process above pretty much put my two children through private school.

The real answer to your question being is (a) this is really really simple to do, on a rolling basis in Excel; no notation required. And (b) there is no theoretically or empirically correct way to define the sample you use for the evolving calculation of your rolling over time. Unless you are lucky enough to have some intuitive economic theory why return should be price-dependent. But, generally speaking, this itself is considered a heresy against efficiency in the published world that uses the notation rather than Excel formulae plus F9 ;-)

If you want to get really cute, however, you can do it... Calculate your rolling correlations. And then calculate your autocorrelation of correlation. From which you can derive a half-life of correlation persistence... Except I think we both know this is the statistical equivalent of "hitting F9" ;-)

The honest answer is that 65d (ie 65 trading days in a calendar quarter), 200d (ie 9m, lots of things might be 6-12m so people use 9) or 12m; and 3y tend to serve as market benchmarks for "short-term", "tactical" and "medium-term" for these kinds of analyses. They're just the devil that analysts and clients know here.

hope this helps, DEM

ps the intuitive interpretation of evolving correlations over time is a whole different subject, one for another day I'm afeared. Assuming they are, of course, non-stationary (as are most instances in market data).


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.