source: distools/private/seleigs.m @ 142

Last change on this file since 142 was 13, checked in by bduin, 14 years ago
File size: 4.6 KB
Line 
1%SELEIGS Select eigenvalues from a list
2%
3%   J = SELEIGS(L,ALF)
4%
5% INPUT
6%   L   List of eigenvalues
7%   ALF Parameter determining the dimensionality and the eigenvalue-based mapping
8%       (0,1)   - fraction of the total (absolute value) preserved variance
9%        Inf    - no dimensionality reduction, keeping all dimensions (it's noisy)
10%       'p'     - projection into a Euclidean space based on positive eigenvalues only
11%       'PARp'  - projection into a Euclidean space based on the PAR fraction of
12%                 positive eigenvalues; e.g. ALF = '0.9p'
13%       'n'     - projection into a Euclidean space based on negative eigenvalues only
14%       'PARn'  - projection into a (negative) Euclidean space based on the PAR fraction
15%                 of negative eigenvalues; e.g. ALF = '0.7n'
16%       'P1pP2n'- projection into a Euclidean space based on the P1 positive eigenvalues
17%                 and P2 negative eigenvalues; e.g. ALF = '0.7p0.1n', ALF = '7p2n'
18%       1 .. N  - number of dimensions in total
19%       [P1 P2] - P1 dimensions or preserved fraction of variance in the positive subspace
20%                 and P2 dimensions or preserved fraction of variance in the negative
21%                 subspace; e.g. ALF = [5 10], ALF = [0.9 0.1]
22%
23% OUTPUT
24%   J   Index of selected eigenvalues
25%
26% DESCRIPTION
27% This is a low-level routine for PSEM, KPSEM, KPCA and PEPCA. From a list of
28% eigenvalues it selects the ones according to ALF.
29%
30% SEE ALSO
31% MAPPINGS, DATASETS, PSEM, KPSEM, KPCA, PEPCA
32%
33
34% Elzbieta Pekalska, e.pekalska@ewi.tudelft.nl
35% Faculty of Electrical Engineering, Mathematics and Computer Science,
36% Delft University of Technology, The Netherlands
37
38
39function [J,sig] = seleigs(L,alf,prec)
40
41alfstr = lower(alf);
42
43if strcmp(alfstr,'cut'),
44  J = cuteigs(L,prec);
45else
46  % Check whether alf is a string
47  if ~isnumeric(alfstr),
48    zp = strfind(alfstr,'p');
49    zn = strfind(alfstr,'n');
50    if isempty(zp), zp = 0; end
51    if isempty(zn), zn = 0; end
52    ALF = 0;
53    if zp > 0,
54      if length(zp) > 1,
55        error('Wrong ALF.');
56      end
57      if zn > zp,
58        sstr = alfstr(1:zp-1);
59      elseif zn < zp,
60        sstr = alfstr(zn+1:zp-1);
61      else
62        error('Wrong ALF.');
63      end
64      if ~isempty(sstr),
65        ALF = str2num(sstr);
66      else
67        ALF = length(find(L>0));
68      end
69    end
70    if zn > 0,
71      if length(zn) > 1,
72        error('Wrong ALF.');
73      end
74      if zn > zp,
75        sstr = alfstr(zp+1:zn-1);
76      elseif zn < zp,
77        sstr = alfstr(1:zn-1);
78      else
79        error('Wrong ALF.');
80      end
81      if ~isempty(sstr),
82        ALF = [ALF str2num(sstr)];
83      else
84        ALF = [ALF length(find(L<0))];
85      end
86    else
87      ALF = [ALF 0];
88    end
89    alf = ALF;
90  end
91
92
93% alf is now the number of eigenvalues
94  if max(size(alf)) > 2 | (alf(1) == 0 & alf(2) == 0),
95    error('Wrong ALF.')
96  elseif max(size(alf)) == 2,
97    J = [];
98    Z  = find(L>0);
99    if alf(1) > 0,
100      if ~isempty(Z),
101        J = selnumalf(alf(1),L,Z)';
102      else
103        prwarning(1,'No positive dimensions are selected.');
104      end
105    end
106    Z  = find(L<0);
107    if alf(2) > 0,
108      if ~isempty(Z),
109        J = [J; selnumalf(alf(2),L,Z)'];
110      else
111        prwarning(1,'No negative dimensions are selected.');
112      end
113    end
114   else
115     J = selnumalf(alf,L,1:length(L));
116  end
117end
118
119
120if isempty(J),
121  error('Wrong choice of ALF; the subspace cannot be found.');
122end
123
124I1  = find(L(J) > 0);
125I2  = find(L(J) < 0);
126sig = [length(I1) length(I2)];
127
128[ll,K1] = sort(-L(J(I1)));
129[ll,K2] = sort(L(J(I2)));
130
131% J - index of selected eigenvalues; SORTED:
132% first decreasing positive ones folowed by increasing negative ones
133JJ = [];
134if sig(1) > 0
135  JJ = J(I1(K1));
136end
137if sig(2) > 0
138  JJ = [JJ; J(I2(K2))];
139end
140J = JJ;
141return
142
143
144
145
146
147function J = selnumalf(alf,L,I)
148
149if alf >= 1,
150  if alf == inf,
151    cs = cumsum(abs(L(I)));
152    nn = min(find(cs./cs(end) == 1));
153    J  = I(1:nn)';
154  elseif alf > length(I),
155    error('ALF is invalid.')
156  else
157    J = I(1:alf)';
158  end
159elseif alf > 0,                     % alf in (0,1), percentage
160  cs = cumsum(abs(L(I)));
161  nn = min(find (cs./cs(end) >= alf));
162  J  = I(1:nn)';
163elseif alf < 0,
164  I   = I(length(I):-1:1);
165  alf = abs(alf);
166  if alf >= 1,
167    if alf > length(I),
168      error('ALF is invalid.')
169    else
170      J  = I(1:alf)';
171    end
172  else                             % |alf| in (0,1), percentage
173    cs = cumsum(abs(L(I)));
174    nn = min(find (cs./cs(end) >= alf));
175    J  = I(1:nn)';
176  end
177else
178  error('Wrong ALF.');
179end
Note: See TracBrowser for help on using the repository browser.