# 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 for a given company, the other is the ratio of positive comments about the company on social networks. How can I statistically verify that they are correlated? Assume they are both sampled weekly or daily. The challenge lies in the fact that there might be a time difference between each other. In the context of my example above, the positive evaluation from consumer might be later reflected on the stock price.

There are two things I want to investigate in this problem:

1. Are the two time series 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? Mar 2, 2012 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. Mar 2, 2012 at 16:43
• Can coherence be calculated for each block, when the signals are divided into multiple overlapping blocks of equal length ? I tried it and I keep getting coherence of 1 for all frequencies in every block. Oct 21, 2020 at 7:25

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. Mar 2, 2012 at 19:35
• I can't answer that question, you might want to open a new thread for specific details. Mar 2, 2012 at 21:27