ADMB Documentation  -a65f1c97
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
df1b2lp6.cpp
Go to the documentation of this file.
1 /*
2  * $Id$
3  *
4  * Author: David Fournier
5  * Copyright (c) 2008-2012 Regents of the University of California
6  */
11 //#define USE_DD_STUFF
12 //#define USE_DD
13 #include <admodel.h>
14 #include <df1b2fun.h>
15 #include <adrndeff.h>
16 #ifndef OPT_LIB
17  #include <cassert>
18  #include <climits>
19 #endif
20 
21 double calculate_laplace_approximation(const dvector& x,const dvector& u0,
22  const banded_symmetric_dmatrix& bHess,const dvector& _xadjoint,
23  const dvector& _uadjoint,
24  const banded_symmetric_dmatrix& _bHessadjoint,function_minimizer * pmin);
25 double calculate_laplace_approximation(const dvector& x,const dvector& u0,
26  const dmatrix& Hess,const dvector& _xadjoint,const dvector& _uadjoint,
27  const dmatrix& _Hessadjoint,function_minimizer * pmin);
28 
29 double calculate_importance_sample_shess(const dvector& x,const dvector& u0,
30  const dmatrix& Hess,const dvector& _xadjoint,const dvector& _uadjoint,
31  const dmatrix& _Hessadjoint,function_minimizer * pmin);
32 
33 int use_dd_nr=0;
35 dvector solve(const dmatrix & st,const dmatrix & Hess,
36  const dvector& grad);
37 
38 #if defined(USE_DD_STUFF)
39 # if defined(_MSC_VER)
40  extern "C" _export void dd_newton_raphson(int n,double * v,double * diag,
41  double * ldiag, double * yy);
42 # else
43  extern "C" void dd_newton_raphson(int n,double * v,double * diag,
44  double * ldiag, double * yy);
45 # endif
46 #endif
47 
54  int& no_converge_flag)
55 {
56  //quadratic_prior * tmpptr=quadratic_prior::ptr[0];
57  //cout << tmpptr << endl;
58 
59 
61  double maxg=fabs(evaluate_function(uhat,pfmin));
62 
63 
65  dvector uhat_old(1,usize);
66  for(int ii=1;ii<=num_nr_iters;ii++)
67  {
68  // test newton raphson
69  switch(hesstype)
70  {
71  case 3:
72  bHess->initialize();
73  break;
74  case 4:
75  Hess.initialize();
76  break;
77  default:
78  cerr << "Illegal value for hesstype here" << endl;
79  ad_exit(1);
80  }
81 
82  grad.initialize();
83  //int check=initial_params::stddev_scale(scale,uhat);
84  //check=initial_params::stddev_curvscale(curv,uhat);
85  //max_separable_g=0.0;
86  sparse_count = 0;
87 
89  //if (bHess)
90  // cout << "norm(*bHess) = " << norm(*bHess) << endl;
91  //cout << "norm(Hess) = " << norm(Hess) << endl;
92  //cout << grad << endl;
93  //check_pool_depths();
95  {
96  std::ostream& output_stream = get_output_stream();
97  output_stream << "Newton raphson " << ii << " ";
98  }
100  {
103  }
104 
105  int ierr=0;
106  if (hesstype==3)
107  {
108  if (use_dd_nr==0)
109  {
111  if (ierr && no_converge_flag ==0)
112  {
113  no_converge_flag=1;
114  //break;
115  }
116  if (ierr)
117  {
118  double oldval;
119  evaluate_function(oldval,uhat,pfmin);
121  }
122  else
123  {
124  if (dd_nr_flag==0)
125  {
126  dvector v=solve(bltd,grad);
127  step=-solve_trans(bltd,v);
128  //uhat_old=uhat;
129  uhat+=step;
130  }
131  else
132  {
133 #if defined(USE_DD_STUFF)
134  int n=grad.indexmax();
135  maxg=fabs(evaluate_function(uhat,pfmin));
136  uhat=dd_newton_raphson2(grad,*bHess,uhat);
137 #else
138  cerr << "high precision Newton Raphson not implemented" << endl;
139  ad_exit(1);
140 #endif
141  }
142  maxg=fabs(evaluate_function(uhat,pfmin));
143  if (f_from_1< pfmin->lapprox->fmc1.fbest)
144  {
146  maxg=fabs(evaluate_function(uhat,pfmin));
147  }
148  }
149  }
150  else
151  {
152  cout << "error not used" << endl;
153  ad_exit(1);
154  /*
155  banded_symmetric_ddmatrix bHessdd=banded_symmetric_ddmatrix(*bHess);
156  ddvector gradd=ddvector(grad);
157  //banded_lower_triangular_ddmatrix bltdd=choleski_decomp(bHessdd,ierr);
158  if (ierr && no_converge_flag ==0)
159  {
160  no_converge_flag=1;
161  break;
162  }
163  if (ierr)
164  {
165  double oldval;
166  evaluate_function(oldval,uhat,pfmin);
167  uhat=banded_calculations_trust_region_approach(uhat,pfmin);
168  maxg=fabs(evaluate_function(uhat,pfmin));
169  }
170  else
171  {
172  ddvector v=solve(bHessdd,gradd);
173  step=-make_dvector(v);
174  //uhat_old=uhat;
175  uhat=make_dvector(ddvector(uhat)+step);
176  maxg=fabs(evaluate_function(uhat,pfmin));
177  if (f_from_1< pfmin->lapprox->fmc1.fbest)
178  {
179  uhat=banded_calculations_trust_region_approach(uhat,pfmin);
180  maxg=fabs(evaluate_function(uhat,pfmin));
181  }
182  }
183  */
184  }
185 
186  if (maxg < 1.e-13)
187  {
188  break;
189  }
190  }
191  else if (hesstype==4)
192  {
193  dvector step;
194 
195 # if defined(USE_ATLAS)
197  {
198  step=-atlas_solve_spd(Hess,grad,ierr);
199  }
200  else
201  {
203  if (!ierr)
204  {
205  step=-solve(Hess,grad);
206  //step=-solve(A*trans(A),grad);
207  }
208  }
209  if (!ierr) break;
210 # else
212  {
213  //step=-solve(*sparse_triplet,Hess,grad,*sparse_symbolic);
215  if (ierr)
216  {
217  step=-temp;
218  }
219  else
220  {
221  cerr << "matrix not pos definite in sparse choleski" << endl;
222  pfmin->bad_step_flag=1;
223 
224  int on;
225  int nopt;
226  if ((on=option_match(ad_comm::argc,ad_comm::argv,"-ieigvec",nopt))
227  >-1)
228  {
230 
231  ofstream ofs3("inner-eigvectors");
232  ofs3 << "eigenvalues and eigenvectors " << endl;
233  dvector v=eigenvalues(M);
234  dmatrix ev=trans(eigenvectors(M));
235  ofs3 << "eigenvectors" << endl;
236  int i;
237  for (i=1;i<=ev.indexmax();i++)
238  {
239  ofs3 << setw(4) << i << " "
240  << setshowpoint() << setw(14) << setprecision(10) << v(i)
241  << " "
242  << setshowpoint() << setw(14) << setprecision(10)
243  << ev(i) << endl;
244  }
245  }
246  }
247  //cout << norm2(step-tmpstep) << endl;
248  //dvector step1=-solve(Hess,grad);
249  //cout << norm2(step-step1) << endl;
250  }
251  else
252  {
253  step=-solve(Hess,grad);
254  }
255 # endif
256  if (pmin->bad_step_flag)
257  break;
258  uhat_old=uhat;
259  uhat+=step;
260 
261  double maxg_old=maxg;
262  maxg=fabs(evaluate_function(uhat,pfmin));
263  if (maxg>maxg_old)
264  {
265  uhat=uhat_old;
266  evaluate_function(uhat,pfmin);
267  break;
268  }
269  if (maxg < 1.e-13)
270  {
271  break;
272  }
273  }
274 
275  if (sparse_hessian_flag==0)
276  {
277  for (int i=1;i<=usize;i++)
278  {
279  y(i+xsize)=uhat(i);
280  }
281  }
282  else
283  {
284  for (int i=1;i<=usize;i++)
285  {
286  value(y(i+xsize))=uhat(i);
287  }
288  }
289  }
290 }
291 
298  function_minimizer * pfmin,int& no_converge_flag)
299 {
300  if (no_converge_flag)
301  {
302  no_converge_flag=0;
303  }
304 
305  if (!inner_lmnflag)
306  {
307  if (!ADqd_flag)
308  {
309  uhat=get_uhat_quasi_newton(x,pfmin);
310  double maxg=fabs(fmc1.gmax);
311  if (maxg>1.0)
312  {
313  uhat=get_uhat_quasi_newton(x,pfmin);
314  }
315  }
316  else
317  {
319  }
320  }
321  else
322  {
323  uhat=get_uhat_lm_newton(x,pfmin);
324  //uhat=get_uhat_lm_newton2(x,pfmin);
325  //maxg=objective_function_value::gmax;
326  }
327  return fmc1.fbest;
328 }
329 
335  (const dvector& _x,const double& _f,function_minimizer * pfmin)
336 {
337  // for use when there is no separability
339  ADUNCONST(double,f)
340  //int i,j;
341  int i;
342 
345  initial_params::reset(x); // get current x values into the model
347 
349  if (init_switch==0)
350  {
352  initial_params::xinit(ubest);
354  }
355  //double maxg;
356  //double maxg_save;
357  //dvector uhat(1,usize);
358  double f_from_1=0.0;
359 
360  int no_converge_flag=0;
361 
362  // this is the main loop to do inner optimization
363  for (;;)
364  {
365  int icount=0;
366  do
367  {
368  icount++;
369  // do the inner optimziation
370  if (inner_maxfn>0)
371  {
372  f_from_1=inner_optimization_banded(/*uhat,*/ x,pfmin,
373  no_converge_flag);
374  }
375 
376  if (sparse_hessian_flag==0)
377  {
378  for (i=1;i<=xsize;i++) { y(i)=x(i); }
379  for (i=1;i<=usize;i++) { y(i+xsize)=uhat(i); }
380  }
381  else
382  {
383  for (i=1;i<=xsize;i++) { value(y(i))=x(i); }
384  for (i=1;i<=usize;i++) { value(y(i+xsize))=uhat(i); }
385  }
386 
388  if (admb_ssflag==0)
389  {
390  do_newton_raphson_banded(pfmin,f_from_1,no_converge_flag);
391  }
392  else
393  {
394  do_newton_raphson_state_space(pfmin,f_from_1,no_converge_flag);
395  }
397 
398  if (num_nr_iters<=0) { evaluate_function(uhat,pfmin); }
399 
400  if (sparse_hessian_flag==0)
401  {
402  for (i=1;i<=usize;i++) { y(i+xsize)=uhat(i); }
403  }
404  else
405  {
406  for (i=1;i<=usize;i++) { value(y(i+xsize))=uhat(i); }
407  }
408  if (icount>2) pfmin->bad_step_flag=1;
409  if (pfmin->bad_step_flag)
410  return xadjoint;
411  }
412  while(no_converge_flag);
413 
414  /* If we are in mcmc phase we just need to calcualte the
415  ln_det(Hess) and return
416  */
417  hs_symbolic & ssymb=*(pmin->lapprox->sparse_symbolic2);
419  {
420  do_newton_raphson_banded(pfmin,f_from_1,no_converge_flag);
421  int sgn=0;
422  //double& f = (double&) _f;
424  if (pmin->lapprox->sparse_hessian_flag==0)
425  {
426  if (!bHess)
427  {
428  cerr << "Block diagonal Hessian is unallocated" << endl;
429  cerr << " Try -shess command line option, perhaps." << endl;
430  ad_exit(1);
431  }
432  else
433  {
434  f+=0.5*ln_det_choleski(*bHess,sgn);
435  }
436  }
437  else
438  {
439  //hs_symbolic & ssymb=*(pmin->lapprox->sparse_symbolic2);
440  //dvariable tmp=0.5*ln_det(*(pmin->lapprox->vsparse_triplet),
441  // ssymb,*(pmin->lapprox->sparse_triplet2));
442  f+=0.5*ln_det(*(pmin->lapprox->sparse_triplet2),ssymb);
443  }
444  }
445  else
446  {
447  xadjoint.initialize();
448  uadjoint.initialize();
449  Dux.initialize();
450 
451  if (hesstype==3)
452  bHess->initialize();
453  else if (hesstype==4)
454  Hess.initialize();
455 
456  block_diagonal_flag=2;
457  used_flags.initialize();
459  sparse_count = 0;
460 
462 
463  if (sparse_triplet2)
464  sparse_triplet2->initialize();
465 
466  pfmin->user_function();
468 
469  int ierr=0;
470 
472  if (!ierr)
473  {
474  if (num_importance_samples==0)
475  {
476  if (hesstype==3)
477  {
478  f=calculate_laplace_approximation(x,uhat,*bHess,xadjoint,uadjoint,
479  *bHessadjoint,pfmin);
480  }
481  else if (hesstype==4)
482  {
483  //cout << "Hess" << endl << Hess << endl;
484  f=calculate_laplace_approximation(x,uhat,Hess,xadjoint,uadjoint,
485  Hessadjoint,pfmin);
486  }
487  else
488  {
489  cerr << "Error in hesstype" << endl;
490  ad_exit(1);
491  }
492  }
493  else
494  {
495  if (hesstype==3)
496  {
497  //cerr << "Error not implemented yet" << endl;
498  //ad_exit(1);
499  f=calculate_importance_sample(x,uhat,*bHess,xadjoint,uadjoint,
500  *bHessadjoint,pfmin);
501  }
502  else if (hesstype==4)
503  {
504  if (pmin->lapprox->sparse_hessian_flag==0)
505  f=calculate_importance_sample(x,uhat,Hess,xadjoint,uadjoint,
506  Hessadjoint,pfmin);
507  else
508  f=calculate_importance_sample_shess(x,uhat,Hess,xadjoint,uadjoint,
509  Hessadjoint,pfmin);
510  }
511  else
512  {
513  cerr << "Error in hesstype" << endl;
514  ad_exit(1);
515  }
516  }
517  }
518  else
519  {
520  f=1.e+30;
521  }
522 
523  // set flag for thrid erivatvies and call function again because
524  // stack is wiped out
525 
526  if (hesstype==3)
527  {
528  bHess->initialize();
529  }
530  else if (hesstype==4)
531  {
532  if (sparse_hessian_flag==0)
533  {
534  Hess.initialize();
535  }
536  else
537  {
538  sparse_triplet2->initialize();
539  }
540  }
541  else
542  {
543  cerr << "Illegal value for hesstype" << endl;
544  ad_exit(1);
545  }
547  block_diagonal_flag=3;
548  local_dtemp.initialize();
549 
550  // ***** Note for quadratic prior code: I don't think that this
551  // part gets added to the Hessian here.
552  sparse_count=0;
553  sparse_count_adjoint=0;
554  pfmin->user_function();
555 
556  // *** Hessian calculated just above did not have quadratic prior
557  // in it so can save this part for quadratci prioer adjoint calculations
559  {
560  if (pHess_non_quadprior_part)
561  {
562  if (pHess_non_quadprior_part->indexmax() != Hess.indexmax())
563  {
564  delete pHess_non_quadprior_part;
565  pHess_non_quadprior_part=0;
566  }
567  }
568  if (!pHess_non_quadprior_part)
569  {
570  pHess_non_quadprior_part=new dmatrix(1,usize,1,usize);
571  if (!pHess_non_quadprior_part)
572  {
573  cerr << "Error allocating memory for Hesssian part" << endl;
574  ad_exit(1);
575  }
576  }
577  (*pHess_non_quadprior_part)=Hess;
578  }
579 
580  block_diagonal_flag=0;
581  //initial_params::straight_through_flag=1;
582  //dmatrix tHess=dmatrix(*bHess);
585  //cout << initial_df1b2params::cobjfun << endl;
586  //f=initial_df1b2params::cobjfun;
587  block_diagonal_flag=0;
588 #ifndef OPT_LIB
589  assert(nvar <= INT_MAX);
590 #endif
591  dvector scale1(1,(int)nvar); // need to get scale from somewhere
593  /*int check=*/initial_params::stddev_scale(scale1,x);
594  //for (i=1;i<=xadjoint.indexmax();i++)
595  // xadjoint(i)*=scale1(i);
597 
599  {
600  // !!!! need to fix this!!!!!!!!!!!!!!!!!!!!!!!
609  }
611  {
612  dvector scale(1,(int)nvar); // need to get scale from somewhere
613  /*int check=*/initial_params::stddev_scale(scale,x);
614  dvector sscale=scale(1,Dux(1).indexmax());
615  for (i=1;i<=usize;i++)
616  {
617  Dux(i)=elem_prod(Dux(i),sscale);
618  }
619  local_dtemp=elem_prod(local_dtemp,sscale);
620  }
621  //cout << trans(Dux)(1) << endl;
622  //cout << trans(Dux)(3) << endl;
624  {
625  dvector tmp=evaluate_function_with_quadprior(x,usize,pfmin);
626  local_dtemp+=tmp;
627  }
628 
629  for (i=1;i<=xsize;i++)
630  {
631  xadjoint(i)+=local_dtemp(i);
632  }
634  {
635  // !!!! need to fix this!!!!!!!!!!!!!!!!!!!!!!!
637  }
638 
639  if (hesstype==3)
640  {
641  //xadjoint -= uadjoint*solve(*bHess,Dux);
642  if (bHess_pd_flag==0)
643  {
644  xadjoint -= solve(*bHess,uadjoint)*Dux;
645  }
646  }
647  else if (hesstype==4)
648  {
649  if (sparse_hessian_flag)
650  {
651  if (!sparse_triplet2 || !sparse_symbolic2)
652  {
653  throw std::bad_alloc();
654  }
655  else
656  {
657  //dvector tmp=solve(*sparse_triplet,Hess,uadjoint,*sparse_symbolic)*Dux;
658  dvector tmp=solve(*sparse_triplet2,uadjoint,*sparse_symbolic2)*Dux;
659  xadjoint -= tmp;
660  }
661  }
662  else
663  {
664  xadjoint -= solve(Hess,uadjoint)*Dux;
665  }
666  }
667  }
668  if (bHess_pd_flag==0) break;
669  }
670 
671  return xadjoint;
672 }
673  //int check_pool_flag1=0;
674 
681 {
682  //***********************************************************
683  //***********************************************************
687  //if (check_pool_flag1)
688  // check_pool_depths();
689  df1b2_gradcalc1();
690  //if (check_pool_flag1)
691  // check_pool_depths();
692 
695  int num_local_re=0;
696  int num_fixed_effects=0;
697 
698 #ifndef OPT_LIB
699  assert(funnel_init_var::num_active_parameters <= INT_MAX);
700 #endif
702  ivector lfe_index(1, (int)funnel_init_var::num_active_parameters);
703 
704  ivector* plisti = &list(1);
705  for (int i=1;i<=(int)funnel_init_var::num_active_parameters;i++)
706  {
707  int listi1 = *(plisti->get_v() + 1);
708  if (listi1 > xsize)
709  {
710  lre_index(++num_local_re)=i;
711  }
712  else if (listi1 > 0)
713  {
714  lfe_index(++num_fixed_effects)=i;
715  }
716  ++plisti;
717  }
718 
719  if (num_local_re > 0)
720  {
721  switch(hesstype)
722  {
723  case 3:
724  for (int i=1;i<=num_local_re;i++)
725  {
726  int lrei=lre_index(i);
727  int i1=list(lrei,1)-xsize;
728  int i2=list(lrei,2);
729  for (int j=1;j<=num_local_re;j++)
730  {
731  int lrej=lre_index(j);
732  int j1=list(lrej,1)-xsize;
733  int j2=list(lrej,2);
734  if (i1>=j1) (*bHess)(i1,j1)+=locy(i2).u_bar[j2-1];
735  }
736  }
737  break;
738  case 4:
739  if (sparse_hessian_flag==0)
740  {
741  int* plre_indexi = lre_index.get_v() + 1;
742  for (int i=1;i<=num_local_re;i++)
743  {
744  int lrei = *plre_indexi;
745  ivector* plisti = &list(lrei);
746  int i1 = *(plisti->get_v() + 1) - xsize;
747  int i2 = *(plisti->get_v() + 2);
748 
749  dvector* pHessi1 = &Hess(i1);
750  int* plre_indexj = lre_index.get_v() + 1;
751  for (int j=1;j<=num_local_re;j++)
752  {
753  int lrej = *plre_indexj;
754  ivector* plistj = &list(lrej);
755  int j1 = *(plistj->get_v() + 1) - xsize;
756  int j2 = *(plistj->get_v() + 2);
757 
758  *(pHessi1->get_v() + j1) += locy(i2).u_bar[j2-1];
759 
760  ++plre_indexj;
761  }
762  ++plre_indexi;
763  }
764  }
765  else
766  {
767  for (int i=1;i<=num_local_re;i++)
768  {
769  int lrei=lre_index(i);
770  int i1=list(lrei,1)-xsize;
771  int i2=list(lrei,2);
772  for (int j=1;j<=num_local_re;j++)
773  {
774  int lrej=lre_index(j);
775  int j1=list(lrej,1)-xsize;
776  int j2=list(lrej,2);
777 
778  if (i1<=j1)
779  {
780  sparse_count++;
781  (*sparse_triplet2)((*sparse_iterator)(sparse_count))
782  +=locy(i2).u_bar[j2-1];
783  }
784  }
785  }
786  }
787  break;
788  default:
789  cerr << "illegal value for hesstype" << endl;
790  ad_exit(1);
791  }
792 
793  for (int i=1;i<=num_local_re;i++)
794  {
795  int lrei=lre_index(i);
796  int i1=list(lrei,1);
797  int i2=list(lrei,2);
798  //grad(i1-xsize)= re_objective_function_value::pobjfun->u_dot[i2-1];
799  grad(i1-xsize)+=ff.u_dot[i2-1];
800  }
801  }
802 
803  f1b2gradlist->reset();
811  funnel_init_var::num_active_parameters=0;
813 }
814 //int tmp_testcount=0;
816 
823 {
825  if (allocated(used_flags))
826  {
827  if (used_flags.indexmax() != nv)
828  {
830  }
831  }
832  if (!allocated(used_flags))
833  {
835  }
836 
837  for (int ip=1;ip<=num_der_blocks;ip++)
838  {
839  if (ip>1) // change to combine sparse matrix stuff with num der blocks
840  { // df 3-4-09
841  sparse_count=0;
842  }
844  // do we need to reallocate memory for df1b2variables?
847  //cout << re_objective_function_value::pobjfun << endl;
848  //cout << re_objective_function_value::pobjfun->ptr << endl;
849  (*re_objective_function_value::pobjfun)=0;
850  df1b2variable pen=0.0;
851  tmp_pen=&pen;
852  df1b2variable zz=0.0;
853 
855 
856  // call function to do block diagonal newton-raphson
857  // the step vector from the newton-raphson is in the vector step
859 
861  //cout << funnel_init_var::lapprox << endl;
863  /*
864  tmp_testcount++;
865  if (tmp_testcount>=9)
866  {
867  pen.deallocate();
868  }
869  */
870 
871  if (ip==1)
872  {
873  if (sparse_triplet2)
875  }
876 
877  pfmin->user_function();
878  /*
879  if (tmp_testcount>=9)
880  {
881  pen.deallocate();
882  }
883  */
886  pen.deallocate();
887  }
888 
889  return step;
890 }
891 
898 {
900  //df1b2_gradlist::set_no_derivatives();
902  df1b2_gradcalc1();
903 
906 
907  int us=0; int xs=0;
908 #ifndef OPT_LIB
909  assert(funnel_init_var::num_active_parameters <= INT_MAX);
910 #endif
912  ivector lfe_index(1,(int)funnel_init_var::num_active_parameters);
913 
914  ivector* plisti = &list(1);
915  for (int i=1;i<=(int)funnel_init_var::num_active_parameters;i++)
916  {
917  int val = *(plisti->get_v() + 1);
918  if (val > xsize)
919  {
920  lre_index(++us)=i;
921  }
922  else if (val > 0)
923  {
924  lfe_index(++xs)=i;
925  }
926  ++plisti;
927  }
928 
929  {
930  int* plfe_indexj = lfe_index.get_v() + 1;
931  for (int j=1;j<=xs;j++)
932  {
933  int lfe_indexj = lfe_index(j);
934  int* plistlfe_indexj = list(lfe_indexj).get_v();
935  int j1 = *(plistlfe_indexj + 1);//list(lfe_index(j),1);
936  int j2 = *(plistlfe_indexj + 2);//list(lfe_index(j),2);
937  xadjoint(j1)+=ff.u_dot[j2-1];
938 
939  ++plfe_indexj;
940  }
941  }
942 
943  if (us>0)
944  {
945  if (hesstype==3)
946  {
947  for (int i=1;i<=us;i++)
948  {
949  int i1=list(lre_index(i),1)-xsize;
950  int i2=list(lre_index(i),2);
951  for (int j=1;j<=us;j++)
952  {
953  int j1=list(lre_index(j),1)-xsize;
954  int j2=list(lre_index(j),2);
955  if (i1>=j1) (*bHess)(i1,j1)+=locy(i2).u_bar[j2-1];
956  }
957  }
958  }
959  else if (hesstype==4)
960  {
961  if (sparse_hessian_flag==0)
962  {
963  for (int i=1;i<=us;i++)
964  {
965  int i1=list(lre_index(i),1)-xsize;
966  int i2=list(lre_index(i),2);
967  for (int j=1;j<=us;j++)
968  {
969  int j1=list(lre_index(j),1)-xsize;
970  int j2=list(lre_index(j),2);
971  Hess(i1,j1)+=locy(i2).u_bar[j2-1];
972  }
973  }
974  }
975  else
976  {
977  for (int i=1;i<=us;i++)
978  {
979  int i1=list(lre_index(i),1)-xsize;
980  int i2=list(lre_index(i),2);
981  for (int j=1;j<=us;j++)
982  {
983  int j1=list(lre_index(j),1)-xsize;
984  int j2=list(lre_index(j),2);
985 
986  if (i1<=j1)
987  {
988  sparse_count++;
989  (*sparse_triplet2)((*sparse_iterator)(sparse_count))
990  +=locy(i2).u_bar[j2-1];
991  }
992  }
993  }
994  }
995  }
996 
997  for (int i=1;i<=us;i++)
998  {
999  int i1=list(lre_index(i),1)-xsize;
1000  int i2=list(lre_index(i),2);
1001  uadjoint(i1)+=ff.u_dot[i2-1];
1002  }
1003 
1004  for (int i=1;i<=us;i++)
1005  {
1006  int i1=list(lre_index(i),1)-xsize;
1007  int i2=list(lre_index(i),2);
1008  for (int j=1;j<=xs;j++)
1009  {
1010  int j1=list(lfe_index(j),1);
1011  int j2=list(lfe_index(j),2);
1012  Dux(i1,j1)+=locy(i2).u_bar[j2-1];
1013  }
1014  }
1015  }
1016  f1b2gradlist->reset();
1024  funnel_init_var::num_active_parameters=0;
1026 }
1027 
1033  const banded_symmetric_dmatrix& bHess,const dvector& _xadjoint,
1034  const dvector& _uadjoint,
1035  const banded_symmetric_dmatrix& _bHessadjoint,function_minimizer * pmin)
1036 {
1037  ADUNCONST(dvector,xadjoint)
1038  ADUNCONST(dvector,uadjoint)
1039  ADUNCONST(banded_symmetric_dmatrix,bHessadjoint)
1040  int bw=bHess.bandwidth();
1041 #if !defined(OPT_LIB) && (__cplusplus >= 201103L)
1042  const int xs = [](unsigned int size)->int
1043  {
1044  assert(size <= INT_MAX);
1045  return static_cast<int>(size);
1046  }(x.size());
1047  const int us = [](unsigned int size)->int
1048  {
1049  assert(size <= INT_MAX);
1050  return static_cast<int>(size);
1051  }(u0.size());
1052 #else
1053  const int xs = static_cast<int>(x.size());
1054  const int us = static_cast<int>(u0.size());
1055 #endif
1057  int nvar = xs + us + ((bw + 1) * (2 * us - bw))/2;
1058  independent_variables y(1,nvar);
1059 
1060  // need to set random effects active together with whatever
1061  // init parameters should be active in this phase
1064  /*int onvar=*/initial_params::nvarcalc();
1065  initial_params::xinit(y); // get the initial values into the
1066  y(1,xs)=x;
1067 
1068  // Here need hooks for sparse matrix structures
1069  int ii=xs+us+1;
1070  for (int i=1;i<=us;i++)
1071  {
1072  int jmin=admax(1,i-bw+1);
1073  for (int j=jmin;j<=i;j++)
1074  y(ii++)=bHess(i,j);
1075  }
1076 
1077  dvar_vector vy=dvar_vector(y);
1078  banded_symmetric_dvar_matrix vHess(1,us,bw);
1079  initial_params::reset(vy); // get the initial values into the
1080  ii=xs+us+1;
1081  for (int i=1;i<=us;i++)
1082  {
1083  int jmin=admax(1,i-bw+1);
1084  for (int j=jmin;j<=i;j++)
1085  vHess(i,j)=vy(ii++);
1086  }
1087 
1088  dvariable vf=0.0;
1089 
1091  pmin->AD_uf_outer();
1093 
1094  int sgn=0;
1095  dvariable ld;
1096 
1097 #ifdef DIAG
1098  int eigswitch=0;
1099  if (eigswitch)
1100  {
1101  ofstream ofs("ee");
1102  dvector ev(bHess.indexmin(),bHess.indexmax());
1103  dmatrix evecs=eigenvectors(dmatrix(bHess),ev);
1104  ofs << setprecision(3) << setw(12) << setscientific() << dmatrix(bHess)
1105  << endl << endl;
1106  ofs << ev << endl << endl << evecs << endl;
1107  }
1108 #endif
1109 
1110  ld=0.5*ln_det_choleski(vHess,sgn);
1111  if (sgn==1)
1112  {
1113  cout << "Choleski failed" << endl;
1114  pmin->lapprox->bHess_pd_flag=1;
1115  }
1116 
1117  vf+=ld;
1118  const double ltp=0.5*log(2.0*PI);
1119  vf-=us*ltp;
1120  double f=value(vf);
1121  dvector g(1,nvar);
1122  gradcalc(nvar,g);
1123 
1124  ii=1;
1125  for (int i=1;i<=xs;i++)
1126  xadjoint(i)=g(ii++);
1127  for (int i=1;i<=us;i++)
1128  uadjoint(i)=g(ii++);
1129  for (int i=1;i<=us;i++)
1130  {
1131  int jmin=admax(1,i-bw+1);
1132  for (int j=jmin;j<=i;j++)
1133  bHessadjoint(i,j)=g(ii++);
1134  }
1135  return f;
1136 }
1137 
1144  function_minimizer * pfmin)
1145 {
1146  dvector& uhat=(dvector&) _uhat;
1147  dvector uhat_old(uhat.indexmin(),uhat.indexmax());
1148  dvector uhat_new(uhat.indexmin(),uhat.indexmax());
1149  dvector uhat_best(uhat.indexmin(),uhat.indexmax());
1150 
1151  double wght=0.0;
1152  double delta=5.e-5;
1153  //do
1154  dvector values(1,300);
1155  double oldfbest=pmin->lapprox->fmc1.fbest;
1156  double newfbest = 0.0;
1157  int have_value=0;
1158  //for (int jj=1;jj<=300;jj++)
1159  int jj=1;
1160  double lastval=oldfbest;
1161  do
1162  {
1163  jj++;
1164  wght+=delta;
1165  //double wght=0.0;
1166  double newval=0.0;
1167  //cout << "Enter weight size " << endl;
1168  //cin >> wght;
1169  if (wght<0.0)
1170  break;
1171  int mmin=bHess->indexmin();
1172  int mmax=bHess->indexmax();
1173  banded_symmetric_dmatrix tmp(mmin,mmax,bHess->bandwidth());
1174  tmp=*bHess;
1175  uhat_old=uhat;
1176  int ierr=0;
1177  for (int i=mmin;i<=mmax;i++)
1178  {
1179  tmp(i,i)+=wght;
1180  }
1182  if (!ierr)
1183  {
1184  dvector v=solve(bltd,grad);
1185  step=-solve_trans(bltd,v);
1186 
1187  uhat_old=uhat;
1188  uhat+=step;
1189  //cout << "norm(uhat_old) = " << norm(uhat_old)
1190  // << " norm(uhat) = " << norm(uhat) << endl;
1191 
1192  /*double maxg=*/evaluate_function(newval,uhat,pfmin);
1193  if (have_value && newval>newfbest)
1194  {
1195  break;
1196  }
1197  if (jj>1)
1198  {
1199  if (newval<lastval) // we are doing better so increasse step size
1200  {
1201  delta*=2;
1202  }
1203  if (newval>lastval && !have_value) // we have gone to far go back
1204  {
1205  wght-=delta;
1206  delta/=16;
1207  }
1208  }
1209  lastval=newval;
1210 
1211  if (newval<newfbest)
1212  {
1213  newfbest=newval;
1214  uhat_best=uhat;
1215  have_value=jj;
1216  }
1217  uhat_new=uhat;
1218  uhat=uhat_old;
1219  }
1220  else
1221  {
1222  delta*=2;
1223  }
1224  }
1225  while(jj<10);
1226  if (!have_value)
1227  {
1228  cerr << "can't improve function value in trust region calculations"
1229  << endl;
1230  //ad_exit(1);
1231  }
1232  return uhat_best;
1255 }
static int straight_through_flag
Definition: admodel.h:839
Description not yet available.
Definition: fvar.hpp:7981
void safe_deallocate()
Safely deallocates memory by reporting if shallow copies are still in scope.
Definition: ivector.cpp:119
laplace_approximation_calculator * lapprox
Definition: admodel.h:1862
void initialize(void)
Description not yet available.
Definition: df1b2f14.cpp:155
void initialize(void)
Definition: hs_sparse.cpp:2566
static void reset(const init_df1b2vector &, const df1b2variable &)
Description not yet available.
Definition: df1b2f15.cpp:51
dcompressed_triplet * sparse_triplet2
Definition: adrndeff.h:271
static void set_yes_derivatives(void)
Definition: df1b2fun.h:761
dvector banded_calculations(const dvector &_x, const double &_f, function_minimizer *pfmin)
Description not yet available.
Definition: df1b2lp6.cpp:335
void do_newton_raphson_banded(function_minimizer *pmin, double, int &)
Description not yet available.
Definition: df1b2lp6.cpp:53
df1b2_gradlist * f1b2gradlist
Definition: df1b2glo.cpp:49
d3_array elem_prod(const d3_array &a, const d3_array &b)
Returns d3_array results with computed elements product of a(i, j, k) * b(i, j, k).
Definition: d3arr2a.cpp:92
void set_dependent_variable(const df1b2variable &_x)
Description not yet available.
Definition: df1b2fn2.cpp:699
static void set_no_derivatives(void)
Definition: df1b2fun.h:760
double calculate_importance_sample(const dvector &x, const dvector &u0, const dmatrix &Hess, const dvector &_xadjoint, const dvector &_uadjoint, const dmatrix &_Hessadjoint, function_minimizer *pmin)
Description not yet available.
Definition: df1b2imp.cpp:23
function_minimizer * pmin
Definition: adrndeff.h:246
Description not yet available.
Definition: imatrix.h:69
static void set_NO_DERIVATIVES(void)
Disable accumulation of derivative information.
Definition: gradstrc.cpp:641
Description not yet available.
static void set_active_random_effects(void)
Definition: model.cpp:267
static double cobjfun
Definition: df1b2fun.h:1360
int admax(int i, int j)
Definition: fvar.hpp:8979
dvector atlas_solve_spd(const dmatrix &M, const dvector &x)
static void get_Lxu_contribution(dmatrix &)
Definition: fquadpri.cpp:249
dmatrix trans(const dmatrix &m1)
Author: David Fournier Copyright (c) 2008-2012 Regents of the University of California.
Definition: dmat2.cpp:13
dvector eigenvalues(const banded_symmetric_dmatrix &_SS)
Description not yet available.
Definition: dmat28.cpp:411
#define x
int allocated(const ivector &v)
Author: David Fournier Copyright (c) 2008-2012 Regents of the University of California.
Definition: fvar_a59.cpp:13
#define ADUNCONST(type, obj)
Creates a shallow copy of obj that is not CONST.
Definition: fvar.hpp:140
Vector of double precision numbers.
Definition: dvector.h:50
int indexmin() const
Get minimum valid index.
Definition: dvector.h:199
int ADqd_flag
Definition: model.cpp:28
static int separable_flag
Definition: df1b2fun.h:1351
static void get_cgradient_contribution(dvector, int)
Description not yet available.
Definition: quadpri.cpp:558
void val(const adstring &s, int &v, int &code)
Definition: val.cpp:11
static char ** argv
Definition: fvar.hpp:8866
static int passnumber
Definition: df1b2fun.h:289
static int num_inactive_vars
Definition: df1b2fnl.h:66
double calculate_importance_sample_shess(const dvector &x, const dvector &u0, const dmatrix &Hess, const dvector &_xadjoint, const dvector &_uadjoint, const dmatrix &_Hessadjoint, function_minimizer *pmin)
Description not yet available.
Definition: df1b2impspf.cpp:25
dvector evaluate_function_with_quadprior(const dvector &x, int usize, function_minimizer *pfmin)
Description not yet available.
Definition: quadpri.cpp:45
static void get_cHessian_contribution_from_vHessian(dmatrix, int)
Description not yet available.
Definition: quadpri.cpp:668
int indexmax(void) const
Definition: fvar.hpp:7999
exitptr ad_exit
Definition: gradstrc.cpp:53
df1_two_variable fabs(const df1_two_variable &x)
Definition: df12fun.cpp:891
double calculate_laplace_approximation(const dvector &x, const dvector &u0, const dmatrix &Hess, const dvector &_xadjoint, const dvector &_uadjoint, const dmatrix &_Hessadjoint, function_minimizer *pmin)
Description not yet available.
Definition: df1b2lap.cpp:1476
void initialize(void)
Description not yet available.
Definition: df1b2f10.cpp:171
static dvariable reset(const dvar_vector &x)
Definition: model.cpp:345
ADMB variable vector.
Definition: fvar.hpp:2172
void do_separable_stuff_laplace_approximation_banded(df1b2variable &)
Description not yet available.
Definition: df1b2lp6.cpp:897
double gmax
maximum gradient
Definition: fvar.hpp:3303
Description not yet available.
Definition: df1b2fun.h:266
void gradcalc(int nvar, const dvector &g)
Definition: sgradclc.cpp:77
df1_one_matrix choleski_decomp(const df1_one_matrix &MM)
Definition: df11fun.cpp:606
static int nvarcalc()
Definition: model.cpp:152
ivector sgn(const dvector &v)
Author: David Fournier Copyright (c) 2008-2012 Regents of the University of California.
Definition: dvect24.cpp:11
fixed_smartlist nlist
Definition: df1b2fun.h:754
banded_symmetric_dmatrix * bHess
Definition: adrndeff.h:265
void deallocate(void)
If no other copies exist, free df1b2variable::ptr.
Definition: df1b2fn2.cpp:286
void initialize(void)
Description not yet available.
Definition: df1b2f13.cpp:158
double evaluate_function(const dvector &x, function_minimizer *pfmin)
Author: David Fournier Copyright (c) 2008-2012 Regents of the University of California.
Definition: df1b2lap.cpp:2026
dvector solve(const dmatrix &aa, const dvector &z)
Solve a linear system using LU decomposition.
Definition: dmat34.cpp:46
void check_for_need_to_reallocate(int ip)
Does Nothing.
Definition: df1b2lap.cpp:1968
prescientific setscientific(void)
Description not yet available.
Definition: admanip.cpp:80
int * get_v() const
Definition: ivector.h:114
static laplace_approximation_calculator * lapprox
Definition: df1b2fnl.h:61
dvector get_uhat_quasi_newton(const dvector &x, function_minimizer *pfmin)
Description not yet available.
Definition: df1b2lap.cpp:63
prnstream & endl(prnstream &)
dmatrix eigenvectors(const banded_symmetric_dmatrix &_SS, const dvector &_e)
Description not yet available.
Definition: dmat28.cpp:442
double fbest
Definition: fvar.hpp:3297
Description not yet available.
Definition: fvar.hpp:1937
test_smartlist list
Definition: df1b2fun.h:753
Array of integers(int) with indexes from index_min to indexmax.
Definition: ivector.h:50
dvector solve_trans(const lower_triangular_dmatrix &M, const dvector &y)
Description not yet available.
Definition: dmat36.cpp:80
static void set_active_only_random_effects(void)
Definition: model.cpp:251
double inner_optimization_banded(dvector &x, function_minimizer *pfmin, int &no_converge_flag)
Description not yet available.
Definition: df1b2lp6.cpp:297
void initialize(void)
Description not yet available.
Definition: dmat41.cpp:17
df1b2variable * tmp_pen
Definition: df1b2lp6.cpp:815
static unsigned int num_active_parameters
Definition: df1b2fnl.h:67
fixed_smartlist2 nlist3
Definition: df1b2fun.h:758
static int argc
Definition: fvar.hpp:8863
int indexmax() const
Get maximum valid index.
Definition: dvector.h:204
fixed_smartlist2 nlist2
Definition: df1b2fun.h:756
preshowpoint setshowpoint(void)
Description not yet available.
Definition: admanip.cpp:38
static imatrix * plist
Definition: df1b2fnl.h:69
static objective_function_value * pobjfun
Definition: admodel.h:2394
Description not yet available.
static void xinit(const dvector &x)
Definition: model.cpp:226
Description not yet available.
Definition: fvar.hpp:8120
#define M
Definition: rngen.cpp:57
static int bad_step_flag
Definition: admodel.h:1853
int indexmax() const
Definition: ivector.h:104
Description not yet available.
Definition: fvar.hpp:2819
dvector get_newton_raphson_info_banded(function_minimizer *pmin)
Description not yet available.
Definition: df1b2lp6.cpp:822
void initialize(void)
Initialze all elements of dvector to zero.
Definition: dvect5.cpp:10
values
Definition: adjson.h:22
void do_separable_stuff_newton_raphson_banded(df1b2variable &)
Description not yet available.
Definition: df1b2lp6.cpp:680
double ln_det(const dmatrix &m1, int &sgn)
Compute log determinant of a constant matrix.
Definition: dmat3.cpp:536
dvector banded_calculations_trust_region_approach(const dvector &_uhat, function_minimizer *pmin)
Description not yet available.
Definition: df1b2lp6.cpp:1143
unsigned int size() const
Get number of elements in array.
Definition: dvector.h:209
int option_match(int argc, char *argv[], const char *string)
Checks if the program has been invoked with a particular command line argument (&quot;string&quot;).
Definition: optmatch.cpp:25
int indexmax() const
Definition: fvar.hpp:2921
std::ostream & get_output_stream()
Definition: adglobl.cpp:45
void initialize(void)
Description not yet available.
Definition: ivec2.cpp:17
test_smartlist list3
Definition: df1b2fun.h:757
dmatrix make_dmatrix(dcompressed_triplet &M)
Definition: hs_sparse.cpp:202
Description not yet available.
Definition: df1b2fun.h:373
Description not yet available.
Definition: fvar.hpp:9410
virtual void AD_uf_outer()
Definition: xmodelm4.cpp:39
int indexmin(void) const
Definition: fvar.hpp:7995
double ln_det_choleski(const banded_symmetric_dmatrix &MM, int &ierr)
Definition: dmat38.cpp:218
static int set_index(void)
Definition: df1b2f15.cpp:90
dvector get_uhat_quasi_newton_qd(const dvector &x, function_minimizer *pfmin)
Description not yet available.
Definition: f1b2lapqd.cpp:28
Description not yet available.
Description not yet available.
Definition: admodel.h:1850
static void set_YES_DERIVATIVES(void)
Enable accumulation of derivative information.
Definition: gradstrc.cpp:650
virtual void user_function()
Definition: xmodelm4.cpp:43
static void get_cHessian_contribution(dmatrix, int)
Description not yet available.
Definition: quadpri.cpp:591
int admb_ssflag
Definition: df1b2lp6.cpp:34
void safe_allocate(int ncl, int ncu)
Description not yet available.
Definition: ivector.cpp:78
static int get_num_quadratic_prior(void)
Definition: df1b2fun.h:1967
dvector value(const df1_one_vector &v)
Definition: df11fun.cpp:69
#define PI
Definition: fvar.hpp:95
static int no_atlas_flag
Definition: fvar.hpp:8840
static int stddev_scale(const dvector &d, const dvector &x)
Definition: model.cpp:202
static int mc_phase
Definition: admodel.h:846
static unsigned int num_vars
Definition: df1b2fnl.h:64
static init_df1b2vector * py
Definition: df1b2fnl.h:68
int use_dd_nr
Definition: df1b2lp6.cpp:33
dvector get_uhat_lm_newton(const dvector &x, function_minimizer *pfmin)
Description not yet available.
Definition: df1b2lap.cpp:218
void initialize(void)
Author: David Fournier Copyright (c) 2008-2012 Regents of the University of California.
Definition: dmat7.cpp:12
static int get_num_quadratic_prior(void)
Definition: df1b2fun.h:1916
double *& get_v(void)
Definition: dvector.h:148
void reset(void)
Description not yet available.
Definition: df1b2fn2.cpp:581
test_smartlist list2
Definition: df1b2fun.h:755
Fundamental data type for reverse mode automatic differentiation.
Definition: fvar.hpp:1518
double * u_dot
Definition: df1b2fun.h:196
d3_array log(const d3_array &arr3)
Author: David Fournier Copyright (c) 2008-2012 Regents of the University of California.
Definition: d3arr2a.cpp:13
dmatrix choleski_decomp_positive(const dmatrix &MM, double bound)
Description not yet available.
Definition: dmat35.cpp:33
Description not yet available.
Definition: fvar.hpp:8065
void df1b2_gradcalc1(void)
Description not yet available.
Definition: df1b2fun.cpp:145
static void set_inactive_only_random_effects(void)
Definition: model.cpp:259
int bandwidth(void) const
Definition: fvar.hpp:7991
static int in_qp_calculations
Definition: df1b2fun.h:1909