代码改变世界

方差,协方差求解

2013-05-08 10:03  钱吉  阅读(1373)  评论(0)    收藏  举报

从我CSDN的博客上面转过来的。http://blog.csdn.net/lovewubo/article/details/8893779

记录一下概率论里面的方差和协方差的求解,是如何实现的,在matlab里面对应求解的几个函数,以及他们的意义。以下是原文:

一:求均值的函数:mean

二:求方差的函数:var

标准差:

方差:

三:求协方差的函数:cov

 

1 如果a,b是向量,则:

cov(a,b)相当于是[D(a),cov(a,b);cov(b,a),D(b)],其中D(a)和D(b)分别代表了a,b的方差。cov(a,b)和cov(b,a)分别代表了两者之间的协方差


举例:

>> a=[1.1 2.2 3 4.5 0.8]


a =


    1.1000    2.2000    3.0000    4.5000    0.8000


>> b=[1.3 2.4 2.1 5 3.3]


b =


    1.3000    2.4000    2.1000    5.0000    3.3000


>> cov(a,b)


ans =


    2.2570    1.3595
    1.3595    1.9970

其中,对角线的值是两个向量的方差,斜对角线的是两个向量之间的协方差

 

2 如果a,b是矩阵,则:

cov(a,b)相当于是cov(a(:),b(:)),举例说明:

>> a=randn(4);
>> a


a =


   -0.4326   -1.1465    0.3273   -0.5883
   -1.6656    1.1909    0.1746    2.1832
    0.1253    1.1892   -0.1867   -0.1364
    0.2877   -0.0376    0.7258    0.1139
>> b=randn(4);
>> b


b =


    1.0668    0.2944   -0.6918   -1.4410
    0.0593   -1.3362    0.8580    0.5711
   -0.0956    0.7143    1.2540   -0.3999
   -0.8323    1.6236   -1.5937    0.6900
>> cov(a,b)


ans =


    0.8513   -0.0860
   -0.0860    1.0129

/////////////////////////////////////////////////////////////////////////////
>> a(:)


ans =


   -0.4326
   -1.6656
    0.1253
    0.2877
   -1.1465
    1.1909
    1.1892
   -0.0376
    0.3273
    0.1746
   -0.1867
    0.7258
   -0.5883
    2.1832
   -0.1364
    0.1139


>> b(:)


ans =


    1.0668
    0.0593
   -0.0956
   -0.8323
    0.2944
   -1.3362
    0.7143
    1.6236
   -0.6918
    0.8580
    1.2540
   -1.5937
   -1.4410
    0.5711
   -0.3999
    0.6900


>> cov(a(:),b(:))


ans =


    0.8513   -0.0860
   -0.0860    1.0129

3 cov(A),如果A(m*n)是矩阵的话,其协方差就是一个方阵B(n*n)。且B的元素e(i,j) = E[(A(:,i)-mean(A(:,i))*(A(:,j)-mean(A(:,j)))]。解释一下,就是A中第i行和第j行的协方差。所以B的对角线元素相当于是A中各个列的方差。所以,A的列相当于是变量,而每一行代表了一个样本。cov(A)求每个变量之间的相关度,即每一列之间的相关度,而不是每一个样本之间的相关度,这点要注意。示例如下:

>> A=randn(3,4)

A =

   -0.4326    0.2877    1.1892    0.1746
   -1.6656   -1.1465   -0.0376   -0.1867
    0.1253    1.1909    0.3273    0.7258

>> cov(A)

ans =

    0.8398    1.0764    0.2809    0.3979
    1.0764    1.3893    0.3057    0.5248
    0.2809    0.3057    0.3968    0.0502
    0.3979    0.5248    0.0502    0.2112

 

另外,matlab里面关于cov函数的说明解释的很详细: 

cov(x), if x is a vector, returns the variance of x. For matrix input X, where each row is an observation, and each column is a variable, cov(X) is the covariance matrix. diag(cov(X)) is a vector of variances for each column, and sqrt(diag(cov(X))) is a vector of standard deviations. cov(X,Y), where X and Y are matrices with the same number of elements, is equivalent to cov([X(:) Y(:)]).

cov(x) or cov(x,y) normalizes by N – 1, if N > 1, where N is the number of observations. This makes cov(X) the best unbiased estimate of the covariance matrix if the observations are from a normal distribution. For N = 1, cov normalizes by N.

cov(x,1) or cov(x,y,1) normalizes by N and produces the second moment matrix of the observations about their mean. cov(X,Y,0) is the same as cov(X,Y) and cov(X,0) is the same as cov(X). 

其使用示例可见:http://www.mathworks.cn/cn/help/matlab/ref/cov.html?searchHighlight=cov

 

参考:

http://pinkyjie.com/2010/08/31/covariance/ 

http://www.cnblogs.com/cvlabs/archive/2010/05/08/1730319.html

http://blog.csdn.net/faceRec/article/details/1697362

http://blog.csdn.net/ybdesire/article/details/6270328