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

slinvcov

PURPOSE ^

SLINVCOV Compute the inverse of an covariance matrix

SYNOPSIS ^

function R = slinvcov(C, method, r)

DESCRIPTION ^

SLINVCOV Compute the inverse of an covariance matrix

 $ Syntax $
   - R = slinvcov(C)
   - R = slinvcov(C, method, r)

 $ Arguments $
   - C:        the covariance matrix (matrices)
   - method:   the method of inverse calculation
   - r:        the additional parameter for computation
   - R:        the computed inverse matrix

 $ Description $
   - R = slinvcov(C) computes the inverse of C using default method. If
     C is d x d x ... array, then R would be an array of the same size.
     Each page of R is the inverse matrix of the corresponding covariance
     matrix in C.

   - R = slinvcov(C, method, r) computes the inverse of C using specific
     method. For some method, extra parameters are needed, which can be
     given subsequently. The method can be either 'direct', i.e. directly
     invoke inv for inverse computing or the names specified in
     slinvevals. For the latter cases, we adopt the following formula
     for computation:
       C^(-1) = U * diag(1 ./ evals) * U^T
     while the reciprocals of eigenvalues are computed in a robust way
     by the methods available in slinvevals. The default method is
     'direct'.

 $ Remarks $
   - C should be a symmetric and positive semidefinite matrix.

 $ History $
   - Created by Dahua Lin on Apr 22, 2006
   - Modified by Dahua Lin on Apr 30, 2006
     - Base on the slinvevals function to eigenvalue processing.
     - Re-organize the code in a clearer way

CROSS-REFERENCE INFORMATION ^

This function calls:
  • slsymeig SLSYMEIG Compute the eigenvalues and eigenvectors for symmetric matrix
  • slinvevals SLINVEVALS Compute the reciprocals of eigenvalues in a robust way
This function is called by:
  • sllinreg SLLINREG Performs Multivariate Linear Regression and Ridge Regression
  • slgaussinv SLGAUSSINV Computes the inverse of variance/covariance in Gaussian model

SUBFUNCTIONS ^

SOURCE CODE ^

0001 function R = slinvcov(C, method, r)
0002 %SLINVCOV Compute the inverse of an covariance matrix
0003 %
0004 % $ Syntax $
0005 %   - R = slinvcov(C)
0006 %   - R = slinvcov(C, method, r)
0007 %
0008 % $ Arguments $
0009 %   - C:        the covariance matrix (matrices)
0010 %   - method:   the method of inverse calculation
0011 %   - r:        the additional parameter for computation
0012 %   - R:        the computed inverse matrix
0013 %
0014 % $ Description $
0015 %   - R = slinvcov(C) computes the inverse of C using default method. If
0016 %     C is d x d x ... array, then R would be an array of the same size.
0017 %     Each page of R is the inverse matrix of the corresponding covariance
0018 %     matrix in C.
0019 %
0020 %   - R = slinvcov(C, method, r) computes the inverse of C using specific
0021 %     method. For some method, extra parameters are needed, which can be
0022 %     given subsequently. The method can be either 'direct', i.e. directly
0023 %     invoke inv for inverse computing or the names specified in
0024 %     slinvevals. For the latter cases, we adopt the following formula
0025 %     for computation:
0026 %       C^(-1) = U * diag(1 ./ evals) * U^T
0027 %     while the reciprocals of eigenvalues are computed in a robust way
0028 %     by the methods available in slinvevals. The default method is
0029 %     'direct'.
0030 %
0031 % $ Remarks $
0032 %   - C should be a symmetric and positive semidefinite matrix.
0033 %
0034 % $ History $
0035 %   - Created by Dahua Lin on Apr 22, 2006
0036 %   - Modified by Dahua Lin on Apr 30, 2006
0037 %     - Base on the slinvevals function to eigenvalue processing.
0038 %     - Re-organize the code in a clearer way
0039 %
0040 
0041 %% parse and verify input arguments
0042 
0043 if size(C, 1) ~= size(C, 2)
0044     error('sltoolbox:invalidarg', 'C should be symmetric matrix (matrices)');
0045 end
0046 
0047 % for method
0048 if nargin < 2 || isempty(method)
0049     method = 'direct';
0050 end
0051 
0052 if nargin < 3
0053     r = [];
0054 end
0055 
0056 
0057 %% delegate to the computation routine
0058 
0059 switch method
0060     case 'direct'
0061         R = frmroutine_for_getinv(C, @inv, {});
0062     case {'pseudo', 'std'}
0063         R = frmroutine_for_getinv(C, @compinv_evd_based, {'std', r});
0064     case 'reg'
0065         R = frmroutine_for_getinv(C, @compinv_evd_based, {'reg', r});
0066     case 'bound'
0067         R = frmroutine_for_getinv(C, @compinv_evd_based, {'bound', r});
0068     case 'gapprox'
0069         R = frmroutine_for_getinv(C, @compinv_evd_based, {'gapprox', r});
0070 end
0071 
0072 
0073 %% the framework routine to compute
0074 function R = frmroutine_for_getinv(C, fh, params)
0075 % fh is the function handle for computing single inverse
0076 
0077 if ndims(C) == 2  % single matrix
0078     M = 0.5 * (C + C');  % enforce symmetry
0079     R = fh(M, params{:});
0080 else
0081     siz = size(C);
0082     n = prod(siz(3:end));
0083     R = zeros(siz);
0084     
0085     for i = 1 : n
0086         M = C(:,:,i);
0087         M = 0.5 * (M + M');  % enforce symmetry
0088         R(:,:,i) = fh(M, params{:});
0089     end
0090 end
0091 
0092 
0093 %% the general routine for the inverse computation based on eigen-decompose
0094 function R = compinv_evd_based(C, method, r)
0095 
0096 % eigen-decompose
0097 [ev, V] = slsymeig(C);
0098 
0099 % compute the reciprocals of the eigenvalues
0100 ev = slinvevals(ev, method, r);
0101 D = diag(ev);
0102 
0103 % construct the inverse matrix
0104 R = V * D * V';
0105 
0106 
0107

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

Contact us at files@mathworks.com