0% found this document useful (0 votes)
3 views

Lec18 HMMs

The document outlines upcoming guest lectures on LLM development and AI policy, followed by a detailed discussion on Hidden Markov Models (HMMs) in the context of artificial intelligence. It covers topics such as reasoning over time, Markov chains, and the application of HMMs in various fields including speech recognition and robot localization. The document also includes examples and algorithms related to filtering and inference in HMMs.

Uploaded by

23020011
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Lec18 HMMs

The document outlines upcoming guest lectures on LLM development and AI policy, followed by a detailed discussion on Hidden Markov Models (HMMs) in the context of artificial intelligence. It covers topics such as reasoning over time, Markov chains, and the application of HMMs in various fields including speech recognition and robot localization. The document also includes examples and algorithms related to filtering and inference in HMMs.

Uploaded by

23020011
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 56

Announcements

§ Guest Lectures Announced!

§ Tuesday, Nov 19: Catherine Olsson (Anthropic) on LLM development &


interpretability

§ Thursday, Dec 3: Miles Brundage (formerly OpenAI) on AI policy and


social impacts
CS 188: Artificial Intelligence
Hidden Markov Models

University of California, Berkeley


[These slides were created by Dan Klein and Pieter Abbeel for CS188 Intro to AI at UC Berkeley. All CS188 materials are available at http://ai.berkeley.edu.]
Reasoning over Time or Space

§ Often, we want to reason about a sequence of observations


§ Speech recognition
§ Robot localization
§ User attention
§ Medical monitoring
§ Language understanding

§ Need to introduce time (or space) into our models and


update beliefs based on:
§ Getting more evidence (we did this with BNs)
§ World changing over time/space (new this week)
Motivating Example: Pacman Sonar
Today’s Topics

§ Quick probability recap


§ Markov Chains & their Stationary Distributions
§ How beliefs about state change with passage of time

§ Hidden Markov Models (HMMs) formulation


§ How beliefs change with passage of time and evidence

§ Filtering with HMMs


§ How to infer beliefs from evidence
Probability Recap

§ Conditional probability

§ Marginal probability

§ Product rule

§ Chain rule
Probability Recap

§ X, Y independent if and only if:

§ X and Y are conditionally independent given Z if and only if:

§ Proportionality: 𝑃 𝑋 ∝ 𝑓 𝑋 or 𝑃 𝑋 ∝! 𝑓 𝑋 means 𝑃 𝑋 = 𝑘𝑓 𝑋 (for some


" !
constant k that doesn’t depend on X). Equivalent to: 𝑃 𝑋 = ∑ "(%)
!

§ Example: 𝑋 ∝𝑓 𝑋 𝑃 𝑋
𝑥! 0.4 0.4 / (0.4 + 0.2)
𝑥" 0.2 0.2 / (0.4 + 0.2)
Markov Models
§ Value of X at a given time is called the state

X1 X2 X3 X4

§ Parameters: called transition probabilities or dynamics, specify how the state


evolves over time (also, initial state probabilities)
§ Stationarity assumption: transition probabilities the same at all times
§ Same as MDP transition model, but no choice of action
§ A "growable” BN (can always use BN methods if we truncate to fixed length)
Conditional Independence

X1 X2 X3 X4

§ Basic conditional independence:


§ Past and future independent given the present
§ Each time step only depends on the previous
§ This is called the (first order) Markov property
Example Markov Chain: Weather
§ States: X = {rain, sun}

§ Initial distribution: 1.0 sun

§ CPT P(Xt | Xt-1): Two new ways of representing the same CPT

Xt-1 Xt P(Xt|Xt-1) 0.9


0.3
sun sun 0.9 0.9
sun sun
sun rain 0.1 rain sun 0.1
rain sun 0.3 0.3
rain rain
rain rain 0.7 0.7 0.7
0.1
Example Markov Chain: Weather
Xt-1 Xt P(Xt|Xt-1)
§ Initial distribution: 1.0 sun sun sun 0.9
§ We know: sun rain 0.1
rain sun 0.3
rain rain 0.7

§ What is the probability distribution after one step?

P( X2 = sun) = Â P(x1 , X2 = sun) = Â P(X2 = sun| x1 ) P(x1 )


x1 x1
Mini-Forward Algorithm

§ Question: What’s P(X) on some day t?

X1 X2 X3 X4 Xt ?

§ We know 𝑃(𝑋& ) and 𝑃 𝑋' 𝑋'(& )

X
P (xt ) = P (xt 1 , xt )
x
X
t 1

= P (xt | xt 1 )P (xt 1 )
xt 1
Forward simulation
Example Run of Mini-Forward Algorithm
§ From initial observation of sun

P(X1) P(X2) P(X3) P(X4) P(X¥)


§ From initial observation of rain

P(X1) P(X2) P(X3) P(X4) P(X¥)


§ From yet another initial distribution P(X1):


P(X1) P(X¥) [Demo: L13D1,2,3]
Video of Demo Ghostbusters Basic Dynamics
Video of Demo Ghostbusters Circular Dynamics
Video of Demo Ghostbusters Whirlpool Dynamics
Stationary Distributions

§ For most chains: § Stationary distribution:


§ Influence of the initial distribution § The distribution we end up with is called
gets less and less over time. the stationary distribution P1of the
§ The distribution we end up in is chain
independent of the initial distribution § It satisfies
X
P1 (X) = P1+1 (X) = P (X|x)P1 (x)
x
Example: Stationary Distributions
§ Question: What’s P(X) at time t = infinity?
X1 X2 X3 X4 X∞ ?
X
P1 (X) = P1+1 (X) = P (X|x)P1 (x)
x
P1 (sun) = P (sun|sun)P1 (sun) + P (sun|rain)P1 (rain)
P1 (rain) = P (rain|sun)P1 (sun) + P (rain|rain)P1 (rain)
Xt-1 Xt P(Xt|Xt-1)
P1 (sun) = 0.9P1 (sun) + 0.3P1 (rain) sun sun 0.9
P1 (rain) = 0.1P1 (sun) + 0.7P1 (rain) sun rain 0.1
rain sun 0.3
P1 (sun) = 3P1 (rain) P1 (sun) = 3/4 rain rain 0.7
PAlso:
1 (rain)
P1=(sun)
1/3P1+(sun)
P1 (rain) = 1 P1 (rain) = 1/4
§ Alternatively: run simulation for a long (ideally infinite) time
Application of Stationary Distribution: Web Link Analysis
§ PageRank over a web graph
§ Each web page is a state
§ Initial distribution: uniform over pages
§ Transitions:
§ With prob. c, uniform jump to a
random page (dotted lines, not all shown)
§ With prob. 1-c, follow a random
outlink (solid lines)

§ Stationary distribution
§ Will spend more time on highly reachable pages
§ E.g. many ways to get to the Acrobat Reader download page
§ Somewhat robust to link spam
§ Google 1.0 returned the set of pages containing all your
keywords in decreasing rank, now all search engines use link
analysis along with many other factors (rank actually getting
less important over time)
Hidden Markov Models
Pacman – Sonar

[Demo: Pacman – Sonar – No Beliefs(L14D1)]


Video of Demo Pacman – Sonar (no beliefs)
Video of Demo Pacman – Sonar (with beliefs)
Hidden Markov Models
§ Markov chains not so useful for most agents

X1 X2 X3 X4 X5
Hidden Markov Models
§ Markov chains not so useful for most agents

X1 X2 X3 X4 X5

§ Need observations to update your beliefs

§ Hidden Markov models (HMMs)


§ Underlying Markov chain over states X
§ You observe outputs (effects) at each time step

X1 X2 X3 X4 X5

E1 E2 E3 E4 E5
Example: Weather HMM
P (Xt | Xt 1)

Raint-1 Raint Raint+1

P (Et | Xt )

Umbrellat-1 Umbrellat Umbrellat+1

Transitions Emissions
§ An HMM is defined by: Rt-1 Rt P(Rt|Rt-1) Rt Ut P(Ut|Rt)
+r +r 0.7 +r +u 0.9
§ Initial distribution:
+r -r 0.3 +r -u 0.1
§ Transitions: P (Xt | Xt 1 ) -r +r 0.3 -r +u 0.2
§ Emissions: P (Et | Xt ) -r -r 0.7 -r -u 0.8
Example: Ghostbusters HMM

§ P(X1) = uniform 1/9 1/9 1/9

1/9 1/9 1/9


§ P(X’|X) = usually move clockwise, but
sometimes move in a random direction or 1/9 1/9 1/9
stay in place
P(X1)

§ P(Rij|X) = same sensor model as before:


1/6 1/6 1/2
red means close, green means far away.
0 1/6 0

X1 X2 X3 X4 0 0 0

X5 P(X’|X = <1,2>)

Ri,j Ri,j Ri,j Ri,j


[Demo: Ghostbusters – Circular Dynamics – HMM (L14D2)]
Video of Demo Ghostbusters – Circular Dynamics -- HMM
Conditional Independence
§ HMMs have two important independence properties:
§ Markov hidden process: future depends on past via the present
§ Current observation independent of all else given current state

X1 X2 X3 X4 X5

E1 E2 E3 E4 E5

§ Does this mean that evidence variables are guaranteed to be independent?


Conditional Independence
§ HMMs have two important independence properties:
§ Markov hidden process: future depends on past via the present
§ Current observation independent of all else given current state

X1 X2 X3 X4 X5

E1 E2 E3 E4 E5

§ Does this mean that evidence variables are guaranteed to be independent?


§ No, they are correlated by the hidden state
Real HMM Examples
§ Speech recognition HMMs:
§ Observations are acoustic signals (continuous valued)
§ States are specific positions in specific words (so, tens of thousands)

§ Machine translation HMMs:


§ Observations are words (tens of thousands)
§ States are translation options

§ Robot tracking:
§ Observations are range readings (continuous)
§ States are positions on a map (continuous) X1 X2 X3 X4 X

E1 E2 E3 E4 E
Filtering / Monitoring

X1 X2 X3 X4 Xt Find: P(Xt | e1, …, et) = Bt(X)

E1 E2 E3 E4 Et Observe

§ Filtering, or monitoring, is the task of tracking the distribution


Bt(X) = Pt(Xt | e1, …, et) (the belief state) over time
§ We start with B1(X) in an initial setting, usually uniform
§ As time passes, or we get observations, we update B(X)

§ The Kalman filter was invented in the 60’s and first


implemented as a method of trajectory estimation for the
Apollo program
Example: Robot Localization
Example from
Michael Pfeiffer

Prob 0 1
t=0
Sensor model: can read in which directions there is a wall,
never more than 1 mistake
Motion model: may not execute action with small prob.
Example: Robot Localization

Prob 0 1
t=1
Lighter grey: was possible to get the reading, but less likely b/c
required 1 mistake
Example: Robot Localization

Prob 0 1

t=2
Example: Robot Localization

Prob 0 1

t=3
Example: Robot Localization

Prob 0 1

t=4
Example: Robot Localization

Prob 0 1

t=5
HMM Inference: Find State Given Evidence

§ We are given evidence at each time and want to know

𝐵! 𝑋 = 𝑃(𝑋! |𝑒":! )

§ Idea: start with 𝑃(𝑋1) and derive 𝐵' (𝑋) in terms of 𝐵'()(𝑋)
§ Two steps: Passage of Time & Observation
𝐵$ % 𝑋 = 𝑃(𝑋% |𝑒!:# )

X1 X2 X3 X4

E1 E2 E3 E4

𝐵# (𝑋) 𝐵% 𝑋 = 𝑃(𝑋% |𝑒!:% )


Inference: Base Cases

Passage of Time: Observation:

X1

X1 X2
E1
Passage of Time: Base Case

X1 X2

Have: 𝑃(𝑋& ) 𝑃(𝑋/ |𝑋& )


Want:
Passage of Time: General Case
§ Assume we have current belief P(X | evidence to date)
X1 X2

§ Then, after one time step passes:


X
P (Xt+1 |e1:t ) = P (Xt+1 , xt |e1:t )
xt
X
= P (Xt+1 |xt , e1:t )P (xt |e1:t ) § Or compactly:
xt X
X 0
B (Xt+1 ) = P (X 0 |xt )B(xt )
= P (Xt+1 |xt )P (xt |e1:t ) xt
xt

§ Basic idea: beliefs get “pushed” through the transitions


§ With the “B” notation, we have to be careful about what time step t the belief is about, and what
evidence it includes
Example: Passage of Time
§ As time passes, uncertainty “accumulates” (Transition model: ghosts usually go clockwise)

T=1 T=2 T=5


Observation: Base Case

X1

E1

Have: 𝑃(𝑋& ) 𝑃(𝐸& |𝑋& )


Want:
Also can write as:
𝑃 𝑥) 𝑃 𝑒) 𝑥))
𝑃 𝑥)|𝑒) =
∑%* 𝑃 𝑥′ 𝑃 𝑒) 𝑥′)
Observation: General Case
§ Assume we have current belief P(X | previous evidence): X1
B 0 (Xt+1 ) = P (Xt+1 |e1:t )
§ Then, after evidence comes in: E1
P (Xt+1 |e1:t+1 ) = P (Xt+1 , et+1 |e1:t )/P (et+1 |e1:t )
/Xt+1 P (Xt+1 , et+1 |e1:t )

= P (et+1 |e1:t , Xt+1 )P (Xt+1 |e1:t )

= P (et+1 |Xt+1 )P (Xt+1 |e1:t )


§ Basic idea: beliefs “reweighted”
§ Or, compactly: by likelihood of evidence
B(Xt+1 ) /Xt+1 P (et+1 |Xt+1 )B 0 (Xt+1 ) § Unlike passage of time, we have
to renormalize
Example: Observation
§ As we get observations, beliefs get reweighted, uncertainty “decreases”

Before observation After observation


Two Steps: Passage of Time + Observation

XX
00 00
BB(X
(Xt+1
t+1))== PP (X
(X |x|xt )B(x
t )B(x
t)t)
xx
tt
X1 X2 X3 X4 X5

E1 E2 E3 E4 B(X E /X PP(e
B(Xt+1
t+15)) / (e t+1 |X
|X t+1 )B
)B 00 (X
(Xt+1t+1))
Xt+1
t+1 t+1 t+1
Pacman – Sonar

[Demo: Pacman – Sonar – No Beliefs(L14D1)]


Video of Demo Pacman – Sonar (with beliefs)
Example: Weather HMM
Passage of Time:X
X
0
X 0
B B 0t+1
(X =
(X)t+1
=) = P (X
(X |xt0)B(x
P t+1
(X |x
|xt )Pt )(xtt)|e1:t )
)B(x
B’(+r) = ?
B’(-r) = ? xxtt xt
Observation:
B(Xt+1 ) /Xt+1 P (et+1 |Xt+1 )B 0 (Xt+1 )
B(+r) = 0.5
B(-r) = 0.5

Rain0 Rain1 Rain2 𝑃(𝑋!"# |𝑋! ) 𝑃(𝐸! |𝑋! )

Rt Rt+1 P(Rt+1|Rt) Rt Ut P(Ut|Rt)


+r +r 0.7 +r +u 0.9
Umbrella1 Umbrella2 +r -r 0.3 +r -u 0.1
-r +r 0.3 -r +u 0.2
-r -r 0.7 -r -u 0.8
Example: Weather HMM
Passage of Time:X
X
0
X 0
B B 0t+1
(X =
(X)t+1
=) = P (X
(X |xt0)B(x
P t+1
(X |x
|xt )Pt )(xtt)|e1:t )
)B(x
B’(+r) = 0.5*0.7 + 0.5*0.3 = 0.5
B’(-r) = 0.5*0.3 + 0.5*0.7 = 0.5 xxtt xt
Observation:
B(Xt+1 ) /Xt+1 P (et+1 |Xt+1 )B 0 (Xt+1 )
B(+r) = 0.5
B(-r) = 0.5

Rain0 Rain1 Rain2 𝑃(𝑋!"# |𝑋! ) 𝑃(𝐸! |𝑋! )

Rt Rt+1 P(Rt+1|Rt) Rt Ut P(Ut|Rt)


+r +r 0.7 +r +u 0.9
Umbrella1 Umbrella2 +r -r 0.3 +r -u 0.1
-r +r 0.3 -r +u 0.2
-r -r 0.7 -r -u 0.8
Example: Weather HMM
Passage of Time:X
X
0
X 0
B B 0t+1
(X =
(X)t+1
=) = P (X
(X |xt0)B(x
P t+1
(X |x
|xt )Pt )(xtt)|e1:t )
)B(x
B’(+r) = 0.5
B’(-r) = 0.5 xxtt xt
Observation:
B(Xt+1 ) /Xt+1 P (et+1 |Xt+1 )B 0 (Xt+1 )
B(+r) = 0.5 B(+r) = ?
B(-r) = 0.5 B(-r) = ?

Rain0 Rain1 Rain2 𝑃(𝑋!"# |𝑋! ) 𝑃(𝐸! |𝑋! )

Rt Rt+1 P(Rt+1|Rt) Rt Ut P(Ut|Rt)


+r +r 0.7 +r +u 0.9
Umbrella1 Umbrella2 +r -r 0.3 +r -u 0.1
-r +r 0.3 -r +u 0.2
-r -r 0.7 -r -u 0.8
Example: Weather HMM
Passage of Time:X
X
0
X 0
B B 0t+1
(X =
(X)t+1
=) = P (X
(X |xt0)B(x
P t+1
(X |x
|xt )Pt )(xtt)|e1:t )
)B(x
B’(+r) = 0.5
B’(-r) = 0.5 xxtt xt
Observation:
normalize
B(Xt+1 ) /Xt+1 P (et+1 |Xt+1 )B 0 (Xt+1 )
B(+r) = 0.5 B(+r) = 0.9*0.5 = 0.45 0.818
B(-r) = 0.5 B(-r) = 0.2*0.5 = 0.10 0.182

Rain0 Rain1 Rain2 𝑃(𝑋!"# |𝑋! ) 𝑃(𝐸! |𝑋! )

Rt Rt+1 P(Rt+1|Rt) Rt Ut P(Ut|Rt)


+r +r 0.7 +r +u 0.9
Umbrella1 Umbrella2 +r -r 0.3 +r -u 0.1
-r +r 0.3 -r +u 0.2
-r -r 0.7 -r -u 0.8
Example: Weather HMM
Passage of Time:X
X
0
X 0
B B 0t+1
(X =
(X)t+1
=) = P (X
(X |xt0)B(x
P t+1
(X |x
|xt )Pt )(xtt)|e1:t )
)B(x
B’(+r) = 0.5 B’(+r) = 0.627
B’(-r) = 0.5 B’(-r) = 0.373 xxtt xt
Observation:
B(Xt+1 ) /Xt+1 P (et+1 |Xt+1 )B 0 (Xt+1 )
B(+r) = 0.5 B(+r) = 0.818 B(+r) = 0.883
B(-r) = 0.5 B(-r) = 0.182 B(-r) = 0.117

Rain0 Rain1 Rain2 𝑃(𝑋!"# |𝑋! ) 𝑃(𝐸! |𝑋! )

Rt Rt+1 P(Rt+1|Rt) Rt Ut P(Ut|Rt)


+r +r 0.7 +r +u 0.9
Umbrella1 Umbrella2 +r -r 0.3 +r -u 0.1
-r +r 0.3 -r +u 0.2
-r -r 0.7 -r -u 0.8
What we did today

§ Markov Chains & their Stationary Distributions


§ How beliefs about state change with passage of time

§ Hidden Markov Models (HMMs) formulation


§ How beliefs change with passage of time and evidence

§ Filtering with HMMs


§ How to infer beliefs from evidence
Next Time: More Filtering!

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy