tests/testthat/test-iks.R

test_that("iks gives the two-sample Kolmogorov-Smirnov statistic", {
  set.seed(1)
  a <- rnorm(150)
  b <- rnorm(150, 0.4)
  s <- iks(seed = 1)
  expect_equal(iks_statistic(s), 0)
  for (i in seq_along(a)) s <- iks_add(iks_add(s, a[i], 0), b[i], 1)
  expect_equal(iks_statistic(s), unname(ks.test(a, b)$statistic))
  expect_equal(s$n, c(150, 150))

  # sliding the window of B
  new <- rnorm(60, 1)
  for (i in seq_along(new)) s <- iks_add(iks_remove(s, b[i], 1), new[i], 1)
  b2 <- c(b[-(1:60)], new)
  expect_equal(iks_statistic(s), unname(ks.test(a, b2)$statistic))
  # Kuiper statistic: max(F_A - F_B) + max(F_B - F_A)
  grid <- sort(c(a, b2))
  diff <- ecdf(a)(grid) - ecdf(b2)(grid)
  expect_equal(iks_kuiper(s), max(diff, 0) + max(-diff, 0))
  expect_equal(iks_test(s, ca = 1.95), iks_statistic(s) > 1.95 * sqrt(2 / 150))

  # the priorities (seed) do not change the statistic
  s2 <- iks(seed = 99)
  for (i in seq_along(a)) s2 <- iks_add(iks_add(s2, a[i], 0), b2[i], 1)
  expect_equal(iks_statistic(s2), iks_statistic(s))
  expect_output(print(s2), "Incremental KS sketch: 150 \\+ 150")
})

test_that("iks handles ratios, ties and errors", {
  set.seed(2)
  a <- rnorm(40)
  b <- rnorm(80)
  s <- iks(ratio = 0.5, seed = 1)
  for (v in a) s <- iks_add(s, v, 0)
  for (v in b) s <- iks_add(s, v, 1)
  expect_equal(iks_statistic(s), unname(ks.test(a, b)$statistic))

  # tied values with tie-breaking values can be removed by (x, tie)
  s <- iks(seed = 1)
  s <- iks_add(s, 1, 0, tie = 0.2)
  s <- iks_add(s, 1, 1, tie = 0.7)
  s <- iks_add(s, 2, 1, tie = 0.1)
  s <- iks_add(s, 0, 0, tie = 0.5)
  s <- iks_remove(s, 1, 1, tie = 0.7)
  s <- iks_add(s, 3, 1, tie = 0.4)
  expect_equal(iks_statistic(s), unname(ks.test(c(0, 1), c(2, 3))$statistic))
  expect_error(iks_remove(s, 1, 1, tie = 0.3), "not in the sample")
  expect_error(iks_add(s, NA, 0), "one number")
  expect_error(iks_add(s, 1, 2), "0 or 1")
  expect_error(iks(ratio = 0), "positive")
  expect_equal(iks_ca(0.001), sqrt(-0.5 * log(0.0005)))
  expect_false(iks_test(iks()))
})

test_that("dfr_iks detects a shift against the reference window", {
  set.seed(3)
  data <- data.frame(a = c(rnorm(300), rnorm(300, 1.5)), b = rnorm(600), label = "x")
  det <- fit(dfr_iks(window_size = 100, seed = 1), data)
  out <- det$drifter_output
  expect_named(out, c("D_a", "D_b", "drift"))
  expect_true(all(is.na(out$D_a[1:99])))
  alarms <- which(out$drift)
  expect_gt(length(alarms), 0)
  expect_gt(alarms[1], 300)
  expect_true(det$drifted)
  expect_identical(det$state$drifted_features, "a")
  expect_false(reset_state(det)$drifted)

  quiet <- fit(dfr_iks(window_size = 100, seed = 1), data.frame(a = rnorm(500)))
  expect_false(any(quiet$drifter_output$drift))

  cons <- fit(dfr_iks(target_feat = "a", window_size = 50, reference = "consecutive", seed = 1), data)
  expect_named(cons$drifter_output, c("D_a", "drift"))
  expect_true(all(is.na(cons$drifter_output$D_a[1:99])))
  expect_true(any(cons$drifter_output$drift[301:600]))

  expect_error(dfr_iks(window_size = 1), "window_size")
  expect_error(fit(dfr_iks(target_feat = "z"), data), "missing features")
})

Try the heimdall package in your browser

Any scripts or data that you put into this service are public.

heimdall documentation built on Dec. 2, 2026, 5:08 p.m.