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 slgausscomb
Home > sltoolbox > stat > slgausscomb.m

slgausscomb

PURPOSE ^

SLGAUSSCOMB Collects the means and variances/covariances to form GS

SYNOPSIS ^

function GS = slgausscomb(varargin)

DESCRIPTION ^

SLGAUSSCOMB Collects the means and variances/covariances to form GS

 $ Syntax $
   - GS = slgausscomb('means', means, 'vars', vars, ...)
   - GS = slgausscomb('means', means, 'covs', covs, ...)

 $ Arguments $
   - means:        the mean vectors
   - vars:         the variance values
   - covs:         the covariance matrices
   - GS:           the formed Gaussian model struct

 $ Description $
   - GS = slgausscomb('means', means, 'vars', vars) forms the Gaussian 
     model struct with varform being 'univar' or 'diagvar' using the mean
     vectors and variance values.
     The means can be given in either of the following forms:
       - a d x k matrix
       - a cell array with k cells, each cell being a d x 1 vector
     The vars can be given in either of the following forms:
       - a 1 x 1 scalar: for shared univar model
       - a 1 x k vector: for non-shared univar model
       - a d x 1 vector: for shared diagvar model
       - a d x k matrix: for non-shared diagvar model
       - a cell array of k scalars: for non-shared univar model
       - a cell array of dx1 vectors: for non-shared diagvar model

   - GS = slgausscomb('means', means, 'covs', covs) forms the Gaussian
     model struct with varform being 'covar' using the mean vector and
     covariance matrices.
     The form of means is as mentioned above.
     The covs can be given in either of the following forms:
       - a d x d matrix: for shared covar model
       - a d x d x k matrix: for non-shared covar model
       - a cell array of k dxd matrices: for non-shared covar model

     You can specify other properties to control the process
       - 'invparams'  the cell array of parameters to compute inverse
                      (default = {})
                      For invvars, the computation is done by 
                      slinvevals;
                      For invcovs, the computation is done by
                      slinvcovs;
       - 'compinv'    whether to compute the inverse covariances
                      (default = true)
       - 'mixweights' the mixture weights (default = [])                        

 $ Remarks $
   - You can specify either covs or vars, but you should not specify
     both of them.

 $ History $
   - Created by Dahua Lin, on Aug 24th, 2006

CROSS-REFERENCE INFORMATION ^

This function calls:
  • slgaussinv SLGAUSSINV Computes the inverse of variance/covariance in Gaussian model
  • slparseprops SLPARSEPROPS Parses input parameters
This function is called by:

SUBFUNCTIONS ^

SOURCE CODE ^

0001 function GS = slgausscomb(varargin)
0002 %SLGAUSSCOMB Collects the means and variances/covariances to form GS
0003 %
0004 % $ Syntax $
0005 %   - GS = slgausscomb('means', means, 'vars', vars, ...)
0006 %   - GS = slgausscomb('means', means, 'covs', covs, ...)
0007 %
0008 % $ Arguments $
0009 %   - means:        the mean vectors
0010 %   - vars:         the variance values
0011 %   - covs:         the covariance matrices
0012 %   - GS:           the formed Gaussian model struct
0013 %
0014 % $ Description $
0015 %   - GS = slgausscomb('means', means, 'vars', vars) forms the Gaussian
0016 %     model struct with varform being 'univar' or 'diagvar' using the mean
0017 %     vectors and variance values.
0018 %     The means can be given in either of the following forms:
0019 %       - a d x k matrix
0020 %       - a cell array with k cells, each cell being a d x 1 vector
0021 %     The vars can be given in either of the following forms:
0022 %       - a 1 x 1 scalar: for shared univar model
0023 %       - a 1 x k vector: for non-shared univar model
0024 %       - a d x 1 vector: for shared diagvar model
0025 %       - a d x k matrix: for non-shared diagvar model
0026 %       - a cell array of k scalars: for non-shared univar model
0027 %       - a cell array of dx1 vectors: for non-shared diagvar model
0028 %
0029 %   - GS = slgausscomb('means', means, 'covs', covs) forms the Gaussian
0030 %     model struct with varform being 'covar' using the mean vector and
0031 %     covariance matrices.
0032 %     The form of means is as mentioned above.
0033 %     The covs can be given in either of the following forms:
0034 %       - a d x d matrix: for shared covar model
0035 %       - a d x d x k matrix: for non-shared covar model
0036 %       - a cell array of k dxd matrices: for non-shared covar model
0037 %
0038 %     You can specify other properties to control the process
0039 %       - 'invparams'  the cell array of parameters to compute inverse
0040 %                      (default = {})
0041 %                      For invvars, the computation is done by
0042 %                      slinvevals;
0043 %                      For invcovs, the computation is done by
0044 %                      slinvcovs;
0045 %       - 'compinv'    whether to compute the inverse covariances
0046 %                      (default = true)
0047 %       - 'mixweights' the mixture weights (default = [])
0048 %
0049 % $ Remarks $
0050 %   - You can specify either covs or vars, but you should not specify
0051 %     both of them.
0052 %
0053 % $ History $
0054 %   - Created by Dahua Lin, on Aug 24th, 2006
0055 %
0056 
0057 %% Take arguments
0058 
0059 args.means = [];
0060 args.vars = [];
0061 args.covs = [];
0062 args.compinv = true;
0063 args.invparams = {};
0064 args.mixweights = [];
0065 args = slparseprops(args, varargin{:});
0066 
0067 
0068 if isempty(args.means)
0069     error('sltoolbox:invalidarg', ...
0070         'The means should be specified');
0071 end
0072 
0073 if isempty(args.vars) && isempty(args.covs)
0074     error('sltoolbox:invalidarg', ...
0075         'You should specify either vars or covs');
0076 end
0077 
0078 if ~isempty(args.vars) && ~isempty(args.covs)
0079     error('sltoolbox:invalidarg', ...
0080         'You should specify both vars and covs');
0081 end
0082 
0083 
0084 %% Parse means
0085 
0086 means = take_arrayform('means', args.means, 2);
0087 [d, k] = size(means);
0088 
0089 GS.dim = d;
0090 GS.nmodels = k;
0091 GS.means = means;
0092 
0093 
0094 %% Parse variances / covariances
0095 
0096 if ~isempty(args.vars)
0097 
0098     vars = take_arrayform('vars', args.vars, 2);
0099     [dv, kv] = size(vars);
0100     
0101     if dv ~= 1 && dv ~= d
0102         error('sltoolbox:sizmismatch', ...
0103             'The size of vars is illegal');
0104     end    
0105     if kv ~= 1 && kv ~= k
0106         error('sltoobox:sizmismatch', ...
0107             'The size of vars is illegal');
0108     end        
0109     
0110     GS.vars = vars;
0111     if args.compinv
0112         GS.invvars = slgaussinv(GS, 'vars', args.invparams);
0113     end
0114             
0115 else
0116    
0117     covs = take_arrayform('covs', args.covs, 3);
0118     [dcv, dcv2, kcv] = size(covs);
0119     
0120     if dcv ~= d || dcv2 ~= d
0121        error('sltoolbox:sizmismatch', ...
0122            'The size of covs is illegal');
0123     end
0124     
0125     if kcv ~= 1 && kcv ~= k
0126         error('sltoolbox:sizmismatch', ...
0127             'The size of covs is illegal');
0128     end
0129     
0130     GS.covs = covs;    
0131     if args.compinv
0132         GS.invcovs = slgaussinv(GS, 'covs', args.invparams);
0133     end
0134                     
0135 end
0136 
0137 %% For mix weights
0138 
0139 if ~isempty(args.mixweights)
0140     
0141     mixweights = args.mixweights(:);
0142     if length(mixweights) ~= k
0143         error('sltoolbox:sizmismatch', ...
0144             'The length of mix weights is illegal');
0145     end
0146     
0147     GS.mixweights = mixweights;
0148     
0149 end
0150 
0151     
0152 
0153 
0154 %% Auxiliary functions
0155 
0156 function V = take_arrayform(name, v, dmax)
0157 
0158 if isnumeric(v)
0159     V = v;
0160 elseif iscell(v)
0161     V = v(:)';
0162     if dmax == 2
0163         V = horzcat(V{:});
0164     elseif dmax == 3
0165         V = cat(3, V{:});
0166     end
0167 else
0168     error('sltoolbox:invalidarg', ...
0169         'The %s should be either an numeric array or a cell array', name);
0170 end
0171 
0172 if ndims(V) > dmax
0173     error('sltoolbox:invalidarg', ...
0174         'The dimension of means should not exceed %d', dmax);
0175 end
0176 
0177 
0178 
0179 
0180 
0181 
0182 
0183 
0184 
0185 
0186

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

Contact us at files@mathworks.com