tests/testthat/test-laws.R

test_that("loglik is the log-probability of the sequence", {
  for (x in list(model_ab1(), model_ab2())) {
    m <- 5
    ap <- all_paths(x, m)
    ll <- vapply(seq_len(nrow(ap$paths)), function(i) loglik(x, ap$paths[i, ])[[1]], numeric(1))
    expect_equal(exp(ll), unname(ap$prob))
    expect_equal(sum(exp(ll)), 1)
  }
})

test_that("aic and bic are computed from loglik", {
  x <- model_ab1()
  s <- c("a", "b", "b", "a", "a")
  ll <- loglik(x, s)[[1]]
  nb <- 2 * 2 * 1
  expect_equal(aic(x, s)[[1]], -2 * ll + 2 * nb)
  expect_equal(bic(x, s)[[1]], -2 * ll + nb * log(5))
})

test_that("getDistribution and distributions give the law of X_t", {
  for (x in list(model_ab1(), model_ab2())) {
    m <- x$length
    ap <- all_paths(x, m)
    exact <- t(vapply(0:m, function(t) tapply(ap$prob, ap$paths[, t + 1], sum)[ab], numeric(2)))
    k <- x$order
    all <- getDistribution(x, all.pos = TRUE)
    expect_equal(unname(all), unname(exact[(k:m) + 1, ]))
    d <- distributions(x)[[1]]
    expect_equal(d[, "position"], 1:m)
    expect_equal(unname(d[, ab]), unname(exact[(1:m) + 1, ]))
    expect_equal(unname(distributions(x, start = 0, end = 0)[[1]][, ab]), unname(exact[1, ]))
  }
  x <- model_ab1()
  expect_equal(as.vector(getDistribution(x, pos = 4)), unname(getDistribution(x, all.pos = TRUE)[4, ]))
})

test_that("reliability, availability, maintainability and failure rates", {
  U <- "a"
  for (x in list(model_ab1(), model_ab2())) {
    k <- x$order
    k2 <- 4
    ap <- all_paths(x, k2 + k - 1)
    up <- ap$paths == U
    exactR <- vapply(0:k2, function(t) sum(ap$prob[rowSums(!up[, 1:(t + k), drop = FALSE]) == 0]), numeric(1))
    exactA <- vapply(0:k2, function(t) sum(ap$prob[up[, t + k]]), numeric(1))
    exactM <- vapply(0:k2, function(t) 1 - sum(ap$prob[rowSums(up[, 1:(t + k), drop = FALSE]) == 0]), numeric(1))

    R <- reliability(x, k1 = 0, k2 = k2, upstates = U)[[1]]
    expect_equal(R[, "positions"], 0:k2)
    expect_equal(R[, "reliability"], exactR)
    expect_equal(availability(x, k1 = 0, k2 = k2, upstates = U)[[1]][, "availability"], exactA)
    expect_equal(maintainability(x, k1 = 0, k2 = k2, upstates = U)[[1]][, "maintainability"], exactM)
    expect_equal(reliability(x, k1 = 2, k2 = k2, upstates = U)[[1]][, "reliability"], exactR[3:5])

    bmp <- failureRate(x, k1 = 0, k2 = k2, upstates = U, failure.rate = "BMP")[[1]][, "BMP"]
    expect_equal(bmp, 1 - exactR / c(1, exactR[-(k2 + 1)]))
    rg <- failureRate(x, k1 = 0, k2 = k2, upstates = U, failure.rate = "RG")[[1]][, "RG"]
    expect_equal(rg, -log(exactR / c(1, exactR[-(k2 + 1)])))
  }
})

test_that("word probabilities", {
  x <- model_ab1(length = 10)
  # P(word "ab" at 3) = stationary law of Pi_3 at "a" times Pi_4(a, b)
  st <- getStationaryLaw(x, 3, internal = TRUE)
  expect_equal(unname(word_probability("ab", 3, x)),
               unname(st[1] * getTransitionMatrix(x, 4)["a", "b"]))
  s <- c("a", "b", "b", "a", "a", "b")
  res <- lengthWord_probabilities(2, s, c(1, 4), x)[[1]]
  expect_type(res$position, "integer")
  expect_type(res$probability, "double")
  expect_equal(res$word, c("ab", "bb", "ba", "aa"))
  expect_equal(res$probability[1], unname(word_probability("ab", 0, x)))
})

Try the drimmR package in your browser

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

drimmR documentation built on Nov. 28, 2026, 1:08 a.m.