Changeset 748 for applications/dual/IterativeLocal
- Timestamp:
- 12/03/09 08:44:14 (15 years ago)
- Files:
-
- 1 modified
Legend:
- Unmodified
- Added
- Removed
-
applications/dual/IterativeLocal/ildp.m
r734 r748 5 5 %pocatecni konstanty 6 6 7 Iterace = 2; %iterace 7 % [b=1, yr=1] - to jsme zkouseli spolu sigma = 0.1; rho = 0.5; 8 % [b=-1, yr=1] sigma = 0.1; rho = 0.5; !aprior 9 % [b=10, yr=5] sigma = 0.5; rho = 2.0; 10 % [b=0.6, yr=10] sigma = 0.1; rho = 1.0; 11 12 Iterace = 20; %iterace 8 13 K = 20; %casy 9 N = 50; %vzorky10 11 sigma = 1;14 N = 100; %vzorky 15 16 sigma = 0.1; 12 17 Sigmas = [[sigma^2 0 0]; [0 0 0]; [0 0 0]]; 13 18 … … 16 21 hdxdx = [[0 0 0]; [0 0 0]; [0 0 0]]; 17 22 18 Rk = ones(1, K);19 x0 = [0; 0; 1];23 Rk = 1*ones(1, K); 24 x0 = [0; 1; 1]; 20 25 21 26 %velikost okoli pro lokalni metodu 22 rho = 5;27 rho = 0.5; 23 28 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 24 29 %globalni promenne … … 26 31 Kpi = ones(4, K); 27 32 Kpi(4, :) = zeros(1, K); 33 % Kpi(3, :) = zeros(1, K); 28 34 29 Wv = zeros( 10, K);35 Wv = zeros(9, K); 30 36 31 37 Xkn = zeros(3, K, N); 32 38 Xstripe = zeros(3, K); 39 40 41 % Kpi = [[1.0308 0.9990 0.9797 0.9899 1.0075 0.9830 1.0050 1.0173 0.9659 1.0000]; 42 % [0.4104 0.5562 0.6933 0.4674 0.3817 0.5036 0.4203 0.3461 0.7750 1.0000]; 43 % [0.9204 0.8378 0.6707 0.3629 0.9815 0.8955 1.2227 1.1550 1.9374 1.0000]; 44 % [0.5756 0.4225 0.4055 0.5941 0.5188 0.3218 0.3435 0.3210 0.0333 0]]; 33 45 34 46 gka = 0; … … 54 66 Xkn(2, k+1, n) = Xkn(2, k, n) + Kk*(Xkn(1, k+1, n) - Xkn(1, k, n) - Xkn(2, k, n)*Uk); 55 67 Xkn(3, k+1, n) = (1 - Kk*Uk)*Xkn(3, k, n); 68 % plot(1:K,Xkn(1,:,n)) 56 69 end 57 70 % hold all … … 66 79 % hold all 67 80 % subplot(4,1,4); 68 % plot(1:K ,UU(:,n))81 % plot(1:K-1,UU(:,n)) 69 82 70 83 end … … 90 103 Xkn(1, k, n) = Xstripe(1, k) + rho*randn(); 91 104 Xkn(2, k, n) = Xstripe(2, k) + rho*randn(); 92 Xkn(3, k, n) = Xstripe(3, k)* rho*exp(randn());105 Xkn(3, k, n) = Xstripe(3, k)*exp(rho*randn()); 93 106 % %rovne okoli 94 107 % Xkn(1, k, n) = Xstripe(1, k) + Epsl(1,n); … … 101 114 for n = 1:N, 102 115 gnu = n; 103 [Uopt(n), Hmin(n)] = fminunc(@Hamilt, uPi(k, Xkn(:, k, n)), optimset('GradObj','on','Display','notify')); 104 % [i, k, n] 116 % if(k == 1) 117 % Uopt(n) = Rk(k)/2; 118 % Hmin(n) = Hamilt(Uopt(n)); 119 % else 120 [Uopt(n), Hmin(n)] = fminunc(@Hamilt, uPi(k, Xkn(:, k, n)), optimset('GradObj','on','Display','notify')); 121 % % [i, k, n] 122 % end 105 123 % 106 124 % interv = -1000:1:1000; … … 138 156 Epsilon = zeros(3, N); 139 157 for n = 1:N, 140 Epsilon(:, n) = Xkn(:, k, n) - Xstripe(:, k); 158 Epsilon(1, n) = Xkn(1, k, n) - Xstripe(1, k); 159 Epsilon(2, n) = Xkn(2, k, n) - Xstripe(2, k); 160 Epsilon(3, n) = Xkn(3, k, n)/Xstripe(3, k); 141 161 end 142 162 mFi = matrixFi(Epsilon); … … 151 171 % Wv(:, k) = FiFiTInvFi' * Vn'; 152 172 % UU(k,:) = Uopt; 173 % for n = 1:N, 174 % rozd(n) = Vn(n) - Vtilde(k,Xkn(:,k,n)); 175 % end 176 153 177 for n = 1:N, 154 178 yt(n) = Xkn(1, k, n); … … 172 196 % subplot(3,1,3); 173 197 % plot(xxz,Vn,'rs',xxz2,vlv,'-b') 198 % clf reset 199 % hold off 200 % 201 % yii = 0.5:0.025:1.5; 202 % bjj = 0.5:0.025:1.5; 203 % for ii= 1:41, 204 % for jj= 1:41, 205 % vmtrx(ii,jj) = Vtilde(k, [yii(ii);bjj(jj);0]); 206 % end 207 % end 208 % 209 % % xlabel('yt') 210 % % ylabel('bt') 211 % % zlabel('Vt') 212 % surf(yii,bjj,vmtrx) 213 % % % for n=1:N, 214 % % % hold all 215 % % % surf(yt(n),bt(n),Vn(n),'LineStyle','','Marker','o'); 216 % % % end 217 % hold all 218 % plot3(bt, yt, Vn, 'ro') 219 220 174 221 end 175 222 % clf reset … … 189 236 % % plot(1:K,UU(:,n),'-b') 190 237 % end 238 % for k=1:K, 239 % riz(k) = uPi(k, Xstripe(:, k)); 240 % ce(k) = (Rk(k) - Xstripe(1, k))/(Xstripe(2, k) + Xstripe(3, k)); 241 % end 242 % plot(1:K,riz,1:K,ce,1:K,Xstripe(1,:)) 243 191 244 end 192 245 %%%%%%%%%%% … … 196 249 %graficky vystup 197 250 198 X = zeros(3, K); 199 UU = zeros(1,K); 200 201 X(:,1) = x0 + [sigma*randn(); 0; 0]; 202 for k = 1:K-1, 203 Upi = uPi(k, X); 204 UU(k) = Upi; 205 Ktmp = Upi*X(3,k)/(Upi^2*X(3,k) + sigma^2); 206 X(1,k+1) = X(1,k)+X(2,k)*Upi + sigma*randn(); 207 X(2,k+1) = X(2,k) + Ktmp*(X(1,k+1) - X(1,k) - X(2,k)*Upi); 208 X(3,k+1) = X(3,k)*(1-Ktmp*Upi); 209 end 210 % X 211 % hold off 212 subplot(4,1,1); 213 plot(1:K,X(1,:),'-gs') 214 % hold off 215 subplot(4,1,2); 216 plot(1:K,X(2,:),'-gs') 217 % hold off 218 subplot(4,1,3); 219 plot(1:K,X(3,:),'-gs') 220 % hold off 221 subplot(4,1,4); 222 plot(1:K,UU,'-gs') 251 % X1 = zeros(3, K); 252 % UU1 = zeros(1,K); 253 % 254 % X1(:,1) = x0 + [sigma*randn(); 0; 0]; 255 % for k = 1:K-1, 256 % Upi = uPi(k, X1); 257 % UU1(k) = Upi; 258 % Ktmp = Upi*X1(3,k)/(Upi^2*X1(3,k) + sigma^2); 259 % X1(1,k+1) = X1(1,k)+X1(2,k)*Upi + sigma*randn(); 260 % X1(2,k+1) = X1(2,k) + Ktmp*(X1(1,k+1) - X1(1,k) - X1(2,k)*Upi); 261 % X1(3,k+1) = X1(3,k)*(1-Ktmp*Upi); 262 % end 263 % % X 264 % % hold off 265 % subplot(4,1,1); 266 % plot(1:K,X(1,:),'-gs') 267 % % hold off 268 % subplot(4,1,2); 269 % plot(1:K,X(2,:),'-gs') 270 % % hold off 271 % subplot(4,1,3); 272 % plot(1:K,X(3,:),'-gs') 273 % % hold off 274 % subplot(4,1,4); 275 % plot(1:K,UU,'-gs') 276 % 277 % figure 278 % for k=1:K, 279 % riz(k) = uPi(k, Xstripe(:, k)); 280 % ce(k) = (Rk(k) - Xstripe(1, k))/(Xstripe(2, k) + Xstripe(3, k)); 281 % end 282 % plot(1:K,riz,1:K,ce,1:K,Xstripe(1,:)) 283 for n = 1:N, 284 Xkn(:, 1, n) = x0 + [sigma*randn(); 0; 0]; 285 for k = 1:K-1, 286 Uk = uPi(k, Xkn(:, k, n)); 287 UU(k,n) = Uk; 288 Kk = Uk*Xkn(3, k, n)/(Uk^2*Xkn(3, k, n) + sigma^2); 289 Xkn(1, k+1, n) = Xkn(1, k, n) + Xkn(2, k, n)*Uk + sigma*randn(); 290 Xkn(2, k+1, n) = Xkn(2, k, n) + Kk*(Xkn(1, k+1, n) - Xkn(1, k, n) - Xkn(2, k, n)*Uk); 291 Xkn(3, k+1, n) = (1 - Kk*Uk)*Xkn(3, k, n); 292 % plot(1:K,Xkn(1,:,n)) 293 end 294 hold all 295 subplot(4,1,1); 296 plot(1:K,Xkn(1,:,n)) 297 hold all 298 subplot(4,1,2); 299 plot(1:K,Xkn(2,:,n)) 300 hold all 301 subplot(4,1,3); 302 plot(1:K,Xkn(3,:,n)) 303 hold all 304 subplot(4,1,4); 305 plot(1:K-1,UU(:,n)) 306 307 end 308 title('iLDP') 309 figure 310 for n = 1:N, 311 Xkn(:, 1, n) = x0 + [sigma*randn(); 0; 0]; 312 for k = 1:K-1, 313 Uk = (Rk(k) - Xkn(1, k, n))/(Xkn(2, k, n) + Xkn(3, k, n)); 314 UU(k,n) = Uk; 315 Kk = Uk*Xkn(3, k, n)/(Uk^2*Xkn(3, k, n) + sigma^2); 316 Xkn(1, k+1, n) = Xkn(1, k, n) + Xkn(2, k, n)*Uk + sigma*randn(); 317 Xkn(2, k+1, n) = Xkn(2, k, n) + Kk*(Xkn(1, k+1, n) - Xkn(1, k, n) - Xkn(2, k, n)*Uk); 318 Xkn(3, k+1, n) = (1 - Kk*Uk)*Xkn(3, k, n); 319 % plot(1:K,Xkn(1,:,n)) 320 end 321 hold all 322 subplot(4,1,1); 323 plot(1:K,Xkn(1,:,n)) 324 hold all 325 subplot(4,1,2); 326 plot(1:K,Xkn(2,:,n)) 327 hold all 328 subplot(4,1,3); 329 plot(1:K,Xkn(3,:,n)) 330 hold all 331 subplot(4,1,4); 332 plot(1:K-1,UU(:,n)) 333 334 end 335 title('CE') 336 337 for vzorek = 1:100, 338 loss(vzorek) = 0; 339 bb = randn() + x0(2); 340 yy(1) = x0(1); 341 for k=1:K-1, 342 yy(k+1)=yy(k)+bb*uPi(k,[yy(k); bb; 0]); 343 loss(vzorek) = loss(vzorek) + (yy(k+1) - Rk(k+1))^2; 344 end 345 end 346 figure 347 hist(log(loss)) 348 349 % disp() 223 350 % X = zeros(1, K); 224 351 % b = 0; … … 243 370 244 371 function [val_uPi] = uPi(k_uPi, x_uPi) 245 val_uPi = (Rk(k_uPi) - Kpi(1, k_uPi)*x_uPi(1))/(Kpi(2, k_uPi)*x_uPi(2) + Kpi(3, k_uPi)*x_uPi(3) + Kpi(4, k_uPi)); 372 val_uPi = (Rk(k_uPi) - Kpi(1, k_uPi)*x_uPi(1))/(Kpi(2, k_uPi)*x_uPi(2) + Kpi(3, k_uPi)*x_uPi(3) + Kpi(4, k_uPi)); 373 246 374 end 247 375 … … 249 377 % Vtddx = Vtilde_dx(gka+1, Xkn(:, gka, gnu)); 250 378 val_ham = (Xkn(1, gka, gnu) + Xkn(2, gka, gnu)*u_ham - Rk(gka+1))^2 + Xkn(3, gka, gnu)*u_ham^2 ... + sigma^2 ... %ztrata l 251 + [Xkn( 1, gka, gnu) + Xkn(2, gka, gnu)*u_ham; ...252 Xkn( 2, gka, gnu) + Xkn(3, gka, gnu)*u_ham*(Xkn(1, gka+1, gnu) - Xkn(1, gka, gnu) - Xkn(2, gka, gnu)*u_ham)/(Xkn(3, gka, gnu)*u_ham^2 + sigma^2); ...253 Xkn(3, gka, gnu) -Xkn(3, gka, gnu)^2*u_ham^2/(Xkn(3, gka, gnu)*u_ham^2 + sigma^2)]' ... %fce f379 + [Xkn(2, gka, gnu)*u_ham; ... 380 Xkn(3, gka, gnu)*u_ham*(Xkn(1, gka+1, gnu) - Xkn(1, gka, gnu) - Xkn(2, gka, gnu)*u_ham)/(Xkn(3, gka, gnu)*u_ham^2 + sigma^2); ... 381 -Xkn(3, gka, gnu)^2*u_ham^2/(Xkn(3, gka, gnu)*u_ham^2 + sigma^2)]' ... %fce f 254 382 *Vtilde_dx(gka+1, Xkn(:, gka, gnu)) ... 255 + Wv(5, gka+1)*sigma;383 + Wv(5, gka+1)*sigma;%+ Wv(4, gka+1)*sigma; 256 384 257 385 val_grad = 2*(Xkn(1, gka, gnu) + Xkn(2, gka, gnu)*u_ham - Rk(gka+1))*Xkn(2, gka, gnu) + 2*Xkn(3, gka, gnu)*u_ham ... %ztrata du 258 + [Xkn(2, gka, gnu);... 386 + [Xkn(2, gka, gnu);... 259 387 (2*u_ham^2*Xkn(3, gka, gnu)^2*(Xkn(1, gka, gnu) - Xkn(1, gka+1, gnu) + u_ham*Xkn(2, gka, gnu)))/(sigma^2 + u_ham^2*Xkn(3, gka, gnu))^2 - (u_ham*Xkn(2, gka, gnu)*Xkn(3, gka, gnu))/(sigma^2 + u_ham^2*Xkn(3, gka, gnu)) - (Xkn(3, gka, gnu)*(Xkn(1, gka, gnu) - Xkn(1, gka+1, gnu) + u_ham*Xkn(2, gka, gnu)))/(sigma^2 + u_ham^2*Xkn(3, gka, gnu));... 260 388 (2*u_ham^3*Xkn(3, gka, gnu)^3)/(sigma^2 + u_ham^2*Xkn(3, gka, gnu))^2 - (2*u_ham*Xkn(3, gka, gnu)^2)/(sigma^2 + u_ham^2*Xkn(3, gka, gnu))]' ... %fce f du … … 266 394 val_Vt = h; 267 395 else 268 val_Vt = vectFi(x_Vt - Xstripe(:, k_Vt))' * Wv(:,k_Vt); 396 Epsl = zeros(3, 1); 397 Epsl(1) = x_Vt(1) - Xstripe(1, k_Vt); 398 Epsl(2) = x_Vt(2) - Xstripe(2, k_Vt); 399 Epsl(3) = x_Vt(3)/Xstripe(3, k_Vt); 400 401 val_Vt = vectFi(Epsl)' * Wv(:,k_Vt); 269 402 end 270 403 end … … 274 407 val_Vt = hdx; 275 408 else 276 val_Vt = difFi(x_Vt - Xstripe(:, k_Vt))' * Wv(:,k_Vt); 409 Epsl = zeros(3, 1); 410 Epsl(1) = x_Vt(1) - Xstripe(1, k_Vt); 411 Epsl(2) = x_Vt(2) - Xstripe(2, k_Vt); 412 Epsl(3) = x_Vt(3)/Xstripe(3, k_Vt); 413 414 val_Vt = difFi(Epsl)' * Wv(:,k_Vt); 277 415 end 278 416 end … … 310 448 x_Fi(1); ... 311 449 x_Fi(2); ... 312 x_Fi(3); ...450 log(x_Fi(3)); ... 313 451 x_Fi(1)^2; ... 314 452 x_Fi(1)*x_Fi(2); ... 315 x_Fi(1)* x_Fi(3); ...453 x_Fi(1)*log(x_Fi(3)); ... 316 454 x_Fi(2)^2; ... 317 x_Fi(2)* x_Fi(3); ...318 x_Fi(3)^2; ...455 x_Fi(2)*log(x_Fi(3)); ... 456 % 2*ln(x_Fi(3)); ... 319 457 ]; 320 458 end … … 325 463 x_Fi(1, :); ... 326 464 x_Fi(2, :); ... 327 x_Fi(3, :); ...465 log(x_Fi(3, :)); ... 328 466 x_Fi(1, :).^2; ... 329 467 x_Fi(1, :).*x_Fi(2, :); ... 330 x_Fi(1, :).* x_Fi(3, :); ...468 x_Fi(1, :).*log(x_Fi(3, :)); ... 331 469 x_Fi(2, :).^2; ... 332 x_Fi(2, :).* x_Fi(3, :); ...333 x_Fi(3, :).^2; ...470 x_Fi(2, :).*log(x_Fi(3, :)); ... 471 % 2*ln(x_Fi(3, :)); ... 334 472 ]; 335 473 % val_Fi = [ ... … … 346 484 1 0 0; ... 347 485 0 1 0; ... 348 0 0 1 ; ...486 0 0 1/(x_Fi(3)); ... 349 487 2*x_Fi(1) 0 0; ... 350 488 x_Fi(2) x_Fi(1) 0; ... 351 x_Fi(3) 0 x_Fi(1); ...489 log(x_Fi(3)) 0 x_Fi(1)/(x_Fi(3)); ... 352 490 0 2*x_Fi(2) 0; ... 353 0 x_Fi(3) x_Fi(2); ...354 0 0 2*x_Fi(3); ...491 0 log(x_Fi(3)) x_Fi(2)/(x_Fi(3)); ... 492 % 0 0 2*x_Fi(3); ... 355 493 ]; 356 494 end