forward_backward: Forward and Backward Probabilities for Hidden Markov Model

View source: R/forwardBackward.R

forward_backwardR Documentation

Forward and Backward Probabilities for Hidden Markov Model

Description

The forward_backward function computes scaled forward and backward probabilities of a hidden Markov model.

Usage

forward_backward(model, forward_only = FALSE, log_space = FALSE, threads = 1)

Arguments

model

Object of class hmm or mhmm.

forward_only

If TRUE, only forward probabilities are computed. The default is FALSE.

log_space

Compute forward and backward probabilities in logarithmic scale instead of scaling. The default is FALSE.

threads

Number of threads used in parallel computing. The default is 1.

Value

List with components

forward_probs

If log_space = FALSE, scaled forward probabilities, i.e. probability of state given observations up to that time point. If log_space = TRUE, logarithms of non-scaled forward probabilities.

backward_probs

Scaled backward probabilities (log_space = FALSE), or logarithms of non-scaled backward probabilities(log_space = TRUE).

scaling_factors

Sum of non-scaled forward probabilities at each time point. Only computed if log_space = FALSE.

In case of multiple observations, these are computed independently for each sequence.

Examples

# Load a pre-defined MHMM
data("mhmm_biofam")

# Compute forward and backward probabilities
fb <- forward_backward(mhmm_biofam)

# The most probable hidden state at time t
# given the observations up to time t for the first subject:
apply(fb$forward_probs[, , 1], 2, which.max)


helske/seqHMM documentation built on July 6, 2023, 6:45 a.m.