缺失值的加权协方差矩阵

时间:2019-06-12 15:44:32

标签: python pandas numpy covariance

我正在尝试为股票投资组合计算协方差矩阵。我希望能够更加重视最近的观察并忽略缺失的数据。我打算使用指数衰减权重,但理想情况下,如果我改变主意,便可以指定任何形式的权重。我看到熊猫具有EWM.cov()函数,而numpy.cov()带有权重参数,但是我一直无法弄清楚如何使用它们。
问题:缺少某些数据时,python中最简单的方法是计算加权协方差矩阵?

我尝试过的事情:

成功忽略了丢失的数据

>>> test=[pd.DataFrame(
... [['2019-06-04',  9.92  ],
... [ '2019-06-03',  9.61  ],
... [ '2019-06-05',  9.78  ],
... [ '2019-06-06',  9.75  ],
... [ '2019-06-07',  9.76  ],
... [ '2019-06-10',  9.82  ],
... [ '2019-06-11',  9.92  ]],columns=['date','close']),pd.DataFrame(
... [['2019-06-04', 35.3512],
... [ '2019-06-05', 35.4600],
... [ '2019-06-06', 35.1900],
... [ '2019-06-07', 35.4900],
... [ '2019-06-10', 36.0100],
... [ '2019-06-11', 36.2000]],columns=['date','close']),pd.DataFrame(
... [['2019-06-03', 178.97 ],
... [ '2019-06-04', 193.60 ],
... [ '2019-06-05', 196.59 ],
... [ '2019-06-06', 205.95 ],
... [ '2019-06-07', 204.50 ],
... [ '2019-06-10', 212.88 ],
... [ '2019-06-11', 217.10 ]],columns=['date','close'])]
>>> d=pd.concat([x.close.pct_change()[1:] for x in test],axis=1, join='outer',sort=True)
>>> np.ma.cov(np.ma.array(d,mask=np.isnan(d)),rowvar=False).data
array([[ 0.00028827, -0.00004108, -0.00040099],
       [-0.00004108,  0.00006706, -0.00005051],
       [-0.00040099, -0.00005051,  0.00094766]])
>>> pd.concat([x.close.pct_change()[1:] for x in test],axis=1, join='outer',sort=True).cov()
          close     close     close
close  0.000288 -0.000041 -0.000401
close -0.000041  0.000067 -0.000051
close -0.000401 -0.000051  0.000948

施加重量失败。

>>> weights=[.9**(6-x) for x in range(7)]
>>> np.ma.cov(np.ma.array(d,mask=np.isnan(d)),rowvar=False, aweights=weights).data
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: cov() got an unexpected keyword argument 'aweights'
>>> pd.concat([x.close.pct_change()[1:] for x in test],axis=1, join='outer',sort=True).ewm(halflife=252).cov()
            close     close     close
1 close       NaN       NaN       NaN
  close       NaN       NaN       NaN
  close       NaN       NaN       NaN
2 close  0.001198 -0.000262 -0.001622
  close -0.000262  0.000057  0.000354
  close -0.001622  0.000354  0.002198
3 close  0.000602 -0.000117 -0.000811
  close -0.000117  0.000067  0.000174
  close -0.000811  0.000174  0.001098
4 close  0.000412 -0.000056 -0.000631
  close -0.000056  0.000089 -0.000069
  close -0.000631 -0.000069  0.001497
5 close  0.000329 -0.000041 -0.000459
  close -0.000041  0.000067 -0.000051
  close -0.000459 -0.000051  0.001130
6 close  0.000287 -0.000041 -0.000399
  close -0.000041  0.000067 -0.000051
  close -0.000399 -0.000051  0.000945

0 个答案:

没有答案