numpy covariance matrix

前端 未结 10 1616
半阙折子戏
半阙折子戏 2021-01-01 13:10

Suppose I have two vectors of length 25, and I want to compute their covariance matrix. I try doing this with numpy.cov, but always end up with a 2x2 matrix.



        
10条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-01-01 13:35

    I suppose what youre looking for is actually a covariance function which is a timelag function. I'm doing autocovariance like that:

     def autocovariance(Xi, N, k):
        Xs=np.average(Xi)
        aCov = 0.0
        for i in np.arange(0, N-k):
            aCov = (Xi[(i+k)]-Xs)*(Xi[i]-Xs)+aCov
        return  (1./(N))*aCov
    
    autocov[i]=(autocovariance(My_wector, N, h))
    

提交回复
热议问题