1 | %KCENTERM Kernel Weighted Centering Mapping |
---|
2 | % |
---|
3 | % W = kcenterm(K,V) |
---|
4 | % |
---|
5 | % INPUT |
---|
6 | % K NxN kernel or similarity matrix (dataset) |
---|
7 | % V 'C' (default) or Nx1 weight vector of nonnegative values; sum(V)=1 |
---|
8 | % |
---|
9 | % OUTPUT |
---|
10 | % W Kernel weighted centering mapping |
---|
11 | % |
---|
12 | % DEFAULT |
---|
13 | % V = 'C'; |
---|
14 | % |
---|
15 | % DESCRIPTION |
---|
16 | % Defines a mapping that centers the given NxN kernel matrix K:=K(X,X) |
---|
17 | % such that the weighted mean coincides with the origin in the vector space |
---|
18 | % induced by K. The nonnegative weights of the mean are defined by an |
---|
19 | % Nx1 vector V; sum(V) = 1. V = 'C' stands for V=ones(N,1)/N, hence |
---|
20 | % the true centering. |
---|
21 | % |
---|
22 | % The mapping works with both positive (semi-)definite and indefinite |
---|
23 | % kernels. V can be applied to a new MxN kernel Knew:=K(Xnew,X). |
---|
24 | % |
---|
25 | % SEE ALSO |
---|
26 | % KPCA, MAPPINGS, DATASETS |
---|
27 | % |
---|
28 | |
---|
29 | % Copyright: Ela Pekalska, ela.pekalska@googlemail.com |
---|
30 | % Faculty EWI, Delft University of Technology and |
---|
31 | % School of Computer Science, University of Manchester |
---|
32 | % |
---|
33 | |
---|
34 | function [V,KK] = kcenterm(K,v) |
---|
35 | |
---|
36 | if nargin < 2 | isempty(v) |
---|
37 | v = 'c'; |
---|
38 | end |
---|
39 | |
---|
40 | if nargin < 1 | isempty(K) |
---|
41 | V = prmapping(mfilename); |
---|
42 | V = setname(V,'kcenterm'); |
---|
43 | return |
---|
44 | end |
---|
45 | |
---|
46 | |
---|
47 | if (isdataset(K) | isa(K,'double')) |
---|
48 | if ismapping(v) |
---|
49 | |
---|
50 | [m,n] = size(K); |
---|
51 | pars = getdata(v); |
---|
52 | |
---|
53 | v = pars{1}; |
---|
54 | Kwm = pars{2}; |
---|
55 | |
---|
56 | H = eye(n) - v*ones(1,n); |
---|
57 | V = (K - repmat(Kwm',m,1))* H; |
---|
58 | return; |
---|
59 | end |
---|
60 | end |
---|
61 | |
---|
62 | |
---|
63 | |
---|
64 | lab = getlab(K); |
---|
65 | lablist = getlablist(K); |
---|
66 | [n,m,c] = getsize(K); |
---|
67 | |
---|
68 | tol = 1e-12; |
---|
69 | if ~issym(K,tol), |
---|
70 | error('Kernel matrix K should be symmetric.') |
---|
71 | end |
---|
72 | |
---|
73 | if v == 'c', |
---|
74 | v = ones(n,1)/n; |
---|
75 | end |
---|
76 | |
---|
77 | if length(v)==1 |
---|
78 | l = intersect(v,1:n) |
---|
79 | v = zeros(n,1); |
---|
80 | v(l) = 1; |
---|
81 | end |
---|
82 | |
---|
83 | if length(v) ~= n, |
---|
84 | error('V has a wrong size.'); |
---|
85 | end |
---|
86 | |
---|
87 | if any(v) < 0 | any(v) > 1, |
---|
88 | error('V should have elements in [0,1].'); |
---|
89 | end |
---|
90 | |
---|
91 | if abs(sum(v) - 1) > tol |
---|
92 | error('sum(V) ~= 1.'); |
---|
93 | end |
---|
94 | |
---|
95 | |
---|
96 | % Center K such that the weighted mean coincides with the origin |
---|
97 | Kwm = K*v; |
---|
98 | if nargout > 1 |
---|
99 | H = eye(n) - v*ones(1,n); |
---|
100 | KK = H * K * H; |
---|
101 | end |
---|
102 | |
---|
103 | V = prmapping(mfilename,'trained',{v,Kwm},[],m,m); |
---|
104 | V = setname(V,'kcenterm'); |
---|
105 | return |
---|