forked from andrewssobral/nway
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fnipals.m
216 lines (200 loc) · 6.05 KB
/
fnipals.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
function [T,P]=fnipals(X,w,T)
%FNIPALS nipals algorithm for PCA
%
% function [T,P]=fnipals(X,w,T)
%
% 'fnipals.m'
%
% This algorithm requires the presence of:
% 'missmean.m'
%
% ----------------------------------------------------
% Find eigenvectors according to NIPALS
% ----------------------------------------------------
%
% [T,P]=fnipals(X,w,T);
% [T,P]=fnipals(X,w);
%
% T is found so that X = T*P', s.t ||T||=1 and T'T=I
%
% X : The matrix to be decomposed.
% w : Number of factors to extract.
% If w is high (perhaps>20) consider using SVD.
% T : Initial guess of the solution, optional.
% If T is not specified, a little time will
% be used on finding orthogonal random
% starting values.
%
% You may want to calculate P afterwards by typing 'P=X*T'.
% Note that the T returned is orthonormal.
% Calculation of P is left of this implementation to save FLOP's.
% It handles missing values NaNs (very dispersed, less than 15%)
% If the problem is small enough you would prefer the SVD rather
% than NIPALS for finding T. NIPALS may be inaccurate when
% extracting too many factors, i.e., many more than the rank
% of X.
%scalar ConvLim WarnLim ItMax a b i
% $ Version 1.01 $ Date 18. June 1998 $ Not compiled $
% Copyright (C) 1995-2006 Rasmus Bro & Claus Andersson
% Copenhagen University, DK-1958 Frederiksberg, Denmark, [email protected]
%
ConvLim=1e-12;
WarnLim=1e-4;
ConvLimMiss=100*ConvLim;
ItMax=100;
filename='fnipals.m';
[a b]=size(X);
if (w>a | w>b) | w<1,
help(filename);
error(['Error in ' filename ': Number of factors to extract is invalid!'])
end;
np=isnan(X);
MissingExist=any(np);
if ~exist('T'),
T=orth(randn(a,w));
end;
if exist('P'),
P=[];
end;
if ~MissingExist
if (size(T) == [a w]),
if a>b,
P=X'*T;
l2=Inf;
Z=X'*X;
for i=1:w,
p=P(:,i);
d=1;
it=0;
while (d>ConvLim) & (it<ItMax),
it=it+1;
p=Z*p;
l1=sqrt(p'*p);
p=p/l1;
d=(l1-l2)^2;
l2=l1;
end;
P(:,i)=sqrt(l1)*p;
Z=Z-P(:,i)*P(:,i)';
WarnLim=sqrt(l1)/1000;
if it>=ItMax & d>WarnLim,
disp('FNIPALS, High-X: Iterated up to the ItMax limit!')
disp('FNIPALS, High-X: The solution has not converged!')
end;
end;
T=X*P;
else
P=[];
l2=Inf;
Z=X*X';
for i=1:w,
t=T(:,i);
d=1;
it=0;
while (d>ConvLim) & (it<ItMax),
it=it+1;
t=Z*t;
l1=sqrt(t'*t);
t=t/l1;
d=(l1-l2).^2;
l2=l1;
end;
T(:,i)=sqrt(l1)*t;
Z=Z-T(:,i)*T(:,i)';
WarnLim=sqrt(l1)/1000;
if it>=ItMax & d>WarnLim,
disp('FNIPALS, Wide-X: Iterated up to the ItMax limit!')
disp('FNIPALS, Wide-X: The solution has not converged!')
end;
end;
end;
T=gsm(T);
else
error(['Error in ' filename ': Number of factors to extract is invalid!'])
end;
else
MissIdx=find(np);
[i j]=find(np);
mnx=missmean(X)/2;
mny=missmean(X')/2;
n=size(i,1);
for k=1:n,
i_i=i(k);
j_j=j(k);
X(i_i,j_j) = mny(i_i) + mnx(j_j);
end;
mnz=(missmean(mnx)+missmean(mny))/2;
ssmisold=sum(sum( X(MissIdx).^2 ));
sstotold=sum(sum( X.^2 ));
ssrealold=sstotold-ssmisold;
iterate=1;
while iterate
if (size(T) == [a w]),
if a>b,
P=X'*T;
l2=Inf;
Z=X'*X;
for i=1:w,
p=P(:,i);
d=1;
it=0;
while (d>ConvLim) & (it<ItMax),
it=it+1;
p=Z*p;
l1=sqrt(p'*p);
p=p/l1;
d=(l1-l2)^2;
l2=l1;
end;
P(:,i)=sqrt(l1)*p;
Z=Z-P(:,i)*P(:,i)';
WarnLim=sqrt(l1)/1000;
if it>=ItMax & d>WarnLim,
disp('FNIPALS, High-X: Iterated up to the ItMax limit!')
disp('FNIPALS, High-X: The solution has not converged!')
end;
end;
T=X*P;
else
P=[];
l2=Inf;
Z=X*X';
for i=1:w,
t=T(:,i);
d=1;
it=0;
while (d>ConvLim) & (it<ItMax),
it=it+1;
t=Z*t;
l1=sqrt(t'*t);
t=t/l1;
d=(l1-l2).^2;
l2=l1;
end;
T(:,i)=sqrt(l1)*t;
Z=Z-T(:,i)*T(:,i)';
WarnLim=sqrt(l1)/1000;
if it>=ItMax & d>WarnLim,
disp('FNIPALS, Wide-X: Iterated up to the ItMax limit!')
disp('FNIPALS, Wide-X: The solution has not converged!')
end;
end;
end;
T=gsm(T);
else
error(['Error in ' filename ': Number of factors to extract is invalid!'])
end;
P=X'*T;
Xm=T*P';
X(MissIdx)=Xm(MissIdx);
ssmis=sum(sum( Xm(MissIdx).^2 ));
sstot=sum(sum( X.^2 ));
ssreal=sstot-ssmis;
if abs(ssreal-ssrealold)<ConvLim*ssrealold & abs(ssmis-ssmisold)<ConvLimMiss*ssmisold,
iterate=0;
end;
ssrealold=ssreal;
ssmisold=ssmis;
end;
end;
T=gsm(T);