Download Hidden markov Models Expectation Maximization - Computational Language | CMSC 723 and more Study notes Computer Science in PDF only on Docsity!
CMSC 723/LING 723
Computational Linguistics I
Hidden Markov Models
Expectation Maximization
Lecture 5
October 1, 2008
1
A Word About Assignments
- Assignment 1 due today
- Assignment 2 out (Due October 15th)
- Exploring various POS-taggers in NLTK
- Evaluating POS tagging in NLTK
- Implementing HMM Decoding
- Submission
- Create PDF/Text file if required (don’t forget your name!)
- Create .zip with all supporting files (run out of the box)
- Send zip ONLY to sajib@umiacs with subject “Assignment N
Submission”
Outline
• HMM as a model (sec. 6.1-6.2)
• What we can do with HMMs (sec. 6.3-6.4)
• Applying HMMs to POS tagging (sec. 5.5)
• Expectation-Maximization (EM) Algorithm
• Briefly: Using EM to train HMMs (sec. 6.5)
3
Recap: Finite State Machines
What do you need to specify an FSM formally?
Finite State Machines
Input alphabet 1 2 3 4 a b c a b c 7
Finite State Machines
Start state 1 2 3 4 a b c a b c
Finite State Machines
Final state(s) 1 2 3 4 a b c a b c 9
Real-world Knowledge
1 2 3 4 a b c a b c
‘a’ is twice as likely to be seen in state 1 as ‘b’ or ‘c’
‘c’ is three times as likely to be seen in state 2 as ‘a’
Weighted FSMs 2 1 1 1 3 1
What do we get out of it?
score(‘ab’) = 2
score(‘bc’) = 3
Markov Chains
0. 0.2 0.
- What’s special about this FSM?
- The ( st (^) order) Markov assumption
- This extension of a prob. FSM is called a Markov Chain or an Observed Markov Model
- Each state corresponds to an observable physical event **BULL BEAR STATIC
0.2 0.
0.** Prob. of any state ONLY depends on previous state
P (qi|q 1 , q 2 ,... , qi− 1 ) = P (qi|qi− 1 )
The stock market
13
Are states always observable?
Day: 1 2 3 4 5 6 ! "! "
!: Market is up
": Market is down
: Market hasn’t changed
Bu Be S Be S Bu
Bu: Bull Market
Be: Bear Market
S : Static Market
Not observable!
Here’s what you actually observe:
Hidden Markov Models
- Markov chains are usually inadequate
- Need to model problems where observed events
don’t correspond to states directly
- Instead observations = fp(states) for some p.d.f^ p
- Solution:^ A Hidden Markov Model (HMM)
- Assume two probabilistic processes
- Underlying process is hidden (states = hidden events)
- Second process produces sequence of observed events 15
Formalizing HMMs
- An HMM^ #^ = (A, B,^ !) is characterized by:
- Set of N states {q^1 , q^2 , ..., qN}
- N x N Transition probability matrix A = [aij]
- Sequence of observations o^1 , o^2 , ... oT, each drawn from a given
set of symbols (vocabulary V)
- N x |V| Emission probability matrix, B = [bit]
- N x^1 Prior probabilities vector^!^ =^ {^!^1 ,^!^2 , ...,^ !N^ } bit = bi(ot) = p(ot|qi) aij = p(qj |qi), ∑ j aij = 1 ∀i ∑^ N i= πi = 1
Stock Market HMM
States $ Transitions?
Valid?
**BEAR BULL STATIC
0.2 0.
BEAR BULL STATIC** 19
Stock Market HMM
States $ Transitions $
Valid $
**BEAR BULL STATIC
0.2 0.
0.** Vocabulary?
V = {↑, ↓, ↔}
Stock Market HMM
States $ Transitions $
Valid $
**BEAR BULL STATIC
0.2 0.
0.** Vocabulary $
V = {↑, ↓, ↔}
Emissions? P (↑ |Bull) = 0. 7 P (↓ |Bull) = 0. 1 P (↔ |Bull) = 0. 2 P (↑ |Bear) = 0. 1 P (↓ |Bear) = 0. 6 P (↔ |Bear) = 0. 3 P (↑ |Static) = 0. 3 P (↓ |Static) = 0. 3 P (↔ |Static) = 0. 4
Valid?
21
Stock Market HMM
States $ Transitions $
Valid $
**BEAR BULL STATIC
0.2 0.
0.** Vocabulary $
V = {↑, ↓, ↔}
Emissions $ P (↑ |Bull) = 0. 7 P (↓ |Bull) = 0. 1 P (↔ |Bull) = 0. 2 P (↑ |Bear) = 0. 1 P (↓ |Bear) = 0. 6 P (↔ |Bear) = 0. 3 P (↑ |Static) = 0. 3 P (↓ |Static) = 0. 3 P (↔ |Static) = 0. 4
Valid $
Comparing to J&M HMMs
25 True? J&M have explicit start & end states with no observations
Observation
Our HMMs are fully functionally equivalent
Claim
Applying HMMs
- 3 problems to solve before HMMs can be useful
- Given an HMM^ #^ = (A, B,^ !), and a sequence of observed events O, find P(O| #) [ Likelihood ]
- Given an HMM^ #^ = (A, B,^ !), and an observation sequence O, find the most likely (hidden) state sequence [ Decoding ]
- Given a set of observation sequences and the set of states Q in #, compute the parameters A and B. [ Learning ]
Computing Likelihood
t : 1 2 3 4 5 6
O :! "! "
#stock
Assuming #stock models the stock market, how likely is it that on day 1, the market is up, on day 2, it’s down etc.? 27 Markov Chain? P (↑ |Bull) = 0. 7 P (↓ |Bull) = 0. 1 P (↔ |Bull) = 0. 2 P (↑ |Bear) = 0. 1 P (↓ |Bear) = 0. 6 P (↔ |Bear) = 0. 3 P (↑ |Static) = 0. 3 P (↓ |Static) = 0. 3 P (↔ |Static) = 0. 4 **BEAR BULL STATIC
0.2 0.
! 1 =0.5** (^)! 2 =0.2! 3 =0.
Computing Likelihood
• Sounds easy!
• Sum over all possible ways in which we
could generate O from
= ∑ q 1 ,q 2 ...qT πq 1 bq 1 (o 1 )aq 1 q 2... aqT − 1 qT bqT (oT )
Takes exponential (! NT) time to compute!
Right idea, wrong algorithm!
P (O|λ) = ∑ Q P (O, Q|λ) = ∑ Q P (O|Q, λ)P (Q|λ)
Forward Algorithm
• Formal Definition
• Initialization
• Recursion
• Termination
α 1 (j) = πj bj (o 1 ), 1 ≤ j ≤ N . .
.. q 1 q 2 q 3 qN qj a1j a2j a3j aNj (t-1) t !(t-1)(i) !t(j) P (O|λ) = ∑^ N i= αT (i) αt(j) = ∑^ N i= αt− 1 (i)aij bj (ot); 1 ≤ j ≤ N, 2 ≤ t ≤ T 31
Forward Algorithm
Bear
Bull
Static
states
time
! "! t=1 t=2 t=
O =! "!
find P(O|#stock)
32
Forward Algorithm
Bear
Bull
Static
states time **! "! t=1 t=2 t=
1 (Bu)
0.20. =0.* Initialization 0.50. =0. 0.30. =0.** 33
Forward Algorithm
Bear
Bull
Static
states time **! "! t=1 t=2 t=
1 (Bu)
0.20. =0.* Recursion 0.50. =0. 0.30. =0. 0.** % 1 (Bu) * aBuBu * bBu(") 0.14 * 0.6 * 0.1=0. 0.05 * 0.5 * 0.1 =0. 0.09 * 0.4 * 0.1 =0.
.... and so on 34
Decoding
t : 1 2 3 4 5 6
O :! "! "
#stock
Given #stock as our model and O as our observations, what are the most likely states the market went through to produce O? 37 P (↑ |Bull) = 0. 7 P (↓ |Bull) = 0. 1 P (↔ |Bull) = 0. 2 P (↑ |Bear) = 0. 1 P (↓ |Bear) = 0. 6 P (↔ |Bear) = 0. 3 P (↑ |Static) = 0. 3 P (↓ |Static) = 0. 3 P (↔ |Static) = 0. 4 **BEAR BULL STATIC
0.2 0.
! 1 =0.5** (^)! 2 =0.2! 3 =0.
Decoding
• “Decoding” because states are hidden
• There’s a simple way to do it
• For each possible hidden state sequence,
compute P(O) using “forward algorithm”
• Pick the one that gives the highest P(O)
• Will this give the right answer?
• Is it practical?
Viterbi Algorithm
• Another dynamic programming algorithm
• Same idea as the forward algorithm
• Store intermediate computation results in a trellis
• Build new cells from existing cells
• Efficient (polynomial vs. exponential)
39
Viterbi Algorithm
• Use an N x T^ trellis^ [vtj]
• vtj^ or vt(j) = P(in state^ j^ after seeing^ t^ observations &
passing through the most likely state sequence so far) = p(q 1 , q 2 , ..., qt-1, qt=j, o 1 , o 2 , ... ot)
• Each cell = extension of most likely path from other cells
• vt-1(i): viterbi probability until (t-^1 )
• aij^ : transition probability of going from state^ i^ to^ j
• bj(ot) : probability of emitting symbol ot^ in state^ j
• P = maxi^ vT(i)
vt(j) =
N
max
i=
vt− 1 (i)aij bj (ot)