tests/testthat/test-analysis.R

test_that("model analysis methods return expected AR(1) quantities", {
  model <- varmapack_model(A = matrix(0.5, 1, 1), Sig = matrix(1, 1, 1))
  expect_equal(model$specrad(), 0.5)
  expect_equal(model$ma_specrad(), 0)
  expect_equal(drop(model$acvf(1)), c(4/3, 2/3), tolerance = 1e-12)
  expect_equal(drop(model$psi(2)), c(1, 0.5, 0.25), tolerance = 1e-12)
  expect_equal(drop(model$irf(2)), c(1, 0.5, 0.25), tolerance = 1e-12)
})

test_that("multivariate impulse responses preserve matrix orientation", {
  A <- matrix(c(0.1, 0.3, 0.2, 0.4), 2, 2)
  B <- matrix(c(0.5, 0.7, 0.6, 0.8), 2, 2)
  Sig <- matrix(c(1, 0.25, 0.25, 2), 2, 2)
  model <- varmapack_model(A = A, B = B, Sig = Sig)
  expected <- array(c(
    1, 0, 0, 1,
    0.6, 1.0, 0.8, 1.2,
    0.26, 0.58, 0.32, 0.72
  ), c(2L, 2L, 3L))
  expect_equal(model$A[, , 1], A)
  expect_equal(model$B[, , 1], B)
  expect_equal(model$psi(2), expected, tolerance = 1e-12)
  expect_gt(model$specrad(), 0)
  expect_gt(model$ma_specrad(), 0)
})

test_that("sample autocovariance uses series in rows", {
  X <- rbind(1:8, 2*(1:8))
  C <- varmapack_autocov(X, maxlag = 1)
  expect_equal(dim(C), c(2L, 2L, 2L))
  expect_equal(C[1, 2, 1], 2*C[1, 1, 1])
  Xc <- X - rowMeans(X)
  expected <- Xc[, 2:8] %*% t(Xc[, 1:7])/8
  expect_equal(C[, , 2], expected)
  expect_error(varmapack_autocov(X, maxlag = 8))
})

test_that("sample autocovariance supports both normalizations", {
  X <- matrix(c(1, 3, 5, 7), 1, 4)
  Xc <- X - rowMeans(X)
  ml <- varmapack_autocov(X, maxlag = 2, norm = "ML")
  corrected <- varmapack_autocov(X, maxlag = 2, norm = "C")
  for (lag in 0:2) {
    current <- (lag + 1):4
    previous <- 1:(4 - lag)
    product <- Xc[, current, drop = FALSE] %*%
      t(Xc[, previous, drop = FALSE])
    expect_equal(drop(ml[, , lag + 1]), drop(product)/4)
    expect_equal(drop(corrected[, , lag + 1]), drop(product)/(4 - lag))
  }
})

test_that("covariance sequences convert to correlations", {
  cov <- array(c(4, 3, 3, 9, 2, 6, -3, 1.5), dim = c(2, 2, 2))
  original <- cov
  expected <- array(c(1, 0.5, 0.5, 1, 0.5, 1, -0.5, 1/6),
                    dim = c(2, 2, 2))
  corr <- varmapack_cov2corr(cov)
  expect_equal(corr, expected)
  expect_identical(cov, original)
  expect_equal(varmapack_cov2corr(cov[, , 1]), expected[, , 1])
})

test_that("theoretical autocovariance rejects VARMAX models", {
  model <- varmapack_model(C = matrix(0.2, 1, 1), Sig = matrix(1, 1, 1))
  expect_error(model$acvf(1))
})

test_that("innovation covariance agrees with scalar formulas", {
  expect_equal(varmapack_cov2sig(matrix(4), A = matrix(0.5)), matrix(3))
  expect_equal(varmapack_cov2sig(matrix(2.125), B = matrix(0.25)), matrix(2))
  expect_equal(varmapack_cov2sig(matrix(3.5), A = matrix(0.5), B = matrix(0.25)),
               matrix(2))
  cov <- matrix(c(2, 0.3, 0.3, 1), 2, 2)
  expect_equal(varmapack_cov2sig(cov), cov)
})

test_that("innovation covariance preserves matrix orientation and inputs", {
  A <- matrix(c(0.4, -0.1, 0.2, 0.3), 2, 2)
  cov <- matrix(c(2, 0.3, NaN, 1), 2, 2)
  original <- cov
  full <- matrix(c(2, 0.3, 0.3, 1), 2, 2)
  expect_equal(varmapack_cov2sig(cov, A = A), full - A %*% full %*% t(A),
               tolerance = 1e-12)
  expect_identical(cov, original)
  for (target in list(matrix(1, 2, 2), matrix(0, 2, 2)))
    expect_equal(varmapack_cov2sig(target, A = 0.5*diag(2)), 0.75*target)
})

test_that("innovation covariance round trips through all named models", {
  for (name in varmapack_testcases()$name) {
    model <- varmapack_testcase(name)
    cov <- matrix(model$acvf(0), model$r, model$r)
    original <- cov
    A <- model$A
    B <- model$B
    Sig <- varmapack_cov2sig(cov, A = A, B = B)
    expect_equal(Sig, model$Sig, tolerance = 1e-9, info = name)
    recovered <- varmapack_model(A = A, B = B, Sig = Sig)$acvf(0)
    expect_equal(matrix(recovered, model$r, model$r), cov,
                 tolerance = 1e-9, info = name)
    expect_identical(cov, original)
    expect_identical(model$A, A)
    expect_identical(model$B, B)
  }
})

Try the varmapack package in your browser

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

varmapack documentation built on Nov. 19, 2026, 5:09 p.m.