tests/testthat/test.forward.R

# Note this is the example from https://en.wikipedia.org/wiki/Forward–backward_algorithm
p_state_0 <- c(0.5, 0.5)
trn_mtx <- matrix(c(0.7, 0.3, 0.3, 0.7), 2, 2, byrow = TRUE)
p_obs <- matrix(c(0.9, 0.2, 0.9, 0.2, 0.1, 0.8, 0.9, 0.2, 0.9, 0.2), 5, 2, byrow = TRUE)

test_that("forward", {
  f_p <- forward(p_state_0, trn_mtx, p_obs)

  exp_results <- matrix(
    c(0.45, 0.1, 0.5645, 0.0745, 0.0653, 0.2772, 0.3386, 0.1247, 0.5331, 0.0815),
    nrow = 5,
    ncol = 2,
    byrow = TRUE
  )

  exp_norm_results <- matrix(
    c(0.8182, 0.1818, 0.8834, 0.1166, 0.1907, 0.8093, 0.7308, 0.2692, 0.8673, 0.1327),
    nrow = 5,
    ncol = 2,
    byrow = TRUE
  )

  expect_true(all(f_p$forward_p - exp_results < 1e-3))
  expect_true(all(f_p$norm_forward_p - exp_norm_results < 1e-3))
})
ricky-kotecha/rkHMM documentation built on May 4, 2020, 12:08 a.m.