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 function [A,weights,dx,v,W,Nmatrix]=...
0042 estimate_dem_robust...
0043 (N,U,Np,Nr,Mc,poi,xa,weights,sigma_k,iter,iter_switch,type_robust,L1,...
0044 out_in,print_type,plot_type);
0045
0046 typec=1;
0047
0048
0049 k= 2;
0050
0051
0052
0053 if print_type > 0
0054 start_time_sol = cputime;
0055 end
0056 A = spalloc(N,U,6*N);
0057 b = zeros(N,1);
0058 W = spalloc(N,N,N);
0059 Ac = spalloc(N,U,6*N);
0060 bc = zeros(N,1);
0061 Wc = spalloc(N,N,N);
0062
0063
0064 tic
0065 for p = 1:Np
0066 x = poi(p,1);
0067 y = poi(p,2);
0068 w = 1/poi(p,4)*sqrt(weights(p))*out_in(p);
0069 i = floor(x);
0070 j = floor(y);
0071 aa = x-i;
0072 bb = y-j;
0073 maa = 1-aa;
0074 mbb= 1-bb;
0075 g1=maa*mbb;
0076 g2=aa*mbb;
0077 g3=maa*bb;
0078 g4=aa*bb;
0079
0080
0081
0082
0083
0084
0085
0086
0087
0088
0089
0090 A(p,i+j*Nr+1) = g1*w;
0091 A(p,i+j*Nr+2) = g2*w;
0092 A(p,i+(j+1)*Nr+1) = g3*w;
0093 A(p,i+(j+1)*Nr+2) = g4*w;
0094 delta_l = poi(p,3) - ...
0095 (g1*xa(i+j*Nr+1) +...
0096 g2*xa(i+j*Nr+2) +...
0097 g3*xa(i+(j+1)*Nr+1)+...
0098 g4*xa(i+(j+1)*Nr+2));
0099 b(p) = delta_l * w;
0100 W(p,p)=w;
0101 end
0102
0103 time_for_A_points=toc
0104
0105
0106 if print_type > 0
0107 time_for_building_An=toc
0108 end
0109
0110
0111 tic
0112 Icc = Np;
0113 Irr = Np + (Nr-2)*Mc;
0114 Irc = Np + (Nr-2)*Mc + Nr*(Mc-2);
0115
0116
0117
0118
0119 Jcc = [-1,0,+1];
0120 Vcc = [ 1 -2 1];
0121 wcc = 1/sigma_k;
0122
0123
0124 Jrr = [-Nr,0,+Nr];
0125 Vrr = [1 -2 1];
0126 wrr = 1/sigma_k;
0127
0128
0129
0130
0131 Jrc = [-Nr-1,-Nr,-1,0];
0132 Vrc = [1 -1 -1 1];
0133
0134 wrc = 1/sigma_k/sqrt(2);
0135
0136
0137 for j=1:Mc
0138 for i=2:Nr-1
0139 IU = i+(j-1)*Nr;
0140
0141 Icc = Icc+1;
0142 A(Icc,IU+Jcc) = Vcc * wcc * sqrt(weights(Icc));
0143 delta_cc = 0-xa(IU+Jcc)' * Vcc';
0144 b(Icc) = delta_cc * wcc * sqrt(weights(Icc));
0145 W(Icc,Icc) = wcc * sqrt(weights(Icc));
0146 end
0147 end
0148
0149
0150 for j=2:Mc-1
0151 for i=1:Nr
0152 IU = i+(j-1)*Nr;
0153
0154 Irr = Irr+1;
0155 A(Irr,IU+Jrr) = Vrr * wrr * sqrt(weights(Irr));
0156 delta_rr = 0-xa(IU+Jrr)' * Vrr';
0157 b(Irr) = delta_rr * wrr * sqrt(weights(Irr));
0158 W(Irr,Irr) = wrr * sqrt(weights(Irr));
0159 end
0160 end
0161
0162
0163
0164
0165 for j=2:Mc
0166 for i=2:Nr
0167 IU = i+(j-1)*Nr;
0168
0169 Irc = Irc+1;
0170 A(Irc,IU+Jrc) = Vrc * wrc * sqrt(weights(Irc));
0171 delta_rc = 0-xa(IU+Jrc)' * Vrc';
0172 b(Irc) = delta_rc * wrc * sqrt(weights(Irc));
0173 W(Irc,Irc) = wrc * sqrt(weights(Irc));
0174 end
0175 end
0176 time_for_A_prior=toc
0177
0178 if print_type > 1
0179 if U < 1000
0180 Ab = 2*[full(A)]
0181 null_A_prior = U-rank(full(A(U+1:end,:)));
0182 rankA = rank(full(A(U+1:end,:)));
0183 end
0184
0185
0186 end
0187
0188
0189
0190
0191 tic
0192 Nmatrix = A'*A;
0193
0194
0195 if iter==1 && plot_type > 1
0196 size_N_matrix = size(Nmatrix);
0197 non_zeros_N_matrix =nnz(Nmatrix);
0198 figure
0199 subplot(1,3,1)
0200 spy(Nmatrix)
0201 title('N')
0202 end
0203
0204 permx = symamd(Nmatrix);
0205 Aperm = A(:,permx);
0206
0207
0208
0209
0210 if iter==1 && plot_type > 1
0211 subplot(1,3,2)
0212 spy(Aperm'*Aperm)
0213 title('N, sorted')
0214 end
0215
0216
0217 [C,R] = qr(Aperm,b,0);
0218
0219 dxperm = R\C;
0220 Pm = speye(U);
0221 Pm = Pm(:,permx);
0222 dx = Pm*dxperm;
0223
0224
0225
0226 time_for_QR_sorted=toc
0227
0228 if iter==1 && plot_type > 1
0229 subplot(1,3,3)
0230 spy(R)
0231 fill_in = nnz(R)-(nnz(Nmatrix)/2+U/2)
0232 percentage_nnz = nnz(R)/(U*(U+1)/2)
0233 relative_fill_in = fill_in/(nnz(Nmatrix)/2+U/2)
0234 title(strcat('reduced N sorted, fill in =',num2str(fill_in)))
0235 end
0236
0237
0238 if iter==1 && plot_type > 1
0239 figure
0240 subplot(1,2,1)
0241 spy(A)
0242 title('A')
0243 subplot(1,2,2)
0244 spy(Aperm)
0245 title('A sorted')
0246 end
0247
0248 dx_sorted = dx;
0249
0250 if U < 1600 && plot_type > 0
0251 Aqt = R\Aperm';
0252 figure
0253 spy(Aqt')
0254 title('R\A sorted')
0255 end
0256
0257 if print_type > 0
0258 time_for_solution=cputime-start_time_sol
0259 end
0260
0261
0262 v = A*dx-b;
0263
0264
0265
0266
0267 eso = norm(v)/sqrt(N-U);
0268
0269 if type_robust > 0
0270
0271
0272
0273 vori = v;
0274 vori(1:Np) = (v(1:Np).*poi(1:Np,4)./sqrt(weights(1:Np)));
0275 vori(Np+1,end) = (v(Np+1,end)*sigma_k./sqrt(weights(Np+1,end)));
0276 vs=vori;
0277
0278
0279
0280 eso_p = median(abs(v(1:Np)))*1.48;
0281
0282
0283
0284 eso_k = median(abs(v(Np+1:end)))*1.48;
0285
0286 if print_type > 0
0287 est_s0_s0p_s0k=[eso,eso_p,eso_k]
0288 end
0289
0290 check_AtV_null=norm((A'*v));
0291 max_v_points = max(abs(v(1:Np)));
0292 max_v_curvat = max(abs(vs(Np+1,end)));
0293
0294
0295 kp = k*eso_p;
0296 kk = k*eso_k;
0297
0298
0299
0300
0301
0302
0303 if print_type > 0
0304 criticalvalue_kp_sigmap_kk_sigma_k=[kp,poi(1,4),kk,sigma_k]
0305 end
0306 end
0307
0308 if iter == 1 && type_robust > 0
0309 weights=ones(N,1);
0310 else
0311 if type_robust > 0 && L1
0312
0313 if type_robust == 1 || (type_robust == 3 && iter > 2*iter_switch)
0314
0315 weights(Np+1:end) = min(1,1./(abs(v(Np+1:end))/kk+eps));
0316 end
0317
0318 if type_robust == 2 || (type_robust ==3 && iter <= iter_switch)
0319 weights(1:Np) = min(1,1./(abs(v(1:Np))/kp+eps)).*out_in(1:Np);
0320 end
0321 else
0322 if type_robust == 1 || (type_robust == 3 && iter > 2*iter_switch)
0323
0324 weights(Np+1:end) = exp(-abs(v(Np+1:end)).^2/kk^2/2)+0.0001;
0325
0326 end
0327 if type_robust == 2 || (type_robust ==3 && iter <= iter_switch)
0328 weights(1:Np) = (exp(-abs(v(1:Np)).^2/kp^2/2)+0.0001).*out_in(1:Np);
0329 end
0330 end
0331 end
0332
0333 abs_dx = norm(dx);
0334 min_w_p=min(weights(1:Np));
0335 min_w_c=min(weights(Np+1:end));
0336
0337 if print_type > 1
0338 full(inv(W)*A)
0339 end
0340
0341 return