Covariance

November 10, 2019

In probability theory and statistics, covariance is a measure of the joint variability of two random variables.

协方差(Covariance)在概率论统计学中用于衡量两个变量的总体误差。而方差是协方差的一种特殊情况,即当两个变量是相同的情况。维基百科定义

Definition

Let and be random variables with means and and standard deviations and . Define the covariance between and by

and the correlation by

Code


def cov(X):
    """
    just like np.cov(X.T)
    """
    sub_mean  = X - np.mean(X, axis=0, keepdims=True)
    cov = np.dot(sub_mean.T, sub_mean) / (X.shape[0] - 1)
    return cov

a = [-2.1, 3, 1]
b = [-1, 1, 2]
c = [4.3, 0.12, 3]
d = [5, 0, 4]
X = np.stack([a, b, c, d]) # 4 samples, 3 variances

print cov(X)

Reference

  1. All of Statistics
Covariance - November 10, 2019 -