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 slcenkernel
Home > sltoolbox > kernel > slcenkernel.m

slcenkernel

PURPOSE ^

SLCENKERNEL Compute the centralized kernel matrix

SYNOPSIS ^

function KC = slcenkernel(K0, K, w)

DESCRIPTION ^

SLCENKERNEL Compute the centralized kernel matrix

 $ Syntax $
   - KC = slcenkernel(K0)
   - KC = slcenkernel(K0, [], w)
   - KC = slcenkernel(K0, K)
   - KC = slcenkernel(K0, K, w)

 $ Arguments $
   - K0:       the gram matrix of the referenced samples
   - K:        the kernel matrix for target samples
   - w:        the weights for the referenced samples
   - KC:       the centralized kernel matrix.

 $ Description $
   - KC = slcenkernel(K0) compute the centralized kernel matrix from
     the original kernel gram matrix K0.
 
   - KC = slcenkernel(K, [], w) compute the centralized kernel gram
     matrix from the original kernel gram matrix K0. The mean feature
     is obtained with the weights for referenced samples, given by w.

   - KC = slcenkernel(K0, K) compute the centralized kernel matrix
     for target samples, with the original gram matrix for referenced
     samples K0 and the kernel matrix for target samples w.r.t the 
     referenced samples K given.

   - KC = slcenkernel(K0, K, w) compute the centralied kernel matrix
     for target samples, with feature mean computed in a weighted 
     manner.

 $ Remarks $
   -# For original kernel matrix K, it is defined as 
      K(i, j) = <phi(i), phi(j)>, given that phi(i) and phi(j) are
      the feature map of the samples x(i) and x(j) respectively.
      Then the centralized kernel matrix is defined as
      KC(i, j) = <phi(i) - mean_phi, phi(j) - mean_phi>, where
      mean_phi is the mean of all referenced feature maps. If w
      is specified mean_phi is given by weighted mean. 
      Kernel centralization plays an important role in many 
      kernelized algorithms such as Kernel PCA.

   -# Suppose the mean feature map is defined by 
      mean_phi = sum_i w_i phi(i).
      Then the centralized kernel gram matrix can be written as
      KC = K - 1 * (w^T * K) - (K * w) * 1^T + 1 * (w^T * K * w) * 1^T.
      It can be easily shown that the mean of columns (rows) of KC is
      a zero vector. Thus, centralize the centralized kernel matrix
      would keep the input unchanged. 

   -# Instead of applying the formula given above, the function 
      implements a more efficient computational routine by reducing
      the redundant computations.

 $ History $
   - Created by Dahua Lin on May 2nd, 2006
   - Modified by Dahua Lin on Sep 10, 2006
       - use sladdrowcols to replace sladd to increase efficiency

CROSS-REFERENCE INFORMATION ^

This function calls:
  • sladdrowcols SLADDROWCOLS Adds the vectors to all rows and all columns
This function is called by:
  • slgda SLGDA Performs Baudat's Generalized Discriminant Analysis
  • slkernelfea SLKERNELFEA Extracts kernelized mapped features
  • slkpca SLPCA Learns a Kernel PCA model from training samples

SOURCE CODE ^

0001 function KC = slcenkernel(K0, K, w)
0002 %SLCENKERNEL Compute the centralized kernel matrix
0003 %
0004 % $ Syntax $
0005 %   - KC = slcenkernel(K0)
0006 %   - KC = slcenkernel(K0, [], w)
0007 %   - KC = slcenkernel(K0, K)
0008 %   - KC = slcenkernel(K0, K, w)
0009 %
0010 % $ Arguments $
0011 %   - K0:       the gram matrix of the referenced samples
0012 %   - K:        the kernel matrix for target samples
0013 %   - w:        the weights for the referenced samples
0014 %   - KC:       the centralized kernel matrix.
0015 %
0016 % $ Description $
0017 %   - KC = slcenkernel(K0) compute the centralized kernel matrix from
0018 %     the original kernel gram matrix K0.
0019 %
0020 %   - KC = slcenkernel(K, [], w) compute the centralized kernel gram
0021 %     matrix from the original kernel gram matrix K0. The mean feature
0022 %     is obtained with the weights for referenced samples, given by w.
0023 %
0024 %   - KC = slcenkernel(K0, K) compute the centralized kernel matrix
0025 %     for target samples, with the original gram matrix for referenced
0026 %     samples K0 and the kernel matrix for target samples w.r.t the
0027 %     referenced samples K given.
0028 %
0029 %   - KC = slcenkernel(K0, K, w) compute the centralied kernel matrix
0030 %     for target samples, with feature mean computed in a weighted
0031 %     manner.
0032 %
0033 % $ Remarks $
0034 %   -# For original kernel matrix K, it is defined as
0035 %      K(i, j) = <phi(i), phi(j)>, given that phi(i) and phi(j) are
0036 %      the feature map of the samples x(i) and x(j) respectively.
0037 %      Then the centralized kernel matrix is defined as
0038 %      KC(i, j) = <phi(i) - mean_phi, phi(j) - mean_phi>, where
0039 %      mean_phi is the mean of all referenced feature maps. If w
0040 %      is specified mean_phi is given by weighted mean.
0041 %      Kernel centralization plays an important role in many
0042 %      kernelized algorithms such as Kernel PCA.
0043 %
0044 %   -# Suppose the mean feature map is defined by
0045 %      mean_phi = sum_i w_i phi(i).
0046 %      Then the centralized kernel gram matrix can be written as
0047 %      KC = K - 1 * (w^T * K) - (K * w) * 1^T + 1 * (w^T * K * w) * 1^T.
0048 %      It can be easily shown that the mean of columns (rows) of KC is
0049 %      a zero vector. Thus, centralize the centralized kernel matrix
0050 %      would keep the input unchanged.
0051 %
0052 %   -# Instead of applying the formula given above, the function
0053 %      implements a more efficient computational routine by reducing
0054 %      the redundant computations.
0055 %
0056 % $ History $
0057 %   - Created by Dahua Lin on May 2nd, 2006
0058 %   - Modified by Dahua Lin on Sep 10, 2006
0059 %       - use sladdrowcols to replace sladd to increase efficiency
0060 %
0061 
0062 %% parse and verify input arguments
0063 
0064 % for K0
0065 n0 = size(K0, 1);
0066 if ndims(K0) ~= 2 || size(K0, 2) ~= n0;
0067     error('sltoolbox:invaliddims', ...
0068         'K0 should be a 2D square matrix');
0069 end
0070 
0071 % for K
0072 if nargin < 2 || isempty(K)
0073     K = K0;
0074 else
0075     if ndims(K) ~= 2
0076         error('sltoolbox:invaliddims', ...
0077             'K should be a 2D matrix');
0078     end
0079     if size(K, 1) ~= n0
0080         error('sltoolbox:sizmismatch', ...
0081             'Size inconsistency between K0 and K');
0082     end
0083 end
0084     
0085 % for w
0086 if nargin < 3 || isempty(w)
0087     isweighted = false;
0088 else
0089     if ~isequal(size(w), [1, n0])
0090         error('sltoolbox:sizmismatch', ...
0091             'Size inconsistency between K0 and w');
0092     end    
0093     isweighted = true;
0094 end
0095 
0096 
0097 %% compute
0098 
0099 % Steps:
0100 % 1. compute v1: mean row vector of K (1 x n)
0101 % 2. compute v2: mean column vector of K0 (n x 1)
0102 % 3. compute s3: mean value of of all elements of K0 (1 x 1)
0103 % 4. KC = K - expand(v1) - expand(v2) + s3
0104 
0105 if ~isweighted  % non-weighted case
0106 
0107     v1 = sum(K, 1) * (1 / n0);
0108     v2 = sum(K0, 2) * (1 / n0);
0109     s3 = sum(v2) * (1 / n0);
0110     
0111 else            % weighted case
0112     
0113     w = w / sum(w);     % normalize the weights
0114     
0115     v1 = w * K;
0116     v2 = K0 * w';
0117     s3 = w * v2;
0118     
0119 end
0120 
0121 KC = sladdrowcols(K, -v1, -v2+s3);
0122 
0123

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

Contact us at files@mathworks.com