rbmatlab  1.16.09
 All Classes Namespaces Files Functions Variables Modules Pages
PCA_fixspace_evalues.m
1 function [B,evalues] = PCA_fixspace_evalues(X,XFix,A,k,method,epsilon)
2 %function [B,evalues] = PCA_fixspace(X,XFix [, [A], [k], [method],[epsilon] ])
3 %
4 % function computing a PCA basis of the set of column vectors X projected on
5 % the orthogonal complement of span(XFix) The computation is performed
6 % explicitly using the gram matrix.
7 %
8 % Parameters:
9 % X: the matrix of column vectors on which the PCA shall be applied
10 % XFix: matrix of column vectors spanning a fix space to which the vectors
11 % in 'X' shall be orthonormalized before the PCA.
12 % A: inner product matrix defining the scalar product `\langle x, y
13 % \rangle = y^t A x`. (default='1')
14 % k: maximum number of principal components to be computed. (default=
15 % number of columns in 'X').
16 % method: string specifying the orthonormalization method, either 'qr' or
17 % 'gram-schmidt'. (default='gram-schmidt')
18 % epsilon: tolerance for the absolute value of prinicipal compent's
19 % eigenvalues. If the eigenvalue is below, the principal component
20 % is discarded. (default='eps')
21 %
22 % Return values:
23 % B: matrix with computed principal component vectors as column vectors
24 % evalues: eigenvalues of the gram matrix, necessary for calculating
25 % the PCA error
26 
27 
28 % 5.10.2005 Bernard Haasdonk
29 
30 %keyboard;
31 
32 %epsilon = 1e-10;
33 
34 % trivial weighting if matrix is not given:
35 if nargin < 3 || isempty(A)
36  A = 1;
37 end;
38 
39 if nargin < 4 || isempty(k)
40  k = size(X,2);
41 end;
42 
43 if nargin < 5 || isempty(method)
44  method = 'qr';
45 end;
46 
47 if nargin < 6
48  epsilon = eps; % = 2e-16
49 end;
50 
51 % the following is expensive, use of matlab-function instead ?
52 XFixON = orthonormalize(XFix,A,[],method);
53 %keyboard;
54 %XFixON = delzerocolumns(XFixON,[],A);
55 
56 if ~isempty(XFix)
57  Xo = X - XFixON * (XFixON' * A * X);
58 else
59  Xo = X;
60 end;
61 
62 clear('XFix');
63 clear('XFixON');
64 clear('X');
65 
66 %if size(Xo,1)<size(Xo,2) % ordinary correlation matrix
67 % [e,v] = eig(Xo*Xo');
68 % e = e(:,end:-1:1);
69 % evalues = diag(v);
70 % evalues = evalues(end:-1:1);
71 % fi = find(abs(evalues)>epsilon);
72 % e = e(:,fi);
73 %else
74 % via gram matrix of orthogonalized trajectory
75 K = Xo'*A*Xo;
76 K = 0.5*(K+K'); % required for rounding problems
77 
78 % generate descending list of eigenvectors/values:
79 if k<size(K,2)
80  [ep,vp] = eigs(K,k);
81  evalues = diag(vp);
82 else
83  % use eigendecomposition:
84  [ep,vp] = eig(K);
85  ep = ep(:,end:-1:1);
86  evalues = diag(vp);
87  evalues = evalues(end:-1:1);
88  % svd gives similar results:
89  %[ep,vp,dummy] = svd(K);
90  %evalues = diag(vp);
91 end;
92 fi = find(abs(evalues)>=epsilon);
93 %fi = 1:length(evalues);
94 
95 
96 % project Xo vectors on eigenvectors
97 B = Xo * ep(:,fi) * diag(evalues(fi).^(-0.5));
98 %end;
99 
100 clear('Xo');
101 
102 % ensure that only real valued vectors are returned
103 while (~isreal(B))
104 % disp('complex eigenvector occured: please check!');
105 % keyboard;
106  B= B(:,1:end-1);
107 end;
108 
109 % the following is necessary for correct scaling and improving the
110 % orthogonality, i.e. e' A e => identity up to 1e-8
111 % B' A B => identity up to 1e-16
112 %(XFix,A,[],method);
113 B = orthonormalize(B,A,epsilon,method);
114 %max(max(abs(eye(size(B,2))-B' * A * B)))
115 
116 %keyboard;
117 
function Xdel = delzerocolumns(X, epsilon, A)
function deleting zero-columns from matrix X.