如何从使用pandas.ewm计算的相关矩阵中提取成对相关的时间序列?

How to extract time series of pairwise correlation from correlation matrix calculated using pandas.ewm?

我有资产 return 的时间序列数据。 Return 数据框索引是日期,列是资产名称。

             L/S HF        US     World
1995-02-28   0.030366  0.029288  0.014742
1995-03-31   0.008086  0.017165  0.027338
1995-04-28   0.013615  0.013851  0.018561
1995-05-31   0.020304  0.029865  0.016769
1995-06-30   0.035106  0.011546 -0.001471
...          ...       ...       ...
2021-02-26   0.045488  0.008619  0.005904

我计算了指数加权相关性。

corHist = returnTS.ewm(halflife = 36).corr()

print(corHist.info())

<class 'pandas.core.frame.DataFrame'>
MultiIndex: 939 entries, (1995-02-28 00:00:00, US) to (2021-02-26 00:00:00, L/S HF)
Data columns (total 3 columns):
US        936 non-null float64
World     936 non-null float64
L/S HF    936 non-null float64
dtypes: float64(3)
memory usage: 37.3+ KB
None
print(corHist)

                         US     World    L/S HF
1995-02-28 US           NaN       NaN       NaN
           World        NaN       NaN       NaN
           L/S HF       NaN       NaN       NaN
1995-03-31 US      1.000000 -1.000000  1.000000
           World  -1.000000  1.000000 -1.000000
...                     ...       ...       ...
2021-01-29 World   0.976792  1.000000  0.896372
           L/S HF  0.881601  0.896372  1.000000
2021-02-26 US      1.000000  0.976763  0.857102
           World   0.976763  1.000000  0.869567
           L/S HF  0.857102  0.869567  1.000000

我知道如何提取单个日期的相关矩阵。

corHist.xs('2021-02-26', level = 0, axis=0, drop_level=True)

              US     World    L/S HF
US      1.000000  0.976763  0.857102
World   0.976763  1.000000  0.869567
L/S HF  0.857102  0.869567  1.000000

我想要的是提取成对相关的时间序列,比如美国和世界之间或其他一些成对。我该怎么做?

这应该适用于 'US'/'World' 相关性(其他对的想法相同):

corHist.xs('US',level=1)['World']

生成(对于您的基本示例)系列:

1995-02-28         NaN
1995-03-31   -1.000000
1995-04-28   -0.484825
1995-05-31   -0.592066
1995-06-30    0.433440
2021-02-26    0.529957
Name: World, dtype: float64

此外,这

corHist.unstack(level=1)

为所有对生成按日期索引的 df:

            L/S HF                          US                               World
            L/S HF  US          World       L/S HF      US      World         L/S HF    US        World
1995-02-28  NaN     NaN         NaN         NaN         NaN     NaN           NaN       NaN           NaN
1995-03-31  1.0     1.000000    -1.000000   1.000000    1.0    -1.000000    -1.000000   -1.000000   1.0
1995-04-28  1.0     0.877267    -0.845137   0.877267    1.0     -0.484825   -0.845137   -0.484825   1.0
1995-05-31  1.0     0.783590    -0.853993   0.783590    1.0     -0.592066   -0.853993   -0.592066   1.0
1995-06-30  1.0    -0.146586    -0.928883   -0.146586   1.0     0.433440    -0.928883   0.433440    1.0
2021-02-26  1.0    -0.517059    -0.826607   -0.517059   1.0     0.529957    -0.826607   0.529957    1.0

所以你可以使用更对称的命令

corHist.unstack(level=1)[('US','World')]

获取与之前相同的系列

1995-02-28         NaN
1995-03-31   -1.000000
1995-04-28   -0.484825
1995-05-31   -0.592066
1995-06-30    0.433440
2021-02-26    0.529957
Name: (US, World), dtype: float64