0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032
0033
0034
0035
0036
0037
0038
0039
0040
0041
0042
0043
0044
0045
0046
0047
0048
0049
0050
0051
0052
0053
0054
0055
0056
0057
0058
0059
0060
0061
0062
0063
0064
0065
0066
0067
0068
0069
0070
0071
0072
0073
0074
0075
0076
0077
0078
0079
0080
0081
0082
0083
0084
0085
0086
0087 function out = l1homotopy(A, y, opts)
0088
0089 N = size(A,2);
0090 M = size(A,1);
0091
0092
0093
0094 if isfield(opts,'W'); W = opts.W; else W = opts.tau; end
0095 if isfield(opts,'xh_old');
0096
0097 xh_old = opts.xh_old;
0098
0099 pk_old = opts.pk_old;
0100
0101 gamma_xh = opts.gamma;
0102
0103 u = opts.u;
0104 else
0105 xh_old = zeros(N,1);
0106 end
0107
0108
0109 if norm(xh_old) == 0;
0110 pk_old = -A'*y;
0111 [tau_old gamma_xh] = max(abs(pk_old));
0112 z_x = zeros(N,1);
0113 z_x(gamma_xh) = -sign(pk_old(gamma_xh));
0114 u = -W.*z_x-pk_old;
0115 pk_old = pk_old+u;
0116 end
0117
0118
0119 if norm(y) == 0;
0120 out = opts;
0121 out.x_out = zeros(N,1);
0122 out.gamma = [];
0123 out.iter = 0;
0124 out.time = 0;
0125 disp('input is a zero vector');
0126 return;
0127 end
0128
0129 if nnz(abs(A'*y) < W) == N
0130 out = opts;
0131 out.x_out = zeros(N,1);
0132 out.gamma = [];
0133 out.iter = 0;
0134 out.time = 0;
0135 disp('output is a zero vector');
0136 return;
0137 end
0138
0139
0140 W = ones(N,1).*W;
0141
0142
0143 if isfield(opts,'maxiter'); maxiter = opts.maxiter; else maxiter = 2*N; end
0144
0145 if isfield(opts,'nonneg'); nonneg = opts.nonneg; else nonneg = 0; end
0146
0147 if isfield(opts,'Te'); Te = opts.Te; else Te = inf; end
0148
0149 if isfield(opts,'record'); err_record = opts.record; else err_record = 0; end
0150 if err_record; err_fun = opts.err_fun; end
0151
0152 if isfield(opts,'debias'); debias = opts.debias; else debias = 0; end
0153
0154
0155 if isfield(opts,'early_terminate'); early_terminate = opts.early_terminate; else early_terminate = 0; end
0156
0157 if isfield(opts,'verbose'); verbose = opts.verbose; else verbose = 0; end
0158
0159 if isfield(opts,'plots'); plots = opts.plots; else plots = 0; end
0160
0161
0162 t0 = cputime;
0163
0164
0165 epsilon = 0;
0166
0167 temp_gamma = zeros(N,1);
0168 temp_gamma(gamma_xh) = gamma_xh;
0169 gamma_xc = find([1:N]' ~= temp_gamma);
0170
0171 z_x = zeros(N,1);
0172 z_x(gamma_xh) = -sign(pk_old(gamma_xh));
0173 pk_old(gamma_xh) = sign(pk_old(gamma_xh)).*W(gamma_xh);
0174 pk = pk_old;
0175 dk = 0*pk;
0176 xk_1 = xh_old;
0177
0178
0179 idelta = gamma_xh(1); flag = 1;
0180
0181
0182 in_delx = [];
0183 delx_mode = opts.delx_mode;
0184 rhs = u;
0185
0186 if norm(xh_old)==0 && length(gamma_xh) == 1
0187 update_mode = 'init0';
0188 else
0189 update_mode = 'init1';
0190 end
0191 update_delx;
0192
0193
0194 done = 0;
0195 iter = 0;
0196 itr_history = [];
0197 error_table = [];
0198 if err_record
0199 error_table = [epsilon err_fun(xk_1) length(gamma_xh)];
0200 end
0201
0202 while iter < maxiter
0203 iter = iter+1;
0204
0205
0206
0207 x_k = xk_1;
0208
0209
0210 delx_vec = zeros(N,1);
0211 delx_vec(gamma_xh) = delx;
0212
0213 if ~isempty(idelta) && (sign(delx_vec(idelta)) == sign(pk_old(idelta)) && abs(x_k(idelta)) == 0)
0214 delta = 0; flag = 0;
0215 else
0216 pk = pk_old;
0217
0218
0219 dk_temp = mvprod(A,delx_vec,gamma_xh,0);
0220 dk = A'*dk_temp-u;
0221
0222
0223 in = [];
0224
0225
0226
0227 in.shrinkage_flag = 2;
0228 in.nonneg = nonneg;
0229 in.pk = pk; in.dk = dk;
0230 in.ak = W;
0231 in.gamma = gamma_xh; in.gamma_c = gamma_xc;
0232 in.delx_vec = delx_vec; in.x = xk_1;
0233 out = compute_delta_v2(in);
0234 delta = out.delta; idelta = out.idelta;
0235 flag = out.flag;
0236 end
0237 e0 = epsilon;
0238 epsilon = e0 + delta;
0239
0240 if epsilon > 1
0241 delta_end = 1-e0;
0242 xk_1 = x_k + delta_end*delx_vec;
0243 pk_old = pk + delta_end*dk;
0244 pk_old(gamma_xh) = sign(pk_old(gamma_xh)).*W(gamma_xh);
0245 break;
0246 end
0247
0248 xk_1 = x_k + delta*delx_vec;
0249 gamma_old = gamma_xh;
0250
0251 itr_history = [itr_history; idelta delta flag];
0252
0253
0254 update_supp;
0255
0256 pk_old = pk+delta*dk;
0257 pk_old([gamma_xh; idelta]) = sign(pk_old([gamma_xh; idelta])).*W([gamma_xh; idelta]);
0258
0259
0260 z_x = -sign(pk_old);
0261 rhs = u;
0262 update_mode = 'update';
0263 update_delx;
0264
0265
0266
0267
0268
0269
0270
0271
0272
0273 if early_terminate
0274 if length(gamma_xh) < M/2
0275 xhat = zeros(N,1);
0276
0277 switch delx_mode
0278 case 'mil'
0279 xhat(gamma_xh) = iAtA*(A(:,gamma_xh)'*y);
0280 case {'qr','chol'}
0281 xhat(gamma_xh) = R\(R'\(A(:,gamma_xh)'*y));
0282 end
0283 if norm(y-A*xhat) < tau
0284 xk_1 = xhat;
0285 break;
0286 end
0287 end
0288 end
0289
0290 if err_record
0291 error_table = [error_table; epsilon err_fun(xk_1) length(gamma_xh)];
0292 end
0293
0294
0295 constr_violation = nnz((abs(pk_old(gamma_xc))-W(gamma_xc))>1e-10);
0296 sign_violation = nnz((sign(pk_old(gamma_xh))+sign(xk_1(gamma_xh)))>1);
0297 if constr_violation && nonneg == 0
0298 chk = gamma_xc((abs(pk_old(gamma_xc))-W(gamma_xc))>1e-10);
0299 stp = 1;
0300 fprintf('problem... with constraint violation -- %s \n', mfilename);
0301 fprintf('Refactorize the matrix... recompute delx (consider using qr delx_update instead of mil) \n');
0302
0303 update_mode = 'init0';
0304 update_delx;
0305 end
0306 if sign_violation>=1 && nonneg == 0
0307 chk = gamma_xh(sign(pk_old(gamma_xh))+sign(xk_1(gamma_xh))>1);
0308 stp = 1;
0309 fprintf('problem... sign mismatch -- %s \n',mfilename);
0310 fprintf('Refactorize the matrix... recompute delx (consider using qr delx_update instead of mil)\n');
0311 update_mode = 'init0';
0312 update_delx;
0313 end
0314
0315
0316 if mod(iter-1,verbose) == 0 && verbose
0317 fprintf(['iter = %d, delta = %3.4g, idelta = %d, flag = %d.\n'], iter, delta, idelta, flag);
0318 end
0319 if mod(iter-1,plots) == 0 && plots
0320 fig1 = figure(1234);
0321 subplot(2,1,1)
0322 hold off
0323 plot(pk,'.r', 'MarkerSize',14);
0324 hold on;
0325 plot(pk_old, 'LineWidth',1);
0326
0327 if flag == 1
0328 plot(idelta, pk_old(idelta),'or','MarkerSize',18,'LineWidth',2);
0329 text(idelta, pk_old(idelta)*1.1, ['Incoming \gamma = ',num2str(idelta)],'FontSize',14);
0330 else
0331 plot(idelta, pk_old(idelta),'ok','MarkerSize',18,'LineWidth',2);
0332 text(idelta, pk_old(idelta)*1.1, ['Outgoing \gamma = ',num2str(idelta)],'FontSize',14);
0333 end
0334 set(gca,'FontSize',16, 'XLim',[1 N] );
0335 title(sprintf('BPDN shrinkage constraints: N = %d, M = %d', N, M));
0336 plot(1:N, W,'--k','MarkerSize',12);
0337 plot(1:N, -W, '--k','MarkerSize',12);
0338
0339 figure(fig1);
0340 subplot(2,1,2)
0341 hold off
0342 plot(x_k,'.r','MarkerSize',14); hold on;
0343 plot(xk_1,'LineWidth',1);
0344 if flag == 0
0345 plot(idelta, 0,'ok', 'MarkerSize',18,'LineWidth',2);
0346 else
0347 plot(idelta, 0,'or', 'MarkerSize',18,'LineWidth',2);
0348 end
0349 set(gca,'FontSize',16,'XLim',[1 N]);
0350 title(['Solution estimate at \epsilon = ',num2str(epsilon), ', iter. = ', num2str(iter)]);
0351
0352 if iter == 1 && verbose
0353 disp(' ');
0354 disp('Every frame in the figure corresponds to a critical point on the homotopy path.')
0355 disp('Circle represents an incoming element, star represents an outgoing element.');
0356 disp(' ');
0357 disp('Put pause somewhere in the code to see this. ');
0358 disp('For now press some key to continue...');
0359 pause
0360 end
0361 end
0362
0363 end
0364
0365
0366 if debias
0367 x_out = zeros(N,1);
0368 switch delx_mode
0369 case 'mil'
0370 x_out(gamma_xh) = iAtA*(A(:,gamma_xh)'*y);
0371 case {'qr','chol'}
0372 x_out(gamma_xh) = R\(R'\(A(:,gamma_xh)'*y));
0373 end
0374 else
0375 x_out = xk_1;
0376 end
0377
0378
0379 if err_record
0380 error_table = [error_table; epsilon err_fun(x_out) length(gamma_xh)];
0381 end
0382 total_iter = iter;
0383 total_time = cputime-t0;
0384
0385
0386 out = opts;
0387 out.x_out = x_out;
0388 out.gamma = gamma_xh;
0389 out.iter = total_iter;
0390 out.time = total_time;
0391 out.error_table = error_table;
0392 out.pk = pk_old;
0393 switch delx_mode
0394 case 'mil'
0395 out.iAtA = iAtA;
0396 case 'qr'
0397 out.Q = Q;
0398 out.R = R;
0399 case 'chol'
0400 out.R = R;
0401 case 'qrM'
0402 out.Q0 = Q0;
0403 out.R0 = R0;
0404 end
0405