Docsity

Lecture Notes on Hidden Markov Models - Statistics Computing | STAT 535, Study notes of Statistics

Toggle sidebar
bg1
Stat 535 lecture 2 notes: hidden Markov models
(HMMs)
1 Mixture models
The simplest interesting graphical model is the mixture model, with a state variable Qand
an output variable Ygoverned by PQY =PQPY|Q:
Q
Y
This can be viewed as a generative model that generates samples independently of each
other by the following process:
1. for sample i, choose state qiaccording to PQ;
2. then choose output yiaccording to PY|qi.
In typical applications the state qiis hidden (aka latent), meaning that it is not directly
observable but can only be inferred from yi.
If Q={1. . . n},Y=Rk, and P(y|q)∼ N (µq,Σq) then PYis an n-component Gaussian
mixture with mixture weights P(q), with pdf
P(y) = X
q=1...n
P(q)1
p(2π)k|Σ|exp(−1
2(y−µq)0Σ−1
q(y−µq)).
For example, an equally weighted 3-component mixture in R2:
1
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13

Partial preview of the text

Download Lecture Notes on Hidden Markov Models - Statistics Computing | STAT 535 and more Study notes Statistics in PDF only on Docsity!

Stat 535 lecture 2 notes: hidden Markov models

(HMMs)

1 Mixture models

The simplest interesting graphical model is the mixture model, with a state variable Q and an output variable Y governed by PQY = PQPY |Q:

Q

Y

This can be viewed as a generative model that generates samples independently of each other by the following process:

  1. for sample i, choose state qi according to PQ;
  2. then choose output yi according to PY |qi.

In typical applications the state qi is hidden (aka latent), meaning that it is not directly observable but can only be inferred from yi.

If Q = { 1... n}, Y = Rk, and P (y|q) ∼ N (μq, Σq) then PY is an n-component Gaussian mixture with mixture weights P (q), with pdf

P (y) =

q=1...n

P (q)

(2π)k|Σ|

exp(−

(y − μq)′Σ− q 1 (y − μq)).

For example, an equally weighted 3-component mixture in R^2 :

−2−2 0 2 4 6 8 10 12

0

2

4

6

8

10

12

probability density

−2 −2 0 2 4 6 8 10 12

0

2

4

6

8

10

12

“labeled” samples

−2 −2 0 2 4 6 8 10 12

0

2

4

6

8

10

12

observed samples

Mixture models are often used for classification tasks. In training, labeled pairs (qi, yi) are used to estimate the distributions P (q) and P (y|q). Then given an unlabeled point y one can calculate a distribution over the hidden label (state) using Bayes’ rule:^1

P (q|y) =

P (y, q) P (y)

∝ P (y|q)P (q).

For example, one might use a mixture model to classify small n × n binary images of handwritten characters into labels A..Z0..9. Although far from optimal, a simple param- eterization is P (y = y 11... ynn|q) =

ij Pijq(yij^ ); this^ naive Bayes^ model is equivalent to the graphical model:

(^1) This is playing fast and loose with notation: for discrete Y , P (y, q) and P (y) are probabilities; for continuous Y , P (y, q) and P (y) are probability densities, but the formula still holds.

In the example of handwritten character recognition, the hidden state dependence can be used to model structure in character sequences, for example the tendency for vowels to follow consonants and vice versa.

Consider again the 3-component Gaussian mixture assuming at every step the state pro- gression is governed by

P (qi+1|qi) =

. 8 if qi+1 = qi . 1 otherwise

In that case, the marginal probability distribution is the same as before (all states are equally likely) but consecutive samples are highly correlated:

−2 −2 0 2 4 6 8 10 12

0

2

4

6

8

10

12

labeled sample sequence

Using knowledge of the state dynamics and output history y 1... yi, it is possible to dra- matically improve our predictive power. We will see how to calculate P (qi+1|y 1... yi), and from that P (yi+1|y 1... yi).

−2−2 0 2 4 6 8 10 12

0

2

4

6

8

10

12

−2 −2 0 2 4 6 8 10 12

0

2

4

6

8

10

12

P (yi+1) P (yi+1|y 1 ...i) (last 5 samples shown)

3 Operations

HMMs are used many different ways:

  • Smoothing: If Q represents a hidden true signal that is corrupted by independent noise to produce an observed signal Y , then the distribution PQ|y can be used to “de-noise”, or smooth, the observation. A simple model of noise is PY |q ∼ N (q, σ).
  • Prediction: Given a sequence of observations y 1... yt, predict yt+1 using the identity P (yt+1|y 1... yt) =

qt+1 P^ (qt+1|y^1... yt)P^ (yt+1|qt+1).^ Such models are commonly used in target tracking, where Q represents a true position and Y noisy measurements of it. In some text entry devices predictive probabilities are used to alter the size or order of keys to improve entry speed and accuracy.

  • Classification: Y represents a sequential input and Q a desired classification or labeling of its parts. For example, in an information retrieval application yi may be a word from a document y 1... yn and qi a classification of the word into one of one of NameStart, NameMiddle, NameEnd, Other. Model estimation might take place from labeled examples; extraction of names from documents may be performed by calculating arg maxq P (q|y).

where δ(Qi) is the degree of variable Qi in the graphical model. Simply relabeling the terms gives

P (q, y) =

i=2...n ΦQi− 1 Qi (qi−^1 , qi)^

∏^ i=1...n^ ΦQiYi^ (qi, yi) i=1...n ΦQi^ (qi)δ(Qi)−^1

an expression that shows directed HMMs are equivalent to undirected graphical models. Thus, all of the following can express the same distributions:

Q Q Q Q Q Q Q Q Q

Y Y Y Y Y Y Y Y Y

Q Q Q Q Q Q Q Q Q

Y Y Y Y Y Y Y Y Y

Q Q Q Q Q Q Q Q Q

Y Y Y Y Y Y Y Y Y

In fact, we can generalize the expression of the joint probability one step further to

P (x) =

∏^ (s,t)∈E^ Φst(xs, xt) v∈V Φv^ (xv^ )δ(Xv^ )−^1

where V are the vertices in the graphical model and E are the edges. This expression holds for any acyclic undirected graphical model (tree model), and directed graphical models where no node has more than one parent.

5 Inference

Let us calculate P (y) =

q P^ (q, y), a computation that at first blush appears to require a summation over an exponential (in n) number of hidden state configurations.

P (y) =

q

P (q, y)

q 1 ...qn

P (q 1 )

i=2...n

P (qi|qi− 1 )

i=1...n

P (yi|qi)

q 1

P (q 1 )P (y 1 |q 1 )

q 2 ...qn

i=2...n

P (qi|qi− 1 )P (yi|qi)

q 1

P (q 1 )P (y 1 |q 1 )

q 2

P (q 2 |q 1 )P (y 2 |q 2 )

q 3 ...qn

i=3...n

P (qi|qi− 1 )P (yi|qi)

q 1

P (q 1 )P (y 1 |q 1 )

q 2

P (q 2 |q 1 )P (y 2 |q 2 )

q 3

qn

P (qn|qn− 1 )P (yn|qn).

This re-expression requires that multiplication (left-)distributes over addition: cx + cy = c(x+y); there are many operator combinations ⊗ and ⊕ with this property, besides ordinary multiplication and addition of reals. The algorithms developed below can be generalized by choosing other operators.

It’s clear that the key terms of the computation are the products P (qi|qi− 1 )P (yi|qi), so to simplify notation let

A(1)(qi) ≡ P (qi, yi) = P (qi)P (yi|qi) A(i)(qi− 1 , qi) ≡ P (qi, yi|qi− 1 ) = P (qi|qi− 1 )P (yi|qi) for 2 ≤ i ≤ n

Notice that A is a function of the observation y! Then,

P (y) =

q 1

A(1)(q 1 )

q 2

A(2)(q 1 , q 2 )

q 3

qn

A(n)(qn− 1 , qn).

α(i)(qi) =

q 1

A(1)(q 1 )...

qi− 1

A(i−1)(qi− 2 , qi− 1 )A(i)(qi− 1 , qi)

qi− 1

α(i−1)(qi− 1 )A(i)(qi− 1 , qi)

where α(1)(q 1 ) = A(1)(q 1 ), and

β(i)(qi) =

qi+

A(i+1)(qi, qi+1)...

qn

A(n)(qn− 1 , qn)

qi+

A(i+1)(qi, qi+1)β(i+1)(qi+1)

where β(n)(qn) = 1.

5.1 Matrix formulation

Let us express the equations so far using matrix notation, treating α(i)(qi) as a row vector α(i), β(i)(qi) as a column vector β(i), and A(i)(qi− 1 , qi) as a |Qi− 1 | × |Qi| matrix A(i). The matrix A(1)^ is a 1 × |Q 1 | row and the matrix A(n+1)^ is a |Qn| × 1 column. The forward prob- ability calculation is a sequence of vector-matrix products and the backward probability calculation a sequence of matrix-vector products:

A(n+1)^ = 1 α(i)^ =

A(1)^ for i = 1 α(i−1)A(i)^ for i = 2... (n + 1)

β(i)^ =

A(n+1)^ for i = n A(i+1)β(i+1)^ for i = (n − 1)... 0

The calculation of P (y) can be performed using a vector-vector product of forward and backward probabilities at any point i:

P (y) ≡

qi

P (y 1... yi, qi)P (yi+1... yn|qi)

= α(i)β(i)^ for any 1 ≤ i ≤ n = A(1)A(2)^... A(i)A(i+1)^... A(n)A(n+1) = α(n+1)^ = β(0).

Finally, the marginal conditional probabilities γ(qi) ≡ P (qi|y), also known as the complete probabilities, can be calculated from forward and backward probabilities as a simple scalar product with normalization; this is the famous forward-backward algorithm:

γ(qi) ≡ P (qi|y)

=

P (y 1... yi, qi)P (yi+1... yn|qi) P (y)

=

α(i)qi β(i)qi β(0)^

5.2 Trellis diagram

The trellis diagram below depicts the calculation of P (y) for an HMM with 4 steps and 3 possible state values qi ∈ { 1 , 2 , 3 } at each step; P (y) = A(1)A(2)A(3)A(4)A(5). Comput- ing this product of matrices is equivalent to summing over all left-to-right paths through the trellis, each representing a particular hidden state sequence. The calculation is made efficient by summarizing intermediate results in forward probability vectors α(i).

(p, γ[][]) = FORWARD-BACKWARD(y = y 1... yn)

α[][] = new array[n] // [i][q], initialized to 0, forward probs β[][] = new array[n] // [i][q], initialized to 0, backward probs γ[][] = new array[n] // [i][q], initialized to 0, complete probs

// compute forward probabilities for i = 1... n α[i][] = new array[|Qi|] for q = 1... |Qi| if i == 1 α[i][q] = PQ 1 (q) * PYi|Qi (y[i]|q) else for r = 1... |Qi− 1 | α[i][q] += α[i − 1 ][r] * PQi|Qi− 1 (q|r) * PYi|Qi (y[i]|q)

// compute P (y) p = sum q = 1... |Qn| of α[n][q]

// compute backward and complete probabilities for i = n... 1 β[i][] = new array[|Qi|] γ[i][] = new array[|Qi|] for q = 1... |Qi| if i == n β[i][q] = 1 else for r = 1... |Qi+1| β[i][q] += β[i + 1][r] * PQi+1|Qi (r|q) * PYi+1|Qi+1 (y[i + 1]|r) γ[i][q] = α[i][q] * β[i][q] / p

return p, γ[][]

5.4 Associativity and order of operations

The matrix formulation of HMM operations lets us use our understanding of matrix mul- tiplication to gain insight into computation in graphical models. For example, from

P (y) = A(1)A(2)^... A(n)A(n+1)

it is clear that despite starting from a generative definition of HMMs that proceeds left- to-right, in fact by the associative nature of matrix multiplication the calculation can be performed in many orders:

((((((A(1)A(2))A(3))A(4))A(5))A(6))A(7))A(8)^ left to right A(1)(A(2)(A(3)(A(4)(A(5)(A(6)(A(7)A(8))))))) right to left ((A(1)A(2))(A(3)A(4)))((A(5)A(6))(A(7)A(8))) divide-and-conquer

Certain orders may have better computational properties than others: the divide-and- conquer algorithm multiplies |Q| × |Q| matrices, an operation that using standard algo- rithms on a sequential computer takes Θ(n|Q|^3 ) total time, whereas the traditional left- to-right calculation (of forward probabilities α) and right-to-left calculation (of backward probabilities β) summarize results in vectors and perform only matrix-vector products, take Θ(n|Q|^2 ) time, a substantial savings for large |Q|. However given the possibility of parallel computation, the divide-and-conquer strategy may be preferable, as the computation depth is reduced from Θ(n) to Θ(log n).

5.5 Variable elimination

For HMMs, multivariate conditional distributions over hidden variables can be calculated efficiently by variable elimination, a general inference technique that computes marginal distributions over subsets of variables by constructing a modified graphical model contain- ing only those variables.

Consider the marginal distribution PQiQj Qk|y of the hidden variables Qi, Qj , Qk (where i < j < k are not necessarily consecutive), after conditioning on the observation Y = y:

PQi|y =

α(i)qi β(i)qi P (y)

PQj |Qiy =

PQj Qiy PQiy

α(i)qi A(i+1,j)qi,qj β(j)qj α(i)qi β(i)qi

A(i+1,j)qi,qj β(j)qj β(i)qi

PQk|Qj y =

PQj Qky PQj y

α(j)qj A(j+1,k)qj ,qk β(k)qk α(j)qj β(j)qj

A(j+1,k)qj ,qk β(k)qk β(j)qj

consistent with the directed graphical model

Qi Qj Qk

It is not true for general graphical models that the marginal distribution over a subset of variables takes such a simple form; the hidden variables of HMMs (and more generally, tree graphical models) are a special case.

6 Variations

6.1 The Viterbi algorithm

In smoothing, classification and transduction tasks one frequently desires the most probable configuration q?^ of the hidden variables given an observation:

q?^ = arg max q

P (q|y)

= arg max q

P (q, y).

The configuration q?^ is known as the Viterbi sequence. It can be computed efficiently by replacing the summation in the forward (or backward) calculation with maximization, so that instead of the usual

α( qii) ≡ P (y 1... y 1 , qi) =

q 1 ...qi− 1

P (y 1... yi, q 1... qi− 1 qi)

the forward probabilities are instead defined

α( qii) ≡ (^) qmax 1 ...qi− 1

P (y 1... yi, q 1... qi− 1 qi).

In more detail, let the optimal prefix π for a state value qi be the sequence q 1... qi that maximizes the probability of y 1... yi. It simplifies bookkeeping to store optimal prefixes π q(ii) and their probabilities together in a pair:

π q(ii) = arg max q 1 ...qi− 1 qi

P (y 1... yi, q 1... qi− 1 qi)

α( qii) ≡

P (y 1... yi, π q(ii) ) ; π( qii)

It remains possible to express the computation of α(i)^ using matrix notation if the ⊕ operator maximize probabilities and the ⊗ operator concatenates state sequences:

A(1) q 1 = 〈P (q 1 )P (y 1 |q 1 ) ; q 1 〉 A(2 qi−≤ 1 iq≤i n) = 〈P (qi|qi− 1 )P (yi|qi) ; qi〉 A( qnn+1) = 〈1 ; 〉 ⊗(〈p 1 ; s 1 〉, 〈p 2 ; s 2 〉) = 〈p 1 p 2 ; concat(s 1 , s 2 )〉

⊕(〈p 1 ; s 1 〉, 〈p 2 ; s 2 〉) =

〈p 1 ; s 1 〉 if p 1 ≥ p 2 〈p 2 ; s 2 〉 otherwise

6.2 States and output

6.3 Variable horizon

7 Practicalities

7.1 Memory consumption

Both the forward-backward algorithm and the Viterbi algorithm require Θ(n|Q|) storage space if implemented naively.

7.2 Numerical underflow

7.3 Continuous distributions

7.4 Large state space