转自:http://blog.csdn.net/raocong2010/article/details/5941602
算法实现
- clc;
- clear all;
- M = 5
- N = 3;
- % 生成一个M*N的随机原始矩阵
- OriginMatrix = rand( M, N );
- % 使用自带的cov函数计算相关矩阵
- CovMatrix = cov( OriginMatrix );
- MeanArray = mean( OriginMatrix );
- MeanMatrix = ones( M, 1 ) * MeanArray;
- % 得到每列减去每列均值的的矩阵
- OriginSubMean = OriginMatrix - MeanMatrix;
- % 计算出相关矩阵
- if M == 1
- CovMatrixComputed = OriginSubMean' * OriginSubMean / M;
- else
- CovMatrixComputed = OriginSubMean' * OriginSubMean / ( M-1 );
- end
如何理解
matlab在计算相关矩阵时,把每一列的数作为一个随机变量的样本,每一行作为一个这几个随机变量的联合样本,即第i个随机变量取第k行的样本值时,第j个随机变量也取第k行的样本值。利用这个性质,我们就可以用协方差的公式代入来计算协方差矩阵了。
然而,由于矩阵中给出只是这些随机变量的样本,根据概率论的知识我们知道,由于我们不知道这些随机变量的概率分布(或联合概率分布),我们是不可能计算出这些随机变量的期望、方差或是协方差的,而只能计算出它们的一个无偏估计,即样本均值、样本方差与样本协方差。其计算公式如下所示:
matlab中的解释
cov(x), if X is a vector, returns the variance. For matrices, 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. ForN=1, cov normalizes by N.