-
Notifications
You must be signed in to change notification settings - Fork 24
/
pca.m
354 lines (302 loc) · 8.7 KB
/
pca.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
function [U,S,V] = pca(A,k,its,l)
%PCA Low-rank approximation in SVD form.
%
%
% [U,S,V] = PCA(A) constructs a nearly optimal rank-6 approximation
% USV' to A, using 2 full iterations of a block Lanczos method
% of block size 6+2=8, started with an n x 8 random matrix,
% when A is m x n; the ref. below explains "nearly optimal."
% The smallest dimension of A must be >= 6 when A is
% the only input to PCA.
%
% [U,S,V] = PCA(A,k) constructs a nearly optimal rank-k approximation
% USV' to A, using 2 full iterations of a block Lanczos method
% of block size k+2, started with an n x (k+2) random matrix,
% when A is m x n; the ref. below explains "nearly optimal."
% k must be a positive integer <= the smallest dimension of A.
%
% [U,S,V] = PCA(A,k,its) constructs a nearly optimal rank-k approx. USV'
% to A, using its full iterations of a block Lanczos method
% of block size k+2, started with an n x (k+2) random matrix,
% when A is m x n; the ref. below explains "nearly optimal."
% k must be a positive integer <= the smallest dimension of A,
% and its must be a nonnegative integer.
%
% [U,S,V] = PCA(A,k,its,l) constructs a nearly optimal rank-k approx.
% USV' to A, using its full iterates of a block Lanczos method
% of block size l, started with an n x l random matrix,
% when A is m x n; the ref. below explains "nearly optimal."
% k must be a positive integer <= the smallest dimension of A,
% its must be a nonnegative integer,
% and l must be a positive integer >= k.
%
%
% The low-rank approximation USV' is in the form of an SVD in the sense
% that the columns of U are orthonormal, as are the columns of V,
% the entries of S are all nonnegative, and the only nonzero entries
% of S appear in non-increasing order on its diagonal.
% U is m x k, V is n x k, and S is k x k, when A is m x n.
%
% Increasing its or l improves the accuracy of the approximation USV'
% to A; the ref. below describes how the accuracy depends on its and l.
%
%
% Note: PCA invokes RAND. To obtain repeatable results,
% invoke RAND('seed',j) with a fixed integer j before invoking PCA.
%
% Note: PCA currently requires the user to center and normalize the rows
% or columns of the input matrix A before invoking PCA (if such
% is desired).
%
% Note: The user may ascertain the accuracy of the approximation USV'
% to A by invoking DIFFSNORM(A,U,S,V).
%
%
% inputs (the first is required):
% A -- matrix being approximated
% k -- rank of the approximation being constructed;
% k must be a positive integer <= the smallest dimension of A,
% and defaults to 6
% its -- number of full iterations of a block Lanczos method to conduct;
% its must be a nonnegative integer, and defaults to 2
% l -- block size of the block Lanczos iterations;
% l must be a positive integer >= k, and defaults to k+2
%
% outputs (all three are required):
% U -- m x k matrix in the rank-k approximation USV' to A,
% where A is m x n; the columns of U are orthonormal
% S -- k x k matrix in the rank-k approximation USV' to A,
% where A is m x n; the entries of S are all nonnegative,
% and its only nonzero entries appear in nonincreasing order
% on the diagonal
% V -- n x k matrix in the rank-k approximation USV' to A,
% where A is m x n; the columns of V are orthonormal
%
%
% Example:
% A = rand(1000,2)*rand(2,1000);
% A = A/normest(A);
% [U,S,V] = pca(A,2,0);
% diffsnorm(A,U,S,V)
%
% This code snippet produces a rank-2 approximation USV' to A such that
% the columns of U are orthonormal, as are the columns of V, and
% the entries of S are all nonnegative and are zero off the diagonal.
% diffsnorm(A,U,S,V) outputs an estimate of the spectral norm
% of A-USV', which should be close to the machine precision.
%
%
% Reference:
% Nathan Halko, Per-Gunnar Martinsson, and Joel Tropp,
% Finding structure with randomness: Stochastic algorithms
% for constructing approximate matrix decompositions,
% arXiv:0909.4061 [math.NA; math.PR], 2009
% (available at http://arxiv.org).
%
%
% See also PCACOV, PRINCOMP, SVDS.
%
% Copyright 2009 Mark Tygert.
%
% Check the number of inputs.
%
if(nargin < 1)
error('MATLAB:pca:TooFewIn',...
'There must be at least 1 input.')
end
if(nargin > 4)
error('MATLAB:pca:TooManyIn',...
'There must be at most 4 inputs.')
end
%
% Check the number of outputs.
%
if(nargout ~= 3)
error('MATLAB:pca:WrongNumOut',...
'There must be exactly 3 outputs.')
end
%
% Set the inputs k, its, and l to default values, if necessary.
%
if(nargin == 1)
k = 6;
its = 2;
l = k+2;
end
if(nargin == 2)
its = 2;
l = k+2;
end
if(nargin == 3)
l = k+2;
end
%
% Check the first input argument.
%
if(~isfloat(A))
error('MATLAB:pca:In1NotFloat',...
'Input 1 must be a floating-point matrix.')
end
if(isempty(A))
error('MATLAB:pca:In1Empty',...
'Input 1 must not be empty.')
end
%
% Retrieve the dimensions of A.
%
[m n] = size(A);
%
% Check the remaining input arguments.
%
if(size(k,1) ~= 1 || size(k,2) ~= 1)
error('MATLAB:pca:In2Not1x1',...
'Input 2 must be a scalar.')
end
if(size(its,1) ~= 1 || size(its,2) ~= 1)
error('MATLAB:pca:In3Not1x1',...
'Input 3 must be a scalar.')
end
if(size(l,1) ~= 1 || size(l,2) ~= 1)
error('MATLAB:pca:In4Not1x1',...
'Input 4 must be a scalar.')
end
if(k <= 0)
error('MATLAB:pca:In2NonPos',...
'Input 2 must be > 0.')
end
if((k > m) || (k > n))
error('MATLAB:pca:In2TooBig',...
'Input 2 must be <= the smallest dimension of Input 1.')
end
if(its < 0)
error('MATLAB:pca:In3Neg',...
'Input 3 must be >= 0.')
end
if(l < k)
error('MATLAB:pca:In4ltIn2',...
'Input 4 must be >= Input 2.')
end
%
% SVD A directly if (its+1)*l >= m/1.25 or (its+1)*l >= n/1.25.
%
if(((its+1)*l >= m/1.25) || ((its+1)*l >= n/1.25))
if(~issparse(A))
try
[U,S,V] = svd(A,'econ');
error(lastwarn);
catch
[Q,R]=qr(A,0);
[V,S,U]=svd(R','econ');
U=Q*U;
end
end
if(issparse(A))
[U,S,V] = svd(full(A),'econ');
end
%
% Retain only the leftmost k columns of U, the leftmost k columns of V,
% and the uppermost leftmost k x k block of S.
%
U = U(:,1:k);
V = V(:,1:k);
S = S(1:k,1:k);
return
end
if(m >= n)
%
% Apply A to a random matrix, obtaining H.
%
rand('seed',rand('seed'));
if(isreal(A))
H = A*(2*rand(n,l)-ones(n,l));
end
if(~isreal(A))
H = A*( (2*rand(n,l)-ones(n,l)) + i*(2*rand(n,l)-ones(n,l)) );
end
rand('twister',rand('twister'));
%
% Initialize F to its final size and fill its leftmost block with H.
%
F = zeros(m,(its+1)*l);
F(1:m, 1:l) = H;
%
% Apply A*A' to H a total of its times,
% augmenting F with the new H each time.
%
for it = 1:its
H = (H'*A)';
H = A*H;
F(1:m, (1+it*l):((it+1)*l)) = H;
end
clear H;
%
% Form a matrix Q whose columns constitute an orthonormal basis
% for the columns of F.
%
[Q,R,E] = qr(F,0);
clear F R E;
%
% SVD Q'*A to obtain approximations to the singular values
% and right singular vectors of A; adjust the left singular vectors
% of Q'*A to approximate the left singular vectors of A.
%
[U2,S,V] = svd(Q'*A,'econ');
U = Q*U2;
clear Q U2;
%
% Retain only the leftmost k columns of U, the leftmost k columns of V,
% and the uppermost leftmost k x k block of S.
%
U = U(:,1:k);
V = V(:,1:k);
S = S(1:k,1:k);
end
if(m < n)
%
% Apply A' to a random matrix, obtaining H.
%
rand('seed',rand('seed'));
if(isreal(A))
H = ((2*rand(l,m)-ones(l,m))*A)';
end
if(~isreal(A))
H = (( (2*rand(l,m)-ones(l,m)) + i*(2*rand(l,m)-ones(l,m)) )*A)';
end
rand('twister',rand('twister'));
%
% Initialize F to its final size and fill its leftmost block with H.
%
F = zeros(n,(its+1)*l);
F(1:n, 1:l) = H;
%
% Apply A'*A to H a total of its times,
% augmenting F with the new H each time.
%
for it = 1:its
H = A*H;
H = (H'*A)';
F(1:n, (1+it*l):((it+1)*l)) = H;
end
clear H;
%
% Form a matrix Q whose columns constitute an orthonormal basis
% for the columns of F.
%
[Q,R,E] = qr(F,0);
clear F R E;
%
% SVD A*Q to obtain approximations to the singular values
% and left singular vectors of A; adjust the right singular vectors
% of A*Q to approximate the right singular vectors of A.
%
[U,S,V2] = svd(A*Q,'econ');
V = Q*V2;
clear Q V2;
%
% Retain only the leftmost k columns of U, the leftmost k columns of V,
% and the uppermost leftmost k x k block of S.
%
U = U(:,1:k);
V = V(:,1:k);
S = S(1:k,1:k);
end