Skip to content

Commit

Permalink
fix seed for predict tests
Browse files Browse the repository at this point in the history
  • Loading branch information
helske committed Sep 20, 2021
1 parent a85504e commit d69b505
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 11 deletions.
4 changes: 2 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ Package: bssm
Type: Package
Title: Bayesian Inference of Non-Linear and Non-Gaussian State Space
Models
Version: 1.1.7
Version: 1.1.7-1
Authors@R:
c(person(given = "Jouni",
family = "Helske",
Expand Down Expand Up @@ -41,7 +41,7 @@ Suggests:
Imports: checkmate, coda (>= 0.18-1), diagis, Rcpp (>= 0.12.3)
LinkingTo: Rcpp, RcppArmadillo, ramcmc, sitmo
SystemRequirements: C++11, pandoc (>= 1.12.3, needed for vignettes)
RoxygenNote: 7.1.1
RoxygenNote: 7.1.2
VignetteBuilder: knitr
BugReports: https://github.com/helske/bssm/issues
URL: https://github.com/helske/bssm
Expand Down
6 changes: 5 additions & 1 deletion NEWS
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
bssm 1.1.7 (Release date: 2021-09-15)
bssm 1.1.7-1 (Release date: 2021-09-21)
==============
* Fixed an error in automatic tests due to lack of fixed RNG seed.

bssm 1.1.7 (Release date: 2021-09-20)
==============
* Added a function cpp_example_model which can be used to extract and
compile some non-linear and SDE models used in the examples and vignettes.
Expand Down
4 changes: 3 additions & 1 deletion R/predict.R
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@
#' future, using posterior samples of (theta, alpha_T+1) i.e. the
#' posterior samples of hyperparameters and latest states.
#' Otherwise it is assumed that \code{model} corresponds to the original model.
#' @param seed Seed for RNG (positive integer).
#' @param seed Seed for RNG (positive integer). Note that this affects only the
#' C++ side, and \code{predict} also uses R side RNG for subsampling, so for
#' replicable results you should call \code{set.seed} before \code{predict}.
#' @param ... Ignored.
#' @return A \code{data.frame} consisting of samples from the predictive
#' posterior distribution.
Expand Down
17 changes: 16 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,22 @@ devtools::install_github("helske/bssm")
Recent changes (For all changes, see NEWS file.)
==========================================================================

bssm 1.1.6 (Release date: )
bssm 1.1.7-1 (Release date: 2021-09-21)
==============
* Fixed an error in automatic tests due to lack of fixed RNG seed.

bssm 1.1.7 (Release date: 2021-09-20)
==============
* Added a function cpp_example_model which can be used to extract and
compile some non-linear and SDE models used in the examples and vignettes.
* Added as_draws method for run_mcmc output so samples can be analysed using
the posterior package.
* Added more examples.
* Fixed a tolerance of one MCMC test to pass the test on OSX as well.
* Fixed a bug in iterated extended Kalman smoothing which resulted incorrect
estimates.

bssm 1.1.6 (Release date: 2021-09-06)
==============
* Cleaned codes and added more comprehensive tests in line with pkgcheck
tests. This resulted in finding and fixing multiple bugs:
Expand Down
4 changes: 3 additions & 1 deletion man/predict.mcmc_output.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 20 additions & 5 deletions tests/testthat/test_predict.R
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,18 @@ test_that("Gaussian predictions work", {
mcmc_results <- run_mcmc(model, iter = 1000)
future_model <- model
future_model$y <- rep(NA, 3)
set.seed(1)
pred <- predict(mcmc_results, future_model, type = "mean",
nsim = 100)

expect_gt(mean(pred$value[pred$time == 3]), -0.5)
expect_lt(mean(pred$value[pred$time == 3]), 0.5)

# Posterior predictions for past observations:
set.seed(1)
expect_error(yrep <- predict(mcmc_results, model, type = "response",
future = FALSE, nsim = 100), NA)
set.seed(1)
expect_error(meanrep <- predict(mcmc_results, model, type = "mean",
future = FALSE, nsim = 100), NA)

Expand Down Expand Up @@ -58,19 +61,21 @@ test_that("Gaussian predictions work", {
mcmc_results2$theta[, 2:3] <- log(mcmc_results2$theta[, 2:3])
future_model2 <- model2
future_model2$y <- matrix(NA, 3, 1)
set.seed(1)
expect_error(pred2 <- predict(mcmc_results2, future_model2, type = "mean",
nsim = 100), NA)
expect_equal(pred, pred2)
# Posterior predictions for past observations:
set.seed(1)
expect_error(yrep2 <- predict(mcmc_results2, model2, type = "response",
future = FALSE, nsim = 100), NA)
expect_error(meanrep2 <- predict(mcmc_results2, model2, type = "mean",
set.seed(1)
expect_error(meanrep2 <- predict(mcmc_results2, model2, type = "mean",
future = FALSE, nsim = 100), NA)
expect_equal(yrep, yrep2)
expect_equal(meanrep, meanrep2)
expect_error(predict(mcmc_results2, model, type = "response",
future = FALSE, nsim = 100))

})

test_that("Non-gaussian predictions work", {
Expand All @@ -85,19 +90,22 @@ test_that("Non-gaussian predictions work", {
expect_error(mcmc_results <- run_mcmc(model, iter = 1000, particles = 5), NA)
future_model <- model
future_model$y <- rep(NA, 3)
set.seed(1)
expect_error(pred <- predict(mcmc_results, future_model, type = "mean",
nsim = 100), NA)

expect_gt(mean(pred$value[pred$time == 3]), 1)
expect_lt(mean(pred$value[pred$time == 3]), 1.5)

# Posterior predictions for past observations:
set.seed(1)
expect_error(yrep <- predict(mcmc_results, model, type = "response",
future = FALSE, nsim = 100), NA)
set.seed(1)
expect_error(meanrep <- predict(mcmc_results, model, type = "mean",
future = FALSE, nsim = 100), NA)

expect_equal(mean(yrep$value - meanrep$value), 0, tol = 0.1)
expect_equal(mean(yrep$value - meanrep$value), 0, tol = 0.5)

update_fn <- function(x) {
T <- array(x[1])
Expand Down Expand Up @@ -128,12 +136,15 @@ test_that("Non-gaussian predictions work", {

future_model2 <- model2
future_model2$y <- rep(NA, 3)
set.seed(1)
expect_error(pred2 <- predict(mcmc_results2, future_model2, type = "mean",
nsim = 100), NA)
expect_equal(pred, pred2)
# Posterior predictions for past observations:
set.seed(1)
expect_error(yrep2 <- predict(mcmc_results2, model2, type = "response",
future = FALSE, nsim = 100), NA)
set.seed(1)
expect_error(meanrep2 <- predict(mcmc_results2, model2, type = "mean",
future = FALSE, nsim = 100), NA)
expect_equal(yrep, yrep2)
Expand Down Expand Up @@ -173,12 +184,14 @@ test_that("Predictions for nlg_ssm work", {
expect_lt(mean(pred$value[pred$time == 3]), 1.5)

# Posterior predictions for past observations:
set.seed(1)
expect_error(yrep <- predict(mcmc_results, model, type = "response",
future = FALSE, nsim = 100), NA)
set.seed(1)
expect_error(meanrep <- predict(mcmc_results, model, type = "mean",
future = FALSE, nsim = 100), NA)

expect_equal(mean(yrep$value - meanrep$value), 0, tol = 0.1)
expect_equal(mean(yrep$value - meanrep$value), 0, tol = 0.5)
})


Expand Down Expand Up @@ -221,11 +234,13 @@ test_that("Predictions for mng_ssm work", {
expect_lt(max(pred$value), 1000)

# Posterior predictions for past observations:
set.seed(1)
expect_error(yrep <- predict(mcmc_results, model, type = "response",
future = FALSE, nsim = 100), NA)
set.seed(1)
expect_error(meanrep <- predict(mcmc_results, model, type = "mean",
future = FALSE, nsim = 100), NA)

expect_equal(mean(yrep$value - meanrep$value), 0, tol = 0.1)
expect_equal(mean(yrep$value - meanrep$value), 0, tol = 0.5)
})

0 comments on commit d69b505

Please sign in to comment.