stan-dev / stan-dev/rstan

tol_rel_obj doesn't seem to work in optimizing()

Open
#881 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
R
Stars
1.1k
Forks
266
Avg merge
2h 56m
Merged PRs (30d)
1

Description

Summary:

tol_rel_obj not terminating when I think it should.

Description:

Unless I set tol_rel_obj to very high values, it does not cause convergence.

Reproducible Steps:

Should be evident in any optimization model. Here's what I get:

> model <- stan_model(...)
> fit_init <- optimizing(model, data = stan_data_tasics,
                       init = 0.1,
		       tol_rel_obj = 1e-1, tol_obj = 0.01e7,
		       tol_rel_grad = 1e1, tol_grad = 1e1,
                       tol_param = 1e-2,
		       iter = 1000,
		       history_size = 10,
                       as_vector = FALSE,
                       verbose = TRUE)
...
Chain 1:       89  -4.86797e+07       11.8801        105693      0.5518      0.5518      164   
Chain 1:     Iter      log prob        ||dx||      ||grad||       alpha      alpha0  # evals  Notes 
Chain 1:       99  -4.66481e+07       21.5455        110251           1           1      174   
Chain 1:     Iter      log prob        ||dx||      ||grad||       alpha      alpha0  # evals  Notes 
Chain 1:      104  -4.58516e+07       4.36499         75484      0.8113      0.8113      179   
Chain 1: Optimization terminated normally: 
Chain 1:   Convergence detected: absolute change in objective function was below tolerance

So it seems like tol_obj is working as expected.

But if I set tol_obj so that doesn't cause convergence, then I don't get termination until it's well beyond the relative tolerance I set:

fit_init <- optimizing(model, data = stan_data_tasics,
                       init = 0.1,
		       tol_rel_obj = 1e10, tol_obj = 1e-10,
		       tol_rel_grad = 1e1, tol_grad = 1e1,
                       tol_param = 1e-2,
		       iter = 1000,
		       history_size = 10,
                       as_vector = FALSE,
                       verbose = TRUE)
...
Chain 1:      559  -3.72821e+07       2.68462       5413.19           1           1      678   
Chain 1:     Iter      log prob        ||dx||      ||grad||       alpha      alpha0  # evals  Notes 
Chain 1:      569  -3.72777e+07      0.922598       5027.67      0.7192      0.7192      688   
Chain 1:     Iter      log prob        ||dx||      ||grad||       alpha      alpha0  # evals  Notes 
Chain 1:      579  -3.72737e+07      0.177795       3061.21           1           1      699   
Chain 1:     Iter      log prob        ||dx||      ||grad||       alpha      alpha0  # evals  Notes 
Chain 1:      589  -3.72697e+07      0.155879       3135.34      0.8214      0.8214      710   
Chain 1:     Iter      log prob        ||dx||      ||grad||       alpha      alpha0  # evals  Notes 
Chain 1:      599  -3.72663e+07      0.884956       3098.07      0.8752      0.8752      720   
Chain 1:     Iter      log prob        ||dx||      ||grad||       alpha      alpha0  # evals  Notes 
Chain 1:      602  -3.72657e+07       2.57617       9432.57           1           1      723   
Chain 1: Optimization terminated normally: 
Chain 1:   Convergence detected: relative change in objective function was below tolerance

If I set tol_rel_obj really high, like to 1e100, it terminates very fast.

Apologies in advance if this is just be some rolling average calculation or something I don't understand.

As an aside, it would help immensely if the as_vector = FALSE option in the doc explained that you get the results in structured form.

sessionInfo()
> sessionInfo()
R version 4.0.1 (2020-06-06)
Platform: x86_64-apple-darwin17.0 (64-bit)
Running under: macOS Catalina 10.15.7

Matrix products: default
BLAS:   /Library/Frameworks/R.framework/Versions/4.0/Resources/lib/libRblas.dylib
LAPACK: /Library/Frameworks/R.framework/Versions/4.0/Resources/lib/libRlapack.dylib

locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

attached base packages:
[1] tools     stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] rstan_2.19.3         StanHeaders_2.21.0-5 ggpubr_0.4.0         ggplot2_3.3.2       

loaded via a namespace (and not attached):
 [1] tidyselect_1.1.0   purrr_0.3.4        haven_2.3.1        lattice_0.20-41    carData_3.0-4      colorspace_1.4-1   vctrs_0.3.1       
 [8] generics_0.0.2     stats4_4.0.1       loo_2.2.0          rlang_0.4.6        pkgbuild_1.0.8     pillar_1.4.4       foreign_0.8-80    
[15] glue_1.4.1         withr_2.2.0        readxl_1.3.1       matrixStats_0.56.0 lifecycle_0.2.0    munsell_0.5.0      ggsignif_0.6.0    
[22] gtable_0.3.0       cellranger_1.1.0   zip_2.0.4          codetools_0.2-16   labeling_0.3       inline_0.3.15      rio_0.5.16        
[29] forcats_0.5.0      callr_3.4.3        ps_1.3.3           parallel_4.0.1     curl_4.3           fansi_0.4.1        broom_0.5.6       
[36] Rcpp_1.0.4.6       backports_1.1.8    scales_1.1.1       RcppParallel_5.0.2 abind_1.4-5        farver_2.0.3       gridExtra_2.3     
[43] digest_0.6.25      hms_0.5.3          stringi_1.4.6      openxlsx_4.1.5     rstatix_0.6.0      processx_3.4.2     dplyr_1.0.0       
[50] grid_4.0.1         cli_2.0.2          magrittr_1.5       tibble_3.0.1       crayon_1.3.4       car_3.0-9          tidyr_1.1.0       
[57] pkgconfig_2.0.3    ellipsis_0.3.1     data.table_1.13.0  prettyunits_1.1.1  assertthat_0.2.1   R6_2.4.1           nlme_3.1-148      
[64] compiler_4.0.1    

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the two optimizing() calls in the issue and then trace the optimizing() implementation where tol_rel_obj and tol_obj are handled. Done means explaining whether the relative tolerance is evaluated as intended and documenting or correcting the behavior, with the as_vector documentation aside addressed if it belongs to this issue.

Written by the indexing model from the issue text.

Assessment

Tech stack
r
Domain
api
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.