numpy.cov()函数如何实现? [英] How numpy.cov() function is implemented?

查看:285
本文介绍了numpy.cov()函数如何实现?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有基于等式​​的协方差函数的实现:

I have my own implementation of the covariance function based on the equation:

'''
Calculate the covariance coefficient between two variables.
'''

import numpy as np

X = np.array([171, 184, 210, 198, 166, 167])
Y = np.array([78, 77, 98, 110, 80, 69])

# Expected value function.
def E(X, P):
    expectedValue = 0
    for i in np.arange(0, np.size(X)):
        expectedValue += X[i] * (P[i] / np.size(X))
    return expectedValue 

# Covariance coefficient function.
def covariance(X, Y):
    '''
    Calculate the product of the multiplication for each pair of variables
    values.
    '''
    XY = X * Y

    # Calculate the expected values for each variable and for the XY.
    EX = E(X, np.ones(np.size(X)))
    EY = E(Y, np.ones(np.size(Y)))
    EXY = E(XY, np.ones(np.size(XY)))

    # Calculate the covariance coefficient.
    return EXY - (EX * EY)

# Display matrix of the covariance coefficient values.
covMatrix = np.array([[covariance(X, X), covariance(X, Y)], 
[covariance(Y, X), covariance(Y, Y)]])  
print("My function:", covMatrix)

# Display standard numpy.cov() covariance coefficient matrix.
print("Numpy.cov() function:", np.cov([X, Y]))

但是问题是,我从函数和numpy.cov()中获得了不同的值,即:

But the problem is, that I'm getting different values from my function and from numpy.cov(), ie:

My function: [[ 273.88888889  190.61111111]
 [ 190.61111111  197.88888889]]
Numpy.cov() function: [[ 328.66666667  228.73333333]
 [ 228.73333333  237.46666667]]

那是为什么? numpy.cov()函数如何实现?如果函数numpy.cov()实现得很好,我在做什么错呢?我只是说,函数covariance()的结果与Internet中用于计算协方差系数的示例paper的结果一致,例如

Why is that? How is numpy.cov() function implemented? If the function numpy.cov() is well-implemented, what am I doing wrong? I'll just say, that results from my function covariance() are consistent with the results from paper examples in the internet for calculating the covariance coefficient, eg http://www.naukowiec.org/wzory/statystyka/kowariancja_11.html.

推荐答案

作为默认设置,numpy函数具有与您不同的规范化.试试吧

The numpy function has a different normalization to yours as a default setting. Try instead

>>> np.cov([X, Y], ddof=0)
array([[ 273.88888889,  190.61111111],
       [ 190.61111111,  197.88888889]])

参考文献:

  • http://docs.scipy.org/doc/numpy/reference/generated/numpy.cov.html
  • http://en.wikipedia.org/wiki/Covariance#Calculating_the_sample_covariance

这篇关于numpy.cov()函数如何实现?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆