简体   繁体   English

熊猫中的多元滚动相关性

[英]Multivariate rolling correlation in pandas

Lets say I have 3 variables A, B and C for a long period of time. 可以说我有3个变量A,B和C了很长一段时间。 I can get a correlation matrix in pandas for a window of first 3 days simply by using .corr() 我可以通过使用.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. 我需要的是3天的滚动窗口,其中有成对的相关性,就像上面的表格一样。 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. 我知道这里有pd.corring_corr(pairwise=True)函数,只是不知道如何以表格式获取所有可能的组合作为列。

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. 您可能可以使用pairwise = True并避免使用concat但是对于您的应用程序,我认为上面的方法很好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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