27 #define ISZERO(d) ((d)==0.0)
34 int iseed,
double size_scale);
44 const double& size_scale);
48 double total_spread,
int probflag=0);
52 double total_spreadd,
int probflag=0);
78 #if defined(USE_BAYES_FACTORS)
79 void newton_raftery_bayes_estimate_new(
double cbf,
int ic,
const dvector& lk,
84 (
int feval,
int iter,
double fval,
double gmax);
87 double fval,
double gmax,
const char * cbuf);
109 cerr <<
"The sdmcmc feature is disabled, so -nosdmcmc does nothing" <<
endl;
130 #if defined(USE_BAYES_FACTORS)
131 double lcurrent_bf=0;
133 double size_scale=1.0;
134 double total_spread=200;
150 int initial_nsim=4800;
184 int have_hist_flag=0;
196 dvector mean_mcmc_values(1,ndvar);
200 dvector square_mcmc_values(1,ndvar);
204 int use_empirical_flag=0;
212 cerr <<
"Error: chain must be >= 1" <<
endl;
221 if(nmcmc>10) refresh = (int)floor(nmcmc/10);
226 cerr <<
"Error: refresh must be >= -1. Use -1 for no refresh or positive integer for rate." <<
endl;
236 cout <<
"Chain " << chain <<
": Setting covariance matrix to diagonal with entries " << dscale
244 int rescale_bounded_flag=0;
245 double rescale_bounded_power=0.5;
251 if (iii < 1 || iii > 9)
253 cerr <<
" -mcrb argument must be integer between 1 and 9 --"
254 " using default of 5" <<
endl;
255 rescale_bounded_power=0.5;
258 rescale_bounded_power=iii/10.0;
262 rescale_bounded_power=0.5;
264 rescale_bounded_flag=1;
268 use_empirical_flag=1;
270 if (use_empirical_flag)
274 else if (!rescale_bounded_flag)
288 cerr <<
"error opening file " << tmpstring <<
endl;
294 cerr <<
"error reading from file " << tmpstring <<
endl;
297 if (tmp_nvar != nvar)
299 cerr <<
"size error reading from " << tmpstring <<
endl;
305 cerr <<
"error reading from file " << tmpstring <<
endl;
319 for (
int i=1;i<=nvar;i++)
321 tmp(i,i)=S(i,i)*(scale(i)*scale(i));
322 for (
int j=1;j<i;j++)
324 tmp(i,j)=S(i,j)*(scale(i)*scale(j));
334 for (
int i=1;i<=nvar;i++)
349 int number_sims = 100000;
387 hist.
allocate(1,ndvar,-nslots,nslots);
390 values.
allocate(1,ndvar,-nslots,nslots);
391 for (
int i=1;i<=ndvar;i++)
393 values(i).fill_seqadd(mean_mcmc_values(i)-0.5*total_spread*s(i)
402 if (tmp) have_hist_flag=1;
404 chdinv=chdinv/size_scale;
411 cerr <<
"Error trying to open mcmc par input file "
417 cerr <<
"Error reading from mcmc par input file "
422 cerr <<
"Illegal option with -mcpin" <<
endl;
446 ofstream ogs(
"sims");
447 ogs << nvar <<
" " << number_sims <<
endl;
450 std::clock_t start0 = clock();
452 double time_nll =
static_cast<double>(std::clock()-start0)/CLOCKS_PER_SEC;
457 #if defined(USE_BAYES_FACTORS)
473 int change_ball= (int)nmcmc/2;
483 cerr <<
" Invalid option following command line option -mcscale -- "
484 << endl <<
" ignored" <<
endl;
526 cerr <<
" Invalid option following command line option -mcsave -- "
532 if ( mcrestart_flag>-1)
539 cerr <<
"Error trying to open file" <<
541 " for mcrestart" <<
endl;
542 cerr <<
" I am starting a new file " <<
endl;
551 cerr <<
"wrong number of independent variables in"
553 <<
"\n starting a new file " <<
endl;
571 if (!pofs_psave|| !(*pofs_psave))
573 cerr <<
"Error trying to open file" <<
584 if (psvflag || (mcrestart_flag == -1) )
586 (*pofs_psave) << nvar_re;
603 cerr <<
"Error: duration must be > 0" <<
endl;
607 duration=_duration*60;
627 if (pprobe<=0.00001 || pprobe > .499)
629 cerr <<
"Invalid argument to option -mcprobe" <<
endl;
635 int java_quit_flag=0;
638 double time_warmup=0;
640 std::clock_t start = clock();
641 time_t now = time(0);
642 tm* localtm = localtime(&now);
644 cout << endl <<
"Chain " << chain <<
": Starting RWM for model '" << m <<
645 "' at " << asctime(localtm);
647 cout <<
"Chain " << chain <<
": Model will run for " << duration/60 <<
648 " minutes or until " << number_sims <<
" total iterations" <<
endl;
652 ofstream rwm_lp(
"rwm_lp.txt", ios::trunc);
653 rwm_lp <<
"log-posterior" <<
endl;
656 ofstream unbounded(
"unbounded.csv", ios::trunc);
660 cout <<
"Chain " << chain <<
": Initial negative log density=" << -llbest <<
endl;
661 cout <<
"Chain " << chain <<
": Model eval took " << time_nll <<
662 " sec. " << nmcmc <<
" iter will take approximately " ;
665 printf(
"%.2f", time_nll); cout <<
" seconds." <<
endl;
666 }
else if(time_nll <=60*60){
667 printf(
"%.2f", time_nll/60); cout <<
" minutes." <<
endl;
668 }
else if(time_nll <= (60*60*24)){
669 printf(
"%.2f", time_nll/(60*60)); cout <<
" hours." <<
endl;
671 printf(
"%.2f", time_nll/(24*60*60)); cout <<
" days." <<
endl;
675 for (
int i=1;i<=number_sims;i++)
678 if (java_quit_flag)
break;
686 double tratio=double(liac)/200;
711 if (i>50 && s_option && i<change_ball && !restart_flag)
727 else if (tratio > .5)
734 else if (tratio > .4)
768 nvar,symbds(1),symbds(2),chd,lprob,pprobe,rng);
776 lpinv,-1*(chdinv*bmn1),rng);
779 symbds(2), chd,lpinv,-1*(chdinv*bmn1),pprobe,rng);
787 double ldiff=lprob-lpinv;
788 logr= ll - ldiff - llc;
799 lpinv,-1*(chdinv*bmn1),rng);
801 double ldiff=lprob-lpinv;
802 logr= ll - ldiff - llc;
808 if (logr>=0 || br<
exp(logr) )
830 double lbf=llbest-llc;
831 if (lbf>lbmax) lbmax=lbf;
835 #if defined(USE_BAYES_FACTORS)
836 lkvector(ibfcount)=llc;
838 lcurrent_bf=-1.*
log(bfsum/
double(ibfcount))+llbest;
840 if (mcsave_flag && (!((i-1)%mcsave_flag)))
843 (*pofs_psave) << parsave;
844 rwm_lp << llc <<
endl;
848 for(
int i=1;i<nvar;i++) {
850 unbounded << y(i) <<
", ";
854 unbounded << y(nvar) <<
endl;
870 for (ii=mmin;ii<=mmax;ii++)
872 (*pofs_sd) << float(mcmc_values(ii));
874 (*pofs_sd) << (float)(llc);
875 mean_mcmc_values+=mcmc_values;
876 square_mcmc_values+=
square(mcmc_values);
881 h,nslots,total_spread);
890 if (!no_sd_mcmc && iac>5 && isim>initial_nsim)
897 mean_mcmc_values/=double(isim);
898 square_mcmc_values/=double(isim);
899 #if defined(USE_DDOUBLE)
900 s=2.*
sqrt(
double(1.e-20)+square_mcmc_values
901 -
square(mean_mcmc_values));
903 s=2.*
sqrt(1.e-20+square_mcmc_values-
square(mean_mcmc_values));
906 nslots,total_spread);
910 time_warmup =
static_cast<double>(std::clock()-start)/CLOCKS_PER_SEC;
911 time_total =
static_cast<double>(std::clock()-start)/CLOCKS_PER_SEC;
912 if(use_duration==1 && time_total > duration){
914 cout <<
"Chain " << chain <<
": " << i <<
" samples generated after " << duration/60 <<
915 " minutes running." <<
endl;
922 if (!no_sd_mcmc && !have_hist_flag)
926 mean_mcmc_values/=double(isim);
927 square_mcmc_values/=double(isim);
928 #if defined(USE_DDOUBLE)
929 s=2.*
sqrt(
double(1.e-20)+square_mcmc_values-
square(mean_mcmc_values));
931 s=2.*
sqrt(1.e-20+square_mcmc_values-
square(mean_mcmc_values));
943 cout <<
"Chain " << chain <<
":Final acceptance ratio=";
944 printf(
"%.2f", iac/
double(isim));
946 cout << endl <<
endl;
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)
dvector bounded_multivariate_uniform(int nvar, const dvector &a1, const dvector &b1, dmatrix &ch, const double &lprob, random_number_generator &rng)
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)
void read_hessian_matrix_and_scale(int nvar, const dmatrix &S, const dvector &pen_vector)
static void set_NO_DERIVATIVES(void)
Disable accumulation of derivative information.
static void set_active_random_effects(void)
void allocate(void)
Author: David Fournier Copyright (c) 2008-2012 Regents of the University of California.
void read_mle_hmc(int nvar, dvector &mle)
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)
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)
Vector of double precision numbers.
int indexmin() const
Get minimum valid index.
void allocate(int ncl, int ncu)
Allocate memory for a dvector.
static void add_random_vector(const dvector &x)
Author: David Fournier Copyright (c) 2008-2012 Regents of the University of California.
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)
void rwm_mcmc_routine(int, int, double, int)
void print_mcmc_progress(int is, int nmcmc, int nwarmup, int chain, int refresh)
void set_labels_for_mcmc(void)
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)
void ad_update_function_minimizer_report(int feval, int iter, int phase, double fval, double gmax, const char *cbuf)
static void set_all_simulation_bounds(const dmatrix &symbds)
static void restore_start_phase(void)
static adstring adprogram_name
static int num_stddev_calc(void)
double better_rand()
Random number generator.
int atoi(adstring &s)
Returns a integer converted from input s.
df1_one_matrix choleski_decomp(const df1_one_matrix &MM)
Description not yet available.
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 '...
prnstream & endl(prnstream &)
Description not yet available.
Array of integers(int) with indexes from index_min to indexmax.
d3_array sqrt(const d3_array &arr3)
Author: David Fournier Copyright (c) 2008-2012 Regents of the University of California.
Description not yet available.
int indexmax() const
Get maximum valid index.
Description not yet available.
static void xinit(const dvector &x)
d3_array exp(const d3_array &arr3)
Returns d3_array results with computed exp from elements in arr3.
static void set_inactive_random_effects(void)
dmatrix outer_prod(const dvector &v1, const dvector &v2)
Description not yet available.
Description not yet available.
void read_covariance_matrix(const dmatrix &S, int nvar, int &hbf, dvector &sscale)
void initialize(void)
Initialze all elements of dvector to zero.
static void copy_all_values(const dvector &x, const int &ii)
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 '...
int option_match(int argc, char *argv[], const char *string)
Checks if the program has been invoked with a particular command line argument ("string").
void print_mcmc_timing(double time_warmup, double time_total, int chain)
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)
int maxnz(const dvector &xa)
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)
int minnz(const dvector &x)
double better_rand(long int &idum)
Description not yet available.
Description not yet available.
static void copy_all_values(const dvector &x, const int &ii)
void read_hessian_matrix_and_scale1(int nvar, const dmatrix &_SS, double s, int mcmc2_flag)
std::string get_filename(const char *f)
double get_monte_carlo_value(int nvar, const independent_variables &x)
static int stddev_scale(const dvector &d, const dvector &x)
void initialize(void)
Author: David Fournier Copyright (c) 2008-2012 Regents of the University of California.
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)
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)
double square(const double value)
Return square of value; constant object.
df1_one_variable inv(const df1_one_variable &x)
d3_array log(const d3_array &arr3)
Author: David Fournier Copyright (c) 2008-2012 Regents of the University of California.
static int random_effects_flag