rbmatlab  1.16.09
 All Classes Namespaces Files Functions Variables Modules Pages
PCA_fixspace.m
1 function B = PCA_fixspace(X,XFix,A,k,method,epsilon)
2 %function B = 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 
25 
26 % 5.10.2005 Bernard Haasdonk
27 
28 %keyboard;
29 
30 %epsilon = 1e-10;
31 
32 % trivial weighting if matrix is not given:
33 if nargin < 3 || isempty(A)
34  A = 1;
35 end;
36 
37 if nargin < 4 || isempty(k)
38  k = size(X,2);
39 end;
40 
41 if nargin < 5 || isempty(method)
42  method = 'qr';
43 end;
44 
45 if nargin < 6
46  epsilon = eps; % = 2e-16
47 end;
48 
49 % the following is expensive, use of matlab-function instead ?
50 XFixON = orthonormalize(XFix,A,[],method);
51 %keyboard;
52 %XFixON = delzerocolumns(XFixON,[],A);
53 
54 if ~isempty(XFix)
55  Xo = X - XFixON * (XFixON' * A * X);
56 else
57  Xo = X;
58 end;
59 
60 clear('XFix');
61 clear('XFixON');
62 clear('X');
63 
64 %if size(Xo,1)<size(Xo,2) % ordinary correlation matrix
65 % [e,v] = eig(Xo*Xo');
66 % e = e(:,end:-1:1);
67 % evalues = diag(v);
68 % evalues = evalues(end:-1:1);
69 % fi = find(abs(evalues)>epsilon);
70 % e = e(:,fi);
71 %else
72 % via gram matrix of orthogonalized trajectory
73 K = Xo'*A*Xo;
74 K = 0.5*(K+K'); % required for rounding problems
75 
76 % generate descending list of eigenvectors/values:
77 if k<size(K,2)
78  [ep,vp] = eigs(K,k);
79  evalues = diag(vp);
80 else
81  % use eigendecomposition:
82  [ep,vp] = eig(K);
83  ep = ep(:,end:-1:1);
84  evalues = diag(vp);
85  evalues = evalues(end:-1:1);
86  % svd gives similar results:
87  %[ep,vp,dummy] = svd(K);
88  %evalues = diag(vp);
89 end;
90 fi = find(abs(evalues)>=epsilon);
91 %fi = 1:length(evalues);
92 
93 
94 % project Xo vectors on eigenvectors
95 B = Xo * ep(:,fi) * diag(evalues(fi).^(-0.5));
96 %end;
97 
98 clear('Xo');
99 
100 % ensure that only real valued vectors are returned
101 while (~isreal(B))
102 % disp('complex eigenvector occured: please check!');
103 % keyboard;
104  B= B(:,1:end-1);
105 end;
106 
107 % the following is necessary for correct scaling and improving the
108 % orthogonality, i.e. e' A e => identity up to 1e-8
109 % B' A B => identity up to 1e-16
110 %(XFix,A,[],method);
111 B = orthonormalize(B,A,epsilon,method);
112 %max(max(abs(eye(size(B,2))-B' * A * B)))
113 
114 %keyboard;
115 
function Xdel = delzerocolumns(X, epsilon, A)
function deleting zero-columns from matrix X.