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

后端 未结 3 1227
囚心锁ツ
囚心锁ツ 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:21

    To build up on Andre's answer - if you only care about (lagged) correlation to the target, but want to test various lags (e.g. to see which lag gives the highest correlations), you can do something like this:

    lagged_correlation = pd.DataFrame.from_dict(
        {x: [df[target].corr(df[x].shift(-t)) for t in range(max_lag)] for x in df.columns})
    

    This way, each row corresponds to a different lag value, and each column corresponds to a different variable (one of them is the target itself, giving the autocorrelation).

提交回复
热议问题