Skip to content

Commit

Permalink
Implemented qtrunc.beta() (#54)
Browse files Browse the repository at this point in the history
  • Loading branch information
wleoncio committed Jul 3, 2024
1 parent 536ee4b commit af896aa
Show file tree
Hide file tree
Showing 5 changed files with 122 additions and 0 deletions.
10 changes: 10 additions & 0 deletions R/qtrunc.R
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,16 @@ qtrunc.generic <- function(p, ..., lower.tail, log.p) {
UseMethod("qtrunc", p)
}

qtrunc.beta <- function(
p, shape1, shape2, a = 0, b = 1, ..., lower.tail, log.p
) {
F_a <- pbeta(a, shape1, shape2, ncp = 0, lower.tail, FALSE)
F_b <- pbeta(b, shape1, shape2, ncp = 0, lower.tail, FALSE)
rescaled_p <- rescale_p(p, F_a, F_b, lower.tail, log.p)
q <- qbeta(rescaled_p, shape1, shape2, ncp = 0, lower.tail, FALSE)
return(q)
}

qtrunc.normal <- function(
p, mean = 0, sd = 1, a = -Inf, b = Inf, ..., lower.tail, log.p
) {
Expand Down
28 changes: 28 additions & 0 deletions tests/testthat/test-qtrunc-truncated-a.R
Original file line number Diff line number Diff line change
@@ -1,5 +1,33 @@
context("qtrunc, lower truncation")

test_that("qtrunc() works as expected (beta)", {
for (lg in c(FALSE, TRUE)) {
for (lt in c(TRUE, FALSE)) {
for (i in seq_len(3L)) {
shp1 <- sample(1:10, 1L)
shp2 <- sample(1:10, 1L)
pt <- runif(i)
if (lg) pt <- log(pt)
a <- qtrunc(min(pt) / 2, "beta", shp1, shp2, lower.tail = lt, log.p = lg)
q_trunc <- qtrunc(
pt, "beta", shp1, shp2, a = a, lower.tail = lt, log.p = lg
)
q_stats <- qbeta(pt, shp1, shp2, lower.tail = lt, log.p = lg)
expect_length(pt, i)
expect_length(q_trunc, i)
for (ii in seq_along(pt)) {
expect_gt(q_trunc[ii], q_stats[ii])
# Working back to p from q
ptr <- ptrunc(
q_trunc[ii], "beta", shp1, shp2, lower.tail = lt, log.p = lg, a = a
)
expect_equal(pt[ii], ptr)
}
}
}
}
})

test_that("qtrunc() works as expected (normal)", {
for (lg in c(FALSE, TRUE)) {
for (lt in c(TRUE, FALSE)) {
Expand Down
29 changes: 29 additions & 0 deletions tests/testthat/test-qtrunc-truncated-ab.R
Original file line number Diff line number Diff line change
@@ -1,5 +1,34 @@
context("qtrunc, upper truncation")

test_that("qtrunc() works as expected (beta)", {
for (lg in c(FALSE, TRUE)) {
for (lt in c(TRUE, FALSE)) {
for (i in seq_len(3L)) {
shp1 <- sample(1:10, 1L)
shp2 <- sample(1:10, 1L)
pt <- runif(i)
ab <- c(runif(100L), pt)
b <- qtrunc(max(pt), "beta", shp1, shp2, lower.tail = lt, log.p = FALSE)
a <- qtrunc(min(pt), "beta", shp1, shp2, lower.tail = lt, log.p = FALSE)
if (lg) pt <- log(pt)
q_trunc <- qtrunc(
pt, "beta", shp1, shp2, b = b, lower.tail = lt, log.p = lg
)
q_stats <- qbeta(pt, shp1, shp2, 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
ptr <- ptrunc(
q_trunc[ii], "beta", shp1, shp2, lower.tail = lt, log.p = lg, b = b
)
expect_equal(pt[ii], ptr)
}
}
}
}
})

test_that("qtrunc() works as expected (normal)", {
for (lg in c(FALSE, TRUE)) {
for (lt in c(TRUE, FALSE)) {
Expand Down
30 changes: 30 additions & 0 deletions tests/testthat/test-qtrunc-truncated-b.R
Original file line number Diff line number Diff line change
@@ -1,5 +1,35 @@
context("qtrunc, upper truncation")

test_that("qtrunc() works as expected (beta)", {
for (lg in c(FALSE, TRUE)) {
for (lt in c(TRUE, FALSE)) {
for (i in seq_len(3L)) {
shp1 <- sample(1:10, 1L)
shp2 <- sample(1:10, 1L)
pt <- runif(i)
if (lg) pt <- log(pt)
b <- qtrunc(
max(runif(10L, pt)), "beta", shp1, shp2, lower.tail = lt, log.p = FALSE
)
q_trunc <- qtrunc(
pt, "beta", shp1, shp2, b = b, lower.tail = lt, log.p = lg
)
q_stats <- qbeta(pt, shp1, shp2, lower.tail = lt, log.p = lg)
expect_length(pt, i)
expect_length(q_trunc, i)
for (ii in seq_along(pt)) {
expect_lt(q_trunc[ii], q_stats[ii])
# Working back to p from q
ptr <- ptrunc(
q_trunc[ii], "beta", shp1, shp2, lower.tail = lt, log.p = lg, b = b
)
expect_equal(pt[ii], ptr)
}
}
}
}
})

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

test_that("qtrunc() works as expected (beta)", {
for (lg in c(FALSE, TRUE)) {
for (lt in c(TRUE, FALSE)) {
for (i in seq_len(3L)) {
shp1 <- sample(1:10, 1L)
shp2 <- sample(1:10, 1L)
pt <- runif(i)
if (lg) pt <- log(pt)
q_trunc <- qtrunc(pt, "beta", shp1, shp2, lower.tail = lt, log.p = lg)
q_stats <- qbeta(pt, shp1, shp2, 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
ptr <- ptrunc(
q_trunc[ii], "beta", shp1, shp2, lower.tail = lt, log.p = lg
)
expect_equal(pt[ii], ptr)
}
}
}
}
})

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 af896aa

Please sign in to comment.