test_that("input variables are valid", { # case when the input density is not a function expect_error(ars(hello, 100)) # N requires input to be numeric expect_error(ars(dnorm, N="test")) # bounds requires input to be vector expect_error(ars(bounds=list(1,2))) }) message(" Testing if the inputted arguments into the ars() function are valid.") test_that("ars ouputs the required amount of samples", { # standard normal dist expect_length(ars(dnorm, 200), 200) # standard normal dist using the default N = 1000 expect_length(ars(dnorm), 1000) # gamma dist with shape 5, rate 7 expect_length(ars(dgamma, 50, bounds=c(0.0, Inf), shape=5.0, rate=7.0), 50) }) message(" Testing that N number of samples are outputted.") expect_density_apx_1 <- function(samples) { # https://stackoverflow.com/a/40851780/16800940 calculate_density <- function(samples) { d <- density(samples) # plot(d) xx <- d$x dx <- xx[2L] - xx[1L] yy <- d$y C <- sum(yy) * dx return(C) } density_sum <- calculate_density(samples) return(expect_lt(density_sum, 1.1) && expect_gt(density_sum, 0.9)) } test_that("The density is around 1", { samples <- ars(dgamma, 1000, bounds=c(0,Inf), shape = 2, rate = 5) expect_density_apx_1(samples) samples <- ars(dbeta, 1000, bounds=c(0,1), shape1 = 2, shape2 = 6) expect_density_apx_1(samples) samples = ars(dweibull, 1000, bounds=c(0,20), shape = 1.5 ) expect_density_apx_1(samples) }) message(" Testing that the density of our sampled distribution is ~ 1.")