source: distools/psem.m @ 118

Last change on this file since 118 was 79, checked in by bduin, 11 years ago
File size: 6.1 KB
Line 
1%PSEM Pseudo-Euclidean Linear Embedding
2%
3%     [W,SIG,L] = PSEM(D,ALF,P)
4%                 OR
5%     [W,SIG,L] = PSEM(W,ALF)
6%
7% INPUT
8%   D   NxN symmetric dissimilarity matrix (dataset)
9%   W   Trained linear embedding into a pseudo-Euclidean space
10%   ALF Parameter determining the dimensionality and the mapping (optional, default: Inf)
11%       (0,1)   - Fraction of the total (absolute value) preserved variance
12%        Inf    - No dimensionality reduction, keeping all dimensions (it's VERY noisy)
13%       'p'     - Projection into a Euclidean space based on positive eigenvalues only
14%       'PARp'  - Projection into a Euclidean space based on the PAR fraction of
15%                 positive eigenvalues; e.g. ALF = '0.9p'
16%       'n'     - Projection into a Euclidean space based on negative eigenvalues only
17%       'PARn'  - Projection into a (negative) Euclidean space based on the PAR fraction
18%                 of negative eigenvalues; e.g. ALF = '0.7n'
19%       'P1pP2n'- Projection into a Euclidean space based on the P1 positive eigenvalues
20%                 and P2 negative eigenvalues; e.g. ALF = '0.7p0.1n', ALF = '7p2n'
21%       1 .. N  - Number of dimensions in total
22%       [P1 P2] - P1 dimensions or preserved fraction of variance in the positive subspace
23%                 and P2 dimensions or preserved fraction of variance in the negative
24%                 subspace; e.g. ALF = [5 10], ALF = [0.9 0.1]
25%   P   Integer between 0 and N specifying which object is mapped at the origin;
26%       0 stands for the mean; (optional, default: 0)
27%
28% OUTPUT
29%   W   Linear embedding into a pseudo-Euclidean space
30%   SIG Signature of the space
31%   L   List of eigenvalues
32%
33% DEFAULT
34%   P   = 0
35%   ALF = INF
36%
37% DESCRIPTION
38% Linear mapping W onto an M-dimensional pseudo-Euclidean subspace from a symmetric,
39% square dissimilarity matrix D such that the dissimilarities are preserved. M
40% M is determined by ALF. E.g., the subspace is found such that at least a fraction
41% ALF of the total variance is preserved for ALF in (0,1). The resulting X is found
42% by D*W. The parameter SIG describes the signature of the subspace. L is a sorted
43% list of eigenvalues describing the variances in the (pseudo-)Euclidean space.
44%
45% A trained mapping can be reduced further by:
46%   [W,SIG,L] = PSEM(D,ALF)
47%
48% SEE ALSO
49% MAPPINGS, DATASETS, AUGPSEM, PCA
50%
51% LITERATURE
52% 1. L. Goldfarb, A unified approach to pattern recognition, Pattern Recognition, vol.17,
53% 575-582, 1984.
54% 2. E. Pekalska, P. Paclik, and R.P.W. Duin, A Generalized Kernel Approach to
55% Dissimilarity-based Classification, Journal of Machine Learning Research,
56% vol.2, no.2, 175-211, 2002.
57%
58
59% Copyright: Elzbieta Pekalska, ela.pekalska@googlemail.com
60% Faculty EWI, Delft University of Technology and
61% School of Computer Science, University of Manchester
62
63
64
65function [W,sig,L,Q] = psem(d,alf,pzero,prec)
66
67% PREC is the precision parameter used for the automatic
68% selection (heuristic) of the number of dominant eigenvalues.
69% This happens when SELEIGS is called with the parameter 'CUT'.
70
71if nargin < 4 | isempty(prec),
72  prec = 1e-4;
73end
74if nargin < 3 | isempty(pzero),
75  pzero = 0;
76end
77if nargin < 2 | isempty(alf),
78  alf = inf;
79end
80if nargin < 1 | isempty(d),
81  W = prmapping(mfilename,{alf,pzero,prec});
82  W = setname(W,'PE embedding');
83  return
84end
85
86
87
88if (isdataset(d) | isa(d,'double'))
89  if ismapping(alf)
90    % APPLY THE MAPPING
91    pars  = getdata(alf);
92    [m,n] = size(d);
93    d     = d.^2;
94
95    Q   = pars{1};  % Eigenvectors
96    me  = pars{2};  % Vector of the average squared original dissimilarities
97    p   = pars{3};  % p=0 -> the mean of the embedded configuration lies at 0, otherwise, it lies at pzero
98    L   = pars{4};  % Eigenvalues
99
100    % Project new data depending on p
101    % (whether the mean or other object lies at the origin)
102    if p == 0,
103      H = -repmat(1,n,n)/n;
104      H(1:n+1:end) = H(1:n+1:end) + 1;     % H = eye(n) - ones(n,n)/n
105      W = -0.5 * (d - me(ones(m,1),:)) * H * Q * diag(sqrt(abs(L))./L);
106    else
107      W =  0.5 * (d(:,p) * ones(1,n) + me(ones(m,1),:) - d) * Q * diag(sqrt(abs(L))./L);
108    end
109
110    % Store signature in the USER field
111    if isdataset(W),
112      W = setname(W,['Projected ' updname(W.name)]);
113      W = setsig(W,pars{5});
114    end
115    return
116  end
117end
118
119
120
121% REDUCE A TRAINED MAPPING
122if ismapping(d)
123  pars = getdata(d);
124  Q    = pars{1};
125  L    = pars{4};
126  m    = size(Q,1);
127
128  [ll,K] = sort(-abs(L));
129  L = L(K);
130  Q = Q(:,K);
131  [J,sig] = seleigs(L,alf,pars{6});
132  Q = Q(:,J);        % Eigenvectors
133  L = L(J);          % Eigenvalues
134
135  W = prmapping(mfilename,'trained',{Q, pars{2},pars{3},L,pars{5},pars{6}},[],m,length(J));
136  W = setname(W,'PE embedding');
137  return
138end
139
140
141
142% TRAIN THE MAPPING
143% Tolerance value used in comparisons
144if mean(+d(:)) < 1,
145  tol = 1e-12;
146else
147  tol = 1e-10;
148end
149
150[n,m] = size(d);
151if ~issym(d,tol),
152  prwarning(1,'Matrix should be symmetric. It is made symmetric by averaging.')
153  d = 0.5*(d+d');
154end
155
156if pzero > n,
157  error('Wrong third parameter.');
158end
159
160d = (+d).^2;
161
162if pzero == 0,
163  % Project the data such that the mean lies at the origin
164  H = -repmat(1/n,n,n);
165  H(1:n+1:end) = H(1:n+1:end) + 1; % H = eye(n) - ones(n,n)/n
166  H = -0.5 * H * d * H;            % H is now the matrix of inner products
167else
168  % Project the data such that pzero's object lies at the origin
169  H = 0.5 * (d(:,pzero) * ones(1,n) + ones(n,1) * d(:,pzero)' - d);
170end
171H = 0.5*(H+H');                    % Make sure H is symmetric
172
173[Q,L]  = preig(H);
174Q      = real(Q);
175l      = diag(real(L));
176[lm,Z] = sort(-abs(l));
177Q      = Q(:,Z);
178l      = l(Z);                     % Eigenvalues are sorted by decreasing absolute value
179
180[J,sig] = seleigs(l,alf,prec);     % J is the index of the selected eigenvalues
181L = l(J);                          % Eigenvalues
182Q = Q(:,J);                        % Eigenvectors
183
184%A  = Q * diag(sqrt(abs(L)));      % Data in a pseudo-Euclidean space
185
186% Determine the mapping depending on pzero
187if pzero == 0,
188  W = prmapping(mfilename,'trained',{Q,mean(+d,2)',pzero,L,sig,prec},[],m,sum(sig));
189else
190  W = prmapping(mfilename,'trained',{Q,+d(:,pzero)',pzero,L,sig,prec},[],m,sum(sig));
191end
192W = setname(W,'PE embedding');
193return
Note: See TracBrowser for help on using the repository browser.