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