Skip to content

Commit 0d56a8d

Browse files
committed
test: round LR values before comparison (digits = 10)
1 parent 1f43b42 commit 0d56a8d

1 file changed

Lines changed: 13 additions & 17 deletions

File tree

tests/testthat/test-lr.R

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,11 @@ agg_probs <- function(res) {
55
tapply(res$prob, res$LR, sum, simplify = TRUE)
66
}
77

8-
collapse_close <- function(res, tol = 1e-12) {
9-
o <- order(res$LR)
10-
LR <- res$LR[o]; P <- res$prob[o]
11-
keep <- c(TRUE, abs(diff(LR)) > tol)
12-
grp <- cumsum(keep)
13-
LRc <- tapply(LR, grp, `[`, 1L)
14-
Pc <- tapply(P, grp, sum)
15-
list(LR = as.numeric(LRc), prob = as.numeric(Pc) / sum(Pc))
8+
collapse_close <- function(res, digits = 10) {
9+
res$LR <- round(res$LR, digits)
10+
agg <- tapply(res$prob, res$LR, sum)
11+
list(LR = as.numeric(names(agg)),
12+
prob = as.numeric(agg) / sum(agg))
1613
}
1714

1815
strip_tiny <- function(res, prob_tol = 1e-12) {
@@ -24,17 +21,16 @@ strip_tiny <- function(res, prob_tol = 1e-12) {
2421
}
2522

2623
## tolerance settings
27-
TOL <- 1e-8 # numeric comparison
28-
COLL_TOL <- 1e-12 # collapse nearly-equal LR
29-
PROB_TOL <- 1e-12 # drop negligible probability
24+
TOL <- 1e-8 # numeric comparison
25+
PROB_TOL <- 1e-12 # drop negligible probability
3026

3127
check_lr_dist <- function(gen_cpp, gen_R,
3228
n_vec = c(5, 10),
3329
alpha = 0.05,
3430
tol = 1e-8) {
3531
for (n in n_vec) {
36-
dist_cpp <- strip_tiny(collapse_close(gen_cpp(n, alpha), COLL_TOL), PROB_TOL)
37-
dist_R <- strip_tiny(collapse_close(gen_R (n, alpha), COLL_TOL), PROB_TOL)
32+
dist_cpp <- strip_tiny(collapse_close(gen_cpp(n, alpha)), PROB_TOL)
33+
dist_R <- strip_tiny(collapse_close(gen_R (n, alpha)), PROB_TOL)
3834

3935
allLR <- sort(unique(c(dist_cpp$LR, dist_R$LR)))
4036
p_cpp <- dist_cpp$prob[match(allLR, dist_cpp$LR)]
@@ -57,8 +53,8 @@ test_that("lr_ind_dist – C++ and R engines numerically identical", {
5753
n <- 40
5854
alpha <- 0.05
5955

60-
res_cpp <- strip_tiny(collapse_close(lr_ind_dist(n, alpha), COLL_TOL), PROB_TOL)
61-
res_R <- strip_tiny(collapse_close(fb_lrind_R (n, alpha), COLL_TOL), PROB_TOL)
56+
res_cpp <- strip_tiny(collapse_close(lr_ind_dist(n, alpha)), PROB_TOL)
57+
res_R <- strip_tiny(collapse_close(fb_lrind_R (n, alpha)), PROB_TOL)
6258

6359
expect_equal(cumsum(res_cpp$prob), cumsum(res_R$prob), tolerance = TOL)
6460
expect_true(all(is.finite(res_cpp$prob)))
@@ -73,8 +69,8 @@ test_that("lr_cc_dist – C++ and R engines numerically identical", {
7369
n <- 40
7470
alpha <- 0.05
7571

76-
res_cpp <- strip_tiny(collapse_close(lr_cc_dist(n, alpha), COLL_TOL), PROB_TOL)
77-
res_R <- strip_tiny(collapse_close(fb_lrcc_R (n, alpha), COLL_TOL), PROB_TOL)
72+
res_cpp <- strip_tiny(collapse_close(lr_cc_dist(n, alpha)), PROB_TOL)
73+
res_R <- strip_tiny(collapse_close(fb_lrcc_R (n, alpha)), PROB_TOL)
7874

7975
expect_equal(cumsum(res_cpp$prob), cumsum(res_R$prob), tolerance = TOL)
8076
expect_true(all(is.finite(res_cpp$prob)))

0 commit comments

Comments
 (0)