IRootLab
An Open-Source MATLAB toolbox for vibrational biospectroscopy
fisher_ld.m
Go to the documentation of this file.
1 %>@ingroup maths
2 %>@file
3 %>@brief Calculates Fisher's Linear Discriminant vectors (loadings).
4 %>
5 %> If either data or pieces is passed, the scatter matrixes will be
6 %> calculated.
7 %>
8 %> The rank of @c S_W (within-class scatter matrix) will be tested, and if it is lower
9 %> than the dimensionality of the problem (i.e., number of features), PCA
10 %> will be run first to project the data onto a space where the within-class
11 %> scatter matrix (say @c S_W_PCA) will be non-singular and then the obtained
12 %> LDA loadings will be projected back to the original space.
13 %>
14 %> The equation to solve [1]:
15 %>
16 %> @code
17 %> S_B*w = lambda*S_W*w
18 %> @endcode
19 %>
20 %> There are rank(S_B) (at most no_classes-1) different w's.
21 %>
22 %> <h3>References:</h3>
23 %> [1] R. O. Duda, P. E. Hart, and D. G. Stork, Pattern Classification, 2nd ed. New York: John Wiley & Sons, 2001.
24 %>
25 %> [2] T. Hastie, J. H. Friedman, and R. Tibshirani, The Elements of Statistical Learning, 2nd ed. New York: Springer, 2007.
26 %>
27 %> @sa calculate_scatters.m
28 %>
29 %> @param data Dataset
30 %> @param flag_sphere=0. Deac
31 %> @param flag_modified_s_b: see calculate_scatters.m
32 %> @param n_max Maximum number of loadings vectors to be returned
33 %> @return <em>[W_star]</em> or <em>[W_star, lambdas]</em>
34 
35 function [W_star, varargout] = fisher_ld(data, flag_sphere, flag_modified_s_b, P, n_max)
36 
37 flag_sphere = 0;
38 
39 if data.nc < 2
40  irerror('Cannot run LDA on dataset with less than 2 classes!');
41 end;
42 
43 if ~exist('flag_modified_s_b', 'var')
44  flag_modified_s_b = 0;
45 end;
46 
47 s = 'None';
48 
49 if ~exist('P')
50  P = 0;
51 end;
52 flag_p = any(P(:) ~= 0);
53 
54 flag_n_max = nargin >= 5 && ~isempty(n_max);
55 
56 
57 [S_B, S_W] = data_calculate_scatters(data, flag_modified_s_b, P);
58 
59 [V, D] = eig(S_B, S_W);
60 
61 no_lambdas = size(D, 2);
62 
63 % Creates an index table and sorts it by eigenvalue in ascending order
64 % (because sortrows() hasn't a descending order option)
65 lambdas = diag(D);
66 [vv, ii] = sort(lambdas, 'descend');
67 
68 % % % if numel(vv) < data.nc-1
69 % % % % disp('OLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLHAAAAA');
70 % % % % dbstack;
71 % % % % keyboard;
72 % % % end;
73 
74 if ~flag_p
75  % Returns the nc-1 vectors corresponding to the c-1 largest eigenvalues
76  if flag_n_max
77  % eigenvectors in descending order of eigenvalue
78  W_star = V(:, ii(1:min([data.nc-1, n_max, data.nf])));
79  else
80  W_star = V(:, ii(1:min([data.nc-1, data.nf])));
81  end;
82 else
83  if flag_n_max
84  W_star = V(:, ii(1:min(size(V, 2), n_max)));
85  else
86 
87  W_star = V(:, ii);
88  end;
89 end;
90 
91 % % % % % % % % % % % % % % % % % % % % % %> keyboard;
92 % % % % % % % % % % % % % % % % % % % % % lambdas(:, 2) = (1:no_lambdas)';
93 % % % % % % % % % % % % % % % % % % % % % lambdas = sortrows(lambdas);
94 % % % % % % % % % % % % % % % % % % % % %
95 % % % % % % % % % % % % % % % % % % % % % % sweeps the [lambda, index] table backwards in search for no_classes-1
96 % % % % % % % % % % % % % % % % % % % % % % largest and properly numeric eigenvalues
97 % % % % % % % % % % % % % % % % % % % % % W_star = zeros(data.nf, data.nc-1);
98 % % % % % % % % % % % % % % % % % % % % % lambdas2 = zeros(no_lambdas, 1);
99 % % % % % % % % % % % % % % % % % % % % % maxlambda = max(lambdas(:, 1));
100 % % % % % % % % % % % % % % % % % % % % % no_found = 0;
101 % % % % % % % % % % % % % % % % % % % % % for i = no_lambdas:-1:1
102 % % % % % % % % % % % % % % % % % % % % % lambda = lambdas(i, 1);
103 % % % % % % % % % % % % % % % % % % % % % % if lambda ~= NaN & lambda ~= Inf & lambda > maxlambda*1e-6
104 % % % % % % % % % % % % % % % % % % % % % no_found = no_found+1;
105 % % % % % % % % % % % % % % % % % % % % %
106 % % % % % % % % % % % % % % % % % % % % % v_temp = V(:, lambdas(i, 2));
107 % % % % % % % % % % % % % % % % % % % % % W_star(:, no_found) = v_temp/norm(v_temp);
108 % % % % % % % % % % % % % % % % % % % % % lambdas2(no_found) = lambdas(i, 1);
109 % % % % % % % % % % % % % % % % % % % % %
110 % % % % % % % % % % % % % % % % % % % % % if no_found >= 3 %data.nc-1
111 % % % % % % % % % % % % % % % % % % % % % break;
112 % % % % % % % % % % % % % % % % % % % % % end;
113 % % % % % % % % % % % % % % % % % % % % % % end;
114 % % % % % % % % % % % % % % % % % % % % % end;
115 % % % % % % % % % % % % % % % % % % % % %
116 % % % % % % % % % % % % % % % % % % % % %
117 % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % Don't mess here again, JULIO, this needs to be like that, unnormalized
118 % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % W_star = adjust_unitnorm(W_star);
119 % % % % % % % % % % % % % % % % % % % % % % W_star = adjust_turn(W_star);
120 % % % % % % % % % % % % % % % % % % % % %
121 % % % % % % % % % % % % % % % % % % % % % if no_found < (data.nc-1)
122 % % % % % % % % % % % % % % % % % % % % % W_star = W_star(:, 1:no_found);
123 % % % % % % % % % % % % % % % % % % % % % end
124 
125 
126 if nargout() == 2
127  varargout(1) = {vv};
128 end;
129 
130 % irverbose('F-LDA applied');
131 
132 
133 
134 
135 
136 
137 
138 
139 
140 
141 
142 
143 
144 
145 
146 
147 
148 
149 
150 
151 
152 
153 
154 
155 
156 
157 
158 
159 
160 
161 
162 
163 
164 
165 
166 
167 
168 
169 
170 
171 
172 
173 
174 
175 
176 
177 
178 
179 
180 % flag_sphere = 0;
181 %
182 % if data.nc < 2
183 % irerror('Cannot run LDA on dataset with less than 2 classes!');
184 % end;
185 %
186 % if ~exist('flag_modified_s_b', 'var')
187 % flag_modified_s_b = 0;
188 % end;
189 %
190 % s = 'None';
191 %
192 % if ~exist('P')
193 % P = 0;
194 % end;
195 %
196 % flag_pca = 0;
197 %
198 % [S_B, S_W] = data_calculate_scatters(data, flag_modified_s_b, P);
199 % % S_W = diag(diag(S_W));
200 %
201 % % S_W = eye(size(S_W, 1)); %> HACK let's try to see how it is dealt with if S_W is an eye
202 %
203 % rsw = rank(S_W);
204 % flag_deficient = rank(S_W) < length(S_W);
205 %
206 % if flag_sphere && flag_deficient
207 % error('Cannot make S_W spherical because it is singular!');
208 % end;
209 %
210 % if flag_sphere
211 % [V, lambdas] = eig_ordered(S_W);
212 %
213 % V_ = V/sqrt(diag(lambdas));
214 % X = X*V_;
215 %
216 % [S_B, S_W] = data_calculate_scatters(data, flag_modified_s_b, P);
217 % end;
218 %
219 % if flag_deficient
220 % fprintf('Fisher''s LDA will be calculated in PC eigenspace because rank(S_W) = %>d which is lower than %>d\n', rsw, length(S_W));
221 % flag_pca = 1;
222 %
223 % [loadings_pca, scores] = princomp2(data.X);
224 % no_factors = size(scores, 2);
225 %
226 % if rsw < no_factors
227 % fprintf('PCA Scores matrix will be truncated from %>d to %>d columns to match rank of S_W.\n', no_factors, rsw);
228 % scores = scores(:, 1:rsw);
229 % loadings_pca = loadings_pca(:, 1:rsw);
230 % elseif no_factors < rsw
231 % fprintf('Actually, the number of PCA factors derived (%>d) was even lower than %>d said before.\n', no_factors, rsw);
232 % rsw = no_factors;
233 % end;
234 %
235 % data.X = scores;
236 %
237 % [S_B, S_W] = data_calculate_scatters(data, flag_modified_s_b, P);
238 % end;
239 %
240 % no_classes = rank(S_B)+1;
241 % nf = size(S_B, 1);
242 %
243 % rank_S_W = rank(S_W);
244 % %> if rank_S_W < nf
245 % %> error('Cannot calculate Fisher Linear Discriminant because rank(S_W) = %>d which is lower than %>d.\n', rank_S_W, nf);
246 % %> end;
247 %
248 % %> if flag_sphere
249 % %> %> [V, D] = eigs(S_B, no_classes-1);
250 % %> [V, D] = eig(S_B);
251 % %> V = V(:, 1:(no_classes-1));
252 % %> D = D(:, 1:(no_classes-1));
253 % %> else
254 % %> OPTS.tol = 1e-50;
255 % [V, D] = eigs(S_B, S_W, no_classes-1);
256 % % end;
257 % % [V, D] = eig(S_B, S_W);
258 %
259 % no_lambdas = size(D, 2);
260 %
261 % % Creates an index table and sorts it by eigenvalue in ascending order
262 % % (because sortrows() hasn't a descending order option)
263 % lambdas = diag(D);
264 % %> keyboard;
265 % lambdas(:, 2) = (1:no_lambdas)';
266 % lambdas = sortrows(lambdas);
267 %
268 % % sweeps the [lambda, index] table backwards in search for no_classes-1
269 % % largest and properly numeric eigenvalues
270 % W_star = zeros(nf, no_lambdas);
271 % lambdas2 = zeros(no_lambdas, 1);
272 % no_found = 0;
273 % for i = no_lambdas:-1:1
274 % lambda = lambdas(i, 1);
275 % if lambda ~= NaN & lambda ~= Inf & lambda > 0
276 % no_found = no_found+1;
277 %
278 % v_temp = V(:, lambdas(i, 2));
279 % W_star(:, no_found) = v_temp/norm(v_temp);
280 % lambdas2(no_found) = lambdas(i, 1);
281 % end;
282 % end;
283 %
284 % if flag_sphere
285 % W_star = V_*W_star;
286 % end;
287 %
288 % if flag_pca
289 % W_star = loadings_pca*W_star;
290 % end;
291 %
292 % % % % % % % % % % Don't mess here again, JULIO, this needs to be like that, unnormalized
293 % % % % % % % % % % W_star = adjust_unitnorm(W_star);
294 % W_star = adjust_turn(W_star);
295 %
296 %
297 %
298 %
299 % if nargout() == 2
300 % varargout(1) = {lambdas2};
301 % end;
302 %
303 % fprintf('INFO: F-LDA applied (pre-processing: ''%s'').\n', s);
function adjust_unitnorm(in L)
function irverbose(in s, in level)
function irerror(in s)
function calculate_scatters(in X, in classes, in flag_modified_s_b, in P)
function data_calculate_scatters(in data, in flag_modified_s_b, in P)
function fisher_ld(in data, in flag_sphere, in flag_modified_s_b, in P, in n_max)