forked from greta-dev/greta
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request greta-dev#725 from njtierney/add-checkers-test-pos…
…teriors-723 Add checkers test posteriors 723
- Loading branch information
Showing
10 changed files
with
269 additions
and
178 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
Sys.setenv("RELEASE_CANDIDATE" = "true") | ||
test_that("posterior is correct (binomial)", { | ||
skip_if_not(check_tf_version()) | ||
|
||
skip_if_not_release() | ||
|
||
# analytic solution to the posterior of the paramter of a binomial | ||
# distribution, with uniform prior | ||
n <- 100 | ||
pos <- rbinom(1, n, runif(1)) | ||
theta <- uniform(0, 1) | ||
distribution(pos) <- binomial(n, theta) | ||
m <- model(theta) | ||
|
||
draws <- get_enough_draws(m, hmc(), 2000, verbose = FALSE) | ||
|
||
samples <- as.matrix(draws) | ||
|
||
# analytic solution to posterior is beta(1 + pos, 1 + N - pos) | ||
shape1 <- 1 + pos | ||
shape2 <- 1 + n - pos | ||
|
||
# qq plot against true quantiles | ||
quants <- (1:99) / 100 | ||
q_target <- qbeta(quants, shape1, shape2) | ||
q_est <- quantile(samples, quants) | ||
plot(q_target ~ q_est, main = "binomial posterior") | ||
abline(0, 1) | ||
|
||
n_draws <- round(coda::effectiveSize(draws)) | ||
comparison <- rbeta(n_draws, shape1, shape2) | ||
suppressWarnings(test <- ks.test(samples, comparison)) | ||
expect_gte(test$p.value, 0.01) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
# Currently takes about 30 seconds on an M1 mac | ||
Sys.setenv("RELEASE_CANDIDATE" = "false") | ||
|
||
test_that("samplers are unbiased for bivariate normals", { | ||
skip_if_not(check_tf_version()) | ||
|
||
skip_if_not_release() | ||
|
||
hmc_mvn_samples <- check_mvn_samples(sampler = hmc()) | ||
expect_lte(max(hmc_mvn_samples), stats::qnorm(0.99)) | ||
|
||
rwmh_mvn_samples <- check_mvn_samples(sampler = rwmh()) | ||
expect_lte(max(rwmh_mvn_samples), stats::qnorm(0.99)) | ||
|
||
slice_mvn_samples <- check_mvn_samples(sampler = slice()) | ||
expect_lte(max(rwmh_mvn_samples), stats::qnorm(0.99)) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
Sys.setenv("RELEASE_CANDIDATE" = "true") | ||
|
||
test_that("samplers are unbiased for chi-squared", { | ||
skip_if_not(check_tf_version()) | ||
|
||
skip_if_not_release() | ||
|
||
df <- 5 | ||
x <- chi_squared(df) | ||
iid <- function(n) rchisq(n, df) | ||
|
||
chi_squared_checked <- check_samples(x = x, | ||
iid_function = iid, | ||
sampler = hmc()) | ||
|
||
# do the plotting | ||
qqplot_checked_samples(chi_squared_checked) | ||
|
||
# do a formal hypothesis test | ||
stat <- ks_test_mcmc_vs_iid(chi_squared_checked) | ||
|
||
expect_gte(stat$p.value, 0.01) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
Sys.setenv("RELEASE_CANDIDATE" = "false") | ||
|
||
## TF1/2 - method for this test needs to be updated for TF2 | ||
## See https://github.com/greta-dev/greta/issues/720 | ||
test_that("samplers pass geweke tests", { | ||
skip_if_not(check_tf_version()) | ||
|
||
skip_if_not_release() | ||
|
||
# nolint start | ||
# run geweke tests on this model: | ||
# theta ~ normal(mu1, sd1) | ||
# x[i] ~ normal(theta, sd2) | ||
# for i in N | ||
# nolint end | ||
|
||
n <- 10 | ||
mu1 <- rnorm(1, 0, 3) | ||
sd1 <- rlnorm(1) | ||
sd2 <- rlnorm(1) | ||
|
||
# prior (n draws) | ||
p_theta <- function(n) { | ||
rnorm(n, mu1, sd1) | ||
} | ||
|
||
# likelihood | ||
p_x_bar_theta <- function(theta) { | ||
rnorm(n, theta, sd2) | ||
} | ||
|
||
# define the greta model (single precision for slice sampler) | ||
x <- as_data(rep(0, n)) | ||
greta_theta <- normal(mu1, sd1) | ||
distribution(x) <- normal(greta_theta, sd2) | ||
model <- model(greta_theta, precision = "single") | ||
|
||
# run tests on all available samplers | ||
check_geweke( | ||
sampler = hmc(), | ||
model = model, | ||
data = x, | ||
p_theta = p_theta, | ||
p_x_bar_theta = p_x_bar_theta, | ||
title = "HMC Geweke test" | ||
) | ||
|
||
check_geweke( | ||
sampler = rwmh(), | ||
model = model, | ||
data = x, | ||
p_theta = p_theta, | ||
p_x_bar_theta = p_x_bar_theta, | ||
warmup = 2000, | ||
title = "RWMH Geweke test" | ||
) | ||
|
||
check_geweke( | ||
sampler = slice(), | ||
model = model, | ||
data = x, | ||
p_theta = p_theta, | ||
p_x_bar_theta = p_x_bar_theta, | ||
title = "slice sampler Geweke test" | ||
) | ||
}) |
Oops, something went wrong.