time | Calls | line |
---|
| | 1 | function c = cov(x,varargin)
|
| | 2 | %COV Covariance matrix.
|
| | 3 | % COV(X), if X is a vector, returns the variance. For matrices, where
|
| | 4 | % each row is an observation, and each column a variable, COV(X) is the
|
| | 5 | % covariance matrix. DIAG(COV(X)) is a vector of variances for each
|
| | 6 | % column, and SQRT(DIAG(COV(X))) is a vector of standard deviations.
|
| | 7 | % COV(X,Y), where X and Y are matrices with the same number of elements,
|
| | 8 | % is equivalent to COV([X(:) Y(:)]).
|
| | 9 | %
|
| | 10 | % COV(X) or COV(X,Y) normalizes by (N-1) if N>1, where N is the number of
|
| | 11 | % observations. This makes COV(X) the best unbiased estimate of the
|
| | 12 | % covariance matrix if the observations are from a normal distribution.
|
| | 13 | % For N=1, COV normalizes by N.
|
| | 14 | %
|
| | 15 | % COV(X,1) or COV(X,Y,1) normalizes by N and produces the second
|
| | 16 | % moment matrix of the observations about their mean. COV(X,Y,0) is
|
| | 17 | % the same as COV(X,Y) and COV(X,0) is the same as COV(X).
|
| | 18 | %
|
| | 19 | % The mean is removed from each column before calculating the result.
|
| | 20 | %
|
| | 21 | % C = cov(..., MISSING) specifies how NaN (Not-A-Number) values are
|
| | 22 | % treated. The default is 'includenan':
|
| | 23 | %
|
| | 24 | % 'includenan' - if the input contains NaN, the output also contains NaN.
|
| | 25 | % Specifically, C(I, J) is NaN if column I or J of X
|
| | 26 | % contains NaN values.
|
| | 27 | % 'omitrows' - omit all rows of X that contain NaN values:
|
| | 28 | % ind = all(~isnan(X), 2);
|
| | 29 | % C = cov(X(ind, :));
|
| | 30 | % 'partialrows' - compute each element C(I,J) separately, based only on
|
| | 31 | % the columns I and J of X. Omit rows only if they
|
| | 32 | % contain NaN values in column I or J of X.
|
| | 33 | % The resulting matrix C may not be a positive definite.
|
| | 34 | % ind = all(~isnan(X(:, [I J])));
|
| | 35 | % Clocal = cov(X(ind, [I J]));
|
| | 36 | % C(I, J) = Clocal(1, 2);
|
| | 37 | %
|
| | 38 | % Class support for inputs X,Y:
|
| | 39 | % float: double, single
|
| | 40 | %
|
| | 41 | % See also CORRCOEF, VAR, STD, MEAN.
|
| | 42 |
|
| | 43 | % Copyright 1984-2014 The MathWorks, Inc.
|
| | 44 |
|
| 1 | 45 | if nargin==0
|
| | 46 | error(message('MATLAB:cov:NotEnoughInputs'));
|
| | 47 | end
|
< 0.01 | 1 | 48 | if nargin>4
|
| | 49 | error(message('MATLAB:cov:TooManyInputs'));
|
| | 50 | end
|
| 1 | 51 | if ~ismatrix(x)
|
| | 52 | error(message('MATLAB:cov:InputDim'));
|
| | 53 | end
|
| | 54 |
|
| 1 | 55 | nin = nargin;
|
| | 56 |
|
| | 57 | % Check for cov(..., missing)
|
| 1 | 58 | omitnan = false;
|
| 1 | 59 | if numel(varargin)>0
|
| | 60 | flag = varargin{end};
|
| | 61 | if ischar(flag)
|
| | 62 |
|
| | 63 | if ~isrow(flag)
|
| | 64 | error(message('MATLAB:cov:unknownFlag'));
|
| | 65 | end
|
| | 66 |
|
| | 67 | flag = parseFlag(flag);
|
| | 68 |
|
| | 69 | if ~isscalar(flag)
|
| | 70 | error(message('MATLAB:cov:unknownFlag'));
|
| | 71 | end
|
| | 72 |
|
| | 73 | omitnan = (flag == 'o' || flag == 'p');
|
| | 74 | dopairwise = (flag == 'p');
|
| | 75 |
|
| | 76 | varargin(end) = [];
|
| | 77 | nin = nin-1;
|
| | 78 | end
|
| | 79 | end
|
| | 80 |
|
| | 81 |
|
| | 82 | % Check for cov(x,normfactor) or cov(x,y,normfactor)
|
| 1 | 83 | if nin==4
|
| | 84 | error(message('MATLAB:cov:unknownFlag'));
|
| 1 | 85 | elseif nin==3
|
| | 86 | normfactor = varargin{end};
|
| | 87 | if ~isnormfactor(normfactor)
|
| | 88 | error(message('MATLAB:cov:notScalarFlag'));
|
| | 89 | end
|
| | 90 | nin = nin - 1;
|
| 1 | 91 | elseif nin==2 && isnormfactor(varargin{end})
|
| | 92 | normfactor = varargin{end};
|
| | 93 | nin = nin - 1;
|
| 1 | 94 | else
|
| 1 | 95 | normfactor = 0;
|
< 0.01 | 1 | 96 | end
|
| | 97 |
|
| 1 | 98 | scalarxy = false; % cov(scalar,scalar) is an ambiguous case
|
| 1 | 99 | if nin == 2
|
| | 100 | y = varargin{1};
|
| | 101 | if ~ismatrix(y)
|
| | 102 | error(message('MATLAB:cov:InputDim'));
|
| | 103 | end
|
| | 104 | x = x(:);
|
| | 105 | y = y(:);
|
| | 106 | if length(x) ~= length(y),
|
| | 107 | error(message('MATLAB:cov:XYlengthMismatch'));
|
| | 108 | end
|
| | 109 | scalarxy = isscalar(x) && isscalar(y);
|
| | 110 | x = [x y];
|
| | 111 | end
|
| | 112 |
|
| 1 | 113 | if isvector(x) && ~scalarxy
|
| | 114 | x = x(:);
|
| | 115 | end
|
| | 116 |
|
| 1 | 117 | if omitnan
|
| | 118 | xnan = isnan(x);
|
| | 119 |
|
| | 120 | if any(xnan(:)) % otherwise, just do standard cov
|
| | 121 | if dopairwise
|
| | 122 | c = apply_pairwise(x, normfactor);
|
| | 123 | return;
|
| | 124 | else
|
| | 125 | nanrows = any(xnan, 2);
|
| | 126 | x = x(~nanrows, :);
|
| | 127 | end
|
| | 128 | end
|
| | 129 | end
|
| | 130 |
|
< 0.01 | 1 | 131 | [m,n] = size(x);
|
| 1 | 132 | if isempty(x);
|
| | 133 | if (m==0 && n==0)
|
| | 134 | c = NaN('like', x);
|
| | 135 | else
|
| | 136 | c = NaN(n,'like', x);
|
| | 137 | end
|
| | 138 | return;
|
| | 139 | end
|
| | 140 |
|
< 0.01 | 1 | 141 | if normfactor == 0
|
| | 142 | % The unbiased estimator: divide by (m-1). Can't do this
|
| | 143 | % when m == 0 or 1.
|
< 0.01 | 1 | 144 | if m > 1
|
< 0.01 | 1 | 145 | denom = m - 1;
|
| | 146 | else
|
| | 147 | denom = m;
|
| | 148 | end
|
| | 149 | else
|
| | 150 | % The biased estimator: divide by m.
|
| | 151 | denom = m; % m==0 => return NaNs, m==1 => return zeros
|
| | 152 | end
|
| | 153 |
|
< 0.01 | 1 | 154 | xc = bsxfun(@minus,x,sum(x,1)/m); % Remove mean
|
| | 155 |
|
0.19 | 1 | 156 | c = (xc' * xc) ./ denom;
|
Other subfunctions in this file are not included in this listing.