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

slregcov

PURPOSE ^

SLREGCOV Regularizes covariance matrices

SYNOPSIS ^

function Crs = slregcov(Cs, varargin)

DESCRIPTION ^

SLREGCOV Regularizes covariance matrices

 $ Syntax $
   - Crs = slregcov(Cs, ...)

 $ Description $
   - Crs = slregcov(Cs, ...) regularizes the covariance matrix in Cs
   the output the regularized covariance matrix by Crs. You can specify
   additional properties to control the process of regularization

   The regularization follows the following formula
              (1 - lambda) * P * C + lambda * Cpool
        C1 = ------------------------------------------
               (1 - lambda) * P + lambda

        Cr = (1 - gamma) * C1 + gamma * (tr(C1)/d) * I 

   \t  Table:  Properties of Covariance Regularization
   \h  property name  &  property description
       'lambda'          the lambda coefficient (0 <= lambda <= 1), default = 0
       'gamma'           the gamma coefficient (0 <= gamma <= 1), default = 0
       'prior'           the priori (by default they are set to equal)
       'poolcov'         the pool covariance matrix
       
 $ Remarks $
   - If poolcov is not given, it will be computed by averaging the
     covariance matrix, weighted by prior.
 
 $ History $
   - Created by Dahua Lin on Dec 19th, 2005

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function Crs = slregcov(Cs, varargin)
0002 %SLREGCOV Regularizes covariance matrices
0003 %
0004 % $ Syntax $
0005 %   - Crs = slregcov(Cs, ...)
0006 %
0007 % $ Description $
0008 %   - Crs = slregcov(Cs, ...) regularizes the covariance matrix in Cs
0009 %   the output the regularized covariance matrix by Crs. You can specify
0010 %   additional properties to control the process of regularization
0011 %
0012 %   The regularization follows the following formula
0013 %              (1 - lambda) * P * C + lambda * Cpool
0014 %        C1 = ------------------------------------------
0015 %               (1 - lambda) * P + lambda
0016 %
0017 %        Cr = (1 - gamma) * C1 + gamma * (tr(C1)/d) * I
0018 %
0019 %   \t  Table:  Properties of Covariance Regularization
0020 %   \h  property name  &  property description
0021 %       'lambda'          the lambda coefficient (0 <= lambda <= 1), default = 0
0022 %       'gamma'           the gamma coefficient (0 <= gamma <= 1), default = 0
0023 %       'prior'           the priori (by default they are set to equal)
0024 %       'poolcov'         the pool covariance matrix
0025 %
0026 % $ Remarks $
0027 %   - If poolcov is not given, it will be computed by averaging the
0028 %     covariance matrix, weighted by prior.
0029 %
0030 % $ History $
0031 %   - Created by Dahua Lin on Dec 19th, 2005
0032 %
0033 
0034 %% verify and parse input arguments
0035 [d1, d2, k] = size(Cs);
0036 if d1 ~= d2
0037     error('sltoolbox:notsquaremat', ...
0038         'The covariance matrices should be square');
0039 end
0040 d = d1;
0041 S.lambda = 0;
0042 S.gamma = 0;
0043 S.prior = [];
0044 S.poolcov = [];
0045 S = slparseprops(S, varargin{:});
0046 if isempty(S.prior)
0047     ispriorgiven = false;
0048     S.prior = ones(1, k) / k;
0049 else
0050     ispriorgiven = true;
0051     S.prior = S.prior(:)';
0052     if length(S.prior) ~= k
0053         error('sltoolbox:sizmismatch', ...
0054             'The length of prior is not consistent with the number of covariances');
0055     end
0056     S.prior = S.prior / sum(S.prior);
0057 end
0058 if S.lambda < 0 || S.lambda > 1
0059     error('sltoolbox:invalidarg', ...
0060         'lambda should be within [0, 1]');
0061 end
0062 if S.gamma < 0 || S.gamma > 1
0063     error('sltoolbox:invalidarg', ...
0064         'gamma should be within [0, 1]');
0065 end
0066 if ~isempty(S.poolcov)
0067     ispoolgiven = true;
0068     if ~isequal(size(S.poolcov), [d d]);
0069         error('sltoolbox:invalidarg', ...
0070             'The size of pooled covariance is illegal');
0071     end
0072 else
0073     ispoolgiven = false;
0074 end
0075 
0076 
0077 %% get the pool covariance
0078 if ~ispoolgiven
0079     if ~ispriorgiven
0080         S.poolcov = slpoolcov(Cs);
0081     else
0082         S.poolcov = slpoolcov(Cs, S.prior);
0083     end
0084 end
0085 
0086 
0087 %% lambda-regularize
0088 lambda = S.lambda;
0089 if lambda == 0
0090     Crs = Cs;
0091 elseif lambda == 1
0092     Crs = zeros(size(Cs));
0093     for i = 1 : k
0094         Crs(:, :, i) = S.poolcov;
0095     end
0096 else
0097     Crs = zeros(size(Cs));
0098     coeffsL = zeros(2, k);
0099     coeffsL(1, :) = (1 - lambda) * S.prior;
0100     coeffsL(2, :) = lambda;
0101     coeffsL = slnormalize(coeffsL, 1, 1);
0102     for i = 1 : k
0103         Crs(:,:,i) = coeffsL(1, i) * Cs(:,:,i) + coeffsL(2, i) * S.poolcov;
0104     end
0105 end
0106 
0107 
0108 %% gamma-regularize
0109 gamma = S.gamma;
0110 if gamma == 0
0111     % nothing to do
0112 elseif gamma == 1
0113     for i = 1 : k 
0114         avgtr = trace(Crs(:,:,i)) / d;
0115         Crs(:,:,i) = avgtr * eye(d, d);
0116     end
0117 else
0118     for i = 1 : k
0119         C0 = Crs(:,:,i);
0120         avgtr = trace(C0) / d;
0121         Crs(:,:,i) = (1-gamma) * C0 + gamma * avgtr * eye(d, d);
0122     end
0123 end
0124 
0125

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

Contact us at files@mathworks.com