compute_loglikelihood {regmhmm}R Documentation

Log-Likelihood Calculation for Hidden Markov Models (Forward Algorithm)

Description

Calculate the log-likelihood given parameters of a hidden Markov model using the forward algorithm. This function aids in assessing the likelihood of the observed data under the specified model.

Usage

compute_loglikelihood(delta, Y, A, B, X, family)

Arguments

delta

a vector of length S specifying the initial probabilities.

Y

a vector of observations of size T.

A

a matrix of size S x S specifying the transition probabilities.

B

a matrix of size S x (p + 1) specifying the GLM parameters of the emission probabilities.

X

a design matrix of size T x p.

family

the family of the response.

Value

A value that is the likelihood in log scale.

Examples

# Example usage of the function
parameters_setting <- list()
parameters_setting$emis_mat <- matrix(NA, nrow = 2, ncol = 4)
parameters_setting$emis_mat[1, 1] <- 0.1
parameters_setting$emis_mat[1, 2] <- 0.5
parameters_setting$emis_mat[1, 3] <- -0.75
parameters_setting$emis_mat[1, 4] <- 0.75
parameters_setting$emis_mat[2, 1] <- -0.1
parameters_setting$emis_mat[2, 2] <- -0.5
parameters_setting$emis_mat[2, 3] <- 0.75
parameters_setting$emis_mat[2, 4] <- 1
parameters_setting$trans_mat <- matrix(NA, nrow = 2, ncol = 2)
parameters_setting$trans_mat[1, 1] <- 0.65
parameters_setting$trans_mat[1, 2] <- 0.35
parameters_setting$trans_mat[2, 1] <- 0.2
parameters_setting$trans_mat[2, 2] <- 0.8
parameters_setting$init_vec <- c(0.65, 0.35)
dat <- simulate_HMM_data(
  seed_num = 1,
  p_noise = 7,
  N = 100,
  N_persub = 10,
  parameters_setting = parameters_setting
)
llh_C <- compute_loglikelihood(
  delta = parameters_setting$init_vec,
  Y = dat$y_mat[1, ],
  A = parameters_setting$trans_mat,
  B = parameters_setting$emis_mat,
  X = dat$X_array[, 1:4, 1],
  family = "P"
)


[Package regmhmm version 1.0.0 Index]