22 using std::istringstream;
37 if(anw == (warmup-w3) )
57 bool x2 = i<= (warmup-w3);
65 size_t pos = s.find_last_of(
"/\\");
66 std::string filename_exe = s.substr(pos + 1);
67 size_t dot_pos = s.find_last_of(
".");
68 std::string filename = s.substr(pos + 1, dot_pos - pos - 1);
79 double& _alphaprime,
int& _nalphaprime,
bool& _sprime,
80 int& _nprime,
int& _nfevals,
bool& _divergent,
88 double nll=
leapfrog(nvar, gr, chd, eps*v, p, y, gr2);
95 double Ham=-(nll+0.5*
norm2(p));
99 _divergent = (std::isnan(Ham) || logu > 1000+Ham);
106 _nprime = logu < Ham;
108 _alphaprime =
min(1.0,
exp(Ham-H0));
116 _grprime=gr; _gr2prime=gr2; _nllprime=nll; _Hprime=Ham;
123 build_tree(nvar, gr, chd, eps, p, y, gr2, logu, v, j-1,
124 H0, _thetaprime, _thetaplus, _thetaminus, _rplus, _rminus,
125 _alphaprime, _nalphaprime, _sprime,
126 _nprime, _nfevals, _divergent, rng,
127 gr2_end, _grprime, _gr2prime, _nllprime, _Hprime, _parsaveprime);
136 double nllprime0, Hprime0;
141 thetaprime0=_thetaprime;
142 parsaveprime0=_parsaveprime;
147 thetaplus0=_thetaplus;
148 thetaminus0=_thetaminus;
151 int nprime0 = _nprime;
152 double alphaprime0 = _alphaprime;
153 int nalphaprime0 = _nalphaprime;
157 build_tree(nvar, gr, chd, eps, _rminus, _thetaminus, gr2, logu, v, j-1,
158 H0, _thetaprime, _thetaplus, _thetaminus, _rplus, _rminus,
159 _alphaprime, _nalphaprime, _sprime,
160 _nprime, _nfevals, _divergent, rng,
161 gr2_end, _grprime, _gr2prime, _nllprime, _Hprime, _parsaveprime);
164 thetaminus0=_thetaminus;
166 _thetaplus=thetaplus0;
170 build_tree(nvar, gr, chd, eps, _rplus, _thetaplus, gr2, logu, v, j-1,
171 H0, _thetaprime, _thetaplus, _thetaminus, _rplus, _rminus,
172 _alphaprime, _nalphaprime, _sprime,
173 _nprime, _nfevals, _divergent, rng,
174 gr2_end, _grprime, _gr2prime, _nllprime, _Hprime, _parsaveprime);
177 thetaplus0=_thetaplus;
179 _thetaminus=thetaminus0;
184 int nprime_temp = nprime0 + _nprime;
185 if(std::isnan(static_cast<double>(nprime_temp))) nprime_temp=0;
187 double rr=
randu(rng);
188 if (nprime_temp != 0 && rr <
double(_nprime)/
double(nprime_temp)) {
193 _thetaprime = thetaprime0;
194 _parsaveprime=parsaveprime0;
202 _alphaprime = alphaprime0 + _alphaprime;
203 _nalphaprime = nalphaprime0 + _nalphaprime;
206 bool b =
stop_criterion(nvar, thetaminus0, thetaplus0, rminus0, rplus0);
207 _sprime = _sprime && b;
208 _nprime = nprime_temp;
218 thetavec=thetaplus-thetaminus;
224 for(
int i=1; i<=nvar; i++){
225 x1+=thetavec(i)*rminus(i);
226 x2+=thetavec(i)*rplus(i);
229 bool criterion = (x1 >=0) && (x2 >=0);
235 double& adapt_delta,
double& mu,
238 double gamma=0.05;
double t0=20;
double kappa=0.75;
241 if(std::isnan(alpha)) alpha=0;
242 Hbar(m)= (1-1/(iseps+t0))*Hbar(m-1) + (adapt_delta-alpha)/(iseps+t0);
243 double logeps=mu-
sqrt(iseps)*Hbar(m)/gamma;
244 epsvec(m)=
exp(logeps);
245 double logepsbar=
pow(iseps, -kappa)*logeps+(1-
pow(iseps,-kappa))*
log(epsbar(m-1));
246 epsbar(m)=
exp(logepsbar);
264 cerr <<
"HMC not implemented for random effects models" <<
endl;
288 if (is==1 || is == nmcmc || is % refresh ==0 ){
289 int width=1+(int)std::ceil(
std::log10(static_cast<double>(nmcmc)));
290 cout <<
"Chain " << chain <<
": " <<
"Iteration: " << std::setw(width) << is
291 <<
"/" << nmcmc <<
" [" << std::setw(3)
292 << int(100.0 * (
double(is) /
double(nmcmc) ))
293 <<
"%]" << (is <= nwarmup ?
" (Warmup)" :
" (Sampling)") <<
endl;
299 double time_sampling=time_total-time_warmup;
300 std::string title=
"Elapsed Time: ";
301 std::string title2=
"Chain " + std::to_string(chain) +
": ";
307 }
else if(time_total > 60 && time_total <=60*60){
308 time_total/=60; time_sampling/=60; time_warmup/=60;
310 }
else if(time_total > (60*60) && time_total <= (360*24)){
311 time_total/=(60*60); time_sampling/=(60*60); time_warmup/=(60*60);
314 time_total/=(24*60*60); time_sampling/=(24*60*60); time_warmup/=(24*60*60);
317 cout << title2 << title; printf(
"%5.2f", time_warmup); cout << u <<
" (Warmup | ";
318 printf(
"%.0f", 100*(time_warmup/time_total)); cout <<
"%)" <<
endl;
319 cout << title2 << std::string(title.size(),
' '); printf(
"%5.2f", time_sampling);
320 cout << u <<
" (Sampling | " ;
321 printf(
"%.0f", 100*(time_sampling/time_total)); cout <<
"%)" <<
endl;
322 cout << title2 << std::string(title.size(),
' '); printf(
"%5.2f", time_total);
323 cout << u <<
" (Total | 100%)";
332 bool verbose_adapt_mass,
bool verbose_find_epsilon,
343 double pprob1=0.5*
norm2(p);
347 double H1=nllbegin+pprob1;
350 double nll2=
leapfrog(nvar, gr, chd, eps, p2, y2, gr2);
351 double pprob2=0.5*
norm2(p2);
352 double H2=nll2+pprob2;
353 double alpha=
exp(H1-H2);
358 if(alpha < 0.5 || std::isnan(alpha)){
366 for(
int k=2; k<50; k++){
367 if(verbose_find_epsilon){
368 cout <<
"Chain " << chain <<
": Find epsilson: iteration=" << k-1 <<
"; eps=" << eps <<
"; NLL1=" << nllbegin <<
"; p1=" << pprob1 <<
"; H1=" << H1 <<
369 "; NLL2=" << nll2 <<
"; p2=" << pprob2 <<
"; H2=" << H2<<
"; alpha=" << alpha <<
endl;
377 nll2=
leapfrog(nvar, gr, chd, eps, p2, y2, gr2);
378 pprob2=0.5*
norm2(p2);
383 if(!std::isnan(alpha) &&
pow(alpha,a) <
pow(2,-a)){
384 if(verbose_find_epsilon){
385 cout <<
"Chain " << chain <<
": Final epsilson: iteration=" << k <<
"; eps=" << eps <<
"; NLL1=" << nllbegin <<
"; p1=" << pprob1 <<
"; H1=" << H1 <<
386 "; NLL2=" << nll2 <<
"; p2=" << pprob2 <<
"; H2=" << H2<<
"; alpha=" << alpha <<
endl;
388 if(verbose_adapt_mass) {cout <<
"Chain " << chain <<
": Found reasonable step size of " << eps <<
endl;}
395 cerr <<
"Chain " << chain <<
": Final epsilson: iteration=" << 50 <<
"; eps=" << eps <<
"; NLL1=" << nllbegin <<
"; p1=" << pprob1 <<
"; H1=" << H1 <<
396 "; NLL2=" << nll2 <<
"; p2=" << pprob2 <<
"; H2=" << H2<<
"; alpha=" << alpha <<
endl;
397 cerr <<
"Chain " << chain <<
": Could not find reasonable initial step size. Is something wrong with model/initial value?" <<
endl;
437 cerr <<
"Error reading admodel.hes file to get MLE values. Try re-optimizing model." <<
endl;
442 if (nvar !=tmp_nvar) {
446 cout<<
"NOTE: the number of active parameters ("<<nvar<<
") does not equal the dimension in admodel.hes ("<<tmp_nvar<<
")."<<
endl;
447 cout<<
" This could mean admodel.hes is old or random effects are in use."<<
endl;
449 dmatrix hess(1,tmp_nvar,1,tmp_nvar);
452 cerr <<
"Error reading the Hessian matrix from admodel.hes. Try re-optimizing model." <<
endl;
455 if (debug) cout<<
"the hessian matrix"<<
endl<<hess<<
endl;
460 cerr <<
"Error reading the hybrid flag from admodel.hes. Try re-optimizing model." <<
endl;
466 cerr <<
"Error reading the transformation scales from admodel.hes. Try re-optimizing model." <<
endl;
469 if (debug) cout<<
"sscale = "<<sscale<<
endl;
474 if (debug) cout<<
"flag = "<<temp<<
endl;
477 if(temp != -987 || !cif){
478 cerr <<
"Error reading the check value from admodel.hes. Try re-optimizing model." <<
endl;
482 if (debug) cout<<
"mle = "<<mle<<
endl;
484 cerr <<
"Error reading the bounded MLE values from admodel.hes. Try re-optimizing model." <<
endl;
502 cerr <<
" Incompatible array bounds in "
503 "dvector rotate_pars(const dmaxtrix& m, const dvector& x)\n";
518 for (
int i=mmin; i<=mmax; i++)
521 double * pm= (
double *) &(m(i,xmin));
522 double * px= (
double *) &(
x(xmin));
524 for (
int j=xmin; j<=i; j++)
532 for (
int i=mmin; i<=mmax; i++)
534 double * pm= (
double *) &(m(i,i));
535 double * px= (
double *) &(
x(i));
539 cerr <<
"Invalid value for diagonal_metric_flag in rotate_pars" <<
endl;
551 cerr <<
" Incompatible array bounds in "
552 "dvector rotate_gradient(const dvector& x, const dmatrix& m)\n";
559 for (
int j = mmin; j <= mmax; ++j)
562 double* pm = (
double*)&
column(j);
563 double* px = (
double*)&
x(j);
564 double sum = *px * *pm;
565 for (
int i=j; i < mmax; ++i)
567 sum += *(++px) * *(++pm);
573 for (
int i=mmin; i<=mmax; i++)
575 double * pm= (
double *) &(m(i,i));
576 double * px= (
double *) &(
x(i));
580 cerr <<
"Invalid value for diagonal_metric_flag in rotate_gradient" <<
endl;
661 for(
int i=1;i<=nvar;i++){
663 cerr <<
"Element " << i <<
" of diagonal mass matrix was <0... setting to 1 instead" <<
endl;
666 chd(i,i)=
sqrt(metric(i,i));
668 chdinv(i,i)=1/chd(i,i);
691 cerr <<
"Error in choleski_decomp_hmc. Mass matrix not square" <<
endl;
708 adstring tmpstring =
"Mass matrix not positive definite when updating dense mass matrix adaptation.";
709 if (
M(1,1)<=0) {success=
false;
return success;}
713 L(i,1)=
M(i,1)/L(1,1);
716 for (j=2;j<=i-1;j++) {
718 for (k=1;k<=j-1;k++) {
724 for (k=1;k<=i-1;k++) {
727 if (tmp<=0) {success=
false;
return success;}
laplace_approximation_calculator * lapprox
dvector log10(const dvector &vec)
Returns dvector with the common (base-10) logarithm of vec.
d3_array elem_prod(const d3_array &a, const d3_array &b)
Returns d3_array results with computed elements product of a(i, j, k) * b(i, j, k).
dvector rotate_pars(const dvector &m, const dvector &x)
void read_mle_hmc(int nvar, dvector &mle)
Vector of double precision numbers.
bool stop_criterion(int nvar, dvector &thetaminus, dvector &thetaplus, dvector &rminus, dvector &rplus)
int indexmin() const
Get minimum valid index.
double sum(const d3_array &darray)
Author: David Fournier Copyright (c) 2008-2012 Regents of the University of California.
int compute_next_window(int i, int warmup, int w1, int aws, int w3)
void print_mcmc_progress(int is, int nmcmc, int nwarmup, int chain, int refresh)
bool choleski_decomp_hmc(const dmatrix &metric, dmatrix &L)
static dvariable reset(const dvar_vector &x)
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.
unsigned int colsize() const
dvector rotate_gradient(const dvector &x, const dmatrix &m)
void gradcalc(int nvar, const dvector &g)
double find_reasonable_stepsize(int nvar, dvector y, dvector p, dmatrix &chd, bool verbose_adapt_mass, bool verbose_find_epsilon, int chain)
dvector extract_column(const dmatrix &matrix, int j)
Extract copy of jth column vector from matrix m.
static int stddev_vscale(const dvar_vector &d, const dvar_vector &x)
bool calculate_chd_and_inverse(int nvar, const dmatrix &metric, dmatrix &chd, dmatrix &chdinv)
Calculate the Cholesky decomposition and its inverse given a mass matrix.
double randu(const random_number_generator &rng)
Uniform random number generator.
Description not yet available.
prnstream & endl(prnstream &)
Description not yet available.
d3_array sqrt(const d3_array &arr3)
Author: David Fournier Copyright (c) 2008-2012 Regents of the University of California.
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.
int indexmax() const
Get maximum valid index.
static objective_function_value * pobjfun
Description not yet available.
d3_array exp(const d3_array &arr3)
Returns d3_array results with computed exp from elements in arr3.
dmatrix outer_prod(const dvector &v1, const dvector &v2)
Description not yet available.
Description not yet available.
double norm2(const d3_array &a)
Return sum of squared elements in a.
static void copy_all_values(const dvector &x, const int &ii)
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)
bool slow_phase(int is, int warmup, int w1, int w3)
void rowshift(int min)
Changes the range of valid indices for the rows.
size_t pos(const adstring &substr, const adstring &s)
void colshift(int min)
Description not yet available.
unsigned int rowsize() const
Description not yet available.
dvector column(const dmatrix &matrix, int j)
Author: David Fournier Copyright (c) 2008-2012 Regents of the University of California.
dvector value(const df1_one_vector &v)
virtual void userfunction(void)=0
std::string get_filename(const char *f)
void add_sample_dense(const int nvar, int &is2, dvector &m, dmatrix &m2, const independent_variables &q)
Calculate running covariance using Welford's "online" algorithm.
void initialize(void)
Author: David Fournier Copyright (c) 2008-2012 Regents of the University of California.
void build_tree(int nvar, dvector &gr, dmatrix &chd, double eps, dvector &p, dvector &y, dvector &gr2, double logu, int v, int j, double H0, dvector &_thetaprime, dvector &_thetaplus, dvector &_thetaminus, dvector &_rplus, dvector &_rminus, double &_alphaprime, int &_nalphaprime, bool &_sprime, int &_nprime, int &_nfevals, bool &_divergent, const random_number_generator &rng, dvector &gr2_end, dvector &_grprime, dvector &_gr2prime, double &_nllprime, double &_Hprime, independent_variables &_parsaveprime)
Fundamental data type for reverse mode automatic differentiation.
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.
void add_sample_diag(const int nvar, int &n, dvector &m, dvector &m2, const independent_variables &q)
Calculate running covariance using Welford's "online" algorithm.
d3_array pow(const d3_array &m, int e)
Description not yet available.