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 slscatter
Home > sltoolbox > subspace > slscatter.m

slscatter

PURPOSE ^

SLSCATTER Compute the scatter matrix

SYNOPSIS ^

function S = slscatter(X, type, varargin)

DESCRIPTION ^

SLSCATTER Compute the scatter matrix 

 $ Syntax $
   - S = slscatter(X, type, ...)

 $ Arguments $
   - X:        the sample matrix with each column representing a sample
   - type:     the type of scatter matrix to compute
   - S:        the resulting scatter matrix 

 $ Description $
   - S = slscatter(X, type, ...) computes the scatter matrix for the 
     samples in X of specific type according to the properties specified.
     \*
     \t    Table 1. The types of scatter matrix          \\
     \h    name   &     description                      \\
           'Sw'   &  Within class scatter matrix. That is to compute
                     scatter matrix within the samples in each class, and
                     sum up the class-specific scatters. \\
           'Sb'   &  Between class scatter matrix. That is to compute
                     scatter matrix on the means of the classes. And uses
                     the number of samples in each class or the total
                     sample weight for each class as the weight for
                     the mean vectors.                   \\
           'St'   &  Total scatter matrix. That is to compute the scatter
                     matrix on all the samples. The class-specific info.
                     is ignored for St.                  \\
     \*
     
     You can specify following properties to customize the computation
     of the scatter matrix.
     \*
     \t    Table 2. The properties of scatter computation       \\
     \h    name      &      description                         \\
          'method'   &  The method of computation, can be 'std' or 'pw'.
                        'std' computes the scatter in standard manner
                        S = sum_i w_i (x_i - mv)(x_i - m_v)';
                        'pw' computes the scatter in pairwise manner
                        S = sum_{ij} w_{ij} (x_i - x_j) (x_i - x_j)'; 
                        default = [].                           \\
          'nums'     &  The numbers of samples in all classes. These 
                        numbers are used for groupping the samples for
                        'Sw' and 'Sb'. It is ignored for 'St'. If 
                        it is set to empty, all samples are considered
                        from the same class. default = [].      \\
          'sweights' &  The weights of samples. Suppose there are n
                        samples. Then sweights should be an 1 x n row
                        vector. default = [].                   \\
          'dwrule'   &  The rule for determining the weights from 
                        distances. It should be an invokable object 
                        determining the weights from distances either
                        from samples to corresponding class centers
                        for 'std' method or between samples of pairs 
                        for 'pw' method. default = {}.           \\
     \*

 $ Remarks $
   -# The invokable object for determining weights from distances has
      two forms. In the non-parametric form, it can be a function name,
      function handle, or an inline object, the array of distances would 
      be the unique argument input. In the parametric form, it
      is given in a cell {f, ...}, f can be a function name, function
      handle, or an inline object. The array of distances followed by
      the additonal arguments in the cell will be input to f. 
           
   -# For weighting, if not specified all weights are considered to be 1.
      In 'std' method, the weights w_i for each term is given by 
      the multiplication of sweights(i) and the weights determined by
      dwrule. In 'pw' method, the weights w_{ij} for each term is 
      given by sweights(i) * sweights(j) * the value determined by dwrule.

   -# The computation in 'std' method is based on slcov and its result
      will be scaled by n or total weight. The computation in 'pw' method
      is based on slpwscatter.

 $ History $
   - Created by Dahua Lin on Apr 27, 2005

CROSS-REFERENCE INFORMATION ^

This function calls:
  • slmetric_pw SLMETRIC_PW Compute the metric between column vectors pairwisely
  • slcov SLCOV Compute the covariance matrix
  • slmean SLMEAN Compute the mean vector of samples
  • slmeans SLMEANS Compute the mean vectors
  • slpwscatter SLPWSCATTER Compute the pairwise scatter matrix
  • raise_lackinput RAISE_LACKINPUT Raises an error indicating lack of input argument
  • slnums2bounds SLNUMS2BOUNDS Compute the index-boundaries from section sizes
  • slparseprops SLPARSEPROPS Parses input parameters
This function is called by:
  • slkernelscatter SLKERNELSCATTER Compute the kernelized scatter matrix
  • slkfd SLKFD Perform Kernelized Fisher Discriminant Analysis
  • sldlda SLDLDA Performs Direct Linear Discriminant Analysis
  • slfld SLFLD Performs Fisher Linear Discriminant Analysis
  • slnlda SLNLDA Performs Nullspace-based Linear Discriminant Analysis

SUBFUNCTIONS ^

SOURCE CODE ^

0001 function S = slscatter(X, type, varargin)
0002 %SLSCATTER Compute the scatter matrix
0003 %
0004 % $ Syntax $
0005 %   - S = slscatter(X, type, ...)
0006 %
0007 % $ Arguments $
0008 %   - X:        the sample matrix with each column representing a sample
0009 %   - type:     the type of scatter matrix to compute
0010 %   - S:        the resulting scatter matrix
0011 %
0012 % $ Description $
0013 %   - S = slscatter(X, type, ...) computes the scatter matrix for the
0014 %     samples in X of specific type according to the properties specified.
0015 %     \*
0016 %     \t    Table 1. The types of scatter matrix          \\
0017 %     \h    name   &     description                      \\
0018 %           'Sw'   &  Within class scatter matrix. That is to compute
0019 %                     scatter matrix within the samples in each class, and
0020 %                     sum up the class-specific scatters. \\
0021 %           'Sb'   &  Between class scatter matrix. That is to compute
0022 %                     scatter matrix on the means of the classes. And uses
0023 %                     the number of samples in each class or the total
0024 %                     sample weight for each class as the weight for
0025 %                     the mean vectors.                   \\
0026 %           'St'   &  Total scatter matrix. That is to compute the scatter
0027 %                     matrix on all the samples. The class-specific info.
0028 %                     is ignored for St.                  \\
0029 %     \*
0030 %
0031 %     You can specify following properties to customize the computation
0032 %     of the scatter matrix.
0033 %     \*
0034 %     \t    Table 2. The properties of scatter computation       \\
0035 %     \h    name      &      description                         \\
0036 %          'method'   &  The method of computation, can be 'std' or 'pw'.
0037 %                        'std' computes the scatter in standard manner
0038 %                        S = sum_i w_i (x_i - mv)(x_i - m_v)';
0039 %                        'pw' computes the scatter in pairwise manner
0040 %                        S = sum_{ij} w_{ij} (x_i - x_j) (x_i - x_j)';
0041 %                        default = [].                           \\
0042 %          'nums'     &  The numbers of samples in all classes. These
0043 %                        numbers are used for groupping the samples for
0044 %                        'Sw' and 'Sb'. It is ignored for 'St'. If
0045 %                        it is set to empty, all samples are considered
0046 %                        from the same class. default = [].      \\
0047 %          'sweights' &  The weights of samples. Suppose there are n
0048 %                        samples. Then sweights should be an 1 x n row
0049 %                        vector. default = [].                   \\
0050 %          'dwrule'   &  The rule for determining the weights from
0051 %                        distances. It should be an invokable object
0052 %                        determining the weights from distances either
0053 %                        from samples to corresponding class centers
0054 %                        for 'std' method or between samples of pairs
0055 %                        for 'pw' method. default = {}.           \\
0056 %     \*
0057 %
0058 % $ Remarks $
0059 %   -# The invokable object for determining weights from distances has
0060 %      two forms. In the non-parametric form, it can be a function name,
0061 %      function handle, or an inline object, the array of distances would
0062 %      be the unique argument input. In the parametric form, it
0063 %      is given in a cell {f, ...}, f can be a function name, function
0064 %      handle, or an inline object. The array of distances followed by
0065 %      the additonal arguments in the cell will be input to f.
0066 %
0067 %   -# For weighting, if not specified all weights are considered to be 1.
0068 %      In 'std' method, the weights w_i for each term is given by
0069 %      the multiplication of sweights(i) and the weights determined by
0070 %      dwrule. In 'pw' method, the weights w_{ij} for each term is
0071 %      given by sweights(i) * sweights(j) * the value determined by dwrule.
0072 %
0073 %   -# The computation in 'std' method is based on slcov and its result
0074 %      will be scaled by n or total weight. The computation in 'pw' method
0075 %      is based on slpwscatter.
0076 %
0077 % $ History $
0078 %   - Created by Dahua Lin on Apr 27, 2005
0079 %
0080 
0081 %% parse and verify input arguments
0082 
0083 if nargin < 2
0084     raise_lackinput('slscatter', 2);
0085 end
0086 
0087 % check sample matrix X
0088 
0089 if ndims(X) ~= 2
0090     error('sltoolbox:invaliddims', ...
0091         'The sample matrix X should be a 2D matrix');
0092 end
0093 [d, n] = size(X);
0094 
0095 % check type
0096 
0097 if ~ismember(type, ...
0098         {'Sw', 'Sb', 'St'})
0099     error('sltoolbox:invalidarg', ...
0100         'Invalid type of scatter matrix: %s', type);
0101 end
0102 
0103 % check options
0104 
0105 opts.method = 'std';
0106 opts.nums = [];
0107 opts.sweights = [];
0108 opts.dwrule = {};
0109 
0110 opts = slparseprops(opts, varargin{:});
0111 
0112 switch opts.method
0113     case 'std'
0114         fh_scatter_core = @scatter_core_std;
0115         fh_get_weights = @get_weights_std;
0116     case 'pw'
0117         fh_scatter_core = @scatter_core_pw;
0118         fh_get_weights = @get_weights_pw;
0119     otherwise
0120         error('sltoolbox:invalidarg', ...
0121         'Invalid method for scatter computing: %s', opts.method);
0122 end
0123         
0124     
0125 if isempty(opts.nums)
0126     k = 1;
0127     opts.nums = n;
0128 else
0129     k = length(opts.nums);
0130     if ~isequal(size(opts.nums), [1, k])
0131         error('sltoolbox:invalidarg', ...
0132             'nums should be an 1 x k row vector');
0133     end
0134     if sum(opts.nums) ~= n
0135         error('sltoolbox:sizmismatch', ...
0136             'The total number is consistent with that in X');
0137     end
0138 end
0139 
0140 if ~isempty(opts.sweights) 
0141     if ~isequal(size(opts.sweights), [1, n])
0142         error('sltoolbox:invalidarg', ...
0143             'sample weights should be an 1 x n row vector.');
0144     end
0145 end
0146     
0147 
0148 %% Compute
0149 
0150 switch type
0151     
0152 %% Compute Sw
0153     case 'Sw'        
0154         if k == 1   % single class
0155             w = fh_get_weights(X, opts.sweights, opts.dwrule);
0156             S = fh_scatter_core(X, w);
0157         else        % multiple classes
0158             S = zeros(d, d);
0159             [sp, ep] = slnums2bounds(opts.nums);
0160             for i = 1 : k
0161                 [curX, curw] = get_cur_class(X, sp(i), ep(i), opts, fh_get_weights);
0162                 S = S + fh_scatter_core(curX, curw);
0163             end
0164         end                
0165         
0166 %% Compute Sb
0167     case 'Sb'
0168         if k == 1   % single class (no between class scattering)
0169             S = zeros(d, d);
0170         else        % multiple classes
0171             centers = slmeans(X, opts.sweights, opts.nums);
0172             [sp, ep] = slnums2bounds(opts.nums);
0173             sw = get_class_weights(opts.sweights, opts.nums, sp, ep);
0174             w = fh_get_weights(centers, sw, opts.dwrule);
0175             S = fh_scatter_core(centers, w);
0176         end    
0177                         
0178 %% Compute St
0179     case 'St'        
0180         w = fh_get_weights(X, opts.sweights, opts.dwrule);
0181         S = fh_scatter_core(X, w);                
0182         
0183 end
0184         
0185         
0186 
0187 
0188 %% The function for computing weights
0189 function w = get_weights_std(X, sweights, dwrule)
0190 
0191 if isempty(sweights)    
0192     if isempty(dwrule)      % default weights
0193         w = [];        
0194     else                    % weights from dwrule
0195         mv = slmean(X, [], true);
0196         dists = slmetric_pw(mv, X, 'eucdist');
0197         w = invoke_dwrule(dwrule, dists);
0198     end        
0199 else
0200     if isempty(dwrule)      % weights from sweights
0201         w = sweights;
0202     else                    % both sweights and dwrule
0203         mv = slmean(X, sweights, true);
0204         dists = slmetric_pw(mv, X, 'eucdist');
0205         w = invoke_dwrule(dwrule, dists) .* sweights;
0206     end
0207 end
0208 
0209        
0210 function w = get_weights_pw(X, sweights, dwrule)
0211 
0212 if isempty(sweights)
0213     if isempty(dwrule)      % default weights
0214         w = [];
0215     else                    % weights from dwrule
0216         dists = slmetric_pw(X, X, 'eucdist');
0217         w = invoke_dwrule(dwrule, dists);
0218     end
0219 else
0220     if isempty(dwrule)      % weights from sweights
0221         w = sweights' * sweights;   
0222     else                    % both sweights and dwrule
0223         w = sweights' * sweights;
0224         dists = slmetric_pw(X, X, 'eucdist');
0225         w = invoke_dwrule(dwrule, dists) .* w;
0226     end
0227 end
0228         
0229         
0230 %% The auxiliary function to extracting a class of samples and weights
0231 function [curX, curw] = get_cur_class(X, sp, ep, opts, fhgetw)
0232 
0233 curX = X(:, sp:ep);
0234 if isempty(opts.sweights)
0235     sw = [];
0236 else
0237     sw = opts.sweights(sp:ep);
0238 end
0239 curw = fhgetw(curX, sw, opts.dwrule);
0240 
0241 
0242 %% The auxiliary function to getting the class weights
0243 function w = get_class_weights(sweights, nums, sp, ep)
0244 
0245 if isempty(sweights)
0246     w = nums;
0247 else
0248     k = length(nums);    
0249     w = zeros(1, k);
0250     for i = 1 : k
0251         w(i) = sum(sweights(sp(i):ep(i)));
0252     end
0253 end
0254     
0255 
0256 
0257 %% The core computing function for compute a single scattering
0258 
0259 function S = scatter_core_std(X, w)
0260 
0261 if isempty(w)
0262     n = size(X, 2);
0263     S = slcov(X) * n;
0264 else
0265     tw = sum(w);
0266     S = slcov(X, w) * tw;
0267 end
0268 
0269 
0270 function S = scatter_core_pw(X, w)
0271 
0272 S = slpwscatter(X, w);
0273 
0274 
0275         
0276 %% The auxiliary function for computing weights from dwrule
0277 function w = invoke_dwrule(dwrule, dists)
0278 
0279 if ~iscell(dwrule)
0280     w = feval(dwrule, dists);
0281 else
0282     if length(dwrule) == 1
0283         w = feval(dwrule{1}, dists);
0284     else
0285         w = feval(dwrule{1}, dists, dwrule{2:end});
0286     end
0287 end
0288 
0289

Generated on Wed 20-Sep-2006 12:43:11 by m2html © 2003

Contact us at files@mathworks.com