tests/testthat/test-validation.R

test_that("public arguments reject malformed or insufficient inputs", {
  set.seed(51)
  x <- cumsum(rnorm(80))
  expect_error(hmix(c(1, NA_real_, 3), 1, n_tests = 0), "finite numeric vector")
  expect_error(hmix(matrix(1:80), 1, n_tests = 0), "finite numeric vector")
  expect_error(hmix(x, 0, n_tests = 0), "valid integers")
  expect_error(hmix(x, 2, n_samples = 1, n_tests = 0), "valid integers")
  expect_error(hmix(x, 2, seed = -1, n_tests = 0), "valid integers")
  expect_error(hmix(x, 2, warmup = 1, n_tests = 0), "warmup")
  expect_error(hmix(x, 2, max_iterations = 0, n_tests = 0), "positive integers")
  expect_error(hmix(x, 2, kmeans_starts = 0, n_tests = 0), "positive integers")
  expect_error(hmix(x, 2, training_window = 2, n_tests = 0), "positive integers")
  expect_error(hmix(x, 2, n_tests = 2, warmup = 0.05), "few training windows")
  expect_error(hmix(c(1, 0, 1:78), 2, transform = "return", n_tests = 0), "nonzero")
  expect_error(tune_hmix(x, 2, candidates = data.frame(foo = 1)), "candidates")
  expect_error(tune_hmix(x, 2, n_tests = 0), "positive integer")
})

test_that("empirical marginals and scores behave correctly at boundaries", {
  d <- hmix:::sample_distribution(rep(7, 5))
  expect_equal(d$pfun(c(6, 7, 8)), c(0, 1, 1))
  expect_equal(d$qfun(c(0, .5, 1)), rep(7, 3))
  expect_true(all(d$rfun(10) == 7))
  expect_true(is.finite(d$dfun(7)))
  expect_equal(hmix:::sample_crps(rep(7, 5), 10), 3)
  expect_equal(hmix:::sample_crps(rep(7, 5), 7), 0)
  expect_error(d$qfun(1.1), "probs")
  expect_error(hmix:::sample_distribution(c(NA_real_, Inf)), "no finite")
})

test_that("fitted HMM has normalized probabilities and valid paths", {
  set.seed(71)
  x <- exp(cumsum(rnorm(100, 0, .01)))
  fit <- hmix(x, 4, centers = 3, n_hidden = 2, n_tests = 0,
              stride = 2, n_samples = 50)
  trained <- fit$model$hmm_model$trained_hmm$hmm
  expect_equal(unname(rowSums(trained$transProbs)), c(1, 1), tolerance = 1e-10)
  expect_equal(unname(rowSums(trained$emissionProbs)), c(1, 1), tolerance = 1e-10)
  expect_true(all(trained$transProbs >= 0))
  expect_true(all(trained$emissionProbs >= 0))
  expect_true(all(fit$model$random_path(20) > 0))
  expect_equal(length(fit$model$pred_funs), 4)
  expect_null(fit$error_sets)
})

test_that("a single hidden state produces valid forecasts", {
  x <- exp(cumsum(sin(seq_len(80)) / 100))
  fit <- hmix(x, 3, centers = 3, n_hidden = 1, n_tests = 0, n_samples = 30)
  expect_equal(dim(fit$model$random_path(4)), c(4, 3))
  expect_equal(unname(rowSums(fit$model$hmm_model$trained_hmm$hmm$transProbs)), 1)
})

test_that("the first backtest origin does not use observations beyond its horizon", {
  set.seed(81)
  x <- exp(cumsum(rnorm(100, 0, .01)))
  later <- x
  later[56:100] <- -abs(later[56:100])
  a <- hmix(x, 5, centers = 3, n_hidden = 2, n_tests = 2,
            warmup = 0.5, n_samples = 100)
  b <- hmix(later, 5, centers = 3, n_hidden = 2, n_tests = 2,
            warmup = 0.5, n_samples = 100)
  expect_equal(a$error_sets$crps_set[1, ], b$error_sets$crps_set[1, ])
})

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.