Nothing
q <- f_to_srvf(simu_data$f, simu_data$time)
time <- simu_data$time
pens <- c("none", "roughness", "l2gam", "l2psi", "geodesic")
test_that("`optimum.reparam()` returns a warping function for every penalty", {
for (method in c("DP", "DPo", "RBFGS")) {
for (pen in pens) {
gam <- optimum.reparam(q[, 1], time, q[, 2], time, lambda = 0.01,
pen = pen, method = method)
expect_length(gam, length(time))
expect_equal(gam[1], 0)
expect_equal(gam[length(gam)], 1)
expect_true(all(diff(gam) >= -1e-12))
}
}
})
test_that("`optimum.reparam()` penalty has no effect when lambda is 0", {
for (method in c("DP", "DPo", "RBFGS")) {
gam0 <- optimum.reparam(q[, 1], time, q[, 2], time, method = method)
for (pen in pens) {
gam <- optimum.reparam(q[, 1], time, q[, 2], time, lambda = 0,
pen = pen, method = method)
expect_equal(gam, gam0)
}
}
})
test_that("`optimum.reparam()` penalty 'none' ignores lambda", {
for (method in c("DP", "DPo", "RBFGS")) {
gam0 <- optimum.reparam(q[, 1], time, q[, 2], time, method = method)
gam <- optimum.reparam(q[, 1], time, q[, 2], time, lambda = 0.5,
pen = "none", method = method)
expect_equal(gam, gam0)
}
})
test_that("`optimum.reparam()` passes the penalty to the DP method", {
gam <- lapply(c("none", "roughness", "l2gam"), function(pen) {
optimum.reparam(q[, 1], time, q[, 2], time, lambda = 0.1, pen = pen,
method = "DP")
})
expect_false(isTRUE(all.equal(gam[[1]], gam[[2]])))
expect_false(isTRUE(all.equal(gam[[2]], gam[[3]])))
})
test_that("`optimum.reparam()` rejects an unknown penalty", {
expect_error(
optimum.reparam(q[, 1], time, q[, 2], time, pen = "bogus"),
"invalid penalty selection"
)
})
# f2 = f1 o gam_true, so aligning q2 to q1 should recover the inverse of
# gam_true (as in MATLAB tests/DynamicProgrammingTest.m)
kw_time <- seq(0, 1, length.out = 101)
kw_f <- function(s) sin(3 * pi * s) + 0.5 * cos(7 * pi * s) + 2 * s
kw_gam_true <- kw_time + 0.5 * sin(pi * kw_time) / pi
kw_q1 <- f_to_srvf(kw_f(kw_time), kw_time)
kw_q2 <- f_to_srvf(kw_f(kw_gam_true), kw_time)
kw_gam_inv <- stats::approx(kw_gam_true, kw_time, xout = kw_time)$y
kw_error <- function(gam) {
qw <- warp_q_gamma(kw_q2, kw_time, gam)
sqrt(trapz(kw_time, (kw_q1 - qw)^2))
}
test_that("`optimum.reparam()` DPo recovers a known warp", {
# rounding q2 to the nearest grid point at scl = 1 left the warp up to
# 0.63 grid cells off
gam <- optimum.reparam(kw_q1, kw_time, kw_q2, kw_time, method = "DPo")
expect_lt(max(abs(gam - kw_gam_inv)) * (length(kw_time) - 1), 0.5)
})
test_that("`optimum.reparam()` DP and DPo fit as well as the true warp", {
# the old DPo cost fit 1.35x and the old DP cost (piecewise-constant SRVFs,
# nbhd_dim = 7) 1.52x worse than the true warp
for (method in c("DP", "DPo")) {
gam <- optimum.reparam(kw_q1, kw_time, kw_q2, kw_time, method = method)
expect_lt(kw_error(gam), 1.1 * kw_error(kw_gam_inv))
}
})
test_that("`optimum.reparam()` passes scl to the DPo solver", {
gam <- optimum.reparam(kw_q1, kw_time, kw_q2, kw_time, method = "DPo")
expect_identical(
optimum.reparam(kw_q1, kw_time, kw_q2, kw_time, method = "DPo", scl = 3),
gam
)
gam1 <- optimum.reparam(kw_q1, kw_time, kw_q2, kw_time, method = "DPo",
scl = 1)
expect_length(gam1, length(gam))
expect_true(all(diff(gam1) >= 0))
expect_false(isTRUE(all.equal(gam1, gam)))
expect_error(
optimum.reparam(kw_q1, kw_time, kw_q2, kw_time, method = "DPo", scl = 0),
"scl"
)
})
test_that("`optimum.reparam()` RBFGS fits as well as the true warp", {
# "RBFGS" refines the "DP" warp; started from the identity it stopped half
# a grid cell off
gam <- optimum.reparam(kw_q1, kw_time, kw_q2, kw_time, method = "RBFGS")
expect_lt(max(abs(gam - kw_gam_inv)) * (length(kw_time) - 1), 0.5)
expect_lt(kw_error(gam), 1.1 * kw_error(kw_gam_inv))
})
# DP's objective at gam: the unit norm, linearly interpolated SRVFs, which
# DPQ2 integrates exactly, here on a 20x finer grid
dp_cost <- function(q1, q2, gam, t) {
M <- length(t)
t1 <- seq(0, 1, length.out = M)
tf <- seq(0, 1, length.out = (M - 1) * 20 + 1)
a <- stats::approx(t1, q1, xout = tf)$y
a <- a / sqrt(trapz(tf, a^2))
b <- stats::approx(t1, q2, xout = tf)$y
b <- b / sqrt(trapz(tf, b^2))
gamf <- stats::approx(t1, gam, xout = tf)$y
trapz(tf, (a - warp_q_gamma(b, tf, gamf))^2)
}
test_that("`optimum.reparam()` RBFGS cost matches DP", {
# on random pairs the identity start stopped in local minima, up to 3x the
# "DP" distance. Scored on DP's own objective, RBFGS is below DP on most
# pairs; from the coarse DP start it can land in another minimum
bumps <- function(t) {
f <- rep(0, length(t))
for (j in 1:3)
f <- f + stats::rnorm(1) *
exp(-(t - stats::runif(1))^2 / (2 * (0.05 + 0.1 * stats::runif(1))^2))
f
}
withr::local_seed(0)
r <- replicate(10, {
q1 <- f_to_srvf(bumps(kw_time), kw_time)
q2 <- f_to_srvf(bumps(kw_time), kw_time)
gr <- optimum.reparam(q1, kw_time, q2, kw_time, method = "RBFGS")
gd <- optimum.reparam(q1, kw_time, q2, kw_time, method = "DP")
dp_cost(q1, q2, gr, kw_time) / dp_cost(q1, q2, gd, kw_time)
})
expect_lt(stats::median(r), 1.0)
expect_lt(max(r), 1.35)
})
test_that("`optimum.reparam()` RBFGS matches DP on the growth velocities", {
# their sharp spurts made the last solver 1.04x DP's cost in the median
# in MATLAB
t <- growth_vel$time
q <- f_to_srvf(growth_vel$f[, 1:31], t)
r <- sapply(1:(ncol(q) - 1), function(i) {
gd <- optimum.reparam(q[, i], t, q[, i + 1], t, method = "DP")
gr <- optimum.reparam(q[, i], t, q[, i + 1], t, method = "RBFGS")
dp_cost(q[, i], q[, i + 1], gr, t) / dp_cost(q[, i], q[, i + 1], gd, t)
})
expect_lt(stats::median(r), 1.0)
expect_lt(max(r), 1.25)
})
test_that("`rlbfgs()` starts at gam0 and descends from it", {
# the old solver started up to 2.9x above the cost of a DP warp and stalled
t <- growth_vel$time
q <- f_to_srvf(growth_vel$f[, 11:12], t)
q1 <- q[, 1] / sqrt(sum(q[, 1]^2))
q2 <- q[, 2] / sqrt(sum(q[, 2]^2))
gd <- optimum.reparam(q1, t, q2, t, method = "DP")
t1 <- seq(0, 1, length.out = length(t))
out1 <- rlbfgs(q1, q2, t1, 1, 0, 0, gd)
out30 <- rlbfgs(q1, q2, t1, 30, 0, 0, gd)
psi1 <- sqrt(pmax(gradient(c(out1$gam), 1 / (length(t1) - 1)), 0))
psid <- sqrt(pmax(gradient(gd, 1 / (length(t1) - 1)), 0))
expect_lt(acos(min(trapz(t1, psi1 * psid), 1)), 0.15)
expect_lt(out30$info[3], out1$info[3])
})
test_that("`rlbfgs()` starts from gam0 and reports its stop", {
q1n <- kw_q1 / sqrt(sum(kw_q1^2))
q2n <- kw_q2 / sqrt(sum(kw_q2^2))
out <- rlbfgs(q1n, q2n, kw_time, 30, 0, 0, kw_gam_inv)
expect_length(out$info, 4)
expect_true(out$info[2] %in% 1:4)
expect_true(all(diff(out$gam) >= 0))
expect_lt(max(abs(out$gam - kw_gam_inv)) * (length(kw_time) - 1), 0.5)
expect_error(rlbfgs(q1n, q2n, kw_time, 30, 0, 0, kw_gam_inv[-1]), "gam0")
})
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.