When examining the relationship between two time series, it is often useful to see how they move in tandem. One powerful tool for this is the cross-covariance function defined as
Since we are dealing with two distinct time series, it is generally the case that γx,y(h)=γx,y(−h) for h=0. This is due to the fact that γx,y(h) represents the covariance between y and x at h lags in the future, whereas γx,y(−h) represents the covariance between y and x at h lags in the past. Put differently, there is no reason to assume the covariance between employment and the following month’s inflation is the same as the covariance between employment and the preceding month’s inflation.
Cross-Covariance and Cross-Correlation in statsmodels¶
In statsmodels, the cross-covariance and cross-correlation are accessed by statsmodels.tsa.stattools.ccovf and statsmodels.tsa.stattools.ccf, respectively. The only arguments we need to worry about at this point are x and y, the two time series. Note that these functions only calculate positive h values, to get both positive and negative values (i.e. both x and y leading) you must run both ccf(x,y) and ccf(y,x).
Figure 1:Notional plot of cross-correlation function for two arbitrary time series consisting of phase shifted sine curves with noise. Note that in statsmodels it is necessary to calculate positive and negative h values separately.