面板和数据帧的矩阵矢量积没有循环

时间:2016-10-25 13:16:55

标签: python pandas numpy

我有两个输入:DataFrame个值(按日期索引)和Panel个协方差(项目是日期)

值的一个简单示例是:

values = pd.DataFrame(np.random.randn(10, 20), index=pd.date_range('2010-01-01', '2010-01-10'))

样品:

                   0         1         2         3         4
2010-01-01 -1.703370 -0.618470  0.546668  0.870241 -2.241399
2010-01-02  1.014537 -1.008169 -0.835622 -0.073839  0.323681
2010-01-03 -0.590822 -0.655494  0.980676 -1.004728  0.247670
2010-01-04  0.040766 -1.299328 -0.243147  2.078847  1.037934
2010-01-05 -0.530034  0.011097  0.897639 -0.038444  1.158523
2010-01-06  0.042872 -1.256107 -1.257896  0.134431  1.362559
2010-01-07 -0.027543  2.308903  0.140279 -0.749377  0.688303
2010-01-08 -0.124412 -0.965076  2.478367 -0.465419  0.340245
2010-01-09  0.677635 -0.041608 -0.058514  0.718447 -0.839126
2010-01-10 -0.916301  1.102121  1.065056 -0.373376  0.980630

至于协方差:

covariance = values.rolling(2).cov()

样品:

                0   1   2   3   4
            0 NaN NaN NaN NaN NaN
            1 NaN NaN NaN NaN NaN
2010-01-01  2 NaN NaN NaN NaN NaN
            3 NaN NaN NaN NaN NaN
            4 NaN NaN NaN NaN NaN

                      0         1         2         3         4
            0  0.001589 -0.076168 -0.016951 -0.001697  0.004212
            1 -0.076168  3.651094  0.812570  0.081366 -0.201922
2010-01-02  2 -0.016951  0.812570  0.180842  0.018108 -0.044939
            3 -0.001697  0.081366  0.018108  0.001813 -0.004500
            4  0.004212 -0.201922 -0.044939 -0.004500  0.011167
            ...
                      0         1         2         3         4
            0  0.017162 -0.097745 -0.072702  0.172182  0.215784
            1 -0.097745  0.556687  0.414060 -0.980626 -1.228953
2010-01-10  2 -0.072702  0.414060  0.307974 -0.729382 -0.914086
            3  0.172182 -0.980626 -0.729382  1.727410  2.164847
            4  0.215784 -1.228953 -0.914086  2.164847  2.713058

现在我想简单地将每个面板矩阵乘以其对应的数据帧向量。这是每天:

covariance[day].dot(values.ix[day])

如果不使用for循环并将结果附加到数据框中,我无法想办法做到这一点(我有一大堆要做的事情)。

for i in covariance.keys():
    covariance[i].dot(values.ix[i])
  • 我真的不明白covariance.mul/multiply(values)做了什么,但我得到的是NaN

    covariance.mul(values, 'items')
    
  • 带有Panel.apply
  • axis=('major', 'minor')几乎可以使用,但我找不到检索项目的方法,因此我可以选择正确的values索引。

    covariance.apply(lambda x: x.dot(values.ix[???]), axis=('minor', 'major'))
    

0 个答案:

没有答案