Code covered by the BSD License  

Highlights from
Statistical Learning Toolbox

from Statistical Learning Toolbox by Dahua Lin
Functions for statistical learning, pattern recognition and computer vision, covering many topics.

Description of slcovs
Home > sltoolbox > stat > slcovs.m

slcovs

PURPOSE ^

SLCOVS Computes the sample covariance matrix

SYNOPSIS ^

function varargout = slcovs(X, w, nums, M)

DESCRIPTION ^

SLCOVS Computes the sample covariance matrix

 $ Syntax $
   - C = slcovs(X)
   - C = slcovs(X, w)
   - Cs = slcovs(X, [], nums)
   - Cs = slcovs(X, w, nums)
   - C = slcovs(X, w, [], M)
   - Cs = slcovs(X, w, nums, M)
   - [Cs, Cpool] = slcovs(...)

 $ Arguments $
   - X:           the sample matrix with each column representing a sample
   - w:           the sample weights
   - nums:        the number of samples in the groups
   - M:           the means or related information
   - C:           the overall covariance matrix
   - Cs:          the covariance matrices for the groups
   - Cpool:       the pooled covariance matrix

 $ Description $
   - C = slcovs(X) computes the covariance matrix for the samples in X.

   - C = slcovs(X, w) computes the weighted covariance matrix for the 
     samples in X, with weights specified in w.

   - C = slcovs(X, [], nums) computes the covariance matrices for 
     collections of samples, with the number of samples in each 
     collections specified by the row vector nums.

   - C = slcovs(X, w, nums) computes the weighted covariance matrices for
     collections of samples, with the sample weights specified in w,
     and the number of samples in collections specified in nums.

   - C = slcovs(X, w, [], M) also provides mean information via M. M = 0
     indicates that the mean vector is certainly zero vector. M can
     also be a d x 1 row vector specifying the (weighted) mean vector
     of X. If w is [], then all samples are treated equally.
         
   - Cs = slcovs(X, w, nums, M) computes the (weighted) covariance matrices 
     for collections of vectors. When computing normal covariance without 
     weighting you can set w = []. M = 0 indicates that the (weighted) mean 
     vector for all groups of samples are zero vectors. M can be a 
     d x k matrix storing the (weighted) mean vectors for all groups 
     of samples.

   - [Cs, Cpool] = slcovs(...) computes the pool covariance matrices for
     all collections of vectors. If the samples is not groupped, then
     Cpool is equal to Cs.

 $ History $
   - Created by Dahua Lin on Dec 18th, 2005

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 %SLCOVS Computes the sample covariance matrix
0003 %
0004 % $ Syntax $
0005 %   - C = slcovs(X)
0006 %   - C = slcovs(X, w)
0007 %   - Cs = slcovs(X, [], nums)
0008 %   - Cs = slcovs(X, w, nums)
0009 %   - C = slcovs(X, w, [], M)
0010 %   - Cs = slcovs(X, w, nums, M)
0011 %   - [Cs, Cpool] = slcovs(...)
0012 %
0013 % $ Arguments $
0014 %   - X:           the sample matrix with each column representing a sample
0015 %   - w:           the sample weights
0016 %   - nums:        the number of samples in the groups
0017 %   - M:           the means or related information
0018 %   - C:           the overall covariance matrix
0019 %   - Cs:          the covariance matrices for the groups
0020 %   - Cpool:       the pooled covariance matrix
0021 %
0022 % $ Description $
0023 %   - C = slcovs(X) computes the covariance matrix for the samples in X.
0024 %
0025 %   - C = slcovs(X, w) computes the weighted covariance matrix for the
0026 %     samples in X, with weights specified in w.
0027 %
0028 %   - C = slcovs(X, [], nums) computes the covariance matrices for
0029 %     collections of samples, with the number of samples in each
0030 %     collections specified by the row vector nums.
0031 %
0032 %   - C = slcovs(X, w, nums) computes the weighted covariance matrices for
0033 %     collections of samples, with the sample weights specified in w,
0034 %     and the number of samples in collections specified in nums.
0035 %
0036 %   - C = slcovs(X, w, [], M) also provides mean information via M. M = 0
0037 %     indicates that the mean vector is certainly zero vector. M can
0038 %     also be a d x 1 row vector specifying the (weighted) mean vector
0039 %     of X. If w is [], then all samples are treated equally.
0040 %
0041 %   - Cs = slcovs(X, w, nums, M) computes the (weighted) covariance matrices
0042 %     for collections of vectors. When computing normal covariance without
0043 %     weighting you can set w = []. M = 0 indicates that the (weighted) mean
0044 %     vector for all groups of samples are zero vectors. M can be a
0045 %     d x k matrix storing the (weighted) mean vectors for all groups
0046 %     of samples.
0047 %
0048 %   - [Cs, Cpool] = slcovs(...) computes the pool covariance matrices for
0049 %     all collections of vectors. If the samples is not groupped, then
0050 %     Cpool is equal to Cs.
0051 %
0052 % $ History $
0053 %   - Created by Dahua Lin on Dec 18th, 2005
0054 %
0055 
0056 %% parse and verify input arguments
0057 
0058 % basic
0059 if ndims(X) ~= 2
0060     error('sltoolbox:invaliddims', 'X should be a 2D matrix');
0061 end
0062 [d, n] = size(X);
0063 
0064 % for weights
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 % for groupping
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 % for mean vectors
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 %% compute the covariances
0103 
0104 if ~isgrouped      % not groupped
0105     
0106     % compute the covariance
0107     C = slcov(X, w, M);
0108     
0109     % output
0110     if nargout == 1
0111         varargout = {C};
0112     elseif nargout == 2
0113         varargout = {C, C};
0114     end
0115            
0116 else                % groupped
0117     
0118     % compute groupwise covariances
0119     Cs = compute_groupwise_covariances(nums, d, k, X, w, M);
0120     
0121     % output
0122     if nargout == 1        
0123         varargout = {Cs};        
0124     elseif nargout == 2        
0125         % compute pooled covariance
0126         Cpool = compute_pooled_covariance(Cs, d, k, nums, w);                        
0127         varargout = {Cs, Cpool};
0128     end
0129     
0130 end
0131  
0132 
0133 %% Sub functions
0134 
0135 
0136 %% the function for computing group-wise covariance matrices
0137 function Cs = compute_groupwise_covariances(nums, d, k, X, w, M)
0138 
0139 [sp, ep] = slnums2bounds(nums);
0140 
0141 % prepare storage
0142 Cs = zeros(d, d, k);
0143 
0144 if isempty(M)           % no precomputed means
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)        % pre centralized
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                        % precomputed means
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 %% the function for computing pooled covariance
0184 function Cpool = compute_pooled_covariance(Cs, d, k, nums, w)
0185 
0186 [sp, ep] = slnums2bounds(nums);
0187 
0188 % compute group weights
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 % compute with reshape
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

Contact us at files@mathworks.com