- 将方差和协方差写在一个矩阵中,体现变量间的联动关系:离散程度和相关性.
- 计算公式:
Σ(X,Y,Z)=Cov(X,X)Cov(Y,X)Cov(Z,X)Cov(X,Y)Cov(Y,Y)Cov(Z,Y)Cov(X,Z)Cov(Y,Z)Cov(Z,Z)=Var(X)Cov(Y,X)Cov(Z,X)Cov(X,Y)Var(Y)Cov(Z,Y)Cov(X,Z)Cov(Y,Z)Var(Z)
- 主对角线上元素体现的是随机变量的方差,非对角线上元素体现的是两个随机变量之间的协方差。
- 构造过度矩阵A:
A(X,Y,Z)=x1−xˉx2−xˉx3−xˉy1−yˉy2−yˉy3−yˉz1−zˉz2−zˉz3−zˉ=x1x2x3y1y2y3z1z2z3−xˉxˉxˉyˉyˉyˉzˉzˉzˉ=x1x2x3y1y2y3z1z2z3−31x1+x2+x3x1+x2+x3x1+x2+x3y1+y2+y3y1+y2+y3y1+y2+y3z1+z2+z3z1+z2+z3z1+z2+z3=x1x2x3y1y2y3z1z2z3−31111111111x1x2x3y1y2y3z1z2z3(各个元素和均值的偏差)
- 通过过渡矩阵A求协方差矩阵:
Σ(X,Y,Z)=Cov(X,X)Cov(Y,X)Cov(Z,X)Cov(X,Y)Cov(Y,Y)Cov(Z,Y)Cov(X,Z)Cov(Y,Z)Cov(Z,Z)=n1∑i=1n(xi−Xˉ)(xi−Xˉ)n1∑i=1n(yi−Yˉ)(xi−Xˉ)n1∑i=1n(zi−Zˉ)(xi−Xˉ)n1∑i=1n(xi−Xˉ)(yi−Yˉ)n1∑i=1n(yi−Yˉ)(yi−Yˉ)n1∑i=1n(zi−Zˉ)(yi−Yˉ)n1∑i=1n(xi−Xˉ)(zi−Zˉ)n1∑i=1n(yi−Yˉ)(zi−Zˉ)n1∑i=1n(zi−Zˉ)(zi−Zˉ)=31x1−xˉy1−yˉz1−zˉx2−xˉy2−yˉz2−zˉx3−xˉy3−yˉz3−zˉx1−xˉx2−xˉx3−xˉy1−yˉy2−yˉy3−yˉz1−zˉz2−zˉz3−zˉ=31ATA
import numpy as np
print(np.__version__)
def covariance(x, y):
x_mean = np.mean(x)
y_mean = np.mean(y)
return np.sum((x-x_mean)*(y-y_mean))/(len(x))
def covariance_matrix1(x, y, z):
return np.matrix([[covariance(x, x), covariance(x, y), covariance(x, z)],
[covariance(y, x), covariance(y, y), covariance(y, z)],
[covariance(z, x), covariance(z, y), covariance(z, z)]])
height = [179,187,175,170,185,177,178,187,190,178,170,183,184,180,180]
weight = [74,80,71,72,81,75,71,83,94,73,68,73,78,75,76]
age = [33,31,28,33,32,28,30,35,27,21,29,23,31,24,20]
print(covariance_matrix1(height, weight, age))
"""
输出:
[[32.69333333 29.74666667 1.4 ]
[29.74666667 38.06222222 3.97777778]
[ 1.4 3.97777778 19.42222222]]
"""
def A(x,y,z):
m = np.matrix([x,y,z]).T
return m - np.mean(m, axis=0)
def covariance_matrix2(x, y, z):
a = A(x,y,z)
return np.dot(a.T, a)/len(x)
print(covariance_matrix2(height, weight, age))
"""
输出:
[[32.69333333 29.74666667 1.4 ]
[29.74666667 38.06222222 3.97777778]
[ 1.4 3.97777778 19.42222222]]
"""