21 cerr <<
endl <<
endl <<
"Option -hmc is deprecated, please use option -nuts" <<
endl <<
endl;
24 cerr << endl <<
"Error: Negative iterations for MCMC not meaningful" <<
endl;
53 int old_Hybrid_bounded_flag=-1;
66 cerr <<
"Warning: No step size given after -hyeps, ignoring" <<
endl;
75 cerr <<
"Error: step size (-hyeps argument) needs positive number";
91 cerr <<
"Error: chain must be >= 1" <<
endl;
100 if(nmcmc>10) refresh = (int)floor(nmcmc/10);
105 cerr <<
"Error: refresh must be >= -1. Use -1 for no refresh or positive integer for rate." <<
endl;
121 cerr <<
"Error: hynstep argument must be integer > 0 " <<
endl;
130 int nwarmup= (int)nmcmc/2;
136 if (iii <=0 || iii > nmcmc)
138 cerr <<
"Error: nwarmup must be 0 < nwarmup < nmcmc" <<
endl;
150 double adapt_delta=0.8;
158 if (_adapt_delta < 0 || _adapt_delta > 1 )
160 cerr <<
"Error: adapt_delta must be between 0 and 1"
161 " using default of 0.8" <<
endl;
165 adapt_delta=_adapt_delta;
174 cout <<
" Setting covariance matrix to diagonal with entries " << dscale
179 if(mcrestart_flag > -1){
180 cerr << endl <<
"Error: -mcr option not implemented for HMC" <<
endl;
186 cerr << endl <<
"Error: -mcec option not yet implemented with HMC" <<
endl;
201 for (
int i=1;i<=nvar;i++)
211 cerr <<
"Option -mcsave does not currently work with HMC -- every iteration is saved" <<
endl;
220 if (!pofs_psave|| !(*pofs_psave))
222 cerr <<
"Error trying to open file" <<
226 if (mcrestart_flag == -1 )
228 (*pofs_psave) << nvar;
251 for (
int i=1;i<=nvar;i++)
253 for (
int j=1;j<=nvar;j++)
255 S(i,j)*=current_scale(i)*current_scale(j);
267 if (iseed0) iseed=iseed0;
273 dvector epsvec(1,nmcmc+1), epsbar(1,nmcmc+1), Hbar(1,nmcmc+1);
274 epsvec.initialize(); epsbar.initialize(); Hbar.
initialize();
275 double time_warmup=0;
277 std::clock_t start = clock();
278 time_t now = time(0);
279 tm* localtm = localtime(&now);
281 "' at " << asctime(localtm);
283 ofstream adaptation(
"adaptation.csv", ios::trunc);
284 adaptation <<
"accept_stat__,stepsize__,int_time__,energy__,lp__" <<
endl;
295 if(std::isnan(nllbegin)){
296 cerr <<
"Starting MCMC trajectory at NaN -- something is wrong!" <<
endl;
300 dvector gr2(1,nvar); gr2=gr*chd;
307 dvector gr2begin(1,nvar); gr2begin=gr2;
308 dvector ybegin(1,nvar); ybegin=y;
314 double mu=
log(10*eps);
317 for (
int is=1;is<=nmcmc;is++) {
320 double H0=nll+0.5*
norm2(p);
324 for (
int i=1;i<=L;i++) {
326 nll=
leapfrog(nvar, gr, chd, eps, p, y, gr2);
334 double Ham=nll+0.5*
norm2(p);
335 double alpha=
min(1.0,
exp(H0-Ham));
336 double rr=
randu(rng);
337 if (rr<alpha && !divergence){
351 (*pofs_psave) << parsave;
354 if(useDA && is <= nwarmup){
355 eps=
adapt_eps(is, is, eps, alpha, adapt_delta, mu, epsvec, epsbar, Hbar);
357 adaptation << alpha <<
"," << eps <<
"," << eps*L <<
"," << H0 <<
"," << -nll <<
endl;
358 if(is ==nwarmup) time_warmup =
static_cast<double>(std::clock()-start) / CLOCKS_PER_SEC;
364 cout <<
"Final acceptance ratio=" << iaccept/nmcmc <<
" and target is " << adapt_delta<<
endl;
365 cout <<
"Final step size=" << eps <<
"; after " << nwarmup <<
" warmup iterations"<<
endl;
367 cout <<
"Final acceptance ratio=" << iaccept/nmcmc <<
endl;
370 time_total =
static_cast<double>(std::clock() - start) / CLOCKS_PER_SEC;
static void set_NO_DERIVATIVES(void)
Disable accumulation of derivative information.
static void set_active_random_effects(void)
void shmc_mcmc_routine(int, int, double, int)
Vector of double precision numbers.
void print_mcmc_progress(int is, int nmcmc, int nwarmup, int chain, int refresh)
double leapfrog(int nvar, dvector &gr, dmatrix &chd, double eps, dvector &p, dvector &x, dvector &gr2)
Function to take a single HMC leapfrog step, given current position and momentum variables.
static void restore_start_phase(void)
static adstring adprogram_name
int atoi(adstring &s)
Returns a integer converted from input s.
df1_one_matrix choleski_decomp(const df1_one_matrix &MM)
double randu(const random_number_generator &rng)
Uniform random number generator.
void fill_randn(long int &n)
Fill vector with random numbers.
Description not yet available.
prnstream & endl(prnstream &)
Description not yet available.
double get_hybrid_monte_carlo_value(int nvar, const independent_variables &y, dvector &g)
Written by Dave, commented by Cole starting 8/31/2016 Description not yet available.
Description not yet available.
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)
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.
double norm2(const d3_array &a)
Return sum of squared elements in a.
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").
double adapt_eps(int ii, int iseps, double eps, double alpha, double &adapt_delta, double &mu, dvector &epsvec, dvector &epsbar, dvector &Hbar)
void print_mcmc_timing(double time_warmup, double time_total, int chain)
static void set_YES_DERIVATIVES(void)
Enable accumulation of derivative information.
void read_hessian_matrix_and_scale1(int nvar, const dmatrix &_SS, double s, int mcmc2_flag)
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.
d3_array log(const d3_array &arr3)
Author: David Fournier Copyright (c) 2008-2012 Regents of the University of California.