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

slpcareduce

PURPOSE ^

SLPCAREDUCE Reduces a PCA model to lower dimension

SYNOPSIS ^

function S = slpcareduce(S, cri, varargin)

DESCRIPTION ^

SLPCAREDUCE Reduces a PCA model to lower dimension

 $ Syntax $
   - S = slpcareduce(S, cri, ...)

 $ Arguments $
   - S:        the target PCA model
   - cri:      the criterion for PCA model reduction

 $ Description $
   - S = slpcareduce(S, cri, ...) reduces a PCA model by taking a subset
     of principal components. cri can be a vector of selected indices of 
     principal components, or a string representing the preset, 
     reduction scheme, or a function handle of for selection. The 
     user-supplied selection function takes a PCA model as input arg,
     and return the indices of retained components. Additional arguments
     are the feed to the criterion or user-selection function following
     the PCA model.
     \*
     \t    Table 1.  The preset selection schemes   \\
     \h     name     &      description             \\
           'num'     & Select the first n components: use n as the 
                       first argument.
           'energy'  & Select the smallest number of components, so
                       that the ratio of the preserved energy to the
                       total energy is not less than the specified 
                       ratio r, which is supplied as the first argument.
                       Note that if the energy preservation of original
                       PCA model is lower than r, an error will be raised.
           'eigval'  & Select all components of which the ratio to the
                       maximum eigenvalue is above a specified ratio r,
                       which is supplied as the first argument.                        

 $ Remarks $
   1. The energy of the discarded components in the reduction will be
      added to the residue.

 $ History $
   - Created by Dahua Lin on Apr 24, 2006
   - Modified by Dahua Lin on Aug 17, 2006
       - Add a field energyratio

CROSS-REFERENCE INFORMATION ^

This function calls:
  • raise_lackinput RAISE_LACKINPUT Raises an error indicating lack of input argument
This function is called by:

SUBFUNCTIONS ^

SOURCE CODE ^

0001 function S = slpcareduce(S, cri, varargin)
0002 %SLPCAREDUCE Reduces a PCA model to lower dimension
0003 %
0004 % $ Syntax $
0005 %   - S = slpcareduce(S, cri, ...)
0006 %
0007 % $ Arguments $
0008 %   - S:        the target PCA model
0009 %   - cri:      the criterion for PCA model reduction
0010 %
0011 % $ Description $
0012 %   - S = slpcareduce(S, cri, ...) reduces a PCA model by taking a subset
0013 %     of principal components. cri can be a vector of selected indices of
0014 %     principal components, or a string representing the preset,
0015 %     reduction scheme, or a function handle of for selection. The
0016 %     user-supplied selection function takes a PCA model as input arg,
0017 %     and return the indices of retained components. Additional arguments
0018 %     are the feed to the criterion or user-selection function following
0019 %     the PCA model.
0020 %     \*
0021 %     \t    Table 1.  The preset selection schemes   \\
0022 %     \h     name     &      description             \\
0023 %           'num'     & Select the first n components: use n as the
0024 %                       first argument.
0025 %           'energy'  & Select the smallest number of components, so
0026 %                       that the ratio of the preserved energy to the
0027 %                       total energy is not less than the specified
0028 %                       ratio r, which is supplied as the first argument.
0029 %                       Note that if the energy preservation of original
0030 %                       PCA model is lower than r, an error will be raised.
0031 %           'eigval'  & Select all components of which the ratio to the
0032 %                       maximum eigenvalue is above a specified ratio r,
0033 %                       which is supplied as the first argument.
0034 %
0035 % $ Remarks $
0036 %   1. The energy of the discarded components in the reduction will be
0037 %      added to the residue.
0038 %
0039 % $ History $
0040 %   - Created by Dahua Lin on Apr 24, 2006
0041 %   - Modified by Dahua Lin on Aug 17, 2006
0042 %       - Add a field energyratio
0043 %
0044 
0045 %% parse and verify input arguments
0046 
0047 if nargin < 2
0048     raise_lackinput('slpcareduce', 2);
0049 end
0050 
0051 inds_selected = [];  % the selected index
0052 if isnumeric(cri)
0053     inds_selected = cri;
0054     
0055 elseif ischar(cri)
0056     switch cri
0057         case 'num'
0058             fh_select = @pcselect_num;
0059         case 'energy'
0060             fh_select = @pcselect_energy;
0061         case 'eigval'
0062             fh_select = @pcselect_eigval;
0063         otherwise
0064             error('sltoolbox:invalidarg', ...
0065                 'Invalid selection scheme %s for PCA reduction', cri);
0066     end
0067     
0068 elseif isa(cri, 'function_handle')
0069     fh_select = cri;
0070     
0071 else
0072     error('sltoolbox:invalidarg', ...
0073         'cri should be selected indices, a scheme name or the handle to the selection function');
0074 end
0075 
0076 %% select the components
0077 
0078 if isempty(inds_selected)  % not selected yet
0079     inds_selected = fh_select(S, varargin{:});        
0080 end
0081 RP = S.P(:, inds_selected);
0082 
0083 
0084 %% reduce the PCA model according to the selection
0085 
0086 S.feadim = length(inds_selected);
0087 S.P = RP;
0088 
0089 discarded_eigvals = S.eigvals;
0090 discarded_eigvals(inds_selected) = [];
0091 S.eigvals = S.eigvals(inds_selected);
0092 
0093 S.residue = S.residue + sum(discarded_eigvals);
0094 
0095 prinenergy = sum(S.eigvals);
0096 S.energyratio = prinenergy / (prinenergy + S.residue);
0097 
0098 %% Preset user selection criteria (scheme)
0099 
0100 function inds = pcselect_num(S, n)
0101 
0102 if n > S.feadim
0103     error('sltoolbox:valueexceed', ...
0104         'n is larger than the number of components preserved in the input model');
0105 end
0106 
0107 inds = (1:n)';
0108 
0109 function inds = pcselect_energy(S, r)
0110 
0111 pe = sum(S.eigvals);          % preserved energy in input model
0112 te = pe + S.residue;            % total energy
0113 ub_r = pe / te;               % upper bound on r
0114 
0115 if r > ub_r
0116     error('sltoolbox:valueexceed', ...
0117         'r is larger than the ratio of energy preserved in the input model');
0118 end
0119 
0120 eb = te * r;
0121 ce = cumsum(S.eigvals);
0122 n = find(ce >= eb, 1);
0123 if isempty(n)
0124     n = S.feadim;
0125 end
0126 
0127 inds = (1:n)';
0128 
0129 function inds = pcselect_eigval(S, r)
0130 
0131 if r < 0 || r >= 1
0132     error('sltoolbox:invalidarg', ...
0133         'It should be met that 0 < r <= 1 for eigenvalue ratio based selection');
0134 end
0135 
0136 n = sum(S.eigvals > r * S.eigvals(1));
0137 
0138 inds = (1:n)';
0139 
0140

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

Contact us at files@mathworks.com