ADMB Documentation  -a65f1c97
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
xxmcmc.cpp
Go to the documentation of this file.
1 
7 #include <ctime>
8 #include <admodel.h>
9 
10 #if defined(_MSC_VER)
11  #include <conio.h>
12 #else
13  #define getch getchar
14 #endif
15 
16 #ifndef OPT_LIB
17  #include <cassert>
18  #include <climits>
19 #endif
20 
21 #ifdef ISZERO
22  #undef ISZERO
23 #endif
24 #define ISZERO(d) ((d)==0.0)
25 
26 double better_rand(long int&);
27 void set_labels_for_mcmc(void);
28 
29 void print_hist_data(const dmatrix& hist, const dmatrix& values,
30  const dvector& h, dvector& m, const dvector& s, const dvector& parsave,
31  int iseed, double size_scale);
32 
33 int minnz(const dvector& x);
34 int maxnz(const dvector& xa);
35 
36 void read_hessian_matrix_and_scale1(int nvar, const dmatrix& _SS, double s,
37  int mcmc2_flag);
38 
39 int read_hist_data(const dmatrix& hist, const dvector& h, dvector& m,
40  const dvector& s, const dvector& parsave, int& iseed,
41  const double& size_scale);
42 
43 void make_preliminary_hist(const dvector& s, const dvector& m,int nsim,
44  const dmatrix& values, dmatrix& hist, const dvector& h,int slots,
45  double total_spread,int probflag=0);
46 
47 void add_hist_values(const dvector& s, const dvector& m, const dmatrix& hist,
48  dvector& mcmc_values,double llc, const dvector& h,int nslots,
49  double total_spreadd,int probflag=0);
50 
51 void write_empirical_covariance_matrix(int ncor, const dvector& s_mean,
52  const dmatrix& s_covar, adstring& prog_name);
53 
54 void read_empirical_covariance_matrix(int nvar, const dmatrix& S,
55  const adstring& prog_name);
56 
57 void read_hessian_matrix_and_scale(int nvar, const dmatrix& S,
58  const dvector& pen_vector);
59 
60 int user_stop(void);
61 
62 extern int ctlc_flag;
63 
65  const dvector& b1, dmatrix& ch, const double& wght,double pprobe,
67  // const random_number_generator& rng);
68 
70  const dvector& b1, dmatrix& ch, const double& wght, const dvector& _y,
71  double pprobe, random_number_generator& rng);
72 
73 //void newton_raftery_bayes_estimate(double cbf,int ic, const dvector& lk,
74 // double d);
75 #if defined(USE_BAYES_FACTORS)
76 void newton_raftery_bayes_estimate_new(double cbf,int ic, const dvector& lk,
77  double d);
78 #endif
79 
81  (int feval,int iter,double fval,double gmax);
82 
83 void ad_update_function_minimizer_report(int feval,int iter,int phase,
84  double fval,double gmax,const char * cbuf);
85 void ad_update_mcmc_report(dmatrix& m,int i,int j,int ff=0);
86 void ad_update_mcmchist_report(dmatrix& mcmc_values,ivector& number_offsets,
87  dvector& mean_mcmc_values,dvector& h,int ff=0);
88 
89 //void ADSleep(unsigned int);
90 
126 void function_minimizer::mcmc_routine(int nmcmc,int iseed0, double dscale,
127  int restart_flag)
128  {
129  uostream * pofs_psave=NULL;
130  dmatrix mcmc_display_matrix;
131  //int mcmc_save_index=1;
132  //int mcmc_wrap_flag=0;
133  //int mcmc_gui_length=10000;
134  int no_sd_mcmc=0;
135 
136  int on2=-1;
137  if ( (on2=option_match(ad_comm::argc,ad_comm::argv,"-nosdmcmc"))>-1)
138  {
139  no_sd_mcmc=1;
140  }
141  if (mcmc2_flag==1)
142  {
144  //get the number of active parameters
145  //int nvar1=initial_params::nvarcalc();
146  }
147  if (stddev_params::num_stddev_params==0 && no_sd_mcmc==0)
148  {
149  cerr << " You must declare at least one object of type sdreport "
150  << endl << " to do the mcmc calculations" << endl;
151  return;
152  }
153  {
154  ivector number_offsets;
155  dvector lkvector;
156  //double current_bf=0;
157 #if defined(USE_BAYES_FACTORS)
158  double lcurrent_bf=0;
159 #endif
160  double size_scale=1.0;
161  double total_spread=200;
162  //double total_spread=2500;
163  uostream * pofs_sd = NULL;
164 
166  int nvar_x=initial_params::nvarcalc();
168  int nvar_re=initial_params::nvarcalc();
169 
170  int nvar=initial_params::nvarcalc(); // get the number of active parameters
171  //int scov_option=0;
172  dmatrix s_covar;
173  dvector s_mean;
174  int on=-1;
175  int nslots=800;
176  //int nslots=3600;
177  int initial_nsim=4800;
178  int ncor=0;
179  double bfsum=0;
180  int ibfcount=0;
181  double llbest;
182  double lbmax;
183 
184  //int ntmp=0;
185  //if ( (on=option_match(ad_comm::argc,ad_comm::argv,"-mcscov",ntmp))>-1)
186  //{
187  //scov_option=1;
188  s_covar.allocate(1,nvar,1,nvar);
189  s_mean.allocate(1,nvar);
190  s_mean.initialize();
191  s_covar.initialize();
192 
193  int ndvar=stddev_params::num_stddev_calc();
194  /*
195  int numdvar=stddev_params::num_stddev_number_calc();
196  if (adjm_ptr)
197  {
198  mcmc_display_matrix.allocate(1,numdvar,1,mcmc_gui_length);
199  number_offsets.allocate(1,numdvar);
200  number_offsets=stddev_params::copy_all_number_offsets();
201  }
202  */
203  if (mcmc2_flag==0)
204  {
206  nvar=initial_params::nvarcalc(); // get the number of active parameters
207  }
208  dvector x(1,nvar);
209  dvector scale(1,nvar);
210  dmatrix values;
211  int have_hist_flag=0;
213  dvector pen_vector(1,nvar);
214  {
215  initial_params::reset(dvar_vector(x),pen_vector);
216  //cout << pen_vector << endl << endl;
217  }
218 
222  dvector bmn(1,nvar);
223  dvector mean_mcmc_values(1,ndvar);
224  dvector s(1,ndvar);
225  dvector h(1,ndvar);
226  //dvector h;
227  dvector square_mcmc_values(1,ndvar);
228  square_mcmc_values.initialize();
229  mean_mcmc_values.initialize();
230  bmn.initialize();
231  int use_empirical_flag=0;
232  int diag_option=0;
233  if ( (on=option_match(ad_comm::argc,ad_comm::argv,"-mcdiag"))>-1)
234  {
235  diag_option=1;
236  cout << " Setting covariance matrix to diagonal with entries " << dscale
237  << endl;
238  }
239  dmatrix S(1,nvar,1,nvar);
240  dvector sscale(1,nvar);
241  if (!diag_option)
242  {
243  int nopt = 0;
244  int rescale_bounded_flag=0;
245  double rescale_bounded_power=0.5;
246  if ( (on=option_match(ad_comm::argc,ad_comm::argv,"-mcrb",nopt))>-1)
247  {
248  if (nopt)
249  {
250  int iii=atoi(ad_comm::argv[on+1]);
251  if (iii < 1 || iii > 9)
252  {
253  cerr << " -mcrb argument must be integer between 1 and 9 --"
254  " using default of 5" << endl;
255  rescale_bounded_power=0.5;
256  }
257  else
258  rescale_bounded_power=iii/10.0;
259  }
260  else
261  {
262  rescale_bounded_power=0.5;
263  }
264  rescale_bounded_flag=1;
265  }
266  if ( (on=option_match(ad_comm::argc,ad_comm::argv,"-mcec"))>-1)
267  {
268  use_empirical_flag=1;
269  }
270  if (use_empirical_flag)
271  {
273  }
274  else if (!rescale_bounded_flag)
275  {
276  int hbf;
277  if (mcmc2_flag==0)
278  {
279  read_covariance_matrix(S,nvar,hbf,sscale);
280  }
281  else
282  {
283  int tmp_nvar = 0;
284  adstring tmpstring = ad_comm::adprogram_name + ".bgs";
285  uistream uis((char*)(tmpstring));
286  if (!uis)
287  {
288  cerr << "error opening file " << tmpstring << endl;
289  ad_exit(1);
290  }
291  uis >> tmp_nvar;
292  if (!uis)
293  {
294  cerr << "error reading from file " << tmpstring << endl;
295  ad_exit(1);
296  }
297  if (tmp_nvar != nvar)
298  {
299  cerr << "size error reading from " << tmpstring << endl;
300  ad_exit(1);
301  }
302  uis >> S;
303  if (!uis)
304  {
305  cerr << "error reading from file " << tmpstring << endl;
306  ad_exit(1);
307  }
308  }
309  }
310  else
311  {
312  read_hessian_matrix_and_scale1(nvar,S,rescale_bounded_power,
313  mcmc2_flag);
314  //read_hessian_matrix_and_scale(nvar,S,pen_vector);
315  }
316 
317  { // scale covariance matrix for model space
318  dmatrix tmp(1,nvar,1,nvar);
319 
320  dvector* ptmpi = &tmp(1);
321  double* pscalei = scale.get_v() + 1;
322  dvector* pSi = &S(1);
323  for (int i=1;i<=nvar;i++)
324  {
325  *(ptmpi->get_v() + i) = *(pSi->get_v() + i) * (*pscalei * *pscalei);
326 
327  double* ptmpij = ptmpi->get_v() + 1;
328  dvector* ptmpj = &tmp(1);
329  double* pscalej = scale.get_v() + 1;
330  double* pSij = pSi->get_v() + 1;
331  for (int j=1;j<i;j++)
332  {
333  *ptmpij = *pSij * (*pscalei * *pscalej);
334  *(ptmpj->get_v() + i) = *ptmpij;
335 
336  ++ptmpij;
337  ++ptmpj;
338  ++pscalej;
339  ++pSij;
340  }
341 
342  ++ptmpi;
343  ++pscalei;
344  ++pSi;
345  }
346  S=tmp;
347  }
348  }
349  else
350  {
351  S.initialize();
352  for (int i=1;i<=nvar;i++)
353  {
354  S(i,i)=dscale;
355  }
356  }
357 
358  cout << sort(eigenvalues(S)) << endl;
359  dmatrix chd = choleski_decomp( (dscale*2.4/sqrt(double(nvar))) * S);
360  dmatrix chdinv=inv(chd);
361 
362  dmatrix symbds(1,2,1,nvar);
364  ofstream ofs_sd1((char*)(ad_comm::adprogram_name + adstring(".mc2")));
365 
366  {
367  int number_sims = 100000;
368  if (nmcmc > 0)
369  {
370  number_sims = nmcmc;
371  }
372  int iseed=0;
373  //cin >> iseed;
374  if (iseed0<=0)
375  {
376  iseed=-36519;
377  }
378  else
379  {
380  iseed=-iseed0;
381  }
382  if (iseed>0)
383  {
384  iseed=-iseed;
385  }
386  cout << "Initial seed value " << iseed << endl;
387  random_number_generator rng(iseed);
388  rng.better_rand();
389  double lprob=0.0;
390  double lpinv=0.0;
391  // get lower and upper bounds
392 
393  independent_variables y(1,nvar);
394  independent_variables parsave(1,nvar_re);
396 
397  // read in the mcmc values to date
398  int ii=1;
399  dmatrix hist;
400  if (restart_flag)
401  {
402  int tmp=0;
403  if (!no_sd_mcmc) {
404  hist.allocate(1,ndvar,-nslots,nslots);
405  tmp=read_hist_data(hist,h,mean_mcmc_values,s,parsave,iseed,
406  size_scale);
407  values.allocate(1,ndvar,-nslots,nslots);
408  for (int i=1;i<=ndvar;i++)
409  {
410  values(i).fill_seqadd(mean_mcmc_values(i)-0.5*total_spread*s(i)
411  +.5*h(i),h(i));
412  }
413  }
414  if (iseed>0)
415  {
416  iseed=-iseed;
417  }
418  /*double br=*/rng.better_rand();
419  if (tmp) have_hist_flag=1;
420  chd=size_scale*chd;
421  chdinv=chdinv/size_scale;
422  }
423  else
424  {
425  int nopt=0;
426  if ( (on=option_match(ad_comm::argc,ad_comm::argv,"-mcpin",nopt))>-1)
427  {
428  if (nopt)
429  {
430  cifstream cif((char *)ad_comm::argv[on+1]);
431  if (!cif)
432  {
433  cerr << "Error trying to open mcmc par input file "
434  << ad_comm::argv[on+1] << endl;
435  ad_exit(1);
436  }
437  cif >> parsave;
438  if (!cif)
439  {
440  cerr << "Error reading from mcmc par input file "
441  << ad_comm::argv[on+1] << endl;
442  ad_exit(1);
443  }
444  }
445  else
446  {
447  cerr << "Illegal option with -mcpin" << endl;
448  }
449  }
450  else
451  {
452  ii=1;
454  }
455  }
456 
457  ii=1;
459 
460  if (mcmc2_flag==0)
461  {
463  }
464 
466  ofstream ogs("sims");
467  ogs << nvar << " " << number_sims << endl;
469  double llc=-get_monte_carlo_value(nvar,y);
470  llbest=-get_monte_carlo_value(nvar,y);
471  lbmax=llbest;
472  // store current mcmc variable values in param_values
473  //void store_mcmc_values(const ofstream& ofs);
474  //dmatrix store_mcmc_values(1,number_sims,1,ndvar);
475 #if defined(USE_BAYES_FACTORS)
476  lkvector.allocate(1,number_sims);
477 #endif
478  dvector mcmc_values(1,ndvar);
479  dvector mcmc_number_values;
480  //if (adjm_ptr) mcmc_number_values.allocate(1,numdvar);
481  int offs=1;
482  stddev_params::copy_all_values(mcmc_values,offs);
483 
484  /*
485  if (adjm_ptr)
486  {
487  offs=1;
488  stddev_params::copy_all_number_values(mcmc_number_values,offs);
489  }
490  */
491  int change_ball=2500;
492  int nopt = 0;
493  if ( (on=option_match(ad_comm::argc,ad_comm::argv,"-mcscale",nopt))>-1)
494  {
495  if (nopt)
496  {
497  int iii=atoi(ad_comm::argv[on+1]);
498  if (iii <=0)
499  {
500  cerr << " Invalid option following command line option -mcscale -- "
501  << endl << " ignored" << endl;
502  }
503  else
504  change_ball=iii;
505  }
506  }
507  int iac=0;
508  int liac=0;
509  int isim=0;
510  int itmp=0;
511  double logr;
512  int u_option=0;
513  double ll;
514  int s_option=1;
515  int psvflag=0;
516  if ( (on=option_match(ad_comm::argc,ad_comm::argv,"-mcu"))>-1)
517  {
518  u_option=1;
519  }
520  if ( (on=option_match(ad_comm::argc,ad_comm::argv,"-mcnoscale"))>-1)
521  {
522  s_option=0;
523  }
524  //cout << llc << " " << llc << endl;
525  int iac_old=0;
526  int i_old=0;
527 
528  {
529  if (!restart_flag)
530  {
531  pofs_sd =
532  new uostream((char*)(ad_comm::adprogram_name + adstring(".mcm")));
533  }
534 
535  int mcsave_flag=0;
536  int mcrestart_flag=option_match(ad_comm::argc,ad_comm::argv,"-mcr");
537 
538  if ( (on=option_match(ad_comm::argc,ad_comm::argv,"-mcsave"))>-1)
539  {
540  int jj=(int)atof(ad_comm::argv[on+1]);
541  if (jj <=0)
542  {
543  cerr << " Invalid option following command line option -mcsave -- "
544  << endl;
545  }
546  else
547  {
548  mcsave_flag=jj;
549  if ( mcrestart_flag>-1)
550  {
551  // check that nvar is correct
552  {
553  uistream uis((char*)(ad_comm::adprogram_name + adstring(".psv")));
554  if (!uis)
555  {
556  cerr << "Error trying to open file" <<
557  ad_comm::adprogram_name + adstring(".psv") <<
558  " for mcrestart" << endl;
559  cerr << " I am starting a new file " << endl;
560  psvflag=1;
561  }
562  else
563  {
564  int nv1 = 0;
565  uis >> nv1;
566  if (nv1 !=nvar)
567  {
568  cerr << "wrong number of independent variables in"
569  << ad_comm::adprogram_name + adstring(".psv")
570  << "\n starting a new file " << endl;
571  psvflag=1;
572  }
573  }
574  }
575 
576  if (!psvflag) {
577  pofs_psave=
578  new uostream(
579  (char*)(ad_comm::adprogram_name + adstring(".psv")),ios::app);
580  } else {
581  pofs_psave= new uostream((char*)(ad_comm::adprogram_name
582  + adstring(".psv")));
583  }
584  } else {
585  pofs_psave=
586  new uostream((char*)(ad_comm::adprogram_name + adstring(".psv")));
587  }
588  if (!pofs_psave|| !(*pofs_psave))
589  {
590  cerr << "Error trying to open file" <<
592  mcsave_flag=0;
593  if (pofs_psave)
594  {
595  delete pofs_psave;
596  pofs_psave=NULL;
597  }
598  }
599  else
600  {
601  if (psvflag || (mcrestart_flag == -1) )
602  {
603  (*pofs_psave) << nvar_re;
604  }
605  }
606  }
607  }
608 
609  double pprobe=0.05;
610  int probe_flag=0;
611  nopt=0;
612  on = option_match(ad_comm::argc, ad_comm::argv, "-mcprobe", nopt);
613  if (on == -1)
614  {
615  on = option_match(ad_comm::argc,ad_comm::argv,"-mcgrope",nopt);
616  }
617  if (on > -1)
618  {
619  probe_flag=1;
620  if (nopt)
621  {
622  char* end = 0;
623  pprobe=strtod(ad_comm::argv[on+1],&end);
624  if (pprobe<=0.00001 || pprobe > .499)
625  {
626  cerr << "Invalid argument to option -mcprobe" << endl;
627  pprobe=-1.0;
628  probe_flag=0;
629  }
630  }
631  }
632 
633  int java_quit_flag=0;
634  for (int i=1;i<=number_sims;i++)
635  {
636  if (user_stop()) break;
637  if (java_quit_flag) break;
638 
639  if (!((i-1)%200))
640  {
641  double ratio = double(iac)/i;
642  iac_old=iac-iac_old;
643  i_old=i-i_old;
644  cout << llc << " " << llc << endl;
645  double tratio=double(liac)/200;
646  liac=0;
647  cout << " mcmc sim " << i << " acceptance rate "
648  << ratio << " " << tratio << endl;
649 
650  /*
651  int start_flag;
652  int der_flag,next_flag;
653  if (adjm_ptr && i>1)
654  {
655  ad_update_mcmc_report(mcmc_display_matrix,mcmc_save_index,
656  mcmc_wrap_flag);
657 
658  ad_update_mcmc_stats_report
659  (i,number_sims,100.*tratio,100.*ratio);
660 
661  if (allocated(hist)) ad_update_mcmchist_report(hist,
662  number_offsets,mean_mcmc_values,h);
663  void check_java_flags(int& start_flag,int& quit_flag,int& der_flag,
664  int& next_flag);
665  check_java_flags(start_flag,java_quit_flag,der_flag,next_flag);
666  ADSleep(50);
667  }
668  */
669 
670  if (i>50 && s_option && i<change_ball && !restart_flag)
671  {
672  if (tratio < .15)
673  {
674  chd=.2*chd;
675  size_scale*=0.2;
676  chdinv=chdinv/0.2;
677  cout << "decreasing step size " << ln_det(chd,itmp) << endl;
678  }
679  if (tratio > .6)
680  {
681  chd=2.*chd;
682  size_scale*=2.0;
683  chdinv=chdinv/2.;
684  cout << "increasing step size " << ln_det(chd,itmp) << endl;
685  }
686  else if (tratio > .5)
687  {
688  chd=1.5*chd;
689  size_scale*=1.5;
690  chdinv=chdinv/1.5;
691  cout << "increasing step size " << ln_det(chd,itmp) << endl;
692  }
693  else if (tratio > .4)
694  {
695  chd=1.2*chd;
696  size_scale*=1.2;
697  chdinv=chdinv/1.2;
698  cout << "increasing step size " << ln_det(chd,itmp) << endl;
699  }
700  }
701  }
702  ii=1;
704  {
707  if (mcmc2_flag==0)
708  {
710  }
711  }
712  else
713  {
715  }
717 
718  // option of generating uniform or normal random variables
719  dvector bmn1(1,nvar);
720  if (!u_option)
721  {
722  if (!probe_flag)
723  bmn1=bounded_multivariate_normal(nvar,symbds(1),symbds(2),
724  chd,lprob,rng);
725  else
727  nvar,symbds(1),symbds(2),chd,lprob,pprobe,rng);
728 
731  // get the simulation bounds for the inverse transition
733  if (!probe_flag)
734  bounded_multivariate_normal_mcmc(nvar,symbds(1),symbds(2),chd,
735  lpinv,-1*(chdinv*bmn1),rng);
736  else
738  symbds(2), chd,lpinv,-1*(chdinv*bmn1),pprobe,rng);
739 
740  ll=-get_monte_carlo_value(nvar,y);
742  {
744  }
745  //cout << ll << " " << llc << endl;
746  double ldiff=lprob-lpinv;
747  logr= ll - ldiff - llc;
748  }
749  else
750  {
751  bmn1=bounded_multivariate_uniform(nvar,symbds(1),symbds(2),
752  chd, lprob,rng);
755  // get the simulation bounds for the inverse transition
757  bounded_multivariate_uniform_mcmc(nvar,symbds(1),symbds(2),chd,
758  lpinv,-1*(chdinv*bmn1),rng);
759  ll=-get_monte_carlo_value(nvar,y);
760  double ldiff=lprob-lpinv;
761  logr= ll - ldiff - llc;
762  }
763  //cout << logr << endl;
764  // decide whether to accept the new point
765  isim++;
766  double br=rng.better_rand();
767  if (logr>=0 || br< exp(logr) )
768  {
769  ii=1;
770  // save current parameter values
772  ii=1;
773  // save current mcmc values
774  stddev_params::copy_all_values(mcmc_values,ii);
776  {
777  if (mcmc2_flag==0)
778  {
780  }
781  }
782  // update prob density for current point
783  llc =ll;
784  liac++;
785  iac++;
786  }
787  int mmin=mcmc_values.indexmin();
788  int mmax=mcmc_values.indexmax();
789  double lbf=llbest-llc;
790  if (lbf>lbmax) lbmax=lbf;
791  //of_bf << lbf << endl;
792  bfsum+=exp(lbf);
793  ibfcount+=1;
794 #if defined(USE_BAYES_FACTORS)
795  lkvector(ibfcount)=llc;
796  //current_bf=1.0/(bfsum/double(ibfcount))*exp(llbest);
797  lcurrent_bf=-1.*log(bfsum/double(ibfcount))+llbest;
798 #endif
799  if (mcsave_flag && (!((i-1)%mcsave_flag)))
800  {
801  (*pofs_psave) << parsave;
802  }
803  /*
804  if (adjm_ptr)
805  {
806  void save_mcmc_for_gui1(const dvector& mcmc_values,
807  dmatrix &mdm,int& ids,int& iwrap,ivector& no);
808  save_mcmc_for_gui1(mcmc_values,mcmc_display_matrix,
809  mcmc_save_index,mcmc_wrap_flag,number_offsets);
810  }
811  */
812  if (!no_sd_mcmc)
813  {
814  if (!have_hist_flag)
815  {
816  for (ii=mmin;ii<=mmax;ii++)
817  {
818  (*pofs_sd) << float(mcmc_values(ii));
819  }
820  (*pofs_sd) << (float)(llc);
821  mean_mcmc_values+=mcmc_values;
822  square_mcmc_values+=square(mcmc_values);
823  }
824  else
825  {
826  add_hist_values(s,mean_mcmc_values,hist,mcmc_values,llc,
827  h,nslots,total_spread);
828  }
829  }
830  //if (scov_option)
831  {
832  ncor++;
833  s_mean+=parsave;
834  s_covar+=outer_prod(parsave,parsave);
835  }
836  if (!no_sd_mcmc && iac>5 && isim>initial_nsim)
837  {
838  if (!have_hist_flag)
839  {
840  have_hist_flag=1;
841  delete pofs_sd;
842  pofs_sd=NULL;
843  mean_mcmc_values/=double(isim);
844  square_mcmc_values/=double(isim);
845 #if defined(USE_DDOUBLE)
846  s=2.*sqrt(double(1.e-20)+square_mcmc_values
847  -square(mean_mcmc_values));
848 #else
849  s=2.*sqrt(1.e-20+square_mcmc_values-square(mean_mcmc_values));
850 #endif
851  make_preliminary_hist(s,mean_mcmc_values,isim,values,hist,h,
852  nslots,total_spread);
853  }
854  }
855  }
856  }
857  if (!no_sd_mcmc && !have_hist_flag)
858  {
859  delete pofs_sd;
860  pofs_sd=NULL;
861  mean_mcmc_values/=double(isim);
862  square_mcmc_values/=double(isim);
863 #if defined(USE_DDOUBLE)
864  s=2.*sqrt(double(1.e-20)+square_mcmc_values-square(mean_mcmc_values));
865 #else
866  s=2.*sqrt(1.e-20+square_mcmc_values-square(mean_mcmc_values));
867 #endif
868  make_preliminary_hist(s,mean_mcmc_values,isim,values,hist,h,nslots,
869  total_spread);
870  }
871  if (!no_sd_mcmc)
872  if (iac>5)
873  print_hist_data(hist,values,h,mean_mcmc_values,s,parsave,iseed,
874  size_scale);
875 #ifndef OPT_LIB
876  assert(isim != 0);
877 #endif
878  cout << iac/double(isim) << endl;
880  /*
881  if (adjm_ptr)
882  {
883  ad_update_mcmc_report(mcmc_display_matrix,mcmc_save_index,
884  mcmc_wrap_flag,1);
885 
886  if (allocated(hist)) ad_update_mcmchist_report(hist,
887  number_offsets,mean_mcmc_values,h,1);
888  }
889  */
890  }
891 
892  write_empirical_covariance_matrix(ncor,s_mean,s_covar,
894  //newton_raftery_bayes_estimate(current_bf,ibfcount,exp(lkvector),.01);
895 #if defined(USE_BAYES_FACTORS)
896  cout << "log current bayes factor " << lcurrent_bf << endl;
897  newton_raftery_bayes_estimate_new(lcurrent_bf,ibfcount,lkvector,.01);
898 #endif
899  if (pofs_psave)
900  {
901  delete pofs_psave;
902  pofs_psave=NULL;
903  }
904  }
905 }
906 
917 void write_empirical_covariance_matrix(int ncor, const dvector& s_mean,
918  const dmatrix& s_covar,adstring& prog_name)
919 {
920  uostream ofs((char*)(ad_comm::adprogram_name + adstring(".ecm")));
921  dvector tmp=s_mean/ncor;
922  int nvar=s_mean.indexmax();
923  //ofs << ncor << " " << nvar << endl;
924  dmatrix sigma=s_covar/ncor -outer_prod(tmp,tmp);
925  cout << "In write empirical covariance matrix" << endl;
926  cout << sort(eigenvalues(sigma)) << endl;
927  dvector std(1,nvar);
928  ofs << sigma;
929  /*
930  int i;
931  for (i=1;i<=nvar;i++)
932  {
933  std(i)=sigma(i,i);
934  for (int j=1;j<=i;j++)
935  {
936  sigma(i,j)/=sqrt(std(i)*std(j));
937  sigma(j,i)=sigma(i,j);
938  }
939  }
940  for (i=1;i<=nvar;i++)
941  {
942  ofs << setw(10) << setscientific() << std(i);
943  for (int j=1;j<=i;j++)
944  {
945  ofs << " " << setfixed() << setw(7) << setprecision(4)
946  << sigma(i,j);
947  }
948  ofs << endl;
949  }
950  */
951 }
952 
962  const adstring& prog_name)
963 {
964  adstring infile_name=ad_comm::adprogram_name + adstring(".ecm");
965  uistream ifs((char*)(infile_name));
966  if (!ifs)
967  {
968  cerr << "Error opening file " << infile_name << endl;
969  }
970  ifs >> S;
971  /*
972  int ncor=0;
973  int _nvar=0;
974  ifs >> ncor >> _nvar;
975  if (nvar != _nvar)
976  {
977  cerr << "wromng number of indepdendent variables in routine"
978  " read_empirical_covariance_matrix" << endl;
979  }
980  dvector std(1,nvar);
981  int i;
982  for (i=1;i<=nvar;i++)
983  {
984  ifs >> std(i);
985  for (int j=1;j<=i;j++)
986  {
987  ifs >> S(i,j);
988  S(j,i)=S(i,j);
989  }
990  }
991  if (!ifs)
992  {
993  cerr << "Error reading from file " << infile_name << endl;
994  }
995  for (i=1;i<=nvar;i++)
996  {
997  for (int j=1;j<=i;j++)
998  {
999  S(i,j)*=sqrt(std(i)*std(j));
1000  S(j,i)=S(i,j);
1001  }
1002  }
1003  */
1004 }
1005 
1006 void print_hist_data(const dmatrix& hist, const dmatrix& values,
1007  const dvector& h, dvector& m, const dvector& s, const dvector& parsave,
1008  int iseed, double size_scale)
1009 {
1010  ofstream ofs((char*)(ad_comm::adprogram_name + adstring(".hst")));
1011  int nsdvars=stddev_params::num_stddev_calc();
1012  adstring_array param_labels(1,nsdvars);
1013  ivector param_size(1,nsdvars);
1014  int ii=1;
1015  //int max_name_length=0;
1016  int i;
1017  for (i=0;i< stddev_params::num_stddev_params;i++)
1018  {
1019  param_labels[ii]=
1021  param_size[ii]=
1023 /*
1024  if (max_name_length<param_labels[ii].size())
1025  {
1026  max_name_length=param_labels[ii].size();
1027  }
1028 */
1029  ii++;
1030  }
1031  //int end_stdlabels=ii-1;
1032 
1033  int lc=1;
1034  int ic=1;
1035  ivector mmin(1,nsdvars);
1036  ivector mmax(1,nsdvars);
1037 
1038  for (i=1;i<=nsdvars;i++)
1039  {
1040  mmin(i)=minnz(hist(i));
1041  mmax(i)=maxnz(hist(i));
1042  }
1043 #ifdef OPT_LIB
1044  int nsim = (int)sum(hist(1));
1045 #else
1046  double _nsim = sum(hist(1));
1047  assert(_nsim <= (double)INT_MAX);
1048  int nsim = (int)_nsim;
1049 #endif
1050  ofs << "# samples sizes" << endl;
1051  ofs << nsim << endl;
1052  ofs << "# step size scaling factor" << endl;
1053  ofs << size_scale << endl;
1054  ofs << "# step sizes" << endl;
1055  ofs << h << endl;
1056  ofs << "# means" << endl;
1057  ofs << m << endl;
1058  ofs << "# standard devs" << endl;
1059  ofs << s << endl;
1060  ofs << "# lower bounds" << endl;
1061  ofs << mmin << endl;
1062  ofs << "# upper bounds" << endl;
1063  ofs << mmax<< endl;
1064  ofs << "#number of parameters" << endl;
1065  ofs << parsave.indexmax() << endl;
1066  ofs << "#current parameter values for mcmc restart" << endl;
1067  ofs << parsave << endl;
1068  ofs << "#random number seed" << endl;
1069  ofs << iseed << endl;
1070  for (i=1;i<=nsdvars;i++)
1071  {
1072  ofs << "#" << param_labels[lc];
1073  if (param_size[lc]>1)
1074  {
1075  ofs << "[" << ic << "]";
1076  }
1077  ofs << endl;
1078 
1079  if (++ic> param_size[lc])
1080  {
1081  lc++;
1082  ic=1;
1083  }
1084  for (ii=mmin(i);ii<=mmax(i);ii++)
1085  {
1086  ofs << values(i,ii) << " " << hist(i,ii)/(nsim*h(i)) << endl;
1087  }
1088  if (i<nsdvars) ofs << endl;
1089  }
1090 }
1091 
1092 int read_hist_data(const dmatrix& _hist, const dvector& h, dvector& m,
1093  const dvector& s, const dvector& parsave, int& iseed,
1094  const double& size_scale)
1095 {
1096  dmatrix& hist= (dmatrix&) _hist;
1097  cifstream cif((char*)(ad_comm::adprogram_name + adstring(".hst")));
1098  int nsdvars=stddev_params::num_stddev_calc();
1099  int nsim=0;
1100  int ii=1;
1101  int i;
1102  ivector mmin(1,nsdvars);
1103  ivector mmax(1,nsdvars);
1104  //ofs << # samples sizes << endl;
1105  cif >> nsim;
1106  cif >> size_scale;
1107  //ofs << # step sizes << endl;
1108  cif >> h;
1109  //ofs << # means << endl;
1110  cif >> m;
1111  //ofs << # standard devs << endl;
1112  cif >> s;
1113  //ofs << # lower bounds << endl;
1114  cif >> mmin;
1115  //ofs << # upper bounds << endl;
1116  cif >> mmax;
1117  int num_vars=0;
1118  cif >> num_vars;
1119  int flag=1;
1120  if (num_vars!=parsave.indexmax())
1121  {
1122  cerr << "wrong number of independent variables in file"
1123  << ad_comm::adprogram_name + adstring(".hst") << endl;
1124  flag=0;
1125  }
1126  if (flag)
1127  {
1128  cif >> parsave;
1129  cif >> iseed;
1130  double tmp=0.0;
1131  hist.initialize();
1132  for (i=1;i<=nsdvars;i++)
1133  {
1134  for (ii=mmin(i);ii<=mmax(i);ii++)
1135  {
1136  cif >> tmp >> hist(i,ii);
1137  }
1138  hist(i)*=nsim*h(i);
1139  }
1140  }
1141  return flag;
1142 }
1143 
1144 int minnz(const dvector& x)
1145 {
1146  int mmin=x.indexmin();
1147  int mmax=x.indexmax();
1148  int m=mmin;
1149  for (int ii=mmin;ii<=mmax;ii++)
1150  {
1151  if (!ISZERO(x(ii)))
1152  {
1153  m=ii;
1154  if (m>mmin) m--;
1155  break;
1156  }
1157  }
1158  return m;
1159 }
1160 
1161 int maxnz(const dvector& x)
1162 {
1163  int mmin=x.indexmin();
1164  int mmax=x.indexmax();
1165  int m=mmax;
1166  for (int ii=mmax;ii>=mmin;ii--)
1167  {
1168  if (!ISZERO(x(ii)))
1169  {
1170  m=ii;
1171  if (m<mmax) m++;
1172  break;
1173  }
1174  }
1175  return m;
1176 }
1177 
1178 void add_hist_values(const dvector& s, const dvector& m, const dmatrix& _hist,
1179  dvector& mcmc_values, double llc, const dvector& h, int nslots,
1180  double total_spread,int probflag)
1181 {
1182  dmatrix& hist= (dmatrix&) _hist;
1183  int nsdvars=stddev_params::num_stddev_calc();
1184  for (int ii=1;ii<=nsdvars;ii++)
1185  {
1186  int x;
1187  double xx=(mcmc_values(ii)-m(ii))/h(ii);
1188  if (xx>0.0)
1189  x=int (xx+0.5);
1190  else
1191  x=int(xx-0.5);
1192 
1193  if (x<-nslots)
1194  {
1195  x=-nslots;
1196  }
1197  if (x>nslots)
1198  {
1199  x=nslots;
1200  }
1201  if (!probflag)
1202  {
1203  hist(ii,x)+=1;
1204  }
1205  else
1206  {
1207  hist(ii,x)+=exp(llc);
1208  }
1209  }
1210 }
1211 
1212 /*
1213 void add_guihist_values(const dvector& s, const dvector& m,
1214  const dmatrix& _hist,dvector& mcmcnumber_values,double llc,
1215  const dvector& h,int nslots,double total_spread)
1216 {
1217  dmatrix& hist= (dmatrix&) _hist;
1218  int nsdvars=stddev_params::num_stddev_number_calc();
1219  for (int ii=1;ii<=nsdvars;ii++)
1220  {
1221  int x=int((mcmcnumber_values(ii)-m(ii))/h(ii));
1222  //cout << "xxx = " << xxx << endl;
1223  char ch;
1224  //cin >> ch;
1225 
1226  if (x<1)
1227  {
1228  x=-nslots;
1229  }
1230  if (x>nslots)
1231  {
1232  x=nslots;
1233  }
1234  {
1235  hist(ii,x)+=1;
1236  }
1237  }
1238 }
1239 */
1240 
1241 void make_preliminary_hist(const dvector& s, const dvector& m,int nsim,
1242  const dmatrix& _values, dmatrix& hist, const dvector& _h, int nslots,
1243  double total_spread, int probflag)
1244 {
1246  dvector& h = (dvector&) _h;
1247  int nsdvars=stddev_params::num_stddev_calc();
1248  dvector mcmc_values(1,nsdvars);
1249  values.allocate(1,nsdvars,-nslots,nslots);
1250  h=total_spread/(2*nslots+1)*s;
1251  hist.allocate(1,nsdvars,-nslots,nslots);
1252  hist.initialize();
1253  uistream ifs((char*)(ad_comm::adprogram_name + adstring(".mcm")));
1254  int i;
1255  double llc;
1256  for (i=1;i<=nsdvars;i++)
1257  {
1258  values(i).fill_seqadd(m(i)-.5*total_spread*s(i)+.5*h(i),h(i));
1259  }
1260 
1261  if (!ifs)
1262  {
1263  cerr << "Error trying to open file "
1264  << ad_comm::adprogram_name + adstring(".mcm");
1265  return;
1266  }
1267  if (!ifs)
1268  {
1269  cerr << "Error trying to read number of simulations from file "
1270  << ad_comm::adprogram_name + adstring(".mcm");
1271  return;
1272  }
1273  for (i=1;i<=nsim;i++)
1274  {
1275  float ftmp = 0.0;
1276  int ii;
1277  int mmin=mcmc_values.indexmin();
1278  int mmax=mcmc_values.indexmax();
1279  for (ii=mmin;ii<=mmax;ii++)
1280  {
1281  ifs >> ftmp;
1282  mcmc_values(ii)=double(ftmp);
1283  }
1284  ifs >> ftmp;
1285  llc=double(ftmp);
1286  for (ii=1;ii<=nsdvars;ii++)
1287  {
1288  int x;
1289  double xx=(mcmc_values(ii)-m(ii))/h(ii);
1290  if (xx>0.0)
1291  x=int (xx+0.5);
1292  else
1293  x=int(xx-0.5);
1294  if (x<-nslots)
1295  {
1296  x=-nslots;
1297  }
1298  if (x>nslots)
1299  {
1300  x=nslots;
1301  }
1302  if (!probflag)
1303  {
1304  hist(ii,x)+=1;
1305  }
1306  else
1307  {
1308  hist(ii,x)+=exp(llc);
1309  }
1310  }
1311  if (!ifs)
1312  {
1313  cerr << "Error trying to read data from file "
1314  << ad_comm::adprogram_name + adstring(".mcm");
1315  return;
1316  }
1317  }
1318 }
1319 
1320 void read_covariance_matrix(const dmatrix& S,int nvar,int& oldHbf,
1321  dvector & sscale)
1322 {
1323  adstring tmpstring="admodel.cov";
1324  if (ad_comm::wd_flag)
1325  tmpstring = ad_comm::adprogram_name + ".cov";
1326  uistream cif((char*)tmpstring);
1327  if (!cif)
1328  {
1329  cerr << "Error trying to open file " << tmpstring
1330  << " for reading" << endl;
1331  }
1332  int tmp_nvar = 0;
1333  cif >> tmp_nvar;
1334  if (nvar !=tmp_nvar)
1335  {
1336  cerr << "Incorrect number of independent variables in file"
1337  " model.cov" << endl;
1338  ad_exit(1);
1339  }
1340  cif >> S;
1341  if (!cif)
1342  {
1343  cerr << "error reading covariance matrix from " << tmpstring
1344  << endl;
1345  ad_exit(1);
1346  }
1347  cif >> oldHbf;
1348  if (!cif)
1349  {
1350  cerr << "error reading old_hybrid_bounded_flag from " << tmpstring
1351  << endl;
1352  ad_exit(1);
1353  }
1354  cif >> sscale;
1355  if (!cif)
1356  {
1357  cerr << "error reading sscale from " << tmpstring
1358  << endl;
1359  ad_exit(1);
1360  }
1361 }
1362 void read_hessian_matrix_and_scale(int nvar, const dmatrix& _SS,
1363  const dvector& pen_vector)
1364 {
1365  dmatrix& S= (dmatrix&) _SS;
1366  adstring tmpstring="admodel.hes";
1367  if (ad_comm::wd_flag)
1368  tmpstring = ad_comm::adprogram_name + ".hes";
1369  uistream cif((char*)tmpstring);
1370 
1371  if (!cif)
1372  {
1373  cerr << "Error trying to open file " << tmpstring
1374  << " for reading" << endl;
1375  }
1376  int tmp_nvar = 0;
1377  cif >> tmp_nvar;
1378  if (nvar !=tmp_nvar)
1379  {
1380  cerr << "Incorrect number of independent variables in file"
1381  " admodel.hes" << endl;
1382  ad_exit(1);
1383  }
1384  cif >> S;
1385  if (!cif)
1386  {
1387  cerr << "error reading covariance matrix from model.cov" << endl;
1388  ad_exit(1);
1389  }
1390  cifstream cifs((char*)(ad_comm::adprogram_name + adstring(".bvs")));
1391  dvector tmp(1,nvar);
1392  cifs >> tmp;
1393  dvector wts=pen_vector/.16;
1394  dvector diag_save(1,nvar);
1395  //int neg_flag;
1396  //double base=5.0;
1397  double dmin=min(eigenvalues(S));
1398  cout << "Smallest eigenvalue = " << dmin << endl;
1399  for (int i=1;i<=nvar;i++)
1400  {
1401  if (tmp(i)>0)
1402  {
1403 #if defined(USE_DDOUBLE)
1404  S(i,i)/=pow(double(10.0),tmp(i));
1405 #else
1406  S(i,i)/=pow(10.0,tmp(i));
1407 #endif
1408  }
1409  }
1410  dmin=min(eigenvalues(S));
1411  if (dmin<0)
1412  {
1413  cout << "Smallest eigenvalue = " << dmin << endl;
1414  ad_exit(1);
1415  }
1416  /*
1417  do
1418  {
1419  cout << wts << endl << endl;
1420  for (int i=1;i<=nvar;i++)
1421  {
1422  diag_save(i)=S(i,i);
1423  if (wts(i)>0)
1424  {
1425  S(i,i)/=pow(base,wts(i));
1426  cout << " wts(" << i << ") = " << wts(i) << endl;
1427  }
1428  }
1429  dmin=min(eigenvalues(S));
1430  if (dmin<0)
1431  {
1432  cout << "Smallest eigenvalue = " << dmin << endl;
1433  neg_flag=1;
1434  base=sqrt(base);
1435  cout << "base = " << base << endl;
1436  for (int i=1;i<=nvar;i++)
1437  {
1438  S(i,i)=diag_save(i);
1439  }
1440  dmin=min(eigenvalues(S));
1441  cout << "XX Smallest eigenvalue = " << dmin << endl;
1442  }
1443  else
1444  {
1445  neg_flag=0;
1446  }
1447  }
1448  while (neg_flag);
1449  */
1450  S=inv(S);
1451 }
1452 
1453 void read_hessian_matrix_and_scale1(int nvar, const dmatrix& _SS,
1454  double rbp,int mcmc2_flag)
1455 {
1456  dmatrix& S= (dmatrix&) _SS;
1457  adstring tmpstring="admodel.hes";
1458  if (mcmc2_flag)
1459  {
1460  tmpstring = ad_comm::adprogram_name + ".bgs";
1461  }
1462  else
1463  {
1464  if (ad_comm::wd_flag)
1465  tmpstring = ad_comm::adprogram_name + ".hes";
1466  }
1467  uistream cif((char*)tmpstring);
1468 
1469  if (!cif)
1470  {
1471  cerr << "Error trying to open file " << tmpstring
1472  << " for reading" << endl;
1473  }
1474  int tmp_nvar = 0;
1475  cif >> tmp_nvar;
1476  if (nvar !=tmp_nvar)
1477  {
1478  cerr << "Incorrect number of independent variables in file"
1479  " admodel.hes" << endl;
1480  ad_exit(1);
1481  }
1482  cif >> S;
1483  if (!cif)
1484  {
1485  cerr << "error reading covariance matrix from model.cov" << endl;
1486  ad_exit(1);
1487  }
1488  dmatrix Save=1*S;
1489  // for mcmc2 option Hessian is already inverted.
1490  if (mcmc2_flag==0)
1491  {
1492  S=inv(S);
1493  }
1494  int mmin=S.indexmin();
1495  int mmax=S.indexmax();
1496  dvector diag(mmin,mmax);
1497  int i,j;
1498  for (i=mmin;i<=mmax;i++)
1499  {
1500  diag(i)=sqrt(S(i,i));
1501  }
1502  for (i=mmin;i<=mmax;i++)
1503  for (j=mmin;j<=mmax;j++)
1504  S(i,j)/=diag(i)*diag(j);
1505 
1506  dmatrix ch=choleski_decomp(S);
1507  ofstream ofs("corrtest");
1508  ofs << "corr matrix" << endl;
1509  ofs << S << endl;
1510  ofs << "choleski decomp of corr matrix" << endl;
1511  ofs << ch << endl;
1512  dmatrix tmp(mmin,mmax,mmin,mmax);
1513 
1514  for (i=mmin;i<=mmax;i++)
1515  tmp(i)=ch(i)/norm(ch(i));
1516  ofs << "tmp" << endl;
1517  ofs << tmp << endl;
1518 
1519  for (i=mmin;i<=mmax;i++)
1520  tmp(i)/=tmp(i,i);
1521 
1522 
1523  if (rbp<=0.0 || rbp >= 1.0)
1524  rbp=0.5;
1525  for (i=mmin;i<=mmax;i++)
1526  {
1527  for (j=mmin;j<=mmax;j++)
1528  {
1529  if (tmp(i,j)>=1)
1530  tmp(i,j)=pow(tmp(i,j),rbp);
1531  else if (tmp(i,j)<-1)
1532  tmp(i,j)=-pow(-tmp(i,j),rbp);
1533  }
1534  }
1535 
1536  for (i=mmin;i<=mmax;i++)
1537  tmp(i)/=norm(tmp(i));
1538 
1539  dmatrix T=tmp*trans(tmp);
1540 
1541  ofs << "T-S" << endl;
1542  ofs << T-S << endl;
1543 
1544  S=T;
1545  ofs << "modified corr matrix" << endl;
1546  ofs << S << endl;
1547  for (i=mmin;i<=mmax;i++)
1548  for (j=mmin;j<=mmax;j++)
1549  S(i,j)*=diag(i)*diag(j);
1550 
1551  ofs << "modified S" << endl;
1552  ofs << S << endl;
1553 
1554  ofs << "S* modified S" << endl;
1555  ofs << S*Save << endl;
1556 }
1557 
1558 int user_stop(void)
1559 {
1560  int quit_flag=0;
1561 #if defined(_MSC_VER)
1562  if (kbhit())
1563 #else
1564  if(ctlc_flag)
1565 #endif
1566  {
1567 #if defined(__DJGPP__)
1568  int c = toupper(getxkey());
1569 #else
1570  int c = toupper(getch());
1571 #endif
1572  if (c == 'Q')
1573  {
1574  quit_flag=1;
1575  }
1576  }
1577  return quit_flag;
1578 }
1579 
1580 /*
1581 void newton_raftery_bayes_estimate(double cbf, int ic, const dvector& lk,
1582  double d)
1583 {
1584  double d1=1.0-d;
1585  double cbold=cbf;
1586  do
1587  {
1588  cout << "initial bayes factor" << cbf << endl;
1589  double sum1=0;
1590  double sum2=0;
1591 
1592  for (int i=1;i<=ic;i++)
1593  {
1594  sum1+=1./(d1/cbf+d/lk(i));
1595 
1596  sum2+=1./(d1/cbf*lk(i)+d);
1597  }
1598  sum1+=d*ic*cbf;
1599  sum2+=d*ic;
1600 
1601  cbf=sum1/sum2;
1602  double diff=log(cbf)-log(cbold);
1603  if (fabs(diff)<1.e-5) break;
1604  cbold=cbf;
1605  }
1606  while(1);
1607 }
1608 */
1609 
1610 #if defined(USE_BAYES_FACTORS)
1611 void newton_raftery_bayes_estimate_new(double lcbf, int ic, const dvector& lk,
1612  double d)
1613 {
1614  double d1=1.0-d;
1615  double lcbold=lcbf;
1616  do
1617  {
1618  cout << "initial log bayes factor" << lcbf << endl;
1619  double sum1=0;
1620  double sum2=0;
1621 
1622  for (int i=1;i<=ic;i++)
1623  {
1624  double dtmp=exp(lcbf-lk(i));
1625  sum1+=1./(d1+d*dtmp);
1626  sum2+=1./(d1/dtmp+d);
1627  }
1628  sum1+=d*ic;
1629  sum2+=d*ic;
1630  lcbf=lcbf+log(sum1)-log(sum2);
1631  double diff=lcbf-lcbold;
1632  if (fabs(diff)<1.e-5) break;
1633  lcbold=lcbf;
1634  }
1635  while(1);
1636 }
1637 #endif
1638 
1639 /*
1640 void save_mcmc_for_gui(const dvector& mcmc_number_values,
1641  dmatrix &mdm,int& ids)
1642 {
1643  int imax=mdm.colmax();
1644  if (ids>imax) ids=1;
1645  int rmax=mcmc_number_values.indexmax();
1646  for (int i=1;i<=rmax;i++)
1647  mdm(i,ids)=mcmc_number_values(i);
1648  ids++;
1649 }
1650 
1651 void save_mcmc_for_gui1(const dvector& mcmc_values,
1652  dmatrix &mdm,int& ids,int& iwrap,ivector& no)
1653 {
1654  int rmax=no.indexmax();
1655  int imax=mdm.colmax();
1656  if (ids>imax)
1657  {
1658  ids=1;
1659  iwrap=1;
1660  }
1661  for (int i=1;i<=rmax;i++)
1662  mdm(i,ids)=mcmc_values(no(i));
1663  ids++;
1664 }
1665 */
1666 
1667 dvector read_old_scale(int & old_nvar)
1668 {
1669  adstring tmpstring="admodel.cov";
1670  if (ad_comm::wd_flag)
1671  tmpstring = ad_comm::adprogram_name + ".cov";
1672  uistream cif((char*)tmpstring);
1673  if (!cif)
1674  {
1675  cerr << "Error trying to open file " << tmpstring
1676  << " for reading" << endl;
1677  }
1678  cif >> old_nvar;
1679  dmatrix S(1,old_nvar,1,old_nvar);
1680  cif >> S;
1681  if (!cif)
1682  {
1683  cerr << "error reading covariance matrix from " << tmpstring
1684  << endl;
1685  ad_exit(1);
1686  }
1687  int oldHbf;
1688  cif >> oldHbf;
1689  if (!cif)
1690  {
1691  cerr << "error reading old_hybrid_bounded_flag from " << tmpstring
1692  << endl;
1693  ad_exit(1);
1694  }
1695  dvector sscale(1,old_nvar);
1696  cif >> sscale;
1697  if (!cif)
1698  {
1699  cerr << "error reading sscale from " << tmpstring
1700  << endl;
1701  ad_exit(1);
1702  }
1703  return sscale;
1704 }
void ad_update_mcmchist_report(dmatrix &mcmc_values, ivector &number_offsets, dvector &mean_mcmc_values, dvector &h, int ff=0)
static void restore_all_values(const dvector &x, const int &ii)
Definition: model3.cpp:105
dvector bounded_multivariate_uniform(int nvar, const dvector &a1, const dvector &b1, dmatrix &ch, const double &lprob, random_number_generator &rng)
Definition: monte.cpp:216
void make_preliminary_hist(const dvector &s, const dvector &m, int nsim, const dmatrix &values, dmatrix &hist, const dvector &h, int slots, double total_spread, int probflag=0)
Definition: xxmcmc.cpp:1241
void read_hessian_matrix_and_scale(int nvar, const dmatrix &S, const dvector &pen_vector)
Definition: xxmcmc.cpp:1362
static void set_NO_DERIVATIVES(void)
Disable accumulation of derivative information.
Definition: gradstrc.cpp:641
static void set_active_random_effects(void)
Definition: model.cpp:267
void allocate(void)
Author: David Fournier Copyright (c) 2008-2012 Regents of the University of California.
Definition: dmat0.cpp:8
dvector read_old_scale(int &old_nvar)
Definition: xxmcmc.cpp:1667
dvector new_probing_bounded_multivariate_normal(int nvar, const dvector &a1, const dvector &b1, dmatrix &ch, const double &wght, double pprobe, random_number_generator &rng)
Definition: prmonte.cpp:14
void ad_update_mcmc_stats_report(int feval, int iter, double fval, double gmax)
int read_hist_data(const dmatrix &hist, const dvector &h, dvector &m, const dvector &s, const dvector &parsave, long int &iseed, const double &size_scale)
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
#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
static char ** argv
Definition: fvar.hpp:8866
void allocate(int ncl, int ncu)
Allocate memory for a dvector.
Definition: dvector.cpp:409
static void add_random_vector(const dvector &x)
Author: David Fournier Copyright (c) 2008-2012 Regents of the University of California.
Definition: mod_mc1.cpp:7
double sum(const d3_array &darray)
Author: David Fournier Copyright (c) 2008-2012 Regents of the University of California.
Definition: d3arr.cpp:21
void print_hist_data(const dmatrix &hist, const dmatrix &values, const dvector &h, dvector &m, const dvector &s, const dvector &parsave, long int iseed, double size_scale)
df1_two_variable fabs(const df1_two_variable &x)
Definition: df12fun.cpp:891
exitptr ad_exit
Definition: gradstrc.cpp:53
void set_labels_for_mcmc(void)
#define dmin(a, b)
Definition: cbivnorm.cpp:190
dvector bounded_multivariate_normal(int nvar, const dvector &a1, const dvector &b1, dmatrix &ch, const double &_wght, const random_number_generator &rng)
static dvariable reset(const dvar_vector &x)
Definition: model.cpp:345
void ad_update_function_minimizer_report(int feval, int iter, int phase, double fval, double gmax, const char *cbuf)
ADMB variable vector.
Definition: fvar.hpp:2172
static void set_all_simulation_bounds(const dmatrix &symbds)
Definition: montebds.cpp:25
static void restore_start_phase(void)
Definition: model.cpp:275
static adstring adprogram_name
Definition: fvar.hpp:8860
static int num_stddev_calc(void)
Definition: model2.cpp:51
double norm(const d3_array &a)
Return computed norm value of a.
Definition: d3arr2a.cpp:190
double better_rand()
Random number generator.
Definition: rngen.cpp:134
int atoi(adstring &s)
Returns a integer converted from input s.
Definition: atoi.cpp:20
df1_one_matrix choleski_decomp(const df1_one_matrix &MM)
Definition: df11fun.cpp:606
static int nvarcalc()
Definition: model.cpp:152
virtual unsigned int size_count() const =0
Description not yet available.
Definition: fvar.hpp:7951
virtual const char * label()=0
dmatrix sort(const dmatrix &m, int column, int NSTACK)
Description not yet available.
Definition: dmsort.cpp:17
void ad_update_mcmc_report(dmatrix &m, int i, int j, int ff=0)
void write_empirical_covariance_matrix(int ncor, const dvector &s_mean, const dmatrix &s_covar, adstring &prog_name)
Writes the covariance matrix out to a file, which is prog_name prepended to the extension &#39;...
Definition: xxmcmc.cpp:917
prnstream & endl(prnstream &)
Description not yet available.
Definition: fvar.hpp:1937
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 int flag
Definition: cbivnorm.cpp:66
#define min(a, b)
Definition: cbivnorm.cpp:188
Description not yet available.
Definition: fvar.hpp:3398
static int argc
Definition: fvar.hpp:8863
int indexmax() const
Get maximum valid index.
Definition: dvector.h:204
Description not yet available.
static void xinit(const dvector &x)
Definition: model.cpp:226
d3_array exp(const d3_array &arr3)
Returns d3_array results with computed exp from elements in arr3.
Definition: d3arr2a.cpp:28
#define getch
$Id$
Definition: xxmcmc.cpp:13
static void set_inactive_random_effects(void)
Definition: model.cpp:288
dmatrix outer_prod(const dvector &v1, const dvector &v2)
Description not yet available.
Definition: dmat23.cpp:17
Description not yet available.
Definition: fvar.hpp:2819
void read_covariance_matrix(const dmatrix &S, int nvar, int &hbf, dvector &sscale)
Definition: xxmcmc.cpp:1320
void initialize(void)
Initialze all elements of dvector to zero.
Definition: dvect5.cpp:10
values
Definition: adjson.h:22
static void copy_all_values(const dvector &x, const int &ii)
Definition: model3.cpp:9
int user_stop(void)
Definition: xxmcmc.cpp:1558
double ln_det(const dmatrix &m1, int &sgn)
Compute log determinant of a constant matrix.
Definition: dmat3.cpp:536
void read_empirical_covariance_matrix(int nvar, const dmatrix &S, const adstring &prog_name)
Reads the covariance matrix from a file, which is the program name prepended to the extension &#39;...
Definition: xxmcmc.cpp:961
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
static stddev_params * stddevptr[150]
Definition: admodel.h:2215
#define ISZERO(d)
Definition: xxmcmc.cpp:24
void bounded_multivariate_uniform_mcmc(int nvar, const dvector &a1, const dvector &b1, dmatrix &ch, const double &wght, const dvector &y, const random_number_generator &rng)
Definition: xmonte2.cpp:138
int maxnz(const dvector &xa)
Definition: xxmcmc.cpp:1161
void new_probing_bounded_multivariate_normal_mcmc(int nvar, const dvector &a1, const dvector &b1, dmatrix &ch, const double &wght, const dvector &_y, double pprobe, random_number_generator &rng)
Definition: prmonte.cpp:167
int minnz(const dvector &x)
Definition: xxmcmc.cpp:1144
void mcmc_routine(int, int, double, int)
Monte Carlo Markov Chain minimization routine Approximate the target distribution by performing a ran...
Definition: xxmcmc.cpp:126
double better_rand(long int &idum)
Description not yet available.
Definition: bet_rand.cpp:18
Description not yet available.
Definition: fvar.hpp:3516
static void copy_all_values(const dvector &x, const int &ii)
Definition: model4.cpp:9
void read_hessian_matrix_and_scale1(int nvar, const dmatrix &_SS, double s, int mcmc2_flag)
Definition: xxmcmc.cpp:1453
double get_monte_carlo_value(int nvar, const independent_variables &x)
Definition: mod_mc2.cpp:11
static int stddev_scale(const dvector &d, const dvector &x)
Definition: model.cpp:202
static int mc_phase
Definition: admodel.h:846
void initialize(void)
Author: David Fournier Copyright (c) 2008-2012 Regents of the University of California.
Definition: dmat7.cpp:12
void add_hist_values(const dvector &s, const dvector &m, const dmatrix &hist, dvector &mcmc_values, double llc, const dvector &h, int nslots, double total_spreadd, int probflag=0)
Definition: xxmcmc.cpp:1178
double *& get_v(void)
Definition: dvector.h:148
static unsigned int wd_flag
Definition: fvar.hpp:8864
void bounded_multivariate_normal_mcmc(int nvar, const dvector &a1, const dvector &b1, dmatrix &ch, const double &wght, const dvector &y, const random_number_generator &rng)
Definition: xmonte2.cpp:14
double square(const double value)
Return square of value; constant object.
Definition: d3arr4.cpp:16
df1_one_variable inv(const df1_one_variable &x)
Definition: df11fun.cpp:384
d3_array log(const d3_array &arr3)
Author: David Fournier Copyright (c) 2008-2012 Regents of the University of California.
Definition: d3arr2a.cpp:13
static int random_effects_flag
Definition: admodel.h:1857
int kbhit(void)
Definition: df1b2fun.cpp:15
d3_array pow(const d3_array &m, int e)
Description not yet available.
Definition: d3arr6.cpp:17
int ctlc_flag
Definition: gradstrc.cpp:68
static int num_stddev_params
Definition: admodel.h:2219