协方差
定义
随机变量X、Y 的协方差Cov(X,Y):
Cov(X,Y) = E[(X-E(X))(Y-E(Y))]
= E[XY] - E[Y]E[X]
从直观上来看,协方差表示的是两个变量总体误差的期望。
意义
表示两个变量的变化趋势一致性大小。
如果X与Y是统计独立的,那么二者之间的协方差就是0。
但是,反过来并不成立。即如果X与Y的协方差为0,二者并不一定是统计独立的。
相关性系数是归一化后的协方差:[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-puSHwD9U-1645611629569)(attachment:equation.svg)]
最优线性预测系数A :
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-NtoF6v63-1645611629570)(attachment:equation.svg)]
同时也可以得到A:
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-VF1pCKsL-1645611629571)(attachment:equation.svg)]
性质
(1)Cov(X,Y) = Cov(Y,X);
(2)Cov(aX,bY) = abCov(X,Y),(a,b是常数);
(3)Cov(X1+X2,Y) = Cov(X1,Y)+Cov(X2,Y);
(4)Cov(X+a,Y+b) = Cov(Y,X)
协方差与方差
在统计描述中,方差用来计算每一个变量(观察值)与总体均数之间的差异。=》一个变量误差平方后的期望
协方差表示两个变量总体误差的期望
代码以及协方差矩阵
def Cov(x,y):
import numpy as np
x = np.array(list(x))
y = np.array(list(y))
cov = sum(np.multiply(x-np.mean(x), y-np.mean(y)))/(len(x)-1)
return cov
# or
def Cov(x,y):
import numpy as np
x = np.array(x)
y = np.array(y)
X = np.stack((x,y),axis=0)
cov = np.cov(X)
return cov[0,1]
# or
def Cov(x,y):
import numpy as np
x = np.array(x)
y = np.array(y)
cov = np.cov(x,y)
return cov[0,1]
x = [-2.1, -1, 4.3]
y = [3, 1.1, 0.12]
X = np.stack((x, y), axis=0)
X
array([[-2.1 , -1. , 4.3 ],
[ 3. , 1.1 , 0.12]])
X.shape
(2, 3)
np.cov(X)
array([[11.71 , -4.286 ],
[-4.286 , 2.14413333]])
np.cov(x, y)
array([[11.71 , -4.286 ],
[-4.286 , 2.14413333]])
np.cov(x)
array(11.71)
np.cov(y)
array(2.14413333)
def Cov(x,y):
import numpy as np
x = np.array(list(x))
y = np.array(list(y))
cov = sum(np.multiply(x-np.mean(x), y-np.mean(y)))/(len(x)-1)
return cov
Cov(x,y)
-4.2860000000000005
def Cov(x,y):
import numpy as np
x = np.array(x)
y = np.array(y)
X = np.stack((x,y),axis=0)
cov = np.cov(X)
return cov[0,1]
Cov(x,y)
-4.2860000000000005
def Cov(x,y):
import numpy as np
x = np.array(x)
y = np.array(y)
cov = np.cov(x,y)
return cov[0,1]
Cov(x,y)
-4.2860000000000005
其他博主的博文
# -*- coding: utf-8 -*-
"""
@author: 蔚蓝的天空Tom
Talk is cheap, show me the code
Aim:计算两个维度的协方差covariance
"""
import numpy as np
class CCovariance(object):
'''计算X,Y这俩维度的协方差
'''
def __init__(self, X, Y):
self.X = X
self.Y = Y
self.Covariance_way1()
self.Covariance_way2()
self.Covariance_way3()
def Covariance_way1(self):
'''
协方差公式法计算两个等长向量的协方差convariance
'''
X,Y = np.array(self.X), np.array(self.Y)
meanX, meanY = np.mean(X), np.mean(Y)
n = np.shape(X)[0]
#按照协方差公式计算协方差,Note:分母一定是n-1
covariance = sum(np.multiply(X-meanX, Y-meanY))/(n-1)
print('协方差公式法求得的协方差:', covariance)
return covariance
def Covariance_way2(self):
'''
向量中心化方法计算两个等长向量的协方差convariance
'''
X,Y = np.array(self.X),np.array(self.Y)
n = np.shape(X)[0]
centrX = X-np.mean(X)
centrY = Y-np.mean(Y)
convariance = sum(np.multiply(centrX, centrY))/(n-1)
print('向量中心化方法求得协方差:', convariance)
return convariance
def Covariance_way3(self):
'''
numpy.conv(X,Y)提供的协方差函数求协方差
'''
conv = np.cov(self.X, self.Y)
print('np.cov(X,Y)求得的X的方差:', conv[0,0])
print('np.cov(X,Y)求得的Y的方差:', conv[1,1])
print('np.cov(X,Y)求得的X和Y的协方差:',conv[0,1])
if __name__=='__main__':
X = [10,15,23,11,42,9,11,8,11,21]
Y = [15,46,21,9,45,48,21,5,12,20]
c = CCovariance(X,Y)
# -*- coding: utf-8 -*-
"""
@author: 蔚蓝的天空Tom
Talk is cheap, show me the code
Aim:计算一个多维度样本的协方差矩阵covariance matrix
Note:协方差矩阵是计算的样本中每个特征之间的协方差,所以协方差矩阵是特征个数阶的对称阵
"""
import numpy as np
class CCovMat(object):
'''计算多维度样本集的协方差矩阵
Note:请保证输入的样本集m×n,m行样例,每个样例n个特征
'''
def __init__(self, samples):
#样本集shpae=(m,n),m是样本总数,n是样本的特征个数
self.samples = samples
self.covmat1 = [] #保存方法1求得的协方差矩阵
self.covmat2 = [] #保存方法1求得的协方差矩阵
#用方法1计算协方差矩阵
self._calc_covmat1()
#用方法2计算协方差矩阵
self._calc_covmat2()
def _covariance(self, X, Y):
'''
计算两个等长向量的协方差convariance
'''
n = np.shape(X)[0]
X, Y = np.array(X), np.array(Y)
meanX, meanY = np.mean(X), np.mean(Y)
#按照协方差公式计算协方差,Note:分母一定是n-1
cov = sum(np.multiply(X-meanX, Y-meanY))/(n-1)
return cov
def _calc_covmat1(self):
'''
方法1:根据协方差公式和协方差矩阵的概念计算协方差矩阵
'''
S = self.samples #样本集
na = np.shape(S)[1] #特征attr总数
self.covmat1 = np.full((na, na), fill_value=0.) #保存协方差矩阵
for i in range(na):
for j in range(na):
self.covmat1[i,j] = self._covariance(S[:,i], S[:,j])
return self.covmat1
def _calc_covmat2(self):
'''
方法2:先样本集中心化再求协方差矩阵
'''
S = self.samples #样本集
ns = np.shape(S)[0] #样例总数
mean = np.array([np.mean(attr) for attr in S.T]) #样本集的特征均值
print('样本集的特征均值:\n',mean)
centrS = S - mean ##样本集的中心化
print('样本集的中心化(每个元素将去当前维度特征的均值):\n', centrS)
#求协方差矩阵
self.covmat2 = np.dot(centrS.T, centrS)/(ns - 1)
return self.covmat2
def CovMat1(self):
return self.covmat1
def CovMat2(self):
return self.covmat2
if __name__=='__main__':
'10样本3特征的样本集'
samples = np.array([[10, 15, 29],
[15, 46, 13],
[23, 21, 30],
[11, 9, 35],
[42, 45, 11],
[9, 48, 5],
[11, 21, 14],
[8, 5, 15],
[11, 12, 21],
[21, 20, 25]])
cm = CCovMat(samples)
print('样本集(10行3列,10个样例,每个样例3个特征):\n', samples)
print('按照协方差公式求得的协方差矩阵:\n', cm.CovMat1())
print('按照样本集的中心化求得的协方差矩阵:\n', cm.CovMat1())
print('numpy.cov()计算的协方差矩阵:\n', np.cov(samples.T))