Skip to content

Commit

Permalink
Implemented unit tests for qtrunc.binom() (#54)
Browse files Browse the repository at this point in the history
  • Loading branch information
wleoncio committed Jul 5, 2024
1 parent 5dccf40 commit 26a5c9a
Show file tree
Hide file tree
Showing 4 changed files with 147 additions and 0 deletions.
37 changes: 37 additions & 0 deletions tests/testthat/test-qtrunc-truncated-a.R
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,43 @@ test_that("qtrunc() works as expected (beta)", {
}
})

test_that("qtrunc() works as expected (binomial)", {
fam <- "binomial"
for (lg in c(FALSE, TRUE)) {
for (lt in c(TRUE, FALSE)) {
for (i in seq_len(3L)) {
sz <- sample(1:10, 1L)
pb <- runif(1)
pt <- runif(i)
if (lg) pt <- log(pt)
a <- qtrunc(min(pt) / 2, fam, sz, pb, lower.tail = lt, log.p = lg)
q_trunc <- qtrunc(pt, fam, sz, pb, a = a, lower.tail = lt, log.p = lg)
q_stats <- qbinom(pt, sz, pb, lower.tail = lt, log.p = lg)
expect_length(pt, i)
expect_length(q_trunc, i)
for (ii in seq_along(pt)) {
expect_gte(q_trunc[ii], q_stats[ii])
# Working back to p from q
q_lo <- max(q_trunc[ii] - 1L, 0L, a)
q_hi <- min(q_trunc[ii] + 1L, sz)
ptr_1 <- ptrunc(q_lo, fam, sz, pb, a = a, lower.tail = lt, log.p = lg)
ptr_2 <- ptrunc(q_hi, fam, sz, pb, a = a, lower.tail = lt, log.p = lg)
# because pt will have been rounded
if (q_trunc[ii] > 0L && q_lo > a) {
if (lt) {
expect_gte(pt[ii], ptr_1)
expect_lte(pt[ii], ptr_2)
} else {
expect_lte(pt[ii], ptr_1)
expect_gte(pt[ii], ptr_2)
}
}
}
}
}
}
})

test_that("qtrunc() works as expected (normal)", {
for (lg in c(FALSE, TRUE)) {
for (lt in c(TRUE, FALSE)) {
Expand Down
38 changes: 38 additions & 0 deletions tests/testthat/test-qtrunc-truncated-ab.R
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,44 @@ test_that("qtrunc() works as expected (beta)", {
}
})

test_that("qtrunc() works as expected (binomial)", {
fam <- "binomial"
for (lg in c(FALSE, TRUE)) {
for (lt in c(TRUE, FALSE)) {
for (i in seq_len(3L)) {
sz <- sample(1:10, 1L)
pb <- runif(1)
pt <- runif(i)
ab_probs <- c(runif(100L), pt)
b <- qtrunc(max(ab_probs), fam, sz, pb, lower.tail = TRUE, log.p = FALSE)
a <- qtrunc(min(ab_probs), fam, sz, pb, lower.tail = TRUE, log.p = FALSE)
if (lg) pt <- log(pt)
q_trunc <- qtrunc(pt, fam, sz, pb, a = a, b = b, lower.tail = lt, log.p = lg)
q_stats <- qbinom(pt, sz, pb, lower.tail = lt, log.p = lg)
expect_length(pt, i)
expect_length(q_trunc, i)
for (ii in seq_along(pt)) {
# Working back to p from q
q_lo <- max(q_trunc[ii] - 1L, 0L, a)
q_hi <- min(q_trunc[ii] + 1L, sz, b)
ptr_1 <- ptrunc(q_lo, fam, sz, pb, a = a, b = b, lower.tail = lt, log.p = lg)
ptr_2 <- ptrunc(q_hi, fam, sz, pb, a = a, b = b, lower.tail = lt, log.p = lg)
# because pt will have been rounded
if (q_trunc[ii] > 0L && q_hi < b && q_lo > a) {
if (lt) {
expect_gte(pt[ii], ptr_1)
expect_lte(pt[ii], ptr_2)
} else {
expect_lte(pt[ii], ptr_1)
expect_gte(pt[ii], ptr_2)
}
}
}
}
}
}
})

test_that("qtrunc() works as expected (normal)", {
for (lg in c(FALSE, TRUE)) {
for (lt in c(TRUE, FALSE)) {
Expand Down
39 changes: 39 additions & 0 deletions tests/testthat/test-qtrunc-truncated-b.R
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,45 @@ test_that("qtrunc() works as expected (beta)", {
}
})

test_that("qtrunc() works as expected (binomial)", {
fam <- "binomial"
for (lg in c(FALSE, TRUE)) {
for (lt in c(TRUE, FALSE)) {
for (i in seq_len(3L)) {
sz <- sample(1:10, 1L)
pb <- runif(1)
pt <- runif(i)
if (lg) pt <- log(pt)
b <- qtrunc(
max(runif(10L, pt)), fam, sz, pb, lower.tail = lt, log.p = FALSE
)
q_trunc <- qtrunc(pt, fam, sz, pb, b = b, lower.tail = lt, log.p = lg)
q_stats <- qbinom(pt, sz, pb, lower.tail = lt, log.p = lg)
expect_length(pt, i)
expect_length(q_trunc, i)
for (ii in seq_along(pt)) {
expect_lte(q_trunc[ii], q_stats[ii])
# Working back to p from q
q_lo <- max(q_trunc[ii] - 1L, 0L)
q_hi <- min(q_trunc[ii] + 1L, sz, b)
ptr_1 <- ptrunc(q_lo, fam, sz, pb, b = b, lower.tail = lt, log.p = lg)
ptr_2 <- ptrunc(q_hi, fam, sz, pb, b = b, lower.tail = lt, log.p = lg)
# because pt will have been rounded
if (q_trunc[ii] > 0L && q_hi < b) {
if (lt) {
expect_gte(pt[ii], ptr_1)
expect_lte(pt[ii], ptr_2)
} else {
expect_lte(pt[ii], ptr_1)
expect_gte(pt[ii], ptr_2)
}
}
}
}
}
}
})

test_that("qtrunc() works as expected (normal)", {
for (lg in c(FALSE, TRUE)) {
for (lt in c(TRUE, FALSE)) {
Expand Down
33 changes: 33 additions & 0 deletions tests/testthat/test-qtrunc-untruncated.R
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,39 @@ test_that("qtrunc() works as expected (beta)", {
}
})

test_that("qtrunc() works as expected (binomial)", {
for (lg in c(FALSE, TRUE)) {
for (lt in c(TRUE, FALSE)) {
for (i in seq_len(3L)) {
sz <- sample(1:10, 1L)
pb <- runif(1)
pt <- runif(i)
if (lg) pt <- log(pt)
q_trunc <- qtrunc(pt, "binomial", sz, pb, lower.tail = lt, log.p = lg)
q_stats <- qbinom(pt, sz, pb, lower.tail = lt, log.p = lg)
expect_length(pt, i)
expect_length(q_trunc, i)
for (ii in seq_along(pt)) {
expect_equal(q_trunc[ii], q_stats[ii])
# Working back to p from q
q_lo <- max(q_trunc[ii] - 1L, 0L)
q_hi <- min(q_trunc[ii] + 1L, sz)
ptr_1 <- ptrunc(q_lo, "binomial", sz, pb, lower.tail = lt, log.p = lg)
ptr_2 <- ptrunc(q_hi, "binomial", sz, pb, lower.tail = lt, log.p = lg)
# because pt will have been rounded
if (q_trunc[ii] > 0L && lt) {
expect_gte(pt[ii], ptr_1)
expect_lte(pt[ii], ptr_2)
} else if (q_trunc[ii] > 0L && !lt) {
expect_lte(pt[ii], ptr_1)
expect_gte(pt[ii], ptr_2)
}
}
}
}
}
})

test_that("qtrunc() works as expected (normal)", {
for (lg in c(FALSE, TRUE)) {
for (lt in c(TRUE, FALSE)) {
Expand Down

0 comments on commit 26a5c9a

Please sign in to comment.