Nothing
## proj_polysph()
r <- 3
d <- c(2, 3, 1)
n <- 2
X <- r_unif_polysph(n = n, d = d)
ind_dj <- comp_ind_dj(d)
test_that("Normalization in dist_polysph()", {
expect_equal(proj_polysph(x = X, ind_dj = ind_dj), X)
expect_equal(proj_polysph(x = 1:n * X, ind_dj = ind_dj), X)
})
test_that("proj_polysph() rejects zero-norm blocks", {
expect_error(
proj_polysph(x = rbind(c(0, 0, 1, 1)), ind_dj = comp_ind_dj(c(1, 1))),
"Zero-norm"
)
out <- proj_polysph(x = rbind(c(2, 0, 1, 1)), ind_dj = comp_ind_dj(c(1, 1)))
expect_equal(unname(out[1, 1:2]), c(1, 0))
expect_error(proj_polysph(x = rbind(c(1, 0)), ind_dj = comp_ind_dj(c(1, 1))))
})
## dist_polysph()
r <- 2
d <- c(2, 3)
n <- 10
X1 <- r_unif_polysph(n = n, d = d)
X2 <- r_unif_polysph(n = n, d = d)
ind_dj <- comp_ind_dj(d)
test_that("Normalization in dist_polysph()", {
expect_equal(
dist_polysph(x = X1, y = X2, ind_dj = ind_dj,
norm_x = FALSE, norm_y = FALSE),
dist_polysph(x = X1, y = X2, ind_dj = ind_dj,
norm_x = TRUE, norm_y = TRUE))
expect_error(dist_polysph(x = X1, y = X2[1:2, ], ind_dj = ind_dj))
expect_error(dist_polysph(x = X1, y = X2, ind_dj = c(0, 3)))
})
test_that("Simple case in dist_polysph()", {
expect_equal(
drop(dist_polysph(x = X1, y = X2, ind_dj = ind_dj, std = FALSE)),
sqrt(
acos(rowSums(X1[, 1:3] * X2[, 1:3]))^2 +
acos(rowSums(X1[, 4:7] * X2[, 4:7]))^2
))
})
test_that("Distance between the same points is zero in dist_polysph()", {
expect_equal(drop(dist_polysph(x = X1, y = X1, ind_dj = ind_dj,
norm_x = TRUE, norm_y = TRUE)),
rep(0, n))
expect_equal(drop(dist_polysph(x = X2, y = X2, ind_dj = ind_dj,
norm_x = TRUE, norm_y = TRUE)),
rep(0, n))
expect_equal(drop(dist_polysph(x = X1, y = X1, ind_dj = ind_dj,
norm_x = TRUE, norm_y = TRUE)),
drop(dist_polysph(x = X1, y = X1, ind_dj = ind_dj,
norm_x = FALSE, norm_y = FALSE)))
expect_equal(drop(dist_polysph(x = X2, y = X2, ind_dj = ind_dj,
norm_x = TRUE, norm_y = TRUE)),
drop(dist_polysph(x = X2, y = X2, ind_dj = ind_dj,
norm_x = FALSE, norm_y = FALSE)))
})
## dist_polysph_cross()
test_that("dist_polysph_cross() equals dist_polysph_matrix() for x = y", {
expect_equal(
c(dist_polysph_matrix(x = X1, ind_dj = ind_dj, std = FALSE)),
c(dist_polysph_cross(x = X1, y = X1, ind_dj = ind_dj, std = FALSE)),
tolerance = 1e-6)
})
test_that("dist_polysph_matrix() handles a single row", {
dm <- dist_polysph_matrix(x = rbind(c(1, 0)), ind_dj = c(0, 2))
expect_equal(dim(dm), c(1L, 1L))
expect_equal(unname(dm[1, 1]), 0)
})
test_that("dist_polysph_cross() equals dist_polysph()", {
expect_equal(
cbind(dist_polysph(x = X1[1:3, ], y = X2[1, , drop = FALSE],
ind_dj = ind_dj),
dist_polysph(x = X1[1:3, ], y = X2[2, , drop = FALSE],
ind_dj = ind_dj)),
dist_polysph_cross(x = X1[1:3, ], y = X2[1:2, ], ind_dj = ind_dj,
norm_x = TRUE, norm_y = TRUE),
tolerance = 1e-6)
expect_equal(
dist_polysph(x = X1[1, , drop = FALSE], y = X2[1, , drop = FALSE],
ind_dj = ind_dj),
dist_polysph_cross(x = X1[1, , drop = FALSE], y = X2[1, , drop = FALSE],
ind_dj = ind_dj),
tolerance = 1e-6)
expect_error(dist_polysph_cross(x = X1, y = X2[, -1], ind_dj = ind_dj))
expect_error(dist_polysph_cross(x = X1, y = X2, ind_dj = c(0, 3)))
})
## diamond_crossprod() and diamond_rcrossprod()
# Randomize testing
r <- 2
d <- c(2, 3)
n <- 3
X <- r_unif_polysph(n = n, d = d)
ind_dj <- comp_ind_dj(d)
test_that("Simple case in diamond_crossprod()", {
for (i in seq_len(n)) {
expect_equal(
diamond_crossprod(X = X, ind_dj = ind_dj)[i, , ],
rbind(cbind(tcrossprod(X[i, 1:3]), tcrossprod(X[i, 1:3], X[i, 4:7])),
cbind(tcrossprod(X[i, 4:7], X[i, 1:3]), tcrossprod(X[i, 4:7])))
)
}
expect_error(diamond_crossprod(X = X, ind_dj = c(0, 3)))
})
test_that("Simple case in diamond_rcrossprod()", {
expect_equal(
diamond_rcrossprod(X = X, ind_dj = ind_dj)[, , 1],
tcrossprod(X[, 1:3]))
expect_equal(
diamond_rcrossprod(X = X, ind_dj = ind_dj)[, , 2],
tcrossprod(X[, 4:7]))
expect_error(diamond_rcrossprod(X = X, ind_dj = c(0, 3)))
})
## s()
test_that("Simmetrization using s()", {
expect_true(isSymmetric(s(r_unif_polysph(n = 4, d = 3))))
})
## proj_P()
# Randomize testing
r <- rpois(1, lambda = 3) + 1
d <- rpois(r, lambda = 2) + 1
x <- r_unif_polysph(n = 1, d = d)
ind_dj <- comp_ind_dj(d)
proj_P <- function(x, d) {
# Projections
ind <- cumsum(c(1, d + 1))
P <- matrix(0, nrow = ind[length(d) + 1] - 1, ncol = ind[length(d) + 1] - 1)
for (j in seq_along(d)) {
ind_j <- ind[j]:(ind[j + 1] - 1)
P[ind_j, ind_j] <- -tcrossprod(x[ind_j])
}
diag(P) <- diag(P) + 1
return(P)
}
test_that("AP does the job", {
expect_equal(proj_P(x = x, d = d), AP(x = x, v = x, ind_dj = ind_dj)$P)
expect_equal(diag(1, nrow = ncol(x), ncol = ncol(x)) - proj_P(x = x, d = d),
AP(x = x, v = x, ind_dj = ind_dj, orth = TRUE)$P)
expect_error(AP(x = x, v = x, ind_dj = c(0, 1)))
})
## polylog_minus_exp_mu()
k <- c(0.1, 0.5, 1, 10, 100)
test_that("polylog_minus_exp_mu() is smooth on integer indexes", {
for (s in 1:5) {
expect_equal(polylog_minus_exp_mu(s = s, mu = k),
polylog_minus_exp_mu(s = s + 1e-6, mu = k),
tolerance = 1e-5)
}
})
test_that("polylog_minus_exp_mu() is smooth on half arguments", {
for (s in c(0.5, 1.5, 2.5)) {
expect_equal(polylog_minus_exp_mu(s = s, mu = k),
polylog_minus_exp_mu(s = s + 1e-6, mu = k),
tolerance = 1e-5)
}
})
test_that("polylog_minus_exp_mu() edge cases", {
expect_equal(polylog_minus_exp_mu(s = c(0.5, 1.5), mu = 1),
c(polylog_minus_exp_mu(s = 0.5, mu = 1),
polylog_minus_exp_mu(s = 1.5, mu = 1)))
expect_error(polylog_minus_exp_mu(s = 1:3, mu = 1:2))
})
## Bessel approximations
test_that("Asymptotic-kappa Bessel approximation", {
skip_if_not_installed("Bessel")
paper_asymp <- function(x, d) {
log1p(-d * (d - 2) / (8 * x)) - log(2 * pi * x) / 2
}
Bessel_asymp <- function(x, d) {
Bessel::besselIasym(x = x, nu = (d - 1) / 2, expon.scaled = TRUE,
log = TRUE, k.max = 1)
}
for (d in 1:10) {
expect_equal(paper_asymp(x = c(50:100, 1e4, 1e5), d = d),
Bessel_asymp(x = c(50:100, 1e4, 1e5), d = d))
}
})
## log_sum_exp()
test_that("log_sum_exp() is correct", {
x <- c(1, 2, 3)
expect_equal(log_sum_exp(logs = x), log(sum(exp(x))))
expect_equal(log_sum_exp(logs = x, avg = TRUE), log(mean(exp(x))))
})
## log1m_exp()
test_that("log1m_exp() is correct", {
x <- c(1e-5, 10, NA)
expect_equal(log1m_exp(x), log(1 - exp(-x)))
expect_equal(log1m_exp(x[1]), log(1 - exp(-x[1])))
expect_equal(log1m_exp(x[2]), log(1 - exp(-x[2])))
})
## log_diff_exp()
test_that("log_diff_exp() is correct", {
log_p <- c(-5, 1, 5, NA)
log_n <- rev(log_p)
log_diff <- log_diff_exp(log_p = log_p, log_n = log_n)
expect_equal(log_diff$sgn * exp(log_diff$log_abs),
exp(log_p) - exp(log_n))
for (i in 1:3) {
log_diff <- log_diff_exp(log_p = log_p[i], log_n = log_n[i])
expect_equal(log_diff$sgn * exp(log_diff$log_abs),
exp(log_p[i]) - exp(log_n[i]))
}
})
test_that("log_diff_exp() respects the near-zero tolerance", {
ld <- log_diff_exp(log_p = c(1, 1 + 1e-16, 2), log_n = c(1, 1, 1))
expect_equal(ld$sgn, c(0, 0, 1))
})
test_that("log_diff_exp() returns a null difference on both sides of zero", {
log_p <- c(1, 1, 1 + 3e-16)
log_n <- c(1, 1 + 3e-16, 1)
ld <- expect_no_warning(log_diff_exp(log_p = log_p, log_n = log_n))
expect_equal(ld$sgn, c(0, 0, 0))
expect_equal(ld$log_abs, rep(-Inf, 3))
expect_equal(ld$sgn * exp(ld$log_abs), rep(0, 3))
})
## asinh_log()
test_that("asinh_log() works fine for large positive log_abs arguments", {
log_abs <- c(-20, -10, 0, 50, 500, NA)
sgn <- c(1, -1, 1, -1, 1, 1)
x <- -3:3
expect_equal(asinh(sgn * exp(log_abs)),
asinh_log(log_abs = log_abs, sgn = sgn))
expect_equal(x,
sinh(polykde:::asinh_log(log_abs = log(abs(x)), sgn = sign(x))))
for (i in 1:3) {
expect_equal(asinh(sgn[i] * exp(log_abs[i])),
asinh_log(log_abs = log_abs[i], sgn = sgn[i]))
}
})
test_that("asinh_log() works fine for large negative log_abs arguments", {
log_abs <- c(-50, -500, -1000, NA)
sgn <- c(1, -1, 1, NA)
x <- c(1e-22, -1e-22, 1e-43)
expect_equal(asinh(sgn * exp(log_abs)),
asinh_log(log_abs = log_abs, sgn = sgn))
})
Any scripts or data that you put into this service are public.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.