In reference to https://github.com/stan-dev/stan/issues/2511#issuecomment-378731571.
The source files are available at https://ro-che.info/files/2018-04-03-stan-optimization-issue/.
library(rstan)
library(purrr)
library(reshape2)
library(dplyr)
avg_model <- stan_model("avg.stan", auto_write = T)
infer_std <- function()
optimizing(avg_model, list(X=c(0.3,0.5), sigma=1e-4))$par
infer_no_tol <- function()
optimizing(avg_model, list(X=c(0.3,0.5), sigma=1e-4), tol_grad=-1, tol_rel_grad=-1, tol_obj=-1, tol_rel_obj=-1)$par
infer_fns <- list(infer_std=infer_std, infer_no_tol=infer_no_tol)
n <- 1000
results <- map_dfr(names(infer_fns), function(infer_name) {
map_dfr(seq_len(n), function(i) {
val <- infer_fns[[infer_name]]()[["mu"]]
data_frame(val=val, fn=infer_name)
})})
## Warning in .local(object, ...): non-zero return code in optimizing
## Warning in .local(object, ...): non-zero return code in optimizing
## Warning in .local(object, ...): non-zero return code in optimizing
## Warning in .local(object, ...): non-zero return code in optimizing
## Warning in .local(object, ...): non-zero return code in optimizing
## Warning in .local(object, ...): non-zero return code in optimizing
## Warning in .local(object, ...): non-zero return code in optimizing
## Warning in .local(object, ...): non-zero return code in optimizing
## Warning in .local(object, ...): non-zero return code in optimizing
## Warning in .local(object, ...): non-zero return code in optimizing
## Warning in .local(object, ...): non-zero return code in optimizing
## Warning in .local(object, ...): non-zero return code in optimizing
## Warning in .local(object, ...): non-zero return code in optimizing
## Warning in .local(object, ...): non-zero return code in optimizing
## Warning in .local(object, ...): non-zero return code in optimizing
## Warning in .local(object, ...): non-zero return code in optimizing
## Warning in .local(object, ...): non-zero return code in optimizing
## Warning in .local(object, ...): non-zero return code in optimizing
## Warning in .local(object, ...): non-zero return code in optimizing
## Warning in .local(object, ...): non-zero return code in optimizing
## Warning in .local(object, ...): non-zero return code in optimizing
## Warning in .local(object, ...): non-zero return code in optimizing
## Warning in .local(object, ...): non-zero return code in optimizing
## Warning in .local(object, ...): non-zero return code in optimizing
## Warning in .local(object, ...): non-zero return code in optimizing
## Warning in .local(object, ...): non-zero return code in optimizing
## Warning in .local(object, ...): non-zero return code in optimizing
## Warning in .local(object, ...): non-zero return code in optimizing
## Warning in .local(object, ...): non-zero return code in optimizing
(summarized_results <- results %>% group_by(fn) %>% summarize(correct=sum(abs(val-0.4) < 0.1)))
## # A tibble: 2 x 2
## fn correct
## <chr> <int>
## 1 infer_no_tol 426
## 2 infer_std 434
prop.test(summarized_results$correct, rep(n,2))
##
## 2-sample test for equality of proportions with continuity
## correction
##
## data: summarized_results$correct out of rep(n, 2)
## X-squared = 0.099959, df = 1, p-value = 0.7519
## alternative hypothesis: two.sided
## 95 percent confidence interval:
## -0.05239309 0.03639309
## sample estimates:
## prop 1 prop 2
## 0.426 0.434
sessionInfo()
## R version 3.4.3 (2017-11-30)
## Platform: x86_64-redhat-linux-gnu (64-bit)
## Running under: Fedora 27 (Workstation Edition)
##
## Matrix products: default
## BLAS/LAPACK: /usr/lib64/R/lib/libRblas.so
##
## locale:
## [1] LC_CTYPE=ru_RU.UTF-8 LC_NUMERIC=C
## [3] LC_TIME=en_DK.UTF-8 LC_COLLATE=ru_RU.UTF-8
## [5] LC_MONETARY=en_US.UTF-8 LC_MESSAGES=en_US.UTF-8
## [7] LC_PAPER=en_US.UTF-8 LC_NAME=C
## [9] LC_ADDRESS=C LC_TELEPHONE=C
## [11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C
##
## attached base packages:
## [1] stats graphics grDevices utils datasets base
##
## other attached packages:
## [1] bindrcpp_0.2 dplyr_0.7.4 reshape2_1.4.2
## [4] purrr_0.2.4 rstan_2.16.2 StanHeaders_2.16.0-1
## [7] ggplot2_2.2.1 knitr_1.20
##
## loaded via a namespace (and not attached):
## [1] Rcpp_0.12.13 bindr_0.1 magrittr_1.5 munsell_0.4.3
## [5] colorspace_1.3-2 R6_2.2.2 rlang_0.1.4 stringr_1.3.0
## [9] plyr_1.8.4 tools_3.4.3 grid_3.4.3 gtable_0.2.0
## [13] htmltools_0.3.6 assertthat_0.2.0 lazyeval_0.2.0 rprojroot_1.3-2
## [17] digest_0.6.12 tibble_1.3.4 gridExtra_2.3 codetools_0.2-15
## [21] glue_1.2.0 inline_0.3.14 evaluate_0.10.1 rmarkdown_1.9.7
## [25] stringi_1.1.7 compiler_3.4.3 methods_3.4.3 scales_0.5.0
## [29] backports_1.1.2 stats4_3.4.3 pkgconfig_2.0.1