Cross-correlation (time-lag-correlation) with pandas?

后端 未结 3 1235
囚心锁ツ
囚心锁ツ 2021-01-30 01:49

I have various time series, that I want to correlate - or rather, cross-correlate - with each other, to find out at which time lag the correlation factor is the greatest.

<
3条回答
  •  有刺的猬
    2021-01-30 02:30

    As far as I can tell, there isn't a built in method that does exactly what you are asking. But if you look at the source code for the pandas Series method autocorr, you can see you've got the right idea:

    def autocorr(self, lag=1):
        """
        Lag-N autocorrelation
    
        Parameters
        ----------
        lag : int, default 1
            Number of lags to apply before performing autocorrelation.
    
        Returns
        -------
        autocorr : float
        """
        return self.corr(self.shift(lag))
    

    So a simple timelagged cross covariance function would be

    def crosscorr(datax, datay, lag=0):
        """ Lag-N cross correlation. 
        Parameters
        ----------
        lag : int, default 0
        datax, datay : pandas.Series objects of equal length
    
        Returns
        ----------
        crosscorr : float
        """
        return datax.corr(datay.shift(lag))
    

    Then if you wanted to look at the cross correlations at each month, you could do

     xcov_monthly = [crosscorr(datax, datay, lag=i) for i in range(12)]
    

提交回复
热议问题