tests/testthat/test-forecast.R

test_that("windows retain all available data and honor stride", {
  f <- hmix:::smart_reframer
  expect_equal(unname(f(1:6, 3, 1)), rbind(1:3, 2:4, 3:5, 4:6))
  expect_equal(unname(f(1:7, 3, 3)), rbind(2:4, 5:7))
})

test_that("forecast distribution and CRPS use the samples", {
  d <- hmix:::sample_distribution(c(1, 2, 2, 4))
  expect_equal(d$pfun(c(0, 1, 2, 4, 5)), c(0, .25, .75, 1, 1))
  expect_equal(d$qfun(c(0, .5, 1)), c(1, 2, 4))
  expect_true(all(d$rfun(50) %in% c(1, 2, 4)))
  x <- c(1, 2, 2, 4)
  for (y in c(-2, 2, 7)) {
    expected <- mean(abs(x - y)) - mean(abs(outer(x, x, "-"))) / 2
    expect_equal(hmix:::crps(list(y), list(list(d)), 1, 1), expected)
  }
})

test_that("input validation catches unsupported representations", {
  expect_error(hmix(c(1, 0, 2, 3, 4, 5), 2, transform = "log", n_tests = 0), "positive")
  expect_error(hmix(1:30, 5, stride = 2, n_tests = 0), "divisor")
  expect_error(hmix(rep(1, 30), 2, centers = 3, n_tests = 0), "distinct")
})

test_that("difference forecasts support zero and negative values", {
  set.seed(18)
  x <- cumsum(rnorm(80))
  result <- hmix(x, 2, centers = 3, n_hidden = 2, n_tests = 1,
                 n_samples = 100, transform = "difference")
  expect_equal(dim(result$error_sets$crps_set), c(1, 2))
  expect_true(all(is.finite(result$error_sets$crps_set)))
  expect_true(all(is.finite(result$model$pred_funs$t1$rfun(10))))
})

test_that("complexity tuning compares candidates and reports failures", {
  set.seed(19)
  x <- cumsum(rnorm(70))
  candidates <- data.frame(centers = c(2, 100), n_hidden = c(2, 2))
  tuned <- tune_hmix(x, 2, candidates, n_tests = 1, n_samples = 50,
                     transform = "difference")
  expect_equal(tuned$best$centers, 2)
  expect_true(is.finite(tuned$results$mean_crps[1]))
  expect_true(nzchar(tuned$results$error[2]))
})

test_that("one-step backtests keep matrix dimensions and reproduce forecasts", {
  set.seed(24)
  x <- cumsum(rnorm(65))
  a <- hmix(x, 1, centers = 2, n_hidden = 2, n_tests = 2, n_samples = 30, seed = 7)
  b <- hmix(x, 1, centers = 2, n_hidden = 2, n_tests = 2, n_samples = 30, seed = 7)
  expect_equal(dim(a$error_sets$crps_set), c(2, 1))
  expect_equal(a$error_sets$crps_set, b$error_sets$crps_set)
  expect_equal(a$model$pred_funs$t1$qfun(c(.1, .5, .9)), b$model$pred_funs$t1$qfun(c(.1, .5, .9)))
  expect_length(attr(a$model$pred_funs$t1, "samples"), 30)
  expect_equal(dim(a$model$random_path(4)), c(4, 1))
})

test_that("path sampler keeps steps together and validates n", {
  paths <- rbind(c(1, 100), c(2, 200), c(3, 300))
  colnames(paths) <- c("t1", "t2")
  draw <- hmix:::path_sampler(paths)
  set.seed(11)
  sampled <- draw(50)
  expect_equal(dim(sampled), c(50, 2))
  expect_equal(colnames(sampled), c("t1", "t2"))
  expect_true(all(sampled[, 2] == sampled[, 1] * 100))
  expect_equal(dim(draw(0)), c(0, 2))
  expect_error(draw(-1), "nonnegative integer")
  expect_error(draw(1.5), "nonnegative integer")
})

test_that("model path draws correspond to its marginal samples", {
  set.seed(31)
  x <- cumsum(rnorm(70))
  fit <- hmix(x, 2, centers = 2, n_hidden = 2, n_tests = 0, n_samples = 40)
  set.seed(9)
  paths <- fit$model$random_path(100)
  expect_equal(dim(paths), c(100, 2))
  for (i in seq_len(2)) expect_true(all(paths[, i] %in% attr(fit$model$pred_funs[[i]], "samples")))
})

Try the hmix package in your browser

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

hmix documentation built on Dec. 2, 2026, 1:09 a.m.