24 #define ISZERO(d) ((d)==0.0)
31 int iseed,
double size_scale);
41 const double& size_scale);
45 double total_spread,
int probflag=0);
49 double total_spreadd,
int probflag=0);
75 #if defined(USE_BAYES_FACTORS)
76 void newton_raftery_bayes_estimate_new(
double cbf,
int ic,
const dvector& lk,
81 (
int feval,
int iter,
double fval,
double gmax);
84 double fval,
double gmax,
const char * cbuf);
149 cerr <<
" You must declare at least one object of type sdreport "
150 <<
endl <<
" to do the mcmc calculations" <<
endl;
157 #if defined(USE_BAYES_FACTORS)
158 double lcurrent_bf=0;
160 double size_scale=1.0;
161 double total_spread=200;
177 int initial_nsim=4800;
211 int have_hist_flag=0;
223 dvector mean_mcmc_values(1,ndvar);
227 dvector square_mcmc_values(1,ndvar);
231 int use_empirical_flag=0;
236 cout <<
" 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;
321 double* pscalei = scale.
get_v() + 1;
323 for (
int i=1;i<=nvar;i++)
325 *(ptmpi->
get_v() + i) = *(pSi->
get_v() + i) * (*pscalei * *pscalei);
327 double* ptmpij = ptmpi->
get_v() + 1;
329 double* pscalej = scale.
get_v() + 1;
330 double* pSij = pSi->
get_v() + 1;
331 for (
int j=1;j<i;j++)
333 *ptmpij = *pSij * (*pscalei * *pscalej);
334 *(ptmpj->
get_v() + i) = *ptmpij;
352 for (
int i=1;i<=nvar;i++)
367 int number_sims = 100000;
386 cout <<
"Initial seed value " << iseed <<
endl;
404 hist.
allocate(1,ndvar,-nslots,nslots);
407 values.
allocate(1,ndvar,-nslots,nslots);
408 for (
int i=1;i<=ndvar;i++)
410 values(i).fill_seqadd(mean_mcmc_values(i)-0.5*total_spread*s(i)
419 if (tmp) have_hist_flag=1;
421 chdinv=chdinv/size_scale;
433 cerr <<
"Error trying to open mcmc par input file "
440 cerr <<
"Error reading from mcmc par input file "
447 cerr <<
"Illegal option with -mcpin" <<
endl;
466 ofstream ogs(
"sims");
467 ogs << nvar <<
" " << number_sims <<
endl;
475 #if defined(USE_BAYES_FACTORS)
491 int change_ball=2500;
500 cerr <<
" Invalid option following command line option -mcscale -- "
501 << endl <<
" ignored" <<
endl;
543 cerr <<
" Invalid option following command line option -mcsave -- "
549 if ( mcrestart_flag>-1)
556 cerr <<
"Error trying to open file" <<
558 " for mcrestart" <<
endl;
559 cerr <<
" I am starting a new file " <<
endl;
568 cerr <<
"wrong number of independent variables in"
570 <<
"\n starting a new file " <<
endl;
588 if (!pofs_psave|| !(*pofs_psave))
590 cerr <<
"Error trying to open file" <<
601 if (psvflag || (mcrestart_flag == -1) )
603 (*pofs_psave) << nvar_re;
624 if (pprobe<=0.00001 || pprobe > .499)
626 cerr <<
"Invalid argument to option -mcprobe" <<
endl;
633 int java_quit_flag=0;
634 for (
int i=1;i<=number_sims;i++)
637 if (java_quit_flag)
break;
641 double ratio = double(iac)/i;
644 cout << llc <<
" " << llc <<
endl;
645 double tratio=double(liac)/200;
647 cout <<
" mcmc sim " << i <<
" acceptance rate "
648 << ratio <<
" " << tratio <<
endl;
670 if (i>50 && s_option && i<change_ball && !restart_flag)
677 cout <<
"decreasing step size " <<
ln_det(chd,itmp) <<
endl;
684 cout <<
"increasing step size " <<
ln_det(chd,itmp) <<
endl;
686 else if (tratio > .5)
691 cout <<
"increasing step size " <<
ln_det(chd,itmp) <<
endl;
693 else if (tratio > .4)
698 cout <<
"increasing step size " <<
ln_det(chd,itmp) <<
endl;
727 nvar,symbds(1),symbds(2),chd,lprob,pprobe,rng);
735 lpinv,-1*(chdinv*bmn1),rng);
738 symbds(2), chd,lpinv,-1*(chdinv*bmn1),pprobe,rng);
746 double ldiff=lprob-lpinv;
747 logr= ll - ldiff - llc;
758 lpinv,-1*(chdinv*bmn1),rng);
760 double ldiff=lprob-lpinv;
761 logr= ll - ldiff - llc;
767 if (logr>=0 || br<
exp(logr) )
789 double lbf=llbest-llc;
790 if (lbf>lbmax) lbmax=lbf;
794 #if defined(USE_BAYES_FACTORS)
795 lkvector(ibfcount)=llc;
797 lcurrent_bf=-1.*
log(bfsum/
double(ibfcount))+llbest;
799 if (mcsave_flag && (!((i-1)%mcsave_flag)))
801 (*pofs_psave) << parsave;
816 for (ii=mmin;ii<=mmax;ii++)
818 (*pofs_sd) << float(mcmc_values(ii));
820 (*pofs_sd) << (float)(llc);
821 mean_mcmc_values+=mcmc_values;
822 square_mcmc_values+=
square(mcmc_values);
827 h,nslots,total_spread);
836 if (!no_sd_mcmc && iac>5 && isim>initial_nsim)
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));
849 s=2.*
sqrt(1.e-20+square_mcmc_values-
square(mean_mcmc_values));
852 nslots,total_spread);
857 if (!no_sd_mcmc && !have_hist_flag)
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));
866 s=2.*
sqrt(1.e-20+square_mcmc_values-
square(mean_mcmc_values));
878 cout << iac/double(isim) <<
endl;
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);
925 cout <<
"In write empirical covariance matrix" <<
endl;
968 cerr <<
"Error opening file " << infile_name <<
endl;
1008 int iseed,
double size_scale)
1013 ivector param_size(1,nsdvars);
1038 for (i=1;i<=nsdvars;i++)
1040 mmin(i)=
minnz(hist(i));
1041 mmax(i)=
maxnz(hist(i));
1044 int nsim = (int)
sum(hist(1));
1046 double _nsim =
sum(hist(1));
1047 assert(_nsim <= (
double)INT_MAX);
1048 int nsim = (int)_nsim;
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;
1056 ofs <<
"# means" <<
endl;
1058 ofs <<
"# standard devs" <<
endl;
1060 ofs <<
"# lower bounds" <<
endl;
1061 ofs << mmin <<
endl;
1062 ofs <<
"# upper bounds" <<
endl;
1064 ofs <<
"#number of parameters" <<
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++)
1072 ofs <<
"#" << param_labels[lc];
1073 if (param_size[lc]>1)
1075 ofs <<
"[" << ic <<
"]";
1079 if (++ic> param_size[lc])
1084 for (ii=mmin(i);ii<=mmax(i);ii++)
1086 ofs <<
values(i,ii) <<
" " << hist(i,ii)/(nsim*h(i)) << endl;
1088 if (i<nsdvars) ofs <<
endl;
1094 const double& size_scale)
1122 cerr <<
"wrong number of independent variables in file"
1132 for (i=1;i<=nsdvars;i++)
1134 for (ii=mmin(i);ii<=mmax(i);ii++)
1136 cif >> tmp >> hist(i,ii);
1149 for (
int ii=mmin;ii<=mmax;ii++)
1166 for (
int ii=mmax;ii>=mmin;ii--)
1180 double total_spread,
int probflag)
1184 for (
int ii=1;ii<=nsdvars;ii++)
1187 double xx=(mcmc_values(ii)-m(ii))/h(ii);
1207 hist(ii,x)+=
exp(llc);
1243 double total_spread,
int probflag)
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);
1256 for (i=1;i<=nsdvars;i++)
1258 values(i).fill_seqadd(m(i)-.5*total_spread*s(i)+.5*h(i),h(i));
1263 cerr <<
"Error trying to open file "
1269 cerr <<
"Error trying to read number of simulations from file "
1273 for (i=1;i<=nsim;i++)
1279 for (ii=mmin;ii<=mmax;ii++)
1282 mcmc_values(ii)=double(ftmp);
1286 for (ii=1;ii<=nsdvars;ii++)
1289 double xx=(mcmc_values(ii)-m(ii))/h(ii);
1308 hist(ii,x)+=
exp(llc);
1313 cerr <<
"Error trying to read data from file "
1329 cerr <<
"Error trying to open file " << tmpstring
1330 <<
" for reading" <<
endl;
1334 if (nvar !=tmp_nvar)
1336 cerr <<
"Incorrect number of independent variables in file"
1337 " model.cov" <<
endl;
1343 cerr <<
"error reading covariance matrix from " << tmpstring
1350 cerr <<
"error reading old_hybrid_bounded_flag from " << tmpstring
1357 cerr <<
"error reading sscale from " << tmpstring
1373 cerr <<
"Error trying to open file " << tmpstring
1374 <<
" for reading" <<
endl;
1378 if (nvar !=tmp_nvar)
1380 cerr <<
"Incorrect number of independent variables in file"
1381 " admodel.hes" <<
endl;
1387 cerr <<
"error reading covariance matrix from model.cov" <<
endl;
1398 cout <<
"Smallest eigenvalue = " << dmin <<
endl;
1399 for (
int i=1;i<=nvar;i++)
1403 #if defined(USE_DDOUBLE)
1404 S(i,i)/=
pow(
double(10.0),tmp(i));
1406 S(i,i)/=
pow(10.0,tmp(i));
1413 cout <<
"Smallest eigenvalue = " << dmin <<
endl;
1454 double rbp,
int mcmc2_flag)
1471 cerr <<
"Error trying to open file " << tmpstring
1472 <<
" for reading" <<
endl;
1476 if (nvar !=tmp_nvar)
1478 cerr <<
"Incorrect number of independent variables in file"
1479 " admodel.hes" <<
endl;
1485 cerr <<
"error reading covariance matrix from model.cov" <<
endl;
1494 int mmin=S.indexmin();
1495 int mmax=S.indexmax();
1498 for (i=mmin;i<=mmax;i++)
1500 diag(i)=
sqrt(S(i,i));
1502 for (i=mmin;i<=mmax;i++)
1503 for (j=mmin;j<=mmax;j++)
1504 S(i,j)/=diag(i)*diag(j);
1507 ofstream ofs(
"corrtest");
1508 ofs <<
"corr matrix" <<
endl;
1510 ofs <<
"choleski decomp of corr matrix" <<
endl;
1512 dmatrix tmp(mmin,mmax,mmin,mmax);
1514 for (i=mmin;i<=mmax;i++)
1515 tmp(i)=ch(i)/
norm(ch(i));
1516 ofs <<
"tmp" <<
endl;
1519 for (i=mmin;i<=mmax;i++)
1523 if (rbp<=0.0 || rbp >= 1.0)
1525 for (i=mmin;i<=mmax;i++)
1527 for (j=mmin;j<=mmax;j++)
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);
1536 for (i=mmin;i<=mmax;i++)
1537 tmp(i)/=
norm(tmp(i));
1541 ofs <<
"T-S" <<
endl;
1545 ofs <<
"modified corr matrix" <<
endl;
1547 for (i=mmin;i<=mmax;i++)
1548 for (j=mmin;j<=mmax;j++)
1549 S(i,j)*=diag(i)*diag(j);
1551 ofs <<
"modified S" <<
endl;
1554 ofs <<
"S* modified S" <<
endl;
1555 ofs << S*Save <<
endl;
1561 #if defined(_MSC_VER)
1567 #if defined(__DJGPP__)
1568 int c = toupper(getxkey());
1570 int c = toupper(
getch());
1610 #if defined(USE_BAYES_FACTORS)
1611 void newton_raftery_bayes_estimate_new(
double lcbf,
int ic,
const dvector& lk,
1618 cout <<
"initial log bayes factor" << lcbf <<
endl;
1622 for (
int i=1;i<=ic;i++)
1624 double dtmp=
exp(lcbf-lk(i));
1625 sum1+=1./(d1+d*dtmp);
1626 sum2+=1./(d1/dtmp+d);
1630 lcbf=lcbf+
log(sum1)-
log(sum2);
1631 double diff=lcbf-lcbold;
1632 if (
fabs(diff)<1.e-5)
break;
1675 cerr <<
"Error trying to open file " << tmpstring
1676 <<
" for reading" <<
endl;
1679 dmatrix S(1,old_nvar,1,old_nvar);
1683 cerr <<
"error reading covariance matrix from " << tmpstring
1691 cerr <<
"error reading old_hybrid_bounded_flag from " << tmpstring
1699 cerr <<
"error reading sscale from " << tmpstring
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.
dvector read_old_scale(int &old_nvar)
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)
dmatrix trans(const dmatrix &m1)
Author: David Fournier Copyright (c) 2008-2012 Regents of the University of California.
dvector eigenvalues(const banded_symmetric_dmatrix &_SS)
Description not yet available.
#define ADUNCONST(type, obj)
Creates a shallow copy of obj that is not CONST.
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.
double sum(const d3_array &darray)
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)
df1_two_variable fabs(const df1_two_variable &x)
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 norm(const d3_array &a)
Return computed norm value of a.
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)
virtual unsigned int size_count() const =0
Description not yet available.
virtual const char * label()=0
dmatrix sort(const dmatrix &m, int column, int NSTACK)
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)
double ln_det(const dmatrix &m1, int &sgn)
Compute log determinant of a constant matrix.
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").
static stddev_params * stddevptr[150]
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)
void mcmc_routine(int, int, double, int)
Monte Carlo Markov Chain minimization routine Approximate the target distribution by performing a ran...
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)
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)
static unsigned int wd_flag
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
d3_array pow(const d3_array &m, int e)
Description not yet available.
static int num_stddev_params