1 | %PE_EM Pseudo-Euclidean Linear Embedding |
---|
2 | % |
---|
3 | % W = PE_EM(D,ALF,P) |
---|
4 | % OR |
---|
5 | % W = PE_EM(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 | % |
---|
31 | % DEFAULT |
---|
32 | % P = 0 |
---|
33 | % ALF = INF |
---|
34 | % |
---|
35 | % DESCRIPTION |
---|
36 | % Linear mapping W onto an M-dimensional Pseudo-Euclidean _PE) subspace from a |
---|
37 | % symmetric, square dissimilarity matrix D such that the dissimilarities are preserved. |
---|
38 | % M is determined by ALF. E.g., the subspace is found such that at least a fraction |
---|
39 | % ALF of the total variance is preserved for ALF in (0,1). The resulting X is found |
---|
40 | % by D*W. The signature of the obtained PE space (numbers of positive and negative |
---|
41 | % directions) can be found by PE_SIG(W). The spectrum of the obtained space |
---|
42 | % can be found by PE_SPEC(W). |
---|
43 | % |
---|
44 | % SEE ALSO |
---|
45 | % MAPPINGS, DATASETS, AUGPSEM, PCA, PE_PCA, PE_SPEC, GETSIG, SETSIG |
---|
46 | % |
---|
47 | % LITERATURE |
---|
48 | % 1. L. Goldfarb, A unified approach to pattern recognition, Pattern Recognition, vol.17, |
---|
49 | % 575-582, 1984. |
---|
50 | % 2. E. Pekalska, P. Paclik, and R.P.W. Duin, A Generalized Kernel Approach to |
---|
51 | % Dissimilarity-based Classification, Journal of Machine Learning Research, |
---|
52 | % vol.2, no.2, 175-211, 2002. |
---|
53 | |
---|
54 | % Copyright: Elzbieta Pekalska, ela.pekalska@googlemail.com |
---|
55 | % Faculty EWI, Delft University of Technology and |
---|
56 | % School of Computer Science, University of Manchester |
---|
57 | |
---|
58 | % This routine is adapted from PSEM. |
---|
59 | % Old PSEM mappings W may be converted by W = PE_EM(W) |
---|
60 | |
---|
61 | function [W,sig,L,Q] = pe_em(d,alf,pzero,prec) |
---|
62 | |
---|
63 | % PREC is the precision parameter used for the automatic |
---|
64 | % selection (heuristic) of the number of dominant eigenvalues. |
---|
65 | % This happens when SELEIGS is called with the parameter 'CUT'. |
---|
66 | |
---|
67 | if nargin < 4, prec = []; end |
---|
68 | if nargin < 3, pzero = []; end |
---|
69 | if nargin < 2 alf = []; end |
---|
70 | |
---|
71 | if nargin < 1 | isempty(d), |
---|
72 | W = mapping(mfilename,{alf,pzero,prec}); |
---|
73 | W = setname(W,'PE embedding'); |
---|
74 | return |
---|
75 | end |
---|
76 | |
---|
77 | if isempty(prec), prec = 1e-4; end |
---|
78 | if isempty(pzero), pzero = 0; end |
---|
79 | if isempty(alf), alf = inf; end |
---|
80 | |
---|
81 | if (isdataset(d) | isa(d,'double')) |
---|
82 | if ismapping(alf) |
---|
83 | % APPLY THE MAPPING |
---|
84 | [m,n] = size(d); |
---|
85 | d = d.^2; |
---|
86 | |
---|
87 | Q = getdata(alf,'evec'); % Eigenvectors |
---|
88 | me = getdata(alf,'mdis'); % Vector of the average squared original dissimilarities |
---|
89 | p = getdata(alf,'mean'); % p=0 -> the mean of the embedded configuration lies at 0, |
---|
90 | % otherwise, it lies at pzero |
---|
91 | L = getdata(alf,'eval'); % Eigenvalues |
---|
92 | |
---|
93 | % Project new data depending on p |
---|
94 | % (whether the mean or other object lies at the origin) |
---|
95 | if p == 0, |
---|
96 | H = -repmat(1,n,n)/n; |
---|
97 | H(1:n+1:end) = H(1:n+1:end) + 1; % H = eye(n) - ones(n,n)/n |
---|
98 | W = -0.5 * (d - me(ones(m,1),:)) * H * Q * diag(sqrt(abs(L))./L); |
---|
99 | else |
---|
100 | W = 0.5 * (d(:,p) * ones(1,n) + me(ones(m,1),:) - d) * Q * diag(sqrt(abs(L))./L); |
---|
101 | end |
---|
102 | |
---|
103 | % Store signature in the USER field |
---|
104 | if isdataset(W), |
---|
105 | W = setname(W,['Projected ' updname(W.name)]); |
---|
106 | W = setsig(W,getdata(alf,'sig')); |
---|
107 | end |
---|
108 | return |
---|
109 | end |
---|
110 | end |
---|
111 | |
---|
112 | |
---|
113 | |
---|
114 | % REDUCE A TRAINED MAPPING |
---|
115 | if ismapping(d) |
---|
116 | data = getdata(d); |
---|
117 | if iscell(data) % check for old type of mapping |
---|
118 | ispsem(d); |
---|
119 | dat.evec = data{1}; |
---|
120 | dat.mdis = data{2}; |
---|
121 | dat.mean = data{3}; |
---|
122 | dat.eval = data{4}; |
---|
123 | dat.sig = data{5}; |
---|
124 | dat.prec = data{6}; |
---|
125 | d = setmapping_file(d,mfilename); |
---|
126 | d = setdata(d,dat); |
---|
127 | if nargin < 2 |
---|
128 | W = d; |
---|
129 | return % conversion only |
---|
130 | else |
---|
131 | data = dat; |
---|
132 | end |
---|
133 | end |
---|
134 | Q = data.evec; % Eigenvectors |
---|
135 | L = data.eval; % Eigenvalues |
---|
136 | m = size(Q,1); |
---|
137 | |
---|
138 | [ll,K] = sort(-abs(L)); |
---|
139 | L = L(K); |
---|
140 | Q = Q(:,K); |
---|
141 | [J,sig] = seleigs(L,alf,getdata(d,'prec')); |
---|
142 | data.evec = Q(:,J); % Eigenvectors |
---|
143 | data.eval = L(J); % Eigenvalues |
---|
144 | |
---|
145 | W = mapping(mfilename,'trained',data,[],m,length(J)); |
---|
146 | W = setname(W,'PE embedding'); |
---|
147 | return |
---|
148 | end |
---|
149 | |
---|
150 | |
---|
151 | |
---|
152 | % TRAIN THE MAPPING |
---|
153 | % Tolerance value used in comparisons |
---|
154 | if mean(+d(:)) < 1, |
---|
155 | tol = 1e-12; |
---|
156 | else |
---|
157 | tol = 1e-10; |
---|
158 | end |
---|
159 | |
---|
160 | [n,m] = size(d); |
---|
161 | if ~issym(d,tol), |
---|
162 | prwarning(1,'Matrix should be symmetric. It is made symmetric by averaging.') |
---|
163 | d = 0.5*(d+d'); |
---|
164 | end |
---|
165 | |
---|
166 | if pzero > n, |
---|
167 | error('Wrong third parameter.'); |
---|
168 | end |
---|
169 | |
---|
170 | d = (+d).^2; |
---|
171 | |
---|
172 | if pzero == 0, |
---|
173 | % Project the data such that the mean lies at the origin |
---|
174 | H = -repmat(1/n,n,n); |
---|
175 | H(1:n+1:end) = H(1:n+1:end) + 1; % H = eye(n) - ones(n,n)/n |
---|
176 | H = -0.5 * H * d * H; % H is now the matrix of inner products |
---|
177 | else |
---|
178 | % Project the data such that pzero's object lies at the origin |
---|
179 | H = 0.5 * (d(:,pzero) * ones(1,n) + ones(n,1) * d(:,pzero)' - d); |
---|
180 | end |
---|
181 | H = 0.5*(H+H'); % Make sure H is symmetric |
---|
182 | |
---|
183 | [Q,L] = preig(H); |
---|
184 | Q = real(Q); |
---|
185 | l = diag(real(L)); |
---|
186 | [lm,Z] = sort(-abs(l)); |
---|
187 | Q = Q(:,Z); |
---|
188 | l = l(Z); % Eigenvalues are sorted by decreasing absolute value |
---|
189 | |
---|
190 | [J,sig] = seleigs(l,alf,prec); % J is the index of the selected eigenvalues |
---|
191 | data.eval = l(J); % Eigenvalues |
---|
192 | data.evec = Q(:,J); % Eigenvectors |
---|
193 | data.mdis = mean(+d,2)'; |
---|
194 | data.mean = pzero; |
---|
195 | data.sig = sig; |
---|
196 | data.prec = prec; |
---|
197 | |
---|
198 | %A = Q * diag(sqrt(abs(L))); % Data in a pseudo-Euclidean space |
---|
199 | |
---|
200 | % Determine the mapping depending on pzero |
---|
201 | if pzero == 0, |
---|
202 | W = mapping(mfilename,'trained',data,[],m,sum(sig)); |
---|
203 | else |
---|
204 | data.mdis = +d(:,pzero)'; |
---|
205 | W = mapping(mfilename,'trained',data,[],m,sum(sig)); |
---|
206 | end |
---|
207 | W = setname(W,'PE embedding'); |
---|
208 | return |
---|