-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathiRRR_binary3.m
341 lines (282 loc) · 8.9 KB
/
iRRR_binary3.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
function [C,mu,A,B,Theta]=iRRR_binary3(Y,X,lam1,paramstruct)
% This function uses consensus ADMM to fit the iRRR model. It is suitable
% for binary outcomes with potentially missing entries.
%
% Model:
% -4/n*sum[logL(Y_{non-missing})] + lam1*sum(w_i*|A_i|_*) (+ 0.5*lam0*sum(w_i^2*|B_i|^2_F))
% s.t. A_i=B_i
%
% The -4/n*sum[logL(Y_{non-missing})] can be majorized to
% 1/(2n)|Ystar-1*mu'-sum(Xi*Bi)|^2_F (this involves two steps of
% majorization: first majorize the log likelihood of each observed entry of
% Y; second majorize the missing entries with the current estimation)
%
%
%
% input:
%
% Y n*q binary response data matrix (may have nan entries)
%
% X 1*K cell array, each cell is a n*p_i predictor data matrix
% Note: X1,...XK may need some sort of standardization, because
% we use a single lam0 and lam1 for different predictor sets.
% Namely, we implicitly assume the coefficients are comparable.
%
% lam1 positive scalar, tuning for nuclear norm
%
% paramstruct
% lam0 tuning for ridge penalty, default is 0
%
% weight K*1 weight vector, default: a vector of 1;
% By theory, we should use
% w(i)=(1/n)*max(svd(X{i}))*[sqrt(q)+sqrt(rank(X{i}))]; where
% X's are demeaned.
% Hueristically, one could also use w(i)=|X_i|_F
%
% randomstart 0=false (default); 1=true
%
% varyrho 0=fixed rho (default); 1=adaptive rho
% maxrho 5 (default): max rho. Unused if varyrho==0
%
% rho step size, default rho=0.1
%
% Tol default 1E-3,
%
% Niter default 500
%
% fig 1 (default) show checking figures; 0 no show
%
% Output:
%
% C sum(p_i)*q coefficient matrix, potentially low-rank
%
% mu q*1 intercept vector, for original X (not centered X)
%
% A cell arrays of length K, separate low-rank coefficient matrices
%
% B cell arrays of length K, separate coefficient matrices
%
% Theta cell arrays of length K, Lagrange parameter matrices
%
%
% Modified from iRRR_binary2 on 10/12/2017 by Gen Li
% Note: treat ridge penalty as optional
% default parameters
K=length(X);
weight=ones(K,1);
Tol=1E-3; % stopping rule
Niter=500; % max iterations
lam0=0;
varyrho=0;
rho=0.1;
maxrho=5;
randomstart=0;
fig=1;
if nargin > 3 ; % then paramstruct is an argument
if isfield(paramstruct,'lam0') ;
lam0= paramstruct.lam0 ;
end ;
if isfield(paramstruct,'weight') ;
weight = paramstruct.weight ;
end ;
if isfield(paramstruct,'Tol') ;
Tol = paramstruct.Tol ;
end ;
if isfield(paramstruct,'Niter') ;
Niter = paramstruct.Niter ;
end ;
if isfield(paramstruct,'randomstart') ;
randomstart = paramstruct.randomstart ;
end ;
if isfield(paramstruct,'varyrho') ;
varyrho = paramstruct.varyrho;
end ;
if varyrho && isfield(paramstruct,'maxrho') ;
maxrho = paramstruct.maxrho;
end;
if isfield(paramstruct,'rho') ;
rho = paramstruct.rho;
end ;
if isfield(paramstruct,'fig') ;
fig = paramstruct.fig ;
end ;
end;
[n,q]=size(Y);
missing=isnan(Y);
p=zeros(K,1);
cX=[]; % horizontally concatenated X
meanX=[];
for i=1:K
[n_,p(i)]=size(X{i});
if n_~=n
error('Samples do not match!')
end;
% first center X
meanX=[meanX,mean(X{i},1)];
X{i}=bsxfun(@minus,X{i},mean(X{i},1));
% second, normalize centered X{i}'s
X{i}=X{i}/weight(i);
cX=[cX,X{i}]; % column centered X
end;
% initial values
mu=zeros(q,1); % intercept
B=cell(K,1);
Theta=cell(K,1); % Lagrange params for B
cB=zeros(sum(p),q);% vertically concatenated B
cTheta=zeros(sum(p),q);
if randomstart
cB=randn(sum(p),q);
mu=randn(q,1);
else
for j=1:q
ind=~isnan(Y(:,j));
[temp,info]=lassoglm(cX(ind,:),Y(ind,j),'binomial','Alpha',0.05,'Lambda',0.1);
mu(j)=info.Intercept;
cB(:,j)=temp;
end;
end;
for i=1:K
B{i}=cB((sum(p(1:(i-1)))+1):sum(p(1:i)),:);
Theta{i}=cTheta((sum(p(1:(i-1)))+1):sum(p(1:i)),:);
end;
A=B; % low-rank alias
cA=cB;
%
[~,D_cX,V_cX]=svd((1/sqrt(n))*cX,'econ');
if ~varyrho % fixed rho
DeltaMat=V_cX*diag(1./(diag(D_cX).^2+lam0+rho))*V_cX'+...
(eye(sum(p))-V_cX*V_cX')/(lam0+rho); % inv(1/n*X'X+(lam0+rho)I)
end;
% check obj value
obj=ObjValue1(Y,X,A,mu,lam0,lam1); % full objective function (with penalties)
obj_ls=ObjValue1(Y,X,A,mu,0,0); % only the least square part
%%%%%%%%%%%%%%%
% MM + ADMM(one-step)
niter=0;
diff=inf;
rec_obj=[obj;obj_ls]; % record obj value
rec_Theta=[]; % Fro norm of Theta{1}
rec_primal=[]; % record total primal residual
rec_dual=[]; % record total dual residual
while niter<Niter && abs(diff)>Tol
niter=niter+1;
cB_old=cB;
%%%%%%%%%%%%% Double Majorization %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Eta = ones(n,1)*mu' + cX*cB; % current linear predictor
wY = Eta + 4*(2*Y-1).*(1-invlogit((2*Y-1).*Eta)); % working response for induced LS
wY(missing) = Eta(missing);% majorize to get rid of missing
mu=mean(wY,1)'; % new est of mu, b/c cX is col centered
wY1=bsxfun(@minus,wY,mu');
%%%%%%%%%%%%% ADMM(one-step) part %%%%%%%%%%%%%%%%%%%%
% est B
if varyrho
DeltaMat=V_cX*diag(1./(diag(D_cX).^2+lam0+rho))*V_cX'+...
(eye(sum(p))-V_cX*V_cX')/(lam0+rho);
end;
cB=DeltaMat*((1/n)*cX'*wY1+rho*cA+cTheta);
for i=1:K
B{i}=cB((sum(p(1:(i-1)))+1):sum(p(1:i)),:);
end;
% est A in parallel
% update Theta
parfor i=1:K
% est A
temp=B{i}-Theta{i}/rho;
[tempU,tempD,tempV]=svd(temp,'econ');
A{i}=tempU*SoftThres(tempD,lam1/rho)*tempV';
% update Theta
Theta{i}=Theta{i}+rho*(A{i}-B{i});
end;
% reshape cA and cTheta
for i=1:K
cA((sum(p(1:(i-1)))+1):sum(p(1:i)),:)=A{i};
cTheta((sum(p(1:(i-1)))+1):sum(p(1:i)),:)=Theta{i};
end;
% update rho
if varyrho
rho=min(maxrho,1.1*rho); % steadily increasing rho
end;
%%%%%%%%%%%%%%%% stopping rule %%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% primal and dual residuals
primal=norm(cA-cB,'fro')^2;
rec_primal=[rec_primal,primal];
dual=norm(cB-cB_old,'fro')^2;
rec_dual=[rec_dual,dual];
% objective function value
obj=ObjValue1(Y,X,A,mu,lam0,lam1);
obj_ls=ObjValue1(Y,X,A,mu,0,0);
rec_obj=[rec_obj,[obj;obj_ls]];
% stopping rule
diff=primal;
% diff=dual;
% diff=rec_obj(1,end-1)-rec_obj(1,end);
% Check Figures
if fig==1
% obj fcn values
figure(101);clf;
plot(0:niter,rec_obj(1,:),'bo-');
hold on
plot(0:niter,rec_obj(2,:),'ro-');
legend('Full Obj Value (with penalty)','Nagetive Likelihood Value')
title(['Objective function value (decrease in full=',num2str(rec_obj(1,end-1)-rec_obj(1,end)),')']);
drawnow;
% primal and dual residuals
figure(102);clf;
subplot(1,2,1)
plot(1:niter,rec_primal,'o-');
title(['Primal residual |A-B|^2: ',num2str(primal)]);
subplot(1,2,2)
plot(1:niter,rec_dual,'o-');
title(['Dual residual |B-B|^2: ',num2str(dual)]);
drawnow
figure(103);clf;
rec_Theta=[rec_Theta,norm(Theta{1},'fro')];
plot(rec_Theta,'o-');
title('Theta: Lagrange multiplier for B1');
drawnow
end;
end;
if niter==Niter
disp(['iRRR_binary does NOT converge after ',num2str(Niter),' iterations!']);
else
disp(['iRRR_binary converges after ',num2str(niter),' iterations.']);
end;
% output
% rescale parameter estimate, and add back mean
C=[];
for i=1:K
A{i}=A{i}/weight(i);
B{i}=B{i}/weight(i);
C=[C;A{i}];
end;
clear cA cB;
mu=mu-(meanX*C)'; % convert to the original scale, so that mu+X_original*C is the final linear predictor
end
function Dout=SoftThres(Din,lam)
% this function soft thresholds the diagonal values of Din
% Din is a diagonal matrix
% lam is a positive threshold
% Dout is also a diagonal matrix
d=diag(Din);
d(d>0)=max(d(d>0)-lam,0);
d(d<0)=min(d(d<0)+lam,0);
Dout=diag(d);
end
function obj=ObjValue1(Y,X,B,mu,lam0,lam1) % for binary response
% (-4/n)logL(nonmissing) + penalty
% linear predictor is 1*mu'+XB
[n,~]=size(Y);
K=length(X);
obj=0;
pred=ones(n,1)*mu'; % linear predictor
for i=1:K
pred=pred+X{i}*B{i}; %
obj=obj+lam0/2*norm(B{i},'fro')^2+lam1*sum(svd(B{i}));
end;
core=(2*Y-1).*pred;
neg4loglik=-(4/n)*sum(sum(log(invlogit(core)),'omitnan'),'omitnan'); % (-4/n)logL(nonmissing)
obj=obj +neg4loglik;
end
function out=invlogit(in)
out=exp(in)./(1+exp(in));
end