ADMB Documentation  -a65f1c97
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
df1b2lp8.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 #include <admodel.h>
12 #include <df1b2fun.h>
13 #include <adrndeff.h>
14 #ifndef OPT_LIB
15  #include <cassert>
16  #include <climits>
17 #endif
18 double fcomp1(dvector x,dvector d,int samplesize,int n,dvector & g,
19  dmatrix& M);
20 
26 {
27  //int i;
28  /*
29  int mmax=Hess.indexmax();
30  int nz=sum(Hess);
31  if (sparse_triplet)
32  {
33  delete sparse_triplet;
34  sparse_triplet=0;
35  }
36  */
37  int nz2=0;
39  {
41  int mmin=cti(cti.indexmin()).indexmin();
42  int mmax=cti(cti.indexmin()).indexmax();
43  nz2=1;
44  int lmin=cti(2,1);
45  for (int i=mmin+1;i<=mmax;i++)
46  {
47  if (cti(1,i)>cti(1,i-1))
48  {
49  nz2++;
50  lmin=cti(2,i);
51  }
52  else if (cti(2,i)>lmin)
53  {
54  lmin=cti(2,i);
55  nz2++;
56  }
57  }
58  }
59  //cout << nz2-nz << endl;
60 
61  if (sparse_triplet)
62  {
63  delete sparse_triplet;
65  }
66  //sparse_triplet = new dcompressed_triplet(1,nz2,usize,usize);
67  if (sparse_triplet2)
68  {
69  delete sparse_triplet2;
71  }
73 
75  {
77  int mmin=cti(cti.indexmin()).indexmin();
78  int mmax=cti(cti.indexmin()).indexmax();
79  if (sparse_iterator)
80  {
81  delete sparse_iterator;
83  }
84  sparse_iterator=new ivector(mmin,mmax);
85  int ii=1;
86  int lmin=cti(2,1);
87  (*sparse_triplet2)(1,ii)=cti(1,1);
88  (*sparse_triplet2)(2,ii)=cti(2,1);
89  (*sparse_iterator)(cti(3,1))=ii;
90  for (int i=mmin+1;i<=mmax;i++)
91  {
92  if (cti(1,i)>cti(1,i-1))
93  {
94  ii++;
95  (*sparse_triplet2)(1,ii)=cti(1,i);
96  (*sparse_triplet2)(2,ii)=cti(2,i);
97  lmin=cti(2,i);
98  }
99  else if (cti(2,i)>lmin)
100  {
101  ii++;
102  (*sparse_triplet2)(1,ii)=cti(1,i);
103  (*sparse_triplet2)(2,ii)=cti(2,i);
104  lmin=cti(2,i);
105  }
106  (*sparse_iterator)(cti(3,i))=ii;
107  }
108  }
109  //cout << setw(8) << setprecision(2) << setscientific() << rowsum(Hess)
110  // << endl;
111  //cout << setw(8) << setprecision(2) << setscientific() << Hess << endl;
112  /*
113  int ii=0;
114  for (i=1;i<=mmax;i++)
115  {
116  for (int j=i;j<=mmax;j++)
117  {
118  if (Hess(i,j) != 0.0)
119  {
120  ++ii;
121  (*sparse_triplet)(1,ii)=i;
122  (*sparse_triplet)(2,ii)=j;
123  (*sparse_triplet)(ii)=0.0;
124  }
125  }
126  }
127  */
128  //sparse_symbolic = new hs_symbolic(*sparse_triplet,1);
130 }
131 
137 {
138  // number of random vectors
139  const ivector & itmp=(*num_local_re_array)(1,num_separable_calls);
140  //const ivector & itmpf=(*num_local_fixed_array)(1,num_separable_calls);
141  for (int i=2;i<=num_separable_calls;i++)
142  {
143  if (itmp(i) != itmp(i-1))
144  {
145  cerr << "At present can only use antithetical rv's when "
146  "all separable calls are the same size" << endl;
147  ad_exit(1);
148  }
149  }
150  int n=itmp(1);
151  int samplesize=num_importance_samples;
152 
153  // mesh size
154  double delta=0.01;
155  // maximum of distribution is near here
156  double mid=sqrt(double(n-1));
157  dmatrix weights(1,2*n,1,2);
158  double spread=15;
159  if (mid-spread<=0.001)
160  spread=mid-0.1;
161  double ssum=0.0;
162  double x=0.0;
163  double tmax=(n-1)*log(mid)-0.5*mid*mid;
164  for (x=mid-spread;x<=mid+spread;x+=delta)
165  {
166  ssum+=exp((n-1)*log(x)-0.5*x*x-tmax);
167  }
168  double tsum=0;
169  dvector dist(1,samplesize+1);
170  dist.initialize();
171  int is=0;
172  int ii;
173  for (x=mid-spread;x<=mid+spread;x+=delta)
174  {
175  tsum+=exp((n-1)*log(x)-0.5*x*x-tmax)/ssum*samplesize;
176  int ns=int(tsum);
177  for (ii=1;ii<=ns;ii++)
178  {
179  dist(++is)=x;
180  }
181  tsum-=ns;
182  }
183  if (is==samplesize-1)
184  {
185  dist(samplesize)=mid;
186  }
187  else if (is<samplesize-1)
188  {
189  cerr << "This can't happen" << endl;
190  ad_exit(1);
191  }
192 
193  // get random numbers
194 
196  if (antiepsilon)
197  {
198  if (allocated(*antiepsilon))
199  {
200  delete antiepsilon;
201  antiepsilon=0;
202  }
203  }
204  antiepsilon=new dmatrix(1,samplesize,1,n);
205  dmatrix & M=*antiepsilon;
206  M.fill_randn(rng);
207 
208  for (int i=1;i<=samplesize;i++)
209  {
210  M(i)=M(i)/norm(M(i));
211  }
212  int nvar=(samplesize-1)*n;
213  independent_variables xx(1,nvar);
214  ii=0;
215  for (int i=2;i<=samplesize;i++)
216  {
217  for (int j=1;j<=n;j++)
218  {
219  xx(++ii)=M(i,j);
220  }
221  }
222 
223  fmmt1 fmc(nvar,5);
224  //fmm fmc(nvar,5);
225  fmc.noprintx=1;
226  fmc.iprint = defaults::iprint;
227  fmc.maxfn=2500;
228  fmc.crit=1.e-6;
229 
230  double f;
231  double fbest=1.e+50;;
232  dvector g(1,nvar);
233  dvector gbest(1,nvar);
234  dvector xbest(1,nvar);
235 
236  gbest.fill_seqadd(1.e+50,0.);
237  {
238  while (fmc.ireturn>=0)
239  {
240  //int badflag=0;
241  fmc.fmin(f,xx,g);
242  if (fmc.ihang)
243  {
244  //int hang_flag=fmc.ihang;
245  //double maxg=max(g);
246  //double ccrit=fmc.crit;
247  //int current_ifn=fmc.ifn;
248  }
249  if (fmc.ireturn>0)
250  {
251  f=fcomp1(xx,dist,samplesize,n,g,M);
252  if (f < fbest)
253  {
254  fbest=f;
255  gbest=g;
256  xbest=xx;
257  }
258  }
259  }
260  xx=xbest;
261  }
262  ii=0;
263  for (int i=2;i<=samplesize;i++)
264  {
265  for (int j=1;j<=n;j++)
266  {
267  M(i,j)=xx(++ii);
268  }
269  }
270  for (int i=1;i<=samplesize;i++)
271  {
272  M(i)*=dist(i)/norm(M(i));
273  }
274 }
275 
280 double fcomp1(dvector x,dvector d,int samplesize,int n,dvector & g,
281  dmatrix& M)
282 {
283  dmatrix VM(1,samplesize,1,n);
284  dmatrix C(1,samplesize,1,samplesize);
285  dmatrix VM0(1,samplesize,1,n);
286  dvector N(1,samplesize);
287  dmatrix dfVM(1,samplesize,1,n);
288  dmatrix dfVM0(1,samplesize,1,n);
289  dvector dfN(1,samplesize);
290  dfVM.initialize();
291  dfVM0.initialize();
292  dfN.initialize();
293 
294  double f=0.0;
295  int ii=0;
296  VM0(1)=M(1);
297  for (int i=2;i<=samplesize;i++)
298  {
299  for (int j=1;j<=n;j++)
300  {
301  VM0(i,j)=x(++ii);
302  }
303  }
304  for (int i=1;i<=samplesize;i++)
305  {
306  N(i)=norm(VM0(i));
307  VM(i)=VM0(i)*(d(i)/N(i));
308  }
309  for (int i=1;i<=samplesize;i++)
310  {
311  for (ii=i+1;ii<=samplesize;ii++)
312  {
313  //C(i,ii)=1.0/(0.01+norm2(VM(i)-VM(ii)));
314  //f+=C(i,ii);
315  C(i,ii)=norm2(VM(i)-VM(ii));
316  f-=C(i,ii);
317  //f+=1.0/(0.01+norm2(VM(i)-VM(ii)));
318  }
319  f+=100.0*square(log(N(i)));
320  }
321  for (int i=1;i<=samplesize;i++)
322  {
323  //f+=100.0*square(log(N(i)));
324  dfN(i)+=200*log(N(i))/N(i);
325  for (ii=i+1;ii<=samplesize;ii++)
326  {
327  //f+=1.0/(0.01+norm2(VM(i)-VM(ii)));
328  //double tmp=-1.0/square(0.01+norm2(VM(i)-VM(ii)));
329  //double tmp=-square(C(i,ii));
330  dvector vtmp=-2.0*(VM(i)-VM(ii));
331  dfVM(i)+=vtmp;
332  dfVM(ii)-=vtmp;
333  }
334  }
335  for (int i=1;i<=samplesize;i++)
336  {
337  //VM(i)=VM0(i)*(d(i)/N(i));
338  dfVM0(i)=dfVM(i)*d(i)/N(i);
339  dfN(i)-=(dfVM(i)*VM0(i))*d(i)/square(N(i));
340 
341  //N(i)=norm(VM0(i));
342  dfVM0(i)+=dfN(i)/N(i)*VM0(i);
343  }
344  ii=0;
345  for (int i=2;i<=samplesize;i++)
346  {
347  for (int j=1;j<=n;j++)
348  {
349  //VM0(i,j)=vx(++ii);
350  g(++ii)=dfVM0(i,j);
351  }
352  }
353  return f;
354 }
355 
361  function_minimizer * pfmin)
362 {
363  pfmin->pre_user_function();
364 }
365 
371 {
372  if (lapprox)
373  {
374  if (lapprox->hesstype==2)
375  {
377  }
378  }
379  user_function();
380  /*
381  if (lapprox)
382  {
383  if (lapprox->hesstype==2)
384  {
385  lapprox->nested_shape.trim();
386  cout << lapprox->nested_shape;
387  lapprox->nested_indices.allocate(lapprox->nested_shape);
388  lapprox->separable_calls_counter=0;
389  user_function();
390  }
391  }
392  */
393 }
394 
401 {
402  int ip = 0;
404  {
405  hesstype=4;
406  if (allocated(Hess))
407  {
408  if (Hess.indexmax()!=usize)
409  {
410  Hess.deallocate();
411  Hess.allocate(1,usize,1,usize);
412  }
413  }
414  else
415  {
416  Hess.allocate(1,usize,1,usize);
417  }
418  if (allocated(Hessadjoint))
419  {
420  if (Hessadjoint.indexmax()!=usize)
421  {
424  }
425  }
426  else
427  {
429  }
430  return;
431  }
432  else
433  {
435  if (allocated(used_flags))
436  {
437  if (used_flags.indexmax() != nv)
438  {
440  }
441  }
442  if (!allocated(used_flags))
443  {
445  }
446 
447  //for (ip=1;ip<=num_der_blocks;ip++)
448  {
450  // do we need to reallocate memory for df1b2variables?
453  //cout << re_objective_function_value::pobjfun << endl;
454  //cout << re_objective_function_value::pobjfun->ptr << endl;
455  (*re_objective_function_value::pobjfun)=0;
456  df1b2variable pen=0.0;
457  df1b2variable zz=0.0;
458 
460  // call function to do block diagonal newton-raphson
461  // the step vector from the newton-raphson is in the vector step
463 
466 
468 
470  {
471  // just to get the number of separable calls
473  pfmin->AD_uf_inner();
474  // allocate space for uncompressed sparse hessian information
475 
476  //num_separable_calls=separable_calls_counter;
477  if (triplet_information==0)
478  {
480  }
482  {
483  delete triplet_information;
485  }
488  }
489 
490  pfmin->pre_user_function();
491 
492 
494  {
495  // turn triplet_informaiton into compressed_triplet_information
496  int mmin= triplet_information->indexmin();
497  int mmax= triplet_information->indexmax();
498  int ndim=0;
499  for (int i=mmin;i<=mmax;i++)
500  {
501  if (allocated((*triplet_information)(i)))
502  {
503  ndim+=(*triplet_information)(i,1).indexmax();
504  }
505  }
507  {
510  }
511  compressed_triplet_information=new imatrix(1,ndim,1,3);
512  (*compressed_triplet_information)(3).fill_seqadd(1,1);
513  int ii=0;
514  for (int i=mmin;i<=mmax;i++)
515  {
516  if (allocated((*triplet_information)(i)))
517  {
518  int jmin=(*triplet_information)(i,1).indexmin();
519  int jmax=(*triplet_information)(i,1).indexmax();
520  for (int j=jmin;j<=jmax;j++)
521  {
522  ii++;
523  (*compressed_triplet_information)(ii,1)=
524  (*triplet_information)(i,1,j);
525  (*compressed_triplet_information)(ii,2)=
526  (*triplet_information)(i,2,j);
527  (*compressed_triplet_information)(ii,3)=ii;
528  }
529  }
530  }
532  cti=sort(cti,1);
533  int lmin=1;
534  int lmax=0;
535  for (int i=2;i<=ndim;i++)
536  {
537  if (cti(i,1)>cti(i-1,1))
538  {
539  lmax=i-1;
540  cti.sub(lmin,lmax)=sort(cti.sub(lmin,lmax),2);
541  lmin=i;
542  }
543  }
544  cti.sub(lmin,ndim)=sort(cti.sub(lmin,ndim),2);
545  imatrix tmp=trans(cti);
548  }
549 
551 
552  int non_block_diagonal=0;
553  for (int i=xsize+1;i<=xsize+usize;i++)
554  {
555  if (used_flags(i)>1)
556  {
557  non_block_diagonal=1;
558  break;
559  }
560  }
561  if (non_block_diagonal)
562  {
563  if (bw< usize/2 && sparse_hessian_flag==0)
564  {
565  hesstype=3; //banded
566  if (bHess)
567  {
568  if (bHess->bandwidth() !=bw)
569  {
570  delete bHess;
571  bHess = new banded_symmetric_dmatrix(1,usize,bw);
572  if (bHess==0)
573  {
574  cerr << "Error allocating banded_symmetric_dmatrix" << endl;
575  ad_exit(1);
576  }
577  }
578  }
579  else
580  {
581  bHess = new banded_symmetric_dmatrix(1,usize,bw);
582  if (bHess==0)
583  {
584  cerr << "Error allocating banded_symmetric_dmatrix" << endl;
585  ad_exit(1);
586  }
587  }
588  if (bHessadjoint)
589  {
590  if (bHessadjoint->bandwidth() !=bw)
591  {
592  delete bHessadjoint;
594  if (bHessadjoint==0)
595  {
596  cerr << "Error allocating banded_symmetric_dmatrix" << endl;
597  ad_exit(1);
598  }
599  }
600  }
601  else
602  {
604  if (bHessadjoint==0)
605  {
606  cerr << "Error allocating banded_symmetric_dmatrix" << endl;
607  ad_exit(1);
608  }
609  }
610  }
611  else
612  {
613  //check_sparse_matrix_structure();
614  hesstype=4; // band is so wide so use full matrix
615  if (bHess)
616  {
617  delete bHess;
618  bHess=0;
619  }
620 
621  if (bHessadjoint)
622  {
623  delete bHessadjoint;
624  bHessadjoint=0;
625  }
626 
627  if (allocated(Hess))
628  {
630  {
631  Hess.deallocate();
632  }
633  else
634  {
635  if (Hess.indexmax() != usize)
636  {
637  Hess.deallocate();
638  Hess.allocate(1,usize,1,usize);
639  }
640  }
641  }
642  else
643  {
644  if (sparse_hessian_flag==0)
645  Hess.allocate(1,usize,1,usize);
646  }
648  {
650  }
651 
652  if (allocated(Hessadjoint))
653  {
655  {
656  Hess.deallocate();
657  }
658  else
659  {
660  if (Hessadjoint.indexmax() != usize)
661  {
663  Hessadjoint.allocate(1,usize,1,usize);
664  }
665  }
666  }
667  else
668  {
669  if (sparse_hessian_flag==0)
670  Hessadjoint.allocate(1,usize,1,usize);
671  }
672  }
673  }
674  else
675  {
676  hesstype=2;
677  }
678  if (hesstype==2 && num_importance_samples>0)
679  {
681  {
684  }
688  }
689 
691  {
692  const ivector & itmp=(*num_local_re_array)(1,num_separable_calls);
693  const ivector & itmpf=(*num_local_fixed_array)(1,num_separable_calls);
694 
695  // ****************************************************
696  // ****************************************************
697  if (antiflag>0)
698  {
699  // generate antithetical rv's
701  }
702  if (use_gauss_hermite>0)
703  {
704  if (gh)
705  {
706  delete gh;
707  gh=0;
708  }
710  num_separable_calls,itmp);
711  }
712 
713  if (block_diagonal_vch)
714  {
715  delete block_diagonal_vch;
717  }
719  1,itmp,1,itmp);
720 
721  if (block_diagonal_ch)
722  {
723  delete block_diagonal_ch;
725  }
727  1,itmp,1,itmp);
728 
730  {
731  delete block_diagonal_hessian;
733  }
735  1,itmp,1,itmp);
736  if (block_diagonal_hessian ==0)
737  {
738  cerr << "error_allocating d3_array" << endl;
739  ad_exit(1);
740  }
741 
743  {
744  delete block_diagonal_re_list;
746  }
748  1,itmp);
749  if (block_diagonal_re_list == 0)
750  {
751  cerr << "error_allocating imatrix" << endl;
752  ad_exit(1);
753  }
754 
756  {
757  delete block_diagonal_fe_list;
759  }
761  1,itmpf);
762  if (block_diagonal_fe_list ==0)
763  {
764  cerr << "error_allocating imatrix" << endl;
765  ad_exit(1);
766  }
767 
768  // ****************************************************
769  if (block_diagonal_Dux)
770  {
771  delete block_diagonal_Dux;
773  }
775  1,itmp,1,itmpf);
776  if (block_diagonal_Dux ==0)
777  {
778  cerr << "error_allocating d3_array" << endl;
779  ad_exit(1);
780  }
781 
782  // ****************************************************
783  // ****************************************************
785  {
788  }
790  1,itmp,1,itmp);
791  if (block_diagonal_vhessian ==0)
792  {
793  cerr << "error_allocating d3_array" << endl;
794  ad_exit(1);
795  }
796 
798  {
801  }
803  1,itmp,1,itmp);
805  {
806  cerr << "error_allocating d3_array" << endl;
807  ad_exit(1);
808  }
809  }
812  pen.deallocate();
813  }
814  }
815 }
816 
822 {
823  const ivector & itmp=(*num_local_re_array)(1,num_separable_calls);
824  const ivector & itmpf=(*num_local_fixed_array)(1,num_separable_calls);
825 
826  // ****************************************************
827  // ****************************************************
828  if (block_diagonal_vch)
829  {
830  delete block_diagonal_vch;
832  }
834  1,itmp,1,itmp);
835  if (block_diagonal_ch)
836  {
837  delete block_diagonal_ch;
839  }
841  1,itmp,1,itmp);
843  {
844  delete block_diagonal_hessian;
846  }
848  1,itmp,1,itmp);
849  if (block_diagonal_hessian ==0)
850  {
851  cerr << "error_allocating d3_array" << endl;
852  ad_exit(1);
853  }
855  {
856  delete block_diagonal_re_list;
858  }
860  1,itmp);
861  if (block_diagonal_re_list ==0)
862  {
863  cerr << "error_allocating imatrix" << endl;
864  ad_exit(1);
865  }
867  {
868  delete block_diagonal_fe_list;
870  }
872  1,itmpf);
873  if (block_diagonal_fe_list ==0)
874  {
875  cerr << "error_allocating imatrix" << endl;
876  ad_exit(1);
877  }
878  // ****************************************************
879  if (block_diagonal_Dux)
880  {
881  delete block_diagonal_Dux;
883  }
885  1,itmp,1,itmpf);
886  if (block_diagonal_Dux ==0)
887  {
888  cerr << "error_allocating d3_array" << endl;
889  ad_exit(1);
890  }
891 
892  // ****************************************************
893  // ****************************************************
895  {
898  }
900  1,itmp,1,itmp);
901  if (block_diagonal_vhessian ==0)
902  {
903  cerr << "error_allocating d3_array" << endl;
904  ad_exit(1);
905  }
906 
908  {
911  }
913  1,itmp,1,itmp);
915  {
916  cerr << "error_allocating d3_array" << endl;
917  ad_exit(1);
918  }
919 }
920 
924 void save_number_of_local_effects(int num_separable_calls,
925  ivector ** num_local_re_array, ivector ** num_local_fixed_array,
926  int num_local_re,int num_fixed_effects)
927  //ivector& lre_index,ivector& lfe_index)
928 {
929  if (*num_local_re_array == NULL)
930  {
931  *num_local_re_array = new ivector(1,1000);
932  if (*num_local_re_array == NULL)
933  {
934  cerr << "error allocating ivector" << endl;
935  ad_exit(1);
936  }
937  }
938 
939  if (num_separable_calls> (*num_local_re_array)->indexmax())
940  {
941  if (num_separable_calls != (*num_local_re_array)->indexmax()+1)
942  {
943  cerr << "this can't happen" << endl;
944  ad_exit(1);
945  }
946  int old_max=(*num_local_re_array)->indexmax();
947 #ifdef OPT_LIB
948  int new_max=old_max+100+(int)(10.0*sqrt(double(old_max)));
949 #else
950  double sqrt_oldmax = 10.0 * sqrt(double(old_max));
951  assert(sqrt_oldmax <= INT_MAX);
952  int new_max=old_max+100+(int)sqrt_oldmax;
953 #endif
954  ivector tmp(1,old_max);
955  tmp=(**num_local_re_array);
956  //(*num_local_re_array)=new ivector(1,new_max);
957  delete *num_local_re_array;
958  *num_local_re_array = new ivector(1,new_max);
959  if (*num_local_re_array == NULL)
960  {
961  cerr << "error allocating ivector" << endl;
962  ad_exit(1);
963  }
964  (**num_local_re_array)(1,old_max)=tmp;
965  }
966  (**num_local_re_array)(num_separable_calls)=num_local_re;
967 
968  //***********************************************************
969  //***********************************************************
970  //***********************************************************
971  //***********************************************************
972 
973  if (*num_local_fixed_array == NULL)
974  {
975  *num_local_fixed_array = new ivector(1,1000);
976  if (*num_local_fixed_array == NULL)
977  {
978  cerr << "error allocating ivector" << endl;
979  ad_exit(1);
980  }
981  }
982 
983  if (num_separable_calls> (*num_local_fixed_array)->indexmax())
984  {
985  if (num_separable_calls != (*num_local_fixed_array)->indexmax()+1)
986  {
987  cerr << "this can't happen" << endl;
988  ad_exit(1);
989  }
990  int old_max=(*num_local_fixed_array)->indexmax();
991 #ifdef OPT_LIB
992  int new_max=old_max+100+(int)(10.0*sqrt(double(old_max)));
993 #else
994  double sqrt_oldmax = 10.0 * sqrt(double(old_max));
995  assert(sqrt_oldmax <= INT_MAX);
996  int new_max=old_max+100+(int)sqrt_oldmax;
997 #endif
998  ivector tmp(1,old_max);
999  tmp=(**num_local_fixed_array);
1000  //(*num_local_fixed_array)=new ivector(1,new_max);
1001  delete *num_local_fixed_array;
1002  *num_local_fixed_array = new ivector(1,new_max);
1003  if (*num_local_fixed_array == NULL)
1004  {
1005  cerr << "error allocating ivector" << endl;
1006  ad_exit(1);
1007  }
1008  (**num_local_fixed_array)(1,old_max)=tmp;
1009  }
1010  (**num_local_fixed_array)(num_separable_calls)=num_fixed_effects;
1011 }
1012 
1013 
1020 {
1022 
1024  int num_local_re=0;
1025  int num_fixed_effects=0;
1026 #ifndef OPT_LIB
1027  assert(funnel_init_var::num_active_parameters <= INT_MAX);
1028 #endif
1030  ivector lfe_index(1, (int)funnel_init_var::num_active_parameters);
1031 
1032  for (int i=1;i<=(int)funnel_init_var::num_active_parameters;i++)
1033  {
1034  if (list(i,1)>xsize)
1035  {
1036  lre_index(++num_local_re)=i;
1037  }
1038  else if (list(i,1)>0)
1039  {
1040  lfe_index(++num_fixed_effects)=i;
1041  }
1042  }
1043 
1044  //if (num_local_re > 0)
1045  {
1046  switch(hesstype)
1047  {
1048  case 3:
1051  &num_local_re_array, &num_local_fixed_array, num_local_re,
1052  num_fixed_effects); //,lre_index,lfe_index);
1053  for (int i=1;i<=num_local_re;i++)
1054  {
1055  int lrei=lre_index(i);
1056  for (int j=1;j<=num_local_re;j++)
1057  {
1058  int lrej=lre_index(j);
1059  int i1=list(lrei,1)-xsize;
1060  int j1=list(lrej,1)-xsize;
1061  if (i1>=j1)
1062  {
1063  //(*bHess)(i1,j1)+=locy(i2).u_bar[j2-1];
1064  int w=i1-j1+1;
1065  if (bw<w) bw=w;
1066  }
1067  }
1068  }
1069 
1070  if (sparse_hessian_flag)
1071  {
1072  if (allocated(Hess))
1073  {
1074  Hess.deallocate();
1075  /*
1076  if (Hess.indexmax() !=usize)
1077  {
1078  Hess.deallocate();
1079  Hess.allocate(1,usize,1,usize);
1080  Hess.initialize();
1081  }
1082  */
1083  }
1084  /*
1085  else
1086  {
1087  Hess.allocate(1,usize,1,usize);
1088  Hess.initialize();
1089  }
1090  */
1091  int dim= num_local_re*num_local_re;
1092  imatrix tmp(1,2,1,dim);
1093 
1094  int ii=0;
1095  for (int i=1;i<=num_local_re;i++)
1096  {
1097  int lrei=lre_index(i);
1098  for (int j=1;j<=num_local_re;j++)
1099  {
1100  int lrej=lre_index(j);
1101  int i1=list(lrei,1)-xsize;
1102  int j1=list(lrej,1)-xsize;
1103  if (i1<=0)
1104  {
1105  cout << "cant happen?" << endl;
1106  }
1107  if (i1<=j1)
1108  {
1109  //Hess(i1,j1)=1;
1110  ii++;
1111  tmp(1,ii)=i1;
1112  tmp(2,ii)=j1;
1113  //(*triplet_information)(1,num_separable_calls)=i1;
1114  //(*triplet_information)(2,num_separable_calls)=j1;
1115  }
1116  }
1117  }
1118 
1120  {
1121  (*triplet_information)(num_separable_calls).deallocate();
1122  }
1123  if (ii>0)
1124  {
1125  (*triplet_information)(num_separable_calls).allocate(1,2,1,ii);
1126  (*triplet_information)(num_separable_calls)(1)=tmp(1)(1,ii);
1127  (*triplet_information)(num_separable_calls)(2)=tmp(2)(1,ii);
1128  }
1129  }
1130  }
1131  }
1132  if (derindex)
1133  {
1135  {
1136  cerr << "Need to increase the maximum number of separable calls allowed"
1137  << " to at least " << num_separable_calls
1138  << endl << "Current value is " << derindex->indexmax() << endl;
1139  cerr << "Use the -ndi N command line option" << endl;
1140  ad_exit(1);
1141  }
1142 
1144  (*derindex)(num_separable_calls).deallocate();
1145  (*derindex)(num_separable_calls).allocate(1,num_local_re);
1146  for (int i=1;i<=num_local_re;i++)
1147  {
1148  int lrei=lre_index(i);
1149  int i1=list(lrei,1)-xsize;
1150  //int i1=list(lrei,1);
1151  (*derindex)(num_separable_calls)(i)=i1;
1152  }
1153  }
1154 
1155  f1b2gradlist->reset();
1163  funnel_init_var::num_active_parameters=0;
1165 }
1166 
1172 {
1173  ivector rowsize(1,usize);
1174  rowsize.initialize();
1175 
1176  imatrix tmp(1,usize,1,usize);
1177  tmp.initialize();
1178  for (int ii=1;ii<=num_separable_calls;ii++)
1179  {
1180  if (allocated((*derindex)(ii)))
1181  {
1182  ivector iv = sort((*derindex)(ii));
1183  int n=iv.indexmax();
1184  if (n>1) // so we have off diagonal elements
1185  {
1186  for (int i=1;i<=n;i++)
1187  {
1188  int row=iv(i);
1189  for (int j=1;j<=n;j++)
1190  {
1191  if (i != j)
1192  {
1193  int col=iv(j);
1194  int foundmatch=0;
1195  for (int k=1;k<=rowsize(row);k++)
1196  {
1197  if (tmp(row,k)==col)
1198  {
1199  foundmatch=1;
1200  break;
1201  }
1202  }
1203  if (foundmatch==0) // add a new element to tmp(row)
1204  {
1205  rowsize(row)++;
1206  if (rowsize(row)> tmp(row).indexmax())
1207  {
1208  tmp(row).reallocate(2); // double the size
1209  }
1210  tmp(row,rowsize(row))=col;
1211  }
1212  }
1213  }
1214  }
1215  }
1216  }
1217  }
1218  imatrix M(1,usize,1,rowsize);
1219  ofstream ofs("sparseness.info");
1220  ofs << "# Number of parameters" << endl;
1221  ofs << usize << endl;
1222  ofs << "# Number of off diagonal elements in each row" << endl;
1223  ofs << rowsize << endl;
1224  ofs << "# The nonempty rows of M" << endl;
1225  for (int i=1;i<=usize;i++)
1226  {
1227  if (rowsize(i)>0)
1228  {
1229  M(i)=sort(tmp(i)(1,rowsize(i)));
1230  ofs << setw(4) << M(i) << endl;
1231  }
1232  }
1233  return M;
1234 }
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
static void reset(const init_df1b2vector &, const df1b2variable &)
Description not yet available.
Definition: df1b2f15.cpp:51
dcompressed_triplet * sparse_triplet2
Definition: adrndeff.h:271
virtual void AD_uf_inner()
Definition: xmodelm4.cpp:40
df1b2_gradlist * f1b2gradlist
Definition: df1b2glo.cpp:49
double crit
Definition: fvar.hpp:3184
static void set_no_derivatives(void)
Definition: df1b2fun.h:760
function_minimizer * pmin
Definition: adrndeff.h:246
int indexmax() const
Definition: imatrix.h:142
Description not yet available.
Definition: imatrix.h:69
dcompressed_triplet * sparse_triplet
Definition: adrndeff.h:267
Description not yet available.
void allocate(void)
Author: David Fournier Copyright (c) 2008-2012 Regents of the University of California.
Definition: dmat0.cpp:8
void generate_antithetical_rvs()
Description not yet available.
Definition: df1b2lp8.cpp:136
void deallocate()
Deallocate dmatrix memory.
Definition: dmat.cpp:363
#define N
Definition: rngen.cpp:56
int indexmin() const
Definition: imatrix.h:138
void fmin(const double &f, const dvector &x, const dvector &g)
Description not yet available.
Definition: fmmtr1.cpp:67
dmatrix trans(const dmatrix &m1)
Author: David Fournier Copyright (c) 2008-2012 Regents of the University of California.
Definition: dmat2.cpp:13
#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
Vector of double precision numbers.
Definition: dvector.h:50
dvar_matrix * importance_sampling_components
Definition: adrndeff.h:224
long maxfn
Definition: fvar.hpp:3182
int noprintx
Definition: fvar.hpp:3181
double fcomp1(dvector x, dvector d, int samplesize, int n, dvector &g, dmatrix &M)
Description not yet available.
Definition: df1b2lp8.cpp:280
void fill_seqadd(double, double)
Fills dvector elements with values starting from base and incremented by offset.
Definition: cranfill.cpp:58
static int num_inactive_vars
Definition: df1b2fnl.h:66
void fill_randn(long int &n)
Fill matrix with random numbers.
Definition: ranfill.cpp:244
imatrix * compressed_triplet_information
Definition: adrndeff.h:193
gauss_hermite_stuff * gh
Definition: adrndeff.h:223
exitptr ad_exit
Definition: gradstrc.cpp:53
void initialize(void)
Description not yet available.
Definition: df1b2f10.cpp:171
dvector row(const dmatrix &matrix, int i)
Returns a copied row for matrix at i.
Definition: dmat6.cpp:24
const int iprint
Definition: fvar.hpp:9504
int indexmin() const
Definition: fvar.hpp:4021
void check_hessian_type(const dvector &_x, function_minimizer *)
Description not yet available.
Definition: df1b2lp8.cpp:360
double norm(const d3_array &a)
Return computed norm value of a.
Definition: d3arr2a.cpp:190
Description not yet available.
Definition: df1b2fun.h:266
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
void check_for_need_to_reallocate(int ip)
Does Nothing.
Definition: df1b2lap.cpp:1968
dvar3_array * block_diagonal_vch
Definition: adrndeff.h:228
static laplace_approximation_calculator * lapprox
Definition: df1b2fnl.h:61
Description not yet available.
Definition: fvar.hpp:7951
dmatrix sort(const dmatrix &m, int column, int NSTACK)
Description not yet available.
Definition: dmsort.cpp:17
prnstream & endl(prnstream &)
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
d3_array sqrt(const d3_array &arr3)
Author: David Fournier Copyright (c) 2008-2012 Regents of the University of California.
Definition: d3arr2c.cpp:11
long ihang
Definition: fvar.hpp:3192
static unsigned int num_active_parameters
Definition: df1b2fnl.h:67
fixed_smartlist2 nlist3
Definition: df1b2fun.h:758
void initialize(void)
Description not yet available.
Definition: imat3.cpp:20
fixed_smartlist2 nlist2
Definition: df1b2fun.h:756
static imatrix * plist
Definition: df1b2fnl.h:69
Description not yet available.
d3_array exp(const d3_array &arr3)
Returns d3_array results with computed exp from elements in arr3.
Definition: d3arr2a.cpp:28
#define M
Definition: rngen.cpp:57
Description not yet available.
Definition: fvar.hpp:9345
void pre_user_function(void)
Description not yet available.
Definition: df1b2lp8.cpp:370
int indexmax() const
Definition: ivector.h:104
void save_number_of_local_effects(int num_separable_calls, ivector **num_local_re_array, ivector **num_local_fixed_array, int num_local_re, int num_fixed_effects)
Definition: df1b2lp8.cpp:924
Description not yet available.
Definition: fvar.hpp:2819
void initialize(void)
Initialze all elements of dvector to zero.
Definition: dvect5.cpp:10
Description not yet available.
Definition: fvar.hpp:4197
double norm2(const d3_array &a)
Return sum of squared elements in a.
Definition: d3arr2a.cpp:167
d3_array * block_diagonal_vhessianadjoint
Definition: adrndeff.h:232
void allocate_block_diagonal_stuff(void)
Description not yet available.
Definition: df1b2lp8.cpp:821
int indexmax() const
Definition: fvar.hpp:2921
Description not yet available.
Definition: fvar.hpp:3335
void initialize(void)
Description not yet available.
Definition: ivec2.cpp:17
dvar3_array * block_diagonal_vhessian
Definition: adrndeff.h:233
Description not yet available.
Definition: adrndeff.h:406
test_smartlist list3
Definition: df1b2fun.h:757
imatrix check_sparse_matrix_structure(void)
Description not yet available.
Definition: df1b2lp8.cpp:1171
Description not yet available.
Definition: fvar.hpp:9410
Class definition of matrix with derivitive information .
Definition: fvar.hpp:2480
#define w
int indexmax() const
Definition: fvar.hpp:4025
void make_sparse_triplet(void)
Description not yet available.
Definition: df1b2lp8.cpp:25
static int set_index(void)
Definition: df1b2f15.cpp:90
void do_separable_stuff_hessian_type_information(void)
Description not yet available.
Definition: df1b2lp8.cpp:1019
banded_symmetric_dmatrix * bHessadjoint
Definition: adrndeff.h:266
Description not yet available.
Description not yet available.
Definition: admodel.h:1850
virtual void user_function()
Definition: xmodelm4.cpp:43
void initialize(int sl, int sh, int nrl, const ivector &nrh, int ncl, const ivector &nch)
void safe_allocate(int ncl, int ncu)
Description not yet available.
Definition: ivector.cpp:78
Description not yet available.
Definition: fvar.hpp:3944
long iprint
Definition: fvar.hpp:3183
Description not yet available.
Definition: fvar.hpp:3727
static unsigned int num_vars
Definition: df1b2fnl.h:64
imatrix sub(int, int)
Description not yet available.
Definition: imat.cpp:45
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
int ireturn
Definition: fvar.hpp:3197
void reset(void)
Description not yet available.
Definition: df1b2fn2.cpp:581
test_smartlist list2
Definition: df1b2fun.h:755
double square(const double value)
Return square of value; constant object.
Definition: d3arr4.cpp:16
d3_array log(const d3_array &arr3)
Author: David Fournier Copyright (c) 2008-2012 Regents of the University of California.
Definition: d3arr2a.cpp:13
int bandwidth(void) const
Definition: fvar.hpp:7991
static int in_qp_calculations
Definition: df1b2fun.h:1909