# How to correlate two time series, with possible time differences

The question is similar to How to correlate two time series with gaps and different time bases? but with regular sample frequency and identical time base between two time series.

Say I have two time series, one is the stock price of a given company, the other is the ratio of positive comment about the company on social networks. How can I statistically verify they are correlated? Assume they are both sampled weekly or daily. The challenge lies in the fact there might be time difference from each other. In the context of my example above, the positive evaluation from consumer might be later reflect on the stock price.

There are two things I want to investigate in this problem: 1) is the two time series are correlated? 2) what is the time window to maximize their correlation?

Apply a lag operator on one time series, with the other fixed, and calculate the coherence of the cross-spectrum achieved against each lag. Find the lag that gives you the maximum coherence and interpret it.

Coherence is computed at each frequency-and hence is a vector. Hence, a sum of a weighted coherence would be a good measure. You would typically want to weight the coherences at frequencies that have a high energy in the power spectral density. That way, you would be measuring the similarities at the frequencies that dominate the time series instead of weighting the coherence with a large weight, when the content of that frequency in the time series is negligible.

http://www.stat.rutgers.edu/home/rebecka/Stat565/lab5-2007.pdf is a good link to look at to get started and http://www.atmos.washington.edu/~dennis/552_Notes_6c.pdf is an excellent introduction to cross-spectral analysis.

• Any hints on how can I evaluate how strong is the coherence is? Is it possible to do t-test on the coherence of the cross-spectrum? – clwen Mar 2 '12 at 16:24
• Coherence is computed at each frequency-and hence is a vector. Hence, a sum of a weighted coherence would be a good measure. You would typically want to weight the coherences at frequencies that have a high energy in the power spectral density. That way, you would be measuring the similarities at the frequencies that dominate the time series instead of weighting the coherence with a large weight, when the content of that frequency in the time series is negligible. I added this in the answer above, after reading your comment. This is a good question. – hearse Mar 2 '12 at 16:43

The cross correlation function will give you the Pearson correlation for 2 time-series at different time lags. The R function is ccf().

For further study, a Granger causality test tries to determine a cause-effect relationship between the 2 correlated series by first removing the serial correlation in TS1 (the stock price series in this case).

• Thanks. How does the cross correlation compared to the coherence of the cross-spectrum? In terms of statistical meaning, complexity, etc. – clwen Mar 2 '12 at 19:35
• I can't answer that question, you might want to open a new thread for specific details. – Robert Kubrick Mar 2 '12 at 21:27