Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

apply self-calib to precondition prior #52

Open
wants to merge 171 commits into
base: advi_vignette
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
171 commits
Select commit Hold shift + click to select a range
d42e8a0
apply self-calib to precondition prior
Sep 11, 2021
978aca4
Add two dimensional kernel fitting without correlation
Sep 12, 2021
18acf48
Create two_normal_hp.stan
hyunjimoon Sep 13, 2021
84d53c3
Update two_normal_hp.stan
hyunjimoon Sep 13, 2021
8b6e8f0
Create self-calibration.rmd
hyunjimoon Sep 15, 2021
6bf9356
Create gamma-reg.stan
hyunjimoon Sep 15, 2021
849f75c
Update self-calib.R
hyunjimoon Sep 15, 2021
20d43ed
Update self-calibration.rmd
hyunjimoon Sep 15, 2021
9ea6087
generator, stan for gamma regression
Sep 16, 2021
70e3b5f
Update self-calib
Dashadower Sep 16, 2021
e4088b4
dynamic S and kde preparation
Sep 18, 2021
9b7033f
Setting up
martinmodrak Sep 18, 2021
0550c5c
Add GMM priors.
Dashadower Sep 18, 2021
64f8c3b
Fix density calculation
Dashadower Sep 19, 2021
f961e3c
Rewrite self_calib
Dashadower Sep 24, 2021
66d9d71
Rewrite self_calib - upload omitted files
Dashadower Sep 24, 2021
4b13035
implement hyperparameter update
Sep 26, 2021
eee3f26
implement gmm fitting
Sep 26, 2021
9c7fcd6
Change self-calib to use posterior::draws_rvars
Dashadower Sep 26, 2021
ce8744a
Update calib documentation
Dashadower Sep 26, 2021
e781a89
Update self-calib
Dashadower Sep 26, 2021
505ebc9
Fix error catching
Dashadower Sep 26, 2021
80b3a68
examples with improved ranks plots after self-calibration for both hm…
Sep 26, 2021
a841ec0
getting the nsims input
Sep 27, 2021
a8064af
Add tentative eightschools notebook
Dashadower Oct 3, 2021
be84b0b
Fix critical typo in self_calib
Dashadower Oct 3, 2021
e71403b
Minor changes to vignette
Dashadower Oct 4, 2021
f65808b
Update backends.R
hyunjimoon Oct 4, 2021
ba3aa17
calibrate logistic with laplace approximation
Oct 4, 2021
8970052
fix each in rvar error and change transform type
Oct 4, 2021
ce6ed44
converging normal and gmm inference with binomial likelihood in logis…
Oct 6, 2021
2939fea
Reweight mixture_mean with probability returned from mclust GMM. Add …
Dashadower Oct 11, 2021
4701540
Fix issue with documentation
Dashadower Oct 12, 2021
c12386d
Super tiny changes to documentation
Dashadower Oct 12, 2021
8d9caf4
Update iter-binom-logistic.Rmd
hyunjimoon Oct 14, 2021
47e13ce
change prior to N(0,10^2), N(1, 1^2)
hyunjimoon Oct 14, 2021
638ec47
log for the first two experiments
hyunjimoon Oct 14, 2021
9a26459
bug fix from sample 1 mean then draw 30 to sample 30 means then one d…
Oct 14, 2021
a946810
apply gradient algorithm to binomial logistic
Oct 14, 2021
8e8ec9b
Literally a single character bugfix
Dashadower Oct 14, 2021
3f56104
debug needed for quantile loss gradient update
hyunjimoon Oct 14, 2021
376e648
Minor signature updates
Dashadower Oct 15, 2021
4a3fb34
Experimental change to gradient calculation
Dashadower Oct 15, 2021
29b6dfd
Some more changes to self-calib which don't work atm
Dashadower Oct 16, 2021
07aad3f
observe periodicity
Oct 16, 2021
40687f9
Update update equation for updated update performance :)
Dashadower Oct 20, 2021
9d69502
separate inverselogit-cloglog-quantile
Oct 21, 2021
3f4374d
add link function and fixed iteration algorithm experiment 3
Oct 27, 2021
85a6fb2
two data-averaged posteriors
Oct 28, 2021
ec4bc9e
Intermediate commit to save progress and merge new functions
Dashadower Nov 4, 2021
3beff56
Update gridsearch to use sensitivity
Dashadower Nov 4, 2021
d113c05
'Adaptive' self calib implementation, unfinished(sort of)
Dashadower Nov 8, 2021
f2b5934
Changes to rsensitivity
Dashadower Nov 8, 2021
d8cf682
first running self-calib-adaptive
Nov 9, 2021
01d2da9
lambda eta loss compare
Nov 11, 2021
bacd7e4
rstan::optimizing backend implementation
Dashadower Nov 12, 2021
dd032cf
Merge branch 'rstan_optiming_backend' into advi_self-calib
Dashadower Nov 12, 2021
06a9f17
vignette update
Nov 12, 2021
99c3ae8
merge resolve for optimizing
Nov 12, 2021
526d9a4
self-calib-adaptive
Nov 13, 2021
f792898
Fix update_quantile
Dashadower Nov 13, 2021
7765685
change name as logsigma
Nov 14, 2021
6a4c57f
Fix lambda_current to return named list
Dashadower Nov 14, 2021
aadc9e5
Test code for heuristic update
Dashadower Nov 17, 2021
99b9f25
Add heuristic_cubic_update to self-calib for testing
Dashadower Nov 19, 2021
0be63f5
Minor tweak
Dashadower Nov 19, 2021
56edbfa
Minor tweak 2
Dashadower Nov 19, 2021
ba1e7aa
Calculate displayed loss with new lambda values
Dashadower Nov 19, 2021
cff5a2f
revert loss calculation
Dashadower Nov 19, 2021
8cd7bd4
upload vignette and a few plots
Dashadower Nov 19, 2021
37f3bee
Change heuristic_cubic to use cubic update for sigma too
Dashadower Nov 19, 2021
3f27b1e
provable update based on normal distribution's additive structure
Nov 20, 2021
f43b674
exponentiate sigma when calculating lambda_loss
Dashadower Nov 21, 2021
b956ae7
Add normal KL divergence
Dashadower Nov 21, 2021
3ea9a7f
working binom and half-working gamma regression
Nov 21, 2021
b45ea2f
update normal kl
Nov 21, 2021
d9abaa1
dap inspection
Nov 25, 2021
159c0f2
update to scatter plot
Nov 25, 2021
b1f9016
Add eightschools model
Dashadower Nov 28, 2021
43d623d
Change tau to log(tau) in eightschools model self-calib adaptive. Add…
Dashadower Nov 28, 2021
1834c87
Change self_calib_adaptive to use gamma
Dashadower Dec 5, 2021
50bd95d
Split self-calib-adaptive.Rmd. Change self-calib to support generaliz…
Dashadower Dec 5, 2021
9c30735
Updates to self-calib.R and vignettes
Dashadower Dec 6, 2021
e32899c
Update self-calib and vignette
Dashadower Dec 13, 2021
a373e5e
Fixed typo bug in diagnostics
martinmodrak Dec 14, 2021
bced3cc
plot convex set of gamma
Dec 15, 2021
488354c
change backend and add hyperparameter-focus analysis chuck
Dec 15, 2021
8f29a12
create dap_gamma
Dec 15, 2021
daa17d3
decompose convex to real and calib loss
Dec 15, 2021
b55e733
Merge changes and update dap_gamma.rmd
Dashadower Dec 18, 2021
9549bf3
Add binomial optimization vignette
Dashadower Dec 20, 2021
1322201
Implement lognormal distribution
Dashadower Dec 20, 2021
2697bdc
Experimental grid optimization notebooks for binominal and gamma models
Dashadower Dec 20, 2021
d87af45
Update gamma optimization vignette
Dashadower Dec 23, 2021
214ca43
Update gamma optimizaton
Dashadower Dec 24, 2021
9b12173
Fix gradients
Dashadower Dec 25, 2021
187e751
WIP array-ified gradients
Dashadower Dec 25, 2021
2fc1d8c
optimization update
Dashadower Dec 27, 2021
766c650
Fixed bug in combining GQs
martinmodrak Dec 27, 2021
29b4325
Plot B and V
Dashadower Dec 30, 2021
d7c0cbd
Add prior-DAP parameter web plot
Dashadower Dec 31, 2021
139d824
Update B, V plot for binom
Dashadower Jan 1, 2022
4489fc0
fix B,V calculation and update plots
Dashadower Jan 2, 2022
83dd012
Update dap calculation and add eta simulation plot
Dashadower Jan 3, 2022
6abd8d2
Merge pull request #50 from hyunjimoon/advi_vignette
martinmodrak Jan 21, 2022
dcd48c1
Disambiguate "component" in small_model_workflow.Rmd
martinmodrak Jan 21, 2022
3644f11
Improve rstan support in vignettes
martinmodrak Jan 21, 2022
f5df1e8
Rename functions potentially conflicting with other packages
martinmodrak Jan 21, 2022
d1d8ca6
Merge branch 'master' into rstan_optiming_backend
martinmodrak Jan 21, 2022
d09283f
Added optimizing to vignette, tweaks and improvements
martinmodrak Jan 21, 2022
f1d72b0
Update DAP vignette
Dashadower Jan 22, 2022
0b859a7
Revert models, require rstan 2.26 for the vignette
martinmodrak Jan 22, 2022
cf41640
Rename vignette
martinmodrak Jan 22, 2022
55ba63d
Merge pull request #59 from hyunjimoon/rstan_optiming_backend
martinmodrak Jan 22, 2022
05ba463
Merge branch 'master' into naming_refactor
martinmodrak Jan 22, 2022
cc428d2
Cleared up datasets vs sim(ulation)s vs fits
martinmodrak Jan 22, 2022
519c16a
Cleared up samples vs. draws
martinmodrak Jan 22, 2022
5f845d2
Cleared up parameters vs variables
martinmodrak Jan 24, 2022
575263d
Updated vignettes to match
martinmodrak Jan 24, 2022
da9cdde
remove binaries
Dashadower Jan 25, 2022
a0ccb56
Update vignette
Dashadower Jan 25, 2022
17b1802
Merge branch 'master' into advi_self-calib
Dashadower Jan 25, 2022
fc6d69b
Update DAP binom vignette
Dashadower Jan 25, 2022
c67bec8
Detect silent errors, detect and recover from bad Hessian
martinmodrak Jan 26, 2022
99a9cf1
Merge branch 'master' into naming_refactor
martinmodrak Jan 26, 2022
d3b050f
Bumped version to 0.1.0.9000
martinmodrak Jan 26, 2022
3b230e3
Fixed backwards incompatibility in ECDF plots
martinmodrak Jan 26, 2022
5b70cb6
CmdStan version check in ADVI vignette
martinmodrak Jan 30, 2022
080d0ea
Add combined DAP-self_calib vignette
Dashadower Feb 4, 2022
bfe71e7
Upload DAP self-calib vignette
Dashadower Feb 7, 2022
8c1354e
Upload image
Dashadower Feb 7, 2022
e9d4c1d
Update DAP v ignette
Dashadower Feb 7, 2022
80b13d1
Update DAP vignette
Dashadower Feb 7, 2022
76facfe
Touches to docs site
martinmodrak Feb 7, 2022
cf91fde
Merge branch 'master' into naming_refactor
martinmodrak Feb 7, 2022
078f20c
Add built-in examples
martinmodrak Feb 7, 2022
0c6b398
Update README
martinmodrak Feb 7, 2022
d8822e4
update explanations in vignette
Feb 8, 2022
d2b1048
Document examples, tweak README
martinmodrak Feb 8, 2022
d63c03e
Bump version
martinmodrak Feb 8, 2022
ce6aaeb
Tweaks to docs
martinmodrak Feb 8, 2022
167b447
Merge branch 'master' into visualisations_vignette
martinmodrak Feb 8, 2022
063153e
Merge branch 'naming_refactor' into visualisations_vignette
martinmodrak Feb 9, 2022
5aa38ac
Add ensure_num_ranks_divisor
martinmodrak Feb 11, 2022
cac294b
Add plot_coverage_diff
martinmodrak Feb 11, 2022
77b8d20
Add mock-rng backend and example results for visualisation
martinmodrak Feb 11, 2022
19760e0
Rank visualisations vignette, tweaks to docs
martinmodrak Feb 11, 2022
91b5fe3
Merge branch 'master' into advi_self-calib
Dashadower Feb 11, 2022
cc40c74
Update DAP self calib vignette with explanation
Dashadower Feb 11, 2022
e2684b4
Tweaks to densities plots
martinmodrak Feb 12, 2022
ab8f74f
RJAGS backend working, vignette WIP
martinmodrak Feb 12, 2022
27022b2
Merge remote-tracking branch 'origin/naming_refactor' into advi_self-…
Dashadower Feb 13, 2022
ee83b30
Add marginalized version of the changepoint model in JAGS
martinmodrak Feb 13, 2022
0c2a537
Merge pull request #63 from hyunjimoon/naming_refactor
martinmodrak Feb 13, 2022
8386447
Merge pull request #65 from hyunjimoon/visualisations_vignette
martinmodrak Feb 13, 2022
0178a61
Merge pull request #66 from hyunjimoon/rjags-backend
martinmodrak Feb 13, 2022
eb6732c
Missing link to visualization vignette
martinmodrak Feb 13, 2022
32e1671
Version 0.1.1
martinmodrak Feb 13, 2022
b5db167
Update site
martinmodrak Feb 13, 2022
5004bff
Bumped to new development version
martinmodrak Feb 13, 2022
85a0896
Martin's acknowledgements for funding
martinmodrak Feb 17, 2022
7c35e59
Update site
martinmodrak Feb 17, 2022
8afa52b
Note the warnings in the xample
martinmodrak Feb 21, 2022
b5a97f8
Updated examples to include JAGS
martinmodrak Feb 28, 2022
bb25175
Updated docs
martinmodrak Feb 28, 2022
5b9d5fe
Update explanation regarding motivation
Dashadower Mar 6, 2022
f22ab85
update DAP vignette and eightschools model
Dashadower Mar 10, 2022
4208358
Merge branch 'master' into advi_self-calib
Dashadower Mar 10, 2022
0ea0806
Update DAP vignette and covert eightschools CP to NCP
Dashadower Mar 10, 2022
11c2521
Minor tweaks to DAP vignette
Dashadower Mar 10, 2022
255d06f
Backup DAP_self_calibration with eightschools model
Dashadower May 13, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
3 changes: 3 additions & 0 deletions .Rprofile
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
source("~/.Rprofile")
# Allows to change how all vignettes are run at once (especially to test rstan)
options("SBC.vignettes_cmdstanr" = TRUE)
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,5 @@ Meta
*.exe

*_SBC_cache
vignettes/stan/*.rds
vignettes/small_model_workflow/*.rds
19 changes: 14 additions & 5 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: SBC
Title: Simulation Based Calibration for rstan/cmdstanr models
Version: 0.0.1.9000
Version: 0.1.1.9000
Authors@R:
c(person(given = "Shinyoung",
family = "Kim",
Expand All @@ -16,10 +16,18 @@ Authors@R:
family = "Modrák",
role = c("aut"),
email = "[email protected]",
comment = c()))
Description: SBC helps perform Simulation Based Calibration on models using Stan or brms.
It contains various classes and functions to extract samples from models, calculate SBC metrics and visualize utility plots.
comment = c(ORCID= "0000-0002-8886-7797")),
person(given = "Teemu",
family = "Säilynoja",
role = "aut")
)
Description: SBC helps perform Simulation Based Calibration on Bayesian models.
SBC lets you check for bugs in your model code and/or algorithm that fits
the model. SBC focuses on models built with 'Stan' <https://mc-stan.org>,
but can support other modelling languages as well.
License: MIT + file LICENSE
URL: https://hyunjimoon.github.io/SBC/, https://github.com/hyunjimoon/SBC/
BugReports: https://github.com/hyunjimoon/SBC/issues
Encoding: UTF-8
LazyData: true
Roxygen: list(markdown = TRUE)
Expand All @@ -42,5 +50,6 @@ Suggests:
rstan,
knitr,
rmarkdown,
brms
brms,
mclust
VignetteBuilder: knitr
41 changes: 37 additions & 4 deletions NAMESPACE
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,32 @@ S3method(SBC_backend_default_thin_ranks,default)
S3method(SBC_backend_hash_for_cache,SBC_backend_brms)
S3method(SBC_backend_hash_for_cache,SBC_backend_cmdstan_sample)
S3method(SBC_backend_hash_for_cache,SBC_backend_cmdstan_variational)
S3method(SBC_backend_hash_for_cache,SBC_backend_rjags)
S3method(SBC_backend_hash_for_cache,SBC_backend_rstan_optimizing)
S3method(SBC_backend_hash_for_cache,SBC_backend_rstan_sample)
S3method(SBC_backend_hash_for_cache,default)
S3method(SBC_backend_iid_samples,SBC_backend_cmdstan_variational)
S3method(SBC_backend_iid_samples,default)
S3method(SBC_backend_iid_draws,SBC_backend_cmdstan_variational)
S3method(SBC_backend_iid_draws,SBC_backend_mock_rng)
S3method(SBC_backend_iid_draws,SBC_backend_rstan_optimizing)
S3method(SBC_backend_iid_draws,default)
S3method(SBC_fit,SBC_backend_brms)
S3method(SBC_fit,SBC_backend_cmdstan_sample)
S3method(SBC_fit,SBC_backend_cmdstan_variational)
S3method(SBC_fit,SBC_backend_mock)
S3method(SBC_fit,SBC_backend_mock_rng)
S3method(SBC_fit,SBC_backend_rjags)
S3method(SBC_fit,SBC_backend_rstan_optimizing)
S3method(SBC_fit,SBC_backend_rstan_sample)
S3method(SBC_fit_to_diagnostics,CmdStanMCMC)
S3method(SBC_fit_to_diagnostics,CmdStanVB)
S3method(SBC_fit_to_diagnostics,RStanOptimizingFit)
S3method(SBC_fit_to_diagnostics,brmsfit)
S3method(SBC_fit_to_diagnostics,default)
S3method(SBC_fit_to_diagnostics,stanfit)
S3method(SBC_fit_to_draws_matrix,CmdStanMCMC)
S3method(SBC_fit_to_draws_matrix,CmdStanVB)
S3method(SBC_fit_to_draws_matrix,RStanOptimizingFit)
S3method(SBC_fit_to_draws_matrix,SBC_rjags_fit)
S3method(SBC_fit_to_draws_matrix,brmsfit)
S3method(SBC_fit_to_draws_matrix,default)
S3method(check_all_SBC_diagnostics,SBC_results)
Expand All @@ -31,6 +41,8 @@ S3method(generate_datasets,SBC_generator_custom)
S3method(generate_datasets,SBC_generator_function)
S3method(get_diagnostic_messages,SBC_ADVI_diagnostics)
S3method(get_diagnostic_messages,SBC_ADVI_diagnostics_summary)
S3method(get_diagnostic_messages,SBC_RStanOptimizing_diagnostics)
S3method(get_diagnostic_messages,SBC_RStanOptimizing_diagnostics_summary)
S3method(get_diagnostic_messages,SBC_results)
S3method(get_diagnostic_messages,SBC_results_summary)
S3method(length,SBC_datasets)
Expand All @@ -39,13 +51,16 @@ S3method(plot_contraction,SBC_results)
S3method(plot_contraction,data.frame)
S3method(plot_coverage,SBC_results)
S3method(plot_coverage,data.frame)
S3method(plot_coverage_diff,SBC_results)
S3method(plot_coverage_diff,data.frame)
S3method(plot_rank_hist,SBC_results)
S3method(plot_rank_hist,data.frame)
S3method(plot_sim_estimated,SBC_results)
S3method(plot_sim_estimated,data.frame)
S3method(print,SBC_nuts_diagnostics_summary)
S3method(print,SBC_results_summary)
S3method(summary,SBC_ADVI_diagnostics)
S3method(summary,SBC_RStanOptimizing_diagnostics)
S3method(summary,SBC_nuts_diagnostics)
S3method(summary,SBC_results)
export(SBC_backend_brms)
Expand All @@ -54,25 +69,34 @@ export(SBC_backend_cmdstan_sample)
export(SBC_backend_cmdstan_variational)
export(SBC_backend_default_thin_ranks)
export(SBC_backend_hash_for_cache)
export(SBC_backend_iid_samples)
export(SBC_backend_iid_draws)
export(SBC_backend_mock)
export(SBC_backend_mock_rng)
export(SBC_backend_rjags)
export(SBC_backend_rstan_optimizing)
export(SBC_backend_rstan_sample)
export(SBC_datasets)
export(SBC_diagnostic_messages)
export(SBC_example_backend)
export(SBC_example_generator)
export(SBC_example_results)
export(SBC_fit)
export(SBC_fit_to_diagnostics)
export(SBC_fit_to_draws_matrix)
export(SBC_generator_brms)
export(SBC_generator_custom)
export(SBC_generator_function)
export(SBC_print_example_model)
export(SBC_results)
export(SBC_statistics_from_single_fit)
export(bind_datasets)
export(bind_results)
export(calculate_prior_sd)
export(calculate_ranks_draws_matrix)
export(calculate_sds_draws_matrix)
export(check_all_SBC_diagnostics)
export(cjs_dist)
export(compute_SBC)
export(compute_gen_quants)
export(compute_results)
export(data_for_ecdf_plots)
Expand All @@ -84,17 +108,26 @@ export(empirical_coverage)
export(generate_datasets)
export(generated_quantities)
export(get_diagnostic_messages)
export(invtf_param_vec)
export(max_diff)
export(plot_contraction)
export(plot_coverage)
export(plot_coverage_diff)
export(plot_ecdf)
export(plot_ecdf_diff)
export(plot_rank_hist)
export(plot_sim_estimated)
export(rank2unif)
export(recompute_SBC_statistics)
export(recompute_statistics)
export(rnorm_max_coupling)
export(self_calib_adaptive)
export(self_calib_gaussian)
export(self_calib_gmm)
export(set2set)
export(statistics_from_single_fit)
export(tf_param)
export(tf_param_vec)
export(update_quantile_approximation)
export(validate_SBC_datasets)
export(validate_SBC_results)
export(validate_generated_quantities)
Expand Down
8 changes: 8 additions & 0 deletions R/SBC-deprecated.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#' @title Deprecated functions in package \pkg{SBC}.
#' @description The functions listed below are deprecated and will be defunct in
#' the near future. When possible, alternative functions with similar
#' functionality are also mentioned. Help pages for deprecated functions are
#' available at \code{help("<function>-deprecated")}.
#' @name SBC-deprecated
#' @keywords internal
NULL
39 changes: 39 additions & 0 deletions R/backend-mock.R
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,42 @@ SBC_fit.SBC_backend_mock <- function(backend, generated, cores) {
backend$result
}

#' @export
SBC_backend_mock_rng <- function(..., n_draws = 1000) {
var_to_rng <- list(...)
if(is.null(names(var_to_rng)) ||
length(unique(names(var_to_rng))) != length(var_to_rng) ||
any(names(var_to_rng) == "")
) {
stop("All arguments must have a unique name")
}
var_to_rng <- purrr::map(var_to_rng, purrr::as_mapper)

purrr::iwalk(var_to_rng, function(rng, name) {
tryCatch({
res <- rng(13)
}, error = function(e) {
message(e)
stop("Test invocation for argument '", name, "' failed.\n",
"All arguments must be convertible to a function that takes the number of draws as input and returns a 1D array of draws.")
})
if(!is.numeric(res) || length(res) != 13) {
stop("Test invocation for argument '", name, "' returned unexpected result.\n",
"All arguments must be convertible to a function that takes the number of draws as input and returns a 1D array of draws.")
}
})

structure(list(var_to_rng = var_to_rng, n_draws = n_draws), class = "SBC_backend_mock_rng")
}

#' @export
SBC_fit.SBC_backend_mock_rng <- function(backend, generated, cores) {
draws_list <- purrr::map(backend$var_to_rng, ~ .x(backend$n_draws))

do.call(posterior::draws_matrix, draws_list)
}

#' @export
SBC_backend_iid_draws.SBC_backend_mock_rng <- function(backend) {
TRUE
}
62 changes: 62 additions & 0 deletions R/backend-rjags.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
#' Create a JAGS backend using `rjags`
#'
#' @param file model file or connection to model code (passed to [rjags::jags.model()])
#' @param n.iter number of iterations for sampling (passed to [rjags::coda.samples())
#' @param n.burnin number of iterations used for burnin
#' @param variable.names names of variables to monitor (passed to [rjags::coda.samples()])
#' @param thin thinning (passed to [rjags::coda.samples()])
#' @param na.rm whether to omit variables containing NA (passed to [rjags::coda.samples()])
#' @param ... additional optional arguments passed to [rjags::jags.model()]
#' - most notably `n.chains`, `n.adapt` and `inits`.
#' @export
SBC_backend_rjags <- function(file, n.iter, n.burnin, variable.names, thin = 1, na.rm = TRUE, ...) {
args = list(...)
if(any(names(args) == "data")) {
stop(paste0("Argument 'data' cannot be provided when defining a backend",
" as it needs to be set by the SBC package"))
}

structure(list(file = file,
n.iter = n.iter,
variable.names = variable.names,
n.burnin = n.burnin,
thin = thin,
na.rm = na.rm,
args = args), class = "SBC_backend_rjags")
}


#' @export
SBC_fit.SBC_backend_rjags <- function(backend, generated, cores) {
args_all <- c(list(file = backend$file, data = generated), backend$args)

model <- do.call(rjags::jags.model, args_all)
if(backend$n.burnin > 0) {
stats::update(model, n.iter = backend$n.burnin, progress.bar = "none")
}
samples <- rjags::coda.samples(model,
variable.names = backend$variable.names,
n.iter = backend$n.iter,
thin = backend$thin,
na.rm = backend$na.rm,
progress.bar = "none")

structure(list(model = model, samples = samples),
class = "SBC_rjags_fit")
}


#' @export
SBC_fit_to_draws_matrix.SBC_rjags_fit <- function(fit) {
posterior::as_draws_matrix(fit$samples)
}

#' @export
SBC_backend_hash_for_cache.SBC_backend_rjags <- function(backend) {
model_code <- readLines(backend$file)

backend_for_cache <- backend
backend_for_cache$file <- NULL
backend_for_cache$model_code <- model_code
rlang::hash(backend_for_cache)
}
Loading