-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdualpca.m
68 lines (61 loc) · 2.32 KB
/
dualpca.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
function [alpha,L,Knew,Ktestnew,Ktestvstest] = dualpca(K,Ktest,k)
%function [alpha,L] = dualpca(K,Ktest,k)
%
% Performs dual PCA
%
%INPUTS
% K = the kernel matrix of the training set (ell x ell)
% Ktest = the test kernel matrix ((ell+1) x t), containing
% the kernel evaluations between test sample j and
% training sample i at position (i,j), and where the
% last row contains the kernel evaluations of the
% samples with themselves
% k = the number of components
%
%OUTPUTS
% alpha = the k dual vectors (i.e., the training features)
% L = a column vector containing the corresponding variances
% Knew = the new kernel matrix based on these features
% Ktestnew = the new test kernel matrix based on these features
% Ktestvstest = the test versus test kernel matrix (t x t)
% based on these features
%
%
%For more info, see www.kernel-methods.net
% K is the kernel matrix of the training points
% inner products between ell training and t test points
% are stored in matrix Ktest of dimension (ell + 1) x t
% last entry in each column is inner product with self
% k gives dimension of projection space
% V is ell x k matrix storing the first k eigenvectors
% L is k x k diagonal matrix with eigenvalues
%
%
%For more info, see www.support-vector.net
ell = size(K,1);
D = sum(K) / ell;
E = sum(D) / ell;
J = ones(ell,1) * D;
K = K - J - J' + E * ones(ell, ell);
[V, L] = eigs(K, k, 'LM');
invL = diag(1./diag(L)); % inverse of L
sqrtL = diag(sqrt(diag(L))); % sqrt of eigenvalues
invsqrtL = diag(1./diag(sqrtL)); % inverse of sqrtL
%TestFeat = invsqrtL * V' * Ktest(1:end-1,:);
%TrainFeat = sqrtL * V'; % = invsqrtL * V' * K;
% Note that norm(TrainFeat, 'fro') = sum-squares of
% norms of projections = sum(diag(L)).
% Hence, average squared norm not captured (residual) =
% (sum(diag(K)) - sum(diag(L)))/ell
% If we need the new inner product information:
Knew = V * L * V'; % = TrainFeat' * TrainFeat;
if ~isempty(Ktest)
% between training and test
Ktestnew = V * V' * Ktest(1:end-1,:);
% and between test and test
Ktestvstest = Ktest(1:end-1,:)'*V*invL*V'*Ktest(1:end-1,:);
% The average sum-squared residual of the test points is
sum(Ktest(ell + 1,:) - diag(Ktestvstest)')/size(Ktest,2)
end
alpha=V;
L = diag(L);