PageRenderTime 43ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/other/Matlab/detection/detection_objective.m

http://github.com/jbeezley/wrf-fire
Objective C | 90 lines | 86 code | 4 blank | 0 comment | 8 complexity | 8217d31a3b3239c22d9035ea126e6769 MD5 | raw file
Possible License(s): AGPL-1.0
  1. function varargout=detection_objective(tign,h,g,params,red)
  2. % J =detection_objective(tign,h,g,params,red)
  3. % [J,delta] =detection_objective(tign,h,g,params,red)
  4. % [J,delta,f0,f1]=detection_objective(tign,h,g,params,red)
  5. % compute objective function and optionally gradient delta direction
  6. %
  7. % input:
  8. % tign prior state
  9. % h increment
  10. % g detection structur?e from load_subset_detections
  11. % params parameters set in detect_fit_level2
  12. %
  13. % The objective function is
  14. % J = h'*A*h - log likelihood of tign+h
  15. % to compute data likelihood only, set h=0 and params.alpha=0
  16. %
  17. % output
  18. % J objective function = penalty minus log likelihood
  19. % delta (optional) preconditioned search direction, grad J(tign+h)
  20. % f0 (optional) contributions of mesh cells to log likelihood
  21. % f1 (optional) contributions of mesh cells to the derivative
  22. % of the log likelihood
  23. like_plots=0;
  24. T=tign+h;
  25. f0=0;
  26. f1=0;
  27. for k=1:length(g)
  28. psi = ...
  29. + params.weight(1)*(g(k).fxdata==3)...
  30. + params.weight(2)*(g(k).fxdata==5)...
  31. + params.weight(3)*(g(k).fxdata==7)...
  32. + params.weight(4)*(g(k).fxdata==8)...
  33. + params.weight(5)*(g(k).fxdata==9);
  34. [f0k,f1k]=like2(psi,g(k).time-T,params.TC*params.stretch);
  35. detections=sum(psi(:)>0);
  36. f0=f0+f0k;
  37. f1=f1+f1k;
  38. if like_plots>1,
  39. plot_loglike(4,f0k,'Data likelihood',red)
  40. plot_loglike(5,f1k,'Data likelihood derivative',red)
  41. plot_loglike(6,psi,'psi',red)
  42. plot_loglike(7,g(k).time-T,'Time since fire arrival',red)
  43. hold on;contour3(red.fxlong,red.fxlat,g(k).time-T,[0 0],'r')
  44. hold off
  45. drawnow
  46. end
  47. end
  48. if like_plots>0,
  49. plot_loglike(2,f0,'Data likelihood',red)
  50. plot_loglike(3,f1,'Data likelihood derivative',red)
  51. drawnow
  52. end
  53. F=f1;
  54. % objective function and preconditioned gradient
  55. Ah = poisson_fft2(h,[params.dx,params.dy],params.power);
  56. % compute both parts of the objective function and compare
  57. J1 = 0.5*(h(:)'*Ah(:));
  58. J2 = -ssum(f0);
  59. J = params.alpha*J1 + J2;
  60. fprintf('Objective function J=%g (J1=%g, J2=%g)\n',J,J1,J2);
  61. varargout{1}=J;
  62. if nargout==1,
  63. return
  64. end
  65. gradJ = params.alpha*Ah + F;
  66. fprintf('Gradient: norm Ah %g norm F %g\n', norm(Ah,2), norm(F,2));
  67. if params.doplot,
  68. plotstate(7,f0,'Detection likelihood',0);
  69. plotstate(8,F,'Detection likelihood derivative',0);
  70. plotstate(10,gradJ,'gradient of J',0);
  71. end
  72. if params.alpha>0,
  73. delta = solve_saddle(params.Constr_ign,h,F,0,...
  74. @(u) poisson_fft2(u,[params.dx,params.dy],-params.power)/params.alpha);
  75. varargout{2}=delta;
  76. end
  77. if nargout >= 3,
  78. varargout{3}=f0;
  79. end
  80. if nargout >= 4,
  81. varargout{4}=f1;
  82. end
  83. % figure(17);mesh(red.fxlong,red.fxlat,delta),title('delta')
  84. % plotstate(11,delta,'Preconditioned gradient',0);
  85. %fprintf('norm(grad(J))=%g norm(delta)=%g\n',norm(gradJ,'fro'),norm(delta,'fro'))
  86. end