简体   繁体   中英

Multivariate rolling correlation in pandas

Lets say I have 3 variables A, B and C for a long period of time. I can get a correlation matrix in pandas for a window of first 3 days simply by using .corr()

            A       B       C
23/2/2017   21.93   4.48    13.27
24/2/2017   2.65    10.18   11.95
25/2/2017   8.74    10.67   6.30
26/2/2017   6.88    10.26   8.40
27/2/2017   12.20   9.56    12.82
28/2/2017   8.12    9.54    2.67


    A      B        C
A   1       
B   -0.93   1   
C   0.38    -0.70   1

This matrix can also be presented in table form:

           (A,B)    (B,C)   (C,A)
23/2/2017   NaN      NaN    NaN
24/2/2017   NaN      NaN    NaN
25/2/2017   -0.93   -0.70   0.38

What i need is a rolling window of 3 days with pairwise correlations populated like the table form above. I understand there's the pd.corring_corr(pairwise=True) function, just haven't got a clue how to obtain in a table format, with all possible combinations as columns.

Appreciate if anyone can help. Thanks!

You can probably clean this up if you have a lot of columns, but you get the idea:

df2 = pd.concat([pd.rolling_corr(df['A'], df['B'], 3), pd.rolling_corr(df['B'], df['C'], 3), pd.rolling_corr(df['A'], df['C'], 3)], axis=1)
df2.columns=['(A,B)', '(B,C)', '(A,C)']

You can probably use pairwise = True and avoid the concat but for your application I think the above works nicely.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM