matlab里面的求协方差函数

来源:互联网 发布:淘宝日服苹果账号注册 编辑:程序博客网 时间:2024/03/28 23:52

一: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);>> aa =   -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);>> bb =    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


另外,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

原创粉丝点击