2017-09-28 3 views
0

私はDataFrame dffを取得しました。その形状は5アイテムと4日間の略です。私はEWM ewmmを作成し、EW移動平均を得ました。パンダEW移動相関/共分散が予期しない形状になった

相関と共分散については、5×5の形状の行列が予想通りですが、結果は5×4×4の形になります。

どうすればいいですか?

In[59]: dff 
Out[59]: 

    0 1 2 3 
0 1 2 3 4 
1 4 3 2 1 
2 3 1 4 1 
3 5 9 2 6 
4 1 2 3 4 
In[60]: ewmm = dff.ewm(span=2, axis=1) 
In[61]: ewmm.mean() 
Out[61]: 

    0  1   2  3 
0 1.0 1.75 2.615385 3.550 
1 4.0 3.25 2.384615 1.450 
2 3.0 1.50 3.230769 1.725 
3 5.0 8.00 3.846154 5.300 
4 1.0 1.75 2.615385 3.550 
In[62]: ewmm.corr() 
Out[62]: 

<class 'pandas.core.panel.Panel'> 
Dimensions: 5 (items) x 4 (major_axis) x 4 (minor_axis) 
Items axis: 0 to 4 
Major_axis axis: 0 to 3 
Minor_axis axis: 0 to 3 
In[63]: ewmm.cov() 
Out[63]: 

<class 'pandas.core.panel.Panel'> 
Dimensions: 5 (items) x 4 (major_axis) x 4 (minor_axis) 
Items axis: 0 to 4 
Major_axis axis: 0 to 3 
Minor_axis axis: 0 to 3 

答えて

0

転位ddf.Tを処理することで正しく得ました。形状(4,5)は4日間と5日間を意味します。

ewmm.cov()/ewmm.corr()が呼び出されると、パンダはaxisを考慮しないと思います。

In[73]: dff = dff.T 
In[74]: dff 
Out[74]: 

    0 1 2 3 4 
0 1 4 3 5 1 
1 2 3 1 9 2 
2 3 2 4 2 3 
3 4 1 1 6 4 
In[75]: ewmm = dff.ewm(span=2) 
In[76]: ewmm.mean() 
Out[76]: 

      0   1   2   3   4 
0 1.000000 4.000000 3.000000 5.000000 1.000000 
1 1.750000 3.250000 1.500000 8.000000 1.750000 
2 2.615385 2.384615 3.230769 3.846154 2.615385 
3 3.550000 1.450000 1.725000 5.300000 3.550000 
In[77]: ewmm.corr() 
Out[77]: 

<class 'pandas.core.panel.Panel'> 
Dimensions: 4 (items) x 5 (major_axis) x 5 (minor_axis) 
Items axis: 0 to 3 
Major_axis axis: 0 to 4 
Minor_axis axis: 0 to 4 
In[78]: ewmm.corr()[3] 
Out[78]: 

      0   1   2   3   4 
0 1.000000 -1.000000 -0.532986 0.145400 1.000000 
1 -1.000000 1.000000 0.532986 -0.145400 -1.000000 
2 -0.532986 0.532986 1.000000 -0.908437 -0.532986 
3 0.145400 -0.145400 -0.908437 1.000000 0.145400 
4 1.000000 -1.000000 -0.532986 0.145400 1.000000 
関連する問題