60 if (_N_hess_steps<=0) {
61 cerr <<
"Error: hess_step tolerance must be positive";
64 N_hess_steps=_N_hess_steps;
72 cout <<
"No tolerance given, using default of 1e-12" <<
endl;
77 cerr <<
"Error: hess_step tolerance must be positive";
84 cout <<
endl <<
"Experimental feature to take up to " << N_hess_steps;
85 cout <<
" Newton step(s) using the inverse Hessian" <<
endl <<
endl;
92 if(biopt==-1 && aiopt==-1){
94 cout <<
"Warning: Parameter inputs via '-ainp' and '-binp' were not detected." <<
endl;
95 cout <<
" It is recommended to add option '-binp " << m <<
".bar' when using -hess_step." <<
endl;
96 cout <<
" Otherwise inactive parameters (and thus gradients) may not initialize correctly." << endl <<
endl;
112 double maxgrad0, mingrad0, maxgrad2, mingrad2;
133 for (
int i = 1; i<=nvar; i++){
134 if (
fabs(gr0[i]) > grMax){
135 grMax =
fabs(gr0[i]);
141 cout <<
"Initial max gradient already below threshold of " << eps <<
" so quitting now" <<
endl;
145 cout <<
"Hess step 0: Max gradient=" << maxgrad0 <<
" (" << pars[maxpar] <<
") and min gradient= " << mingrad0 <<
endl;
154 for(
int ii=1; ii<=N_hess_steps; ii++){
170 maxpar=1; grMax=
fabs(gr2[1]);
171 for (
int i = 1; i<=nvar; i++){
172 if (
fabs(gr2[i]) > grMax){
173 grMax =
fabs(gr2[i]);
177 if(grMax!=maxgrad2) cerr <<
"Warning: which parameter has largest gradient may be unreliable" <<
endl;
182 cout <<
"Hess step " << ii <<
": Max gradient="<< maxgrad2 <<
" (" << pars[maxpar] <<
183 ") is below threshold of " << eps <<
" so exiting early" <<
endl;
187 cout <<
"Hess step " << ii <<
": Max gradient=" << maxgrad2 <<
" (" << pars[maxpar] <<
188 ") and min gradient= " << mingrad2 <<
endl;
194 if(ii!=N_hess_steps){
197 cout <<
" Updating Hessian for next step (output suppressed)...";
202 cout <<
"done" <<
endl;
206 if(maxgrad2>maxgrad0){
208 cerr <<
"Experimental feature -hess_step resulted in worse gradients." << endl <<
209 "Consider reoptimizing model to reset files. This suggests that the" << endl <<
210 "the negative log-likelihood is not quadratic at the mode, or that the" << endl <<
211 "Hessian does not approximate it well. In short, it suggests the model is" << endl <<
212 "not converged. Check model structure and other output files to investigate" << endl <<
213 "potential causes and solutions." <<
endl;
220 cout << endl <<
"Redoing uncertainty calculations and updating files (output suppressed)...";
225 cout <<
" done" <<
endl;
226 cout << endl <<
"The " << Nstep <<
" Hessian step(s) reduced maxgrad from " <<
227 maxgrad0 <<
" to " << maxgrad2 <<
" and NLL by " << nll-nll2 <<
"." << endl <<
228 "All output files should be updated, but confirm as this is experimental still." << endl <<
229 "The fact this was successful gives strong evidence of convergence to a mode" << endl <<
230 "with quadratic log-likelihood surface." <<
endl;
243 if (
debug) cout<<
"--num active params = "<<nvar<<
endl;
254 for (
int j = 1; j <= jmax; ++j) {
256 par_names[kk] = varptr->
label();
257 if (jmax > 1) par_names[kk] +=
"["+
str(j)+
"]";
261 if (nvar!=kk) cerr<<
"Error in initial_params::get_param_names: number of parameters does not match"<<
endl;
263 if (
debug) cout<<
"--initial_params::get_param_names(): Model appears to be autodiff only."<<
endl;
276 const std::type_info&
id =
typeid(*p);
277 if (
id == typeid_param_init_bounded_number
278 ||
id == typeid_param_init_bounded_vector
279 ||
id == typeid_param_init_bounded_dev_vector
280 ||
id == typeid_param_init_bounded_matrix)
297 if (
debug) os<<
"--num active params = "<<nvar<<
endl;
308 if (
debug) os<<
"----"<<par_name_base<<
" is active, bounded, and will be checked."<<
endl;
311 if (dynamic_cast<param_init_bounded_number*>(varptr) !=
nullptr){
312 if (
debug) os<<
" "<<par_name_base<<
" is a param_init_bounded_number."<<
endl;
318 if (
debug) os<<
" "<<par_name<<
": "<<minb<<
" < "<<valp<<
" < "<<maxb<<
"?"<<
endl;
319 if ((valp-minb)/(maxb-minb)<0.001) {
320 if(counter==0){os <<
"Warning: the following parameters had issues" <<
endl; counter++;}
321 os<<
" "<<par_name<<
" is within 0.1\% of lower bound: "<<minb<<
" < "<<valp<<
" < "<<maxb<<
endl;
322 }
else if ((valp-minb)/(maxb-minb)<0.01) {
323 if(counter==0){os <<
"Warning: the following parameters had issues" <<
endl; counter++;}
324 os<<
" "<<par_name<<
" is within 1% of lower bound: "<<minb<<
" < "<<valp<<
" < "<<maxb<<
endl;
326 if ((maxb-valp)/(maxb-minb)<0.001) {
327 if(counter==0){os <<
"Warning: the following parameters had issues" <<
endl; counter++;}
328 os<<
" "<<par_name<<
" is within 0.1\% of upper bound: "<<minb<<
" < "<<valp<<
" < "<<maxb<<
endl;
329 }
else if ((maxb-valp)/(maxb-minb)<0.01) {
330 if(counter==0){os <<
"Warning: the following parameters had issues" <<
endl; counter++;}
331 os<<
" "<<par_name<<
" is within 1\% of upper bound: "<<minb<<
" < "<<valp<<
" < "<<maxb<<
endl;
333 }
else if (dynamic_cast<param_init_bounded_dev_vector*>(varptr) !=
nullptr) {
334 if (
debug) os<<
" "<<par_name_base<<
" is a param_init_bounded_dev_vector with "<<jmax<<
" elements."<<
endl;
341 if (
debug) os<<
" "<<par_name<<
": "<<minb<<
" < "<<valp<<
" < "<<maxb<<
"?"<<
endl;
342 if ((valp-minb)/(maxb-minb)<0.001) {
343 if(counter==0){os <<
"Warning: the following parameters had issues" <<
endl; counter++;}
344 os<<
" "<<par_name<<
" is within 0.1\% of lower bound: "<<minb<<
" < "<<valp<<
" < "<<maxb<<
endl;
345 }
else if ((valp-minb)/(maxb-minb)<0.01) {
346 if(counter==0){os <<
"Warning: the following parameters had issues" <<
endl; counter++;}
347 os<<
" "<<par_name<<
" is within 1% of lower bound: "<<minb<<
" < "<<valp<<
" < "<<maxb<<
endl;
349 if ((maxb-valp)/(maxb-minb)<0.001) {
350 if(counter==0){os <<
"Warning: the following parameters had issues" <<
endl; counter++;}
351 os<<
" "<<par_name<<
" is within 0.1\% of upper bound: "<<minb<<
" < "<<valp<<
" < "<<maxb<<
endl;
352 }
else if ((maxb-valp)/(maxb-minb)<0.01) {
353 if(counter==0){os <<
"Warning: the following parameters had issues" <<
endl; counter++;}
354 os<<
" "<<par_name<<
" is within 1\% of upper bound: "<<minb<<
" < "<<valp<<
" < "<<maxb<<
endl;
357 }
else if (dynamic_cast<param_init_bounded_vector*>(varptr) !=
nullptr) {
358 if (
debug) os<<
" "<<par_name_base<<
" is a param_init_bounded_vector with "<<jmax<<
" elements."<<
endl;
365 if (
debug) os<<
" "<<par_name<<
": "<<minb<<
" < "<<valp<<
" < "<<maxb<<
"?"<<
endl;
366 if ((valp-minb)/(maxb-minb)<0.001) {
367 if(counter==0){os <<
"Warning: the following parameters had issues" <<
endl; counter++;}
368 os<<
" "<<par_name<<
" is within 0.1\% of lower bound: "<<minb<<
" < "<<valp<<
" < "<<maxb<<
endl;
369 }
else if ((valp-minb)/(maxb-minb)<0.01) {
370 if(counter==0){os <<
"Warning: the following parameters had issues" <<
endl; counter++;}
371 os<<
" "<<par_name<<
" is within 1% of lower bound: "<<minb<<
" < "<<valp<<
" < "<<maxb<<
endl;
373 if ((maxb-valp)/(maxb-minb)<0.001) {
374 if(counter==0){os <<
"Warning: the following parameters had issues" <<
endl; counter++;}
375 os<<
" "<<par_name<<
" is within 0.1\% of upper bound: "<<minb<<
" < "<<valp<<
" < "<<maxb<<
endl;
376 }
else if ((maxb-valp)/(maxb-minb)<0.01) {
377 if(counter==0){os <<
"Warning: the following parameters had issues" <<
endl; counter++;}
378 os<<
" "<<par_name<<
" is within 1\% of upper bound: "<<minb<<
" < "<<valp<<
" < "<<maxb<<
endl;
381 }
else if (dynamic_cast<param_init_bounded_matrix*>(varptr)!=
nullptr) {
383 if (
debug) os<<
" "<<par_name_base<<
" is a param_init_bounded_matrix with "<<jmax<<
" elements."<<
endl;
391 if (
debug) os<<
" "<<par_name<<
": "<<minb<<
" < "<<valp<<
" < "<<maxb<<
"?"<<
endl;
392 if ((valp-minb)/(maxb-minb)<0.001) {
393 if(counter==0){os <<
"Warning: the following parameters had issues" <<
endl; counter++;}
394 os<<
" "<<par_name<<
" is within 0.1\% of lower bound: "<<minb<<
" < "<<valp<<
" < "<<maxb<<
endl;
395 }
else if ((valp-minb)/(maxb-minb)<0.01) {
396 if(counter==0){os <<
"Warning: the following parameters had issues" <<
endl; counter++;}
397 os<<
" "<<par_name<<
" is within 1% of lower bound: "<<minb<<
" < "<<valp<<
" < "<<maxb<<
endl;
399 if ((maxb-valp)/(maxb-minb)<0.001) {
400 if(counter==0){os <<
"Warning: the following parameters had issues" <<
endl; counter++;}
401 os<<
" "<<par_name<<
" is within 0.1\% of upper bound: "<<minb<<
" < "<<valp<<
" < "<<maxb<<
endl;
402 }
else if ((maxb-valp)/(maxb-minb)<0.01) {
403 if(counter==0){os <<
"Warning: the following parameters had issues" <<
endl; counter++;}
404 os<<
" "<<par_name<<
" is within 1\% of upper bound: "<<minb<<
" < "<<valp<<
" < "<<maxb<<
endl;
410 if (
debug) os<<
"----"<<par_name_base<<
" is active but not bounded and will not be checked."<<
endl;
413 if (
debug) os<<
"--"<<par_name_base<<
" is inactive and will not be checked for bounds."<<
endl;
417 if (
debug) os<<
"--Model appears to be autodiff only: no bounds checking performed."<<
endl;
419 if (
debug) os<<
"Finished initial_params::check_parameters_on_bounds"<<
endl;
420 if (counter > 0) os <<
endl;
bool hess_inv()
Symmetrize and invert the hessian.
void computations1(int argc, char *argv[])
static void restore_all_values(const dvector &x, const int &ii)
dvar_vector & elem(int i)
virtual unsigned int size_count() const =0
adstring_array get_param_names()
Get names of active parameters.
int withinbound(int lb, int n, int ub)
void read_mle_hmc(int nvar, dvector &mle)
Vector of double precision numbers.
double get_maxb() const
Returns maximum bounds value for param_init_bounded_vector.
Description not yet available.
df1_two_variable fabs(const df1_two_variable &x)
static dvariable reset(const dvar_vector &x)
double get_maxb() const
Returns maximum bounds value for param_init_bounded_matrix.
virtual const char * label()=0
Description not yet available.
static adstring adprogram_name
double get_maxb() const
Returns maximum bounds value for param_init_bounded_number.
void gradcalc(int nvar, const dvector &g)
Description not yet available.
Description not yet available.
void allocate(int min, int max)
Allocate vector of adstring with range [min, max].
prnstream & endl(prnstream &)
Description not yet available.
static int max_number_phases
static objective_function_value * pobjfun
Description not yet available.
static void xinit(const dvector &x)
Description not yet available.
void read_covariance_matrix(const dmatrix &S, int nvar, int &hbf, dvector &sscale)
static int num_initial_params
static void copy_all_values(const dvector &x, const int &ii)
int option_match(int argc, char *argv[], const char *string)
Checks if the program has been invoked with a particular command line argument ("string").
adstring str(double x, int minwidth=17, int decplaces=-1)
Convert x to adstring with minimum width and total number of decimal places.
Description not yet available.
bool has_bounds(initial_params *p)
void hess_step()
hess_step is used for HMC. See details in function_minimizer::hess_step.
static void set_YES_DERIVATIVES(void)
Enable accumulation of derivative information.
dvector value(const df1_one_vector &v)
virtual void userfunction(void)=0
void check_for_params_on_bounds(ostream &os)
check for active parameters near bounds
std::string get_filename(const char *f)
void depvars_routine(void)
Calculate the derivatives of dependent variables with respect to the independent variables.
Fundamental data type for reverse mode automatic differentiation.
static adlist_ptr varsptr