| Description of slcovs |
slcovs
PURPOSE 
SLCOVS Computes the sample covariance matrix
SYNOPSIS 
function varargout = slcovs(X, w, nums, M)
DESCRIPTION 
CROSS-REFERENCE INFORMATION 
This function calls:
- slcov SLCOV Compute the covariance matrix
- slnums2bounds SLNUMS2BOUNDS Compute the index-boundaries from section sizes
This function is called by:
SUBFUNCTIONS 
SOURCE CODE 
0001 function varargout = slcovs(X, w, nums, M)
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032
0033
0034
0035
0036
0037
0038
0039
0040
0041
0042
0043
0044
0045
0046
0047
0048
0049
0050
0051
0052
0053
0054
0055
0056
0057
0058
0059 if ndims(X) ~= 2
0060 error('sltoolbox:invaliddims', 'X should be a 2D matrix');
0061 end
0062 [d, n] = size(X);
0063
0064
0065 if nargin < 2 || isempty(w)
0066 w = [];
0067 else
0068 if ~isequal(size(w), [1, n])
0069 error('sltoolbox:sizmismatch', ...
0070 'the weight vector should be a 1 x n row vector');
0071 end
0072 end
0073
0074
0075 if nargin < 3 || isempty(nums)
0076 isgrouped = false;
0077 k = 1;
0078 else
0079 isgrouped = true;
0080 if size(nums, 1) ~= 1
0081 error('sltoolbox:sizmismatch', ...
0082 'the nums vector should be a row vector');
0083 end
0084 if sum(nums) ~= n
0085 error('sltoolbox:sizmismatch', ...
0086 'the nums vector does not match the total number of vectors');
0087 end
0088 k = length(nums);
0089 end
0090
0091
0092 if nargin < 4
0093 M = [];
0094 elseif ~isempty(M) && ~isequal(M, 0)
0095 if ~isequal(size(M), [d, k])
0096 error('sltoolbox:sizmismatch', ...
0097 'the matrix of means should be a d x k matrix');
0098 end
0099 end
0100
0101
0102
0103
0104 if ~isgrouped
0105
0106
0107 C = slcov(X, w, M);
0108
0109
0110 if nargout == 1
0111 varargout = {C};
0112 elseif nargout == 2
0113 varargout = {C, C};
0114 end
0115
0116 else
0117
0118
0119 Cs = compute_groupwise_covariances(nums, d, k, X, w, M);
0120
0121
0122 if nargout == 1
0123 varargout = {Cs};
0124 elseif nargout == 2
0125
0126 Cpool = compute_pooled_covariance(Cs, d, k, nums, w);
0127 varargout = {Cs, Cpool};
0128 end
0129
0130 end
0131
0132
0133
0134
0135
0136
0137 function Cs = compute_groupwise_covariances(nums, d, k, X, w, M)
0138
0139 [sp, ep] = slnums2bounds(nums);
0140
0141
0142 Cs = zeros(d, d, k);
0143
0144 if isempty(M)
0145
0146 if isempty(w)
0147 for i = 1 : k
0148 Cs(:, :, i) = slcov(X(:, sp(i):ep(i)), [], []);
0149 end
0150 else
0151 for i = 1 : k
0152 Cs(:, :, i) = slcov(X(:, sp(i):ep(i)), w(sp(i):ep(i)), []);
0153 end
0154 end
0155
0156 elseif isequal(M, 0)
0157
0158 if isempty(w)
0159 for i = 1 : k
0160 Cs(:, :, i) = slcov(X(:, sp(i):ep(i)), [], 0);
0161 end
0162 else
0163 for i = 1 : k
0164 Cs(:, :, i) = slcov(X(:, sp(i):ep(i)), w(sp(i):ep(i)), 0);
0165 end
0166 end
0167
0168 else
0169
0170 if isempty(w)
0171 for i = 1 : k
0172 Cs(:, :, i) = slcov(X(:, sp(i):ep(i)), [], M(:, i));
0173 end
0174 else
0175 for i = 1 : k
0176 Cs(:, :, i) = slcov(X(:, sp(i):ep(i)), w(sp(i):ep(i)), M(:, i));
0177 end
0178 end
0179
0180 end
0181
0182
0183
0184 function Cpool = compute_pooled_covariance(Cs, d, k, nums, w)
0185
0186 [sp, ep] = slnums2bounds(nums);
0187
0188
0189 if isempty(w)
0190 gw = nums;
0191 else
0192 gw = zeros(1, k);
0193 for i = 1 : k
0194 gw(i) = sum(w(sp(i):ep(i)));
0195 end
0196 end
0197 gw = gw(:) / sum(gw);
0198
0199
0200 Cs = reshape(Cs, [d*d, k]);
0201 Cpool = Cs * gw;
0202 Cpool = reshape(Cpool, [d, d]);
0203
0204
0205
Generated on Wed 20-Sep-2006 12:43:11 by m2html © 2003
|
|