**Overview**

A significant number of algorithms used in machine learning relies on dynamic programming and hidden Markov model (HMM) is no exception. Scala tail elimination represents an excellent alternative to the traditional iterative implementation of the 3 canonical forms of the HMM.

**Introduction to HMM**

Markov processes, and more specifically HMM, are commonly used in speech recognition, language translation, and text classification, document tagging, data compression and decoding.

A HMM algorithm uses 3 key components

A HMM algorithm uses 3 key components

- A set of observations
- A sequence of hidden states
- A model that maximizes the joint probability of the observations and hidden states, known as the Lambda model

There are 3 use cases (or canonical forms) of the HMM.

**Evaluation**: Evaluate the probability of a given sequence of observations, given a model**Training**: Identify (or learn) a model given a set of observations**Decoding**Estimate the state sequence with the highest probability to generate a given as set of observations and a model

**Viterbi recursive implementation**

Given a sequence of states {qt} and sequence of observations {oj}, the probability δt(i) for any sequence to have the highest probability path for the first T observations is defined for the state Si.

**Delta**: sequence to have the highest probability path for the first i observations is defined for a specific test δt(i)**Qstar**: the optimum sequence q* of states Q0:T-1

**HMMState**classfinal protected class HMMState(val lambda: HMMLambda, val maxIters: Int) { // Matrix of elements (t, i) that defines the highest probability of // a single path of t observations reaching state S(i) val delta = Matrix[Double](lambda.getT, lambda.getN) // Auxiliary matrix of indices that maximize the probability of a //given sequence of states val psi = Matrix[Int](lambda.getT, lambda.getN) // Singleton to compute the sequence Q* of states with the highest // probability given a sequence of observations. object QStar { private val qStar = Array.fill(lambda.getT)(0) // Update Q* the optimum sequence of state using backtracking.. def update(t: Int, index: Int): Unit ... } }

The algorithm is conveniently illustrated by the following diagram.

First, Let's create the key member and method for the Lambda model for HMM. The model is defined as a tuple of the transition probability matrix A, emission probability matrix B and the initial probability π

final protected class HMMLambda( val A: Matrix[Double], val B: Matrix[Double], var pi: Array[Double], val numObs: Int) { // Number of observations @inline def getT: Int = numObs // Number of states for a sequence of observations @inline def getN: Int = A.nRows // Number of unique symbols (problem dimension) u @inline def getM: Int = B.nCols // Compute a new estimate of the log of the conditional // probabilities for a given iteration. def estimate(state: HMMState, obs: Array[Int]): Unit // Normalize the state transition matrix A, emission matrix B // and the initial probabilities pi. def normalize: Unit }

Then let's define the basic components for implementing the Viterbi algorithm. The class

The Viterbi algorithm is fully defined with

**ViterbiPath**encapsulates the member variables critical to the recursion.The Viterbi algorithm is fully defined with

**lambda**: Lambda model defined above**state**: State of the computation**obsIndx**: Index of observed states

class ViterbiPath(lambda: HMMLambda, state: HMMState, obsIndx: Array[Int]) { val maxDelta = recurse(lambda.config._T, 0) … }

The recursive method,

**recurse**that implements the formula or steps defined earlier. The method relies on the tail recursion. Tail recursion or tail elimination algorithm avoid the creation of a new stack frame for each invocation, improving the performance of the entire recursion.@scala.annotation.tailrec private def recurse(t: Int): Double = { // Initialization of the delta value, return -1.0 in case of error if( t == 0) initial // then for the subsequent observations ... else { // Update the maximum delta value and its state index for the observation t Range(0, lambda.getN).foreach( updateMaxDelta(t, _) ) // If we reached the last observation... exit by backtracing the // computation of the if( t == obs.size-1) { val idxMaxDelta = Range(0, lambda.getN).map(i => (i, state.delta(t, i))).maxBy(_._2) // Update the Q* value with the index that maximize the delta.A state.QStar.update(t+1, idxMaxDelta._1) idxMaxDelta._2 } else recurse(t+1) } }

Once initialized (

**1**), the maximum value of delta, maxDelta, is computed recursively by applying the formula at each state, s using the Scala maxBy method. (**2**). Next, the index of the column of the transition matrix A corresponding to the maximum of delta is computed (**3**). The last step is to update the matrix psi (**4**) (resp. delta (**5**)). Once the step t reaches the maximum number of observation labels (**6**), the optimum sequence of state q* is computed (**7**).**References**

Scala for machine learning Packt Publishing 2014

*Pattern Recognition and Machine Learning; Chap 13 Sequential Data/Hidden Markov Models*C. Bishop Springer 2009