Skip to content

Commit 4196ba6

Browse files
committed
AHMI = 0 for randomers
1 parent f5a1e8b commit 4196ba6

File tree

2 files changed

+13
-2
lines changed

2 files changed

+13
-2
lines changed

R/hierarchical_mutual_information.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ CharAMI <- function(char, tree, Mean = function(charH, treeH) charH,
185185
num <- mi - emi[[1]]
186186
denom <- M - emi[[1]]
187187
# Return:
188-
structure(if (num < sqrt(.Machine$double.eps)) 0 else num / denom,
188+
structure(if (abs(num) < sqrt(.Machine$double.eps)) 0 else num / denom,
189189
sem = .AHMISEM(mi, M, emi[[1]], attr(emi, "sem")))
190190
}
191191

tests/testthat/test-charmi.R

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,18 @@ test_that("AHMI succeeds with CharMI", {
9999
CharEMI(ch, tr, precision = 0.001)[[1]], tolerance = 0.02)
100100

101101
expect_lt(CharAMI(ch, tr, max), CharAMI(ch, tr, min))
102-
expect_equal(CharAMI(c(1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1), tr, min)[[1]], 0)
102+
expect_lt(CharAMI(c(1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1), tr, min)[[1]], 0)
103103
expect_equal(CharAMI(c(1, 1, rep(0, 9)), tr)[[1]], 1)
104104
expect_equal(CharAMI(c(rep(TRUE, 6), rep(FALSE, 5)), tr, min)[[1]], 1)
105105
})
106+
107+
test_that("AHMI returns zero for random trees", {
108+
ch <- c(1L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L)
109+
tree <- TreeTools::NJTree(TreeTools::StringToPhyDat("12222211111"))
110+
expect_gt(CharAMI(ch, tree), 0)
111+
set.seed(1)
112+
samples <- replicate(256, CharAMI(ch, TreeTools::RandomTree(ch)))
113+
ci <- t.test(samples, mu = 0, conf.level = 0.997)$conf.int
114+
expect_lt(ci[[1]], 0)
115+
expect_gt(ci[[2]], 0)
116+
})

0 commit comments

Comments
 (0)