# Correlation between two time series

What is the easiest way / method to compute the correlation between two time series that are exactly the same size? I thought of multiplying $(x[t]-\mu_x)$ and $(y[t] - \mu_y)$, and adding up the multiplication. So if this single number was positive, can we say these two series are correlated? I can think of some examples however where a linearly another exponentially growing time series would have no relation to eachother, but the computation above would report they were correlated.

Any thoughts?

Macro's point is correct the proper way to compare for relationships between time series is by the cross-correlation function (assuming stationarity). Having the same length is not essential. The cross correlation at lag 0 just computes a correlation like doing the Pearson correlation estimate pairing the data at the identical time points. If they do have the same length as you are assuming, you will have exact T pairs where T is the number of time points for each series. Lag 1 cross correlation matches time t from series 1 with time t+1 in series 2. Note that here even though the series are the same length you only have T-2 pair as one point in the first series has no match in the second and one other point in the second series will not have a match from the first. Given these two series you can estimate the cross-correlation at several lags . If any of the cross correlations is statistically significantly different from 0 it will indicate a correlation between the two series.

• Hi Michael, is it possible to quantify "significanly different" -- can I use 1 or 2 standard deviation away from zero as significant? – NetSmoothMF May 24 '12 at 16:36
• @user423805 I have changed it to read statistically significantly different from 0. Formally that means that you test the null hypothesis that the correlation is zero vs the alternative that it is not 0. Then compute the two-sided p-value for the test statistic. Generally statistical significance mean p-value <=0.05. Sometimes other values are used to define statistical significance (0.01 for example). Most time series software packages that include mutiple time series can do these tests for you. Our friend IrishStat can speak to this regarding Autobox. – Michael R. Chernick May 24 '12 at 17:29
• are there cases in which cross correlation at lag zero and pearson differ? – Bakaburg Apr 9 '15 at 16:42

You might want to look at a similar question and my answer Correlating volume timeseries which suggests that you can compute cross-correlations BUT testing them is a horse of a different color ( an equine of a different hue ) due to autoregressive or deterministic structure within either series.

• if I understand correctly, in that answer you are saying crosscorrelation between timeseries is useless. – NetSmoothMF May 24 '12 at 16:40
• user423805 MAY be useless unless the data is suitably pre-filtered to obtain I.I.D. This speaks directly to the OP's real concerns about spurious conclusions like "storks bringing babies J. Neyman 1938 en.wikipedia.org/wiki/… and amstat.org/about/statisticiansinhistory/…" etc ( I can think of some examples however where a linearly another exponentially growing time series would have no relation to eachother, but the computation above would report they were correlated. ) – IrishStat May 24 '12 at 17:12
• I think the point is that the series need to be stationary for crosscorrelations to make sense. If filtering is necessary it is to mske the series stationary (like differencing or seasonal differencing). But to call it useless is wrong. – Michael R. Chernick May 25 '12 at 3:05
• @Michael I said MAY be useless. – IrishStat May 25 '12 at 11:05
• @IrishStat It was a good comment and took me back to my training in the 1970s. At that time I was learning about time series/forecasting methods for my civilian work in the US Army. We were using exponential smoothing as a way to forecast based on historical data over subjective estimates that were being used at the supply depots. Someone made the great suggestion to me to look at the more general ARIMA models and the 1970 text by Box and Jenkins and so began my interest in time series that became part of my career. – Michael R. Chernick May 25 '12 at 13:26

There is some interesting stuff here

https://stackoverflow.com/questions/3949226/calculating-pearson-correlation-and-significance-in-python

This was actually what I needed. Simple to implement and explain.

• -1 From what I can gather these answers are only concerned with the standard Pearson product-moment correlation. When applied to two time series, the standard Pearson correlation gives nonsensical results! If you follow these suggestions, all you do is produce statistical artefacts. See e.g. math.mcgill.ca/dstephens/OldCourses/204-2007/Handouts/… – Momo Jul 12 '16 at 12:29