Skip to content

Commit

Permalink
Decrease code indentation
Browse files Browse the repository at this point in the history
  • Loading branch information
lshandross committed Jul 29, 2024
1 parent d4338d2 commit 04f30b6
Show file tree
Hide file tree
Showing 2 changed files with 123 additions and 115 deletions.
152 changes: 79 additions & 73 deletions tests/testthat/test-make_p_fn_make_q_fn.R
Original file line number Diff line number Diff line change
Expand Up @@ -139,70 +139,74 @@ test_that("make_p_fn, make_q_fn work, no continuous component; two point masses,
})


test_that("make_p_fn, make_q_fn work, no continuous component;
two point masses, non-zero, duplicated values first only", {
ps <- seq(from = 0.1, to = 0.4, by = 0.1)
qs <- c(rep(1.0, 3), rep(2.0, 1))

test_ps <- sort(c(1 / 3, 1.0, seq(from = 0.01, to = 0.99, by = 0.01)))
test_qs <- c(1, 2, qnorm(seq(from = 0.01, to = 0.99, by = 0.01), mean = 1, sd = 2))

p_actual <- make_p_fn(ps, qs)(test_qs)
p_expected <- (1 / 3) * as.numeric(test_qs >= 1.0) + (2 / 3) * as.numeric(test_qs >= 2.0)
# plot(test_qs, p_actual); lines(test_qs, p_expected)

q_actual <- make_q_fn(ps, qs)(test_ps)
q_expected <- ifelse(test_ps <= 1 / 3, 1.0, 2.0)
# plot(test_ps, q_actual); lines(test_ps, q_expected)

testthat::expect_equal(p_actual, p_expected, tolerance = 1e-3)
testthat::expect_true(all(diff(p_actual[order(test_qs)]) >= 0))

testthat::expect_equal(q_actual, q_expected, tolerance = 1e-3)
testthat::expect_true(all(diff(q_actual[order(test_ps)]) >= 0))

testthat::expect_equal(c(rep(1 / 3, sum(test_ps <= 1 / 3)),
rep(1, sum(test_ps > 1 / 3))),
make_p_fn(ps, qs)(q_actual),
tolerance = 1e-3)
# Commenting this test out -- fails due to floating point precision
# testthat::expect_equal(...,
# make_q_fn(ps, qs)(p_actual),
# tolerance = 1e-3)
})


test_that("make_p_fn, make_q_fn work, no continuous component;
two point masses, non-zero, duplicated values second only", {
ps <- seq(from = 0.3, to = 0.9, by = 0.1)
qs <- c(rep(1.0, 1), rep(2.0, 6))

test_ps <- sort(c(1 / 3, 1.0, seq(from = 0.01, to = 0.99, by = 0.01)))
test_qs <- c(1, 2, qnorm(seq(from = 0.01, to = 0.99, by = 0.01), mean = 1, sd = 2))

p_actual <- make_p_fn(ps, qs)(test_qs)
p_expected <- (1 / 3) * as.numeric(test_qs >= 1.0) + (2 / 3) * as.numeric(test_qs >= 2.0)
# plot(test_qs, p_actual); lines(test_qs, p_expected)

q_actual <- make_q_fn(ps, qs)(test_ps)
q_expected <- ifelse(test_ps <= 1 / 3, 1.0, 2.0)
# plot(test_ps, q_actual); lines(test_ps, q_expected)

testthat::expect_equal(p_actual, p_expected, tolerance = 1e-3)
testthat::expect_true(all(diff(p_actual[order(test_qs)]) >= 0))

testthat::expect_equal(q_actual, q_expected, tolerance = 1e-3)
testthat::expect_true(all(diff(q_actual[order(test_ps)]) >= 0))

testthat::expect_equal(c(rep(1 / 3, sum(test_ps <= 1 / 3)),
rep(1, sum(test_ps > 1 / 3))),
make_p_fn(ps, qs)(q_actual),
tolerance = 1e-3)
# Commenting this test out -- fails due to floating point precision
# testthat::expect_equal(...,
# make_q_fn(ps, qs)(p_actual),
# tolerance = 1e-3)
})
test_that(
"make_p_fn, make_q_fn work, no continuous component; two point masses, non-zero, duplicated values first only",
{
ps <- seq(from = 0.1, to = 0.4, by = 0.1)
qs <- c(rep(1.0, 3), rep(2.0, 1))

test_ps <- sort(c(1 / 3, 1.0, seq(from = 0.01, to = 0.99, by = 0.01)))
test_qs <- c(1, 2, qnorm(seq(from = 0.01, to = 0.99, by = 0.01), mean = 1, sd = 2))

p_actual <- make_p_fn(ps, qs)(test_qs)
p_expected <- (1 / 3) * as.numeric(test_qs >= 1.0) + (2 / 3) * as.numeric(test_qs >= 2.0)
# plot(test_qs, p_actual); lines(test_qs, p_expected)

q_actual <- make_q_fn(ps, qs)(test_ps)
q_expected <- ifelse(test_ps <= 1 / 3, 1.0, 2.0)
# plot(test_ps, q_actual); lines(test_ps, q_expected)

testthat::expect_equal(p_actual, p_expected, tolerance = 1e-3)
testthat::expect_true(all(diff(p_actual[order(test_qs)]) >= 0))

testthat::expect_equal(q_actual, q_expected, tolerance = 1e-3)
testthat::expect_true(all(diff(q_actual[order(test_ps)]) >= 0))

testthat::expect_equal(c(rep(1 / 3, sum(test_ps <= 1 / 3)),
rep(1, sum(test_ps > 1 / 3))),
make_p_fn(ps, qs)(q_actual),
tolerance = 1e-3)
# Commenting this test out -- fails due to floating point precision
# testthat::expect_equal(...,
# make_q_fn(ps, qs)(p_actual),
# tolerance = 1e-3)
}
)


test_that(
"make_p_fn, make_q_fn work, no continuous component; two point masses, non-zero, duplicated values second only",
{
ps <- seq(from = 0.3, to = 0.9, by = 0.1)
qs <- c(rep(1.0, 1), rep(2.0, 6))

test_ps <- sort(c(1 / 3, 1.0, seq(from = 0.01, to = 0.99, by = 0.01)))
test_qs <- c(1, 2, qnorm(seq(from = 0.01, to = 0.99, by = 0.01), mean = 1, sd = 2))

p_actual <- make_p_fn(ps, qs)(test_qs)
p_expected <- (1 / 3) * as.numeric(test_qs >= 1.0) + (2 / 3) * as.numeric(test_qs >= 2.0)
# plot(test_qs, p_actual); lines(test_qs, p_expected)

q_actual <- make_q_fn(ps, qs)(test_ps)
q_expected <- ifelse(test_ps <= 1 / 3, 1.0, 2.0)
# plot(test_ps, q_actual); lines(test_ps, q_expected)

testthat::expect_equal(p_actual, p_expected, tolerance = 1e-3)
testthat::expect_true(all(diff(p_actual[order(test_qs)]) >= 0))

testthat::expect_equal(q_actual, q_expected, tolerance = 1e-3)
testthat::expect_true(all(diff(q_actual[order(test_ps)]) >= 0))

testthat::expect_equal(c(rep(1 / 3, sum(test_ps <= 1 / 3)),
rep(1, sum(test_ps > 1 / 3))),
make_p_fn(ps, qs)(q_actual),
tolerance = 1e-3)
# Commenting this test out -- fails due to floating point precision
# testthat::expect_equal(...,
# make_q_fn(ps, qs)(p_actual),
# tolerance = 1e-3)
}
)


test_that("make_p_fn, make_q_fn work, no continuous component; is_hurdle with one zero and one non-zero", {
Expand Down Expand Up @@ -572,17 +576,19 @@ test_that("make_p_fn, make_q_fn well-behaved with 3 duplicated values, one at ze
})


test_that("make_p_fn, make_q_fn well-behaved: multiple duplicates and
floating point issues with discrete adjustments", {
ps <- c(.01, .025, seq(.05, .95, by = .05), .975, .99)
qs <- c(0, 0, 0, 0, 3, 6, 8, 8, 9, 11, 12, 13, 17, 21, 25, 27, 29, 30, 31, 33, 37, 52, 61)
test_that(
"make_p_fn, make_q_fn well-behaved: multiple duplicates and floating point issues with discrete adjustments",
{
ps <- c(.01, .025, seq(.05, .95, by = .05), .975, .99)
qs <- c(0, 0, 0, 0, 3, 6, 8, 8, 9, 11, 12, 13, 17, 21, 25, 27, 29, 30, 31, 33, 37, 52, 61)

q_hat <- distfromq:::make_q_fn(ps, qs)
testthat::expect_identical(q_hat(c(0.99, 1)), c(61, Inf))
q_hat <- distfromq:::make_q_fn(ps, qs)
testthat::expect_identical(q_hat(c(0.99, 1)), c(61, Inf))

p_hat <- distfromq::make_p_fn(ps, qs)
testthat::expect_identical(p_hat(c(61, Inf)), c(0.99, 1.0))
})
p_hat <- distfromq::make_p_fn(ps, qs)
testthat::expect_identical(p_hat(c(61, Inf)), c(0.99, 1.0))
}
)


test_that("make_p_fn result outputs values <= 1", {
Expand Down
86 changes: 44 additions & 42 deletions tests/testthat/test-split_disc_cont_ps_qs.R
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ test_that("split_disc_cont_ps_qs works, no continuous component;


test_that("split_disc_cont_ps_qs works, no continuous component;
two point masses, non-zero, duplicated values second only", {
two point masses, non-zero, duplicated values second only", {
ps <- seq(from = 0.3, to = 0.9, by = 0.1)
qs <- c(rep(1.0, 1), rep(2.0, 6))
expect_equal(
Expand Down Expand Up @@ -231,47 +231,49 @@ test_that("split_disc_cont_ps_qs works, two point masses, both from duplicated q
})


test_that("split_disc_cont_ps_qs works, two point masses,
is_hurdle with one zero and one non-zero with duplicated qs", {
# mixture of a LogNormal(0,1) with weight 0.6,
# a point mass at 0 with weight 0.3, and a point mass at 1 with weight 0.1

# probabilities and quantiles for normal component
norm_ps <- seq(from = 0.1, to = 0.9, by = 0.1)
norm_qs <- qlnorm(norm_ps)
adj_norm_ps <- norm_ps * 0.6 + 0.3 * (norm_qs > 0.0) + 0.1 * (norm_qs > 1.0)

# probabilities and quantiles for point mass at 0
point_ps_0 <- 1.0
point_qs_0 <- 0.0
adj_point_ps_0 <- 0.3

# probabilities and quantiles for point mass at 1
point_ps_1 <- seq(from = 0.0, to = 1.0, by = 0.1)
point_qs_1 <- rep(1.0, length(point_ps_1))
adj_point_ps_1 <- plnorm(1.0) * 0.6 + 0.3 + point_ps_1 * 0.1

ps <- sort(c(adj_norm_ps, adj_point_ps_0, adj_point_ps_1))
qs <- sort(c(norm_qs, point_qs_0, point_qs_1))
dup_inds <- duplicated(ps)
ps <- ps[!dup_inds]
qs <- qs[!dup_inds]

expect_equal(
split_disc_cont_ps_qs(ps, qs, is_hurdle = TRUE),
list(
disc_weight = 0.4,
disc_ps = c(0.75, 0.25),
disc_qs = c(0.0, 1.0),
cont_ps = sort(c(norm_ps)),
cont_qs = sort(c(norm_qs)),
disc_ps_range = list(
c(0.0, 0.3),
range(adj_point_ps_1)
)
)
)
})
test_that(
"split_disc_cont_ps_qs works, two point masses, is_hurdle with one zero and one non-zero with duplicated qs",
{
# mixture of a LogNormal(0,1) with weight 0.6,
# a point mass at 0 with weight 0.3, and a point mass at 1 with weight 0.1

# probabilities and quantiles for normal component
norm_ps <- seq(from = 0.1, to = 0.9, by = 0.1)
norm_qs <- qlnorm(norm_ps)
adj_norm_ps <- norm_ps * 0.6 + 0.3 * (norm_qs > 0.0) + 0.1 * (norm_qs > 1.0)

# probabilities and quantiles for point mass at 0
point_ps_0 <- 1.0
point_qs_0 <- 0.0
adj_point_ps_0 <- 0.3

# probabilities and quantiles for point mass at 1
point_ps_1 <- seq(from = 0.0, to = 1.0, by = 0.1)
point_qs_1 <- rep(1.0, length(point_ps_1))
adj_point_ps_1 <- plnorm(1.0) * 0.6 + 0.3 + point_ps_1 * 0.1

ps <- sort(c(adj_norm_ps, adj_point_ps_0, adj_point_ps_1))
qs <- sort(c(norm_qs, point_qs_0, point_qs_1))
dup_inds <- duplicated(ps)
ps <- ps[!dup_inds]
qs <- qs[!dup_inds]

expect_equal(
split_disc_cont_ps_qs(ps, qs, is_hurdle = TRUE),
list(
disc_weight = 0.4,
disc_ps = c(0.75, 0.25),
disc_qs = c(0.0, 1.0),
cont_ps = sort(c(norm_ps)),
cont_qs = sort(c(norm_qs)),
disc_ps_range = list(
c(0.0, 0.3),
range(adj_point_ps_1)
)
)
)
}
)


test_that("split_disc_cont_ps_qs fails, one discrete component mismatched ps", {
Expand Down

0 comments on commit 04f30b6

Please sign in to comment.