hidden markov models - autenticação · hidden markov models slides revised and adapted to...

23
1 www.bioalgorithms.info An Introduction to Bioinformatics Algorithms Hidden Markov Models Slides revised and adapted to Bioinformática 55 Engª Biomédica/IST 2005 Ana Teresa Freitas Forward Algorithm For Markov chains we calculate the probability of For Markov chains we calculate the probability of a sequence, P(x) a sequence, P(x) How to calculate this probability for an HMM as How to calculate this probability for an HMM as well? well? We must add the probabilities for all possible We must add the probabilities for all possible paths paths Bioinformática - 55 = π π ) , ( ) ( x P x P

Upload: vandat

Post on 29-Aug-2019

229 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Hidden Markov Models - Autenticação · Hidden Markov Models Slides revised and adapted to Bioinformática 55 Engª Biomédica/IST 2005 Ana Teresa Freitas Forward Algorithm • For

1

www.bioalgorithms.infoAn Introduction to Bioinformatics Algorithms

Hidden Markov Models

Slides revised and adapted to

Bioinformática 55

Engª Biomédica/IST

2005

Ana Teresa Freitas

Forward Algorithm

•• For Markov chains we calculate the probability of For Markov chains we calculate the probability of

a sequence, P(x)a sequence, P(x)

•• How to calculate this probability for an HMM as How to calculate this probability for an HMM as

well?well?

•• We must add the probabilities for all possible We must add the probabilities for all possible

pathspaths

Bioinformática - 55

∑=π

π ),()( xPxP

Page 2: Hidden Markov Models - Autenticação · Hidden Markov Models Slides revised and adapted to Bioinformática 55 Engª Biomédica/IST 2005 Ana Teresa Freitas Forward Algorithm • For

2

Forward Algorithm

•• Define Define ffkk,i,i ((forward probabilityforward probability) as the ) as the

probability of emitting the prefix probability of emitting the prefix xx11……xxii and and

eventually reaching the state eventually reaching the state ππii = k= k..

ffkk,i,i = = P(P(xx11……xxii ,, ππii = k)= k)

•• The recurrence for the forward algorithm:The recurrence for the forward algorithm:

ffl,i+1l,i+1 = = eell((xxii+1+1)) . . ΣΣ ffkk,i,i . . aaklklk k ЄЄ QQ

Bioinformática - 55

Forward algorithm

•• InitializationInitialization::

•• ffbegin,0begin,0 = 1= 1

•• ffk,0k,0 = 0 for = 0 for k k ≠≠ beginbegin..

•• For each i =0,...,L calculate:For each i =0,...,L calculate:

ffl,il,i == eell ((xxii) ) ·· ΣΣkk ffk,k,ii--11 ·· aaklkl

•• TerminationTermination

P(P(xx) =) = ΣΣkk ffk,k,LL . . aakk,end,end

Bioinformática - 55

Page 3: Hidden Markov Models - Autenticação · Hidden Markov Models Slides revised and adapted to Bioinformática 55 Engª Biomédica/IST 2005 Ana Teresa Freitas Forward Algorithm • For

3

An Introduction to Bioinformatics Algorithms www.bioalgorithms.info

Forward-Backward Problem

Given:Given: a sequence of coin tosses generated a sequence of coin tosses generated

by an HMMby an HMM..

Goal:Goal: find the probability that the dealer was find the probability that the dealer was

using a biased coin at a particular time.using a biased coin at a particular time.

An Introduction to Bioinformatics Algorithms www.bioalgorithms.info

Backward Algorithm

•• However, However, forward probability forward probability is not the only is not the only

factor affecting factor affecting P(P(ππii = k|x= k|x))..

•• The sequence of transitions and emissions The sequence of transitions and emissions

that the HMM undergoes between that the HMM undergoes between ππi+1i+1 and and ππnn

also affect also affect P(P(ππii = k|x= k|x))..

forward xi backward

Page 4: Hidden Markov Models - Autenticação · Hidden Markov Models Slides revised and adapted to Bioinformática 55 Engª Biomédica/IST 2005 Ana Teresa Freitas Forward Algorithm • For

4

An Introduction to Bioinformatics Algorithms www.bioalgorithms.info

Backward Algorithm (cont’d)

•• DefineDefine backward probability backward probability bbkk,i,i as the as the

probability of being in state probability of being in state ππii = k = k and emitting and emitting

the the suffixsuffix xxi+1i+1……xxnn..

•• The recurrence for the The recurrence for the backward algorithmbackward algorithm::

bbkk,i,i = = ΣΣ eell(x(xi+1i+1)) . . bbl,i+1l,i+1 . . aakl kl l l ЄЄ QQ

An Introduction to Bioinformatics Algorithms www.bioalgorithms.info

Backward-Forward Algorithm

•• The probability that the dealer used a The probability that the dealer used a biased coin at any moment biased coin at any moment ii::

P(x, P(x, ππii = k= k) ) ffkk(i) . (i) . bbkk(i)(i)

P(P(ππii = k|x= k|x) = ) = _______________ _______________ = = ____________________________

P(x) P(x)P(x) P(x)

P(x) is the sum of P(x, P(x, ππii = k)= k) over all k

Page 5: Hidden Markov Models - Autenticação · Hidden Markov Models Slides revised and adapted to Bioinformática 55 Engª Biomédica/IST 2005 Ana Teresa Freitas Forward Algorithm • For

5

An Introduction to Bioinformatics Algorithms www.bioalgorithms.info

HMM Parameter Estimation

•• So far, we have assumed that the transition So far, we have assumed that the transition

and emission probabilities are known.and emission probabilities are known.

•• However, in most HMM applications, the However, in most HMM applications, the

probabilities are not known. Itprobabilities are not known. It’’s very hard to s very hard to

estimate the probabilities.estimate the probabilities.

An Introduction to Bioinformatics Algorithms www.bioalgorithms.info

HMM Parameter Estimation Problem

�Given

� HMM with states and alphabet (emission

characters)

� Independent training sequences x1, … xm

�Find HMM parameters Θ (that is, akl, ek(b))

that maximize

P(x1, …, xm | Θ)

the joint probability of the training sequences.

Page 6: Hidden Markov Models - Autenticação · Hidden Markov Models Slides revised and adapted to Bioinformática 55 Engª Biomédica/IST 2005 Ana Teresa Freitas Forward Algorithm • For

6

An Introduction to Bioinformatics Algorithms www.bioalgorithms.info

Maximize the likelihoodP(x1, …, xm | Θ) as a function of Θ is called the

likelihood of the model.

The training sequences are assumed independent, therefore

P(x1, …, xm | Θ) = Πi P(xi | Θ)

The parameter estimation problem seeks Θ that realizes

In practice the log likelihood is computed to avoid underflow errors

∏ ΘΘ i

ixP )|(max

An Introduction to Bioinformatics Algorithms www.bioalgorithms.info

Two situations

Known paths for training sequences

CpG islands marked on training sequences

One evening the casino dealer allows us to see

when he changes dice

Unknown paths

CpG islands are not marked

Do not see when the casino dealer changes

dice

Page 7: Hidden Markov Models - Autenticação · Hidden Markov Models Slides revised and adapted to Bioinformática 55 Engª Biomédica/IST 2005 Ana Teresa Freitas Forward Algorithm • For

7

An Introduction to Bioinformatics Algorithms www.bioalgorithms.info

Known paths

Akl = # of times each k → l is taken in the training sequences

Ek(b) = # of times b is emitted from state k in the

training sequences

Compute akl and ek(b) as maximum likelihood

estimators:

=

=

'

'

'

)'(/)()(

/

b

kkk

l

klklkl

bEbEbe

AAa

An Introduction to Bioinformatics Algorithms www.bioalgorithms.info

Pseudocounts

� Some state k may not appear in any of the training sequences. This means Akl = 0 for every state land akl cannot be computed with the given

equation.

� To avoid this overfitting use predetermined pseudocounts rkl and rk(b).

Akl = # of transitions k→l + rkl

Ek(b) = # of emissions of b from k + rk(b)

The pseudocounts reflect our prior biases about the probability values.

Page 8: Hidden Markov Models - Autenticação · Hidden Markov Models Slides revised and adapted to Bioinformática 55 Engª Biomédica/IST 2005 Ana Teresa Freitas Forward Algorithm • For

8

An Introduction to Bioinformatics Algorithms www.bioalgorithms.info

Unknown paths: Viterbi training

Idea: use Viterbi decoding to compute the most probable path for training sequence x

Start with some guess for initial parameters and compute π* the most probable path for x using initial parameters.

Iterate until no change in π* :

1. Determine Akl and Ek(b) as before

2. Compute new parameters akl and ek(b) using the same formulas as before

3. Compute new π* for x and the current parameters

An Introduction to Bioinformatics Algorithms www.bioalgorithms.info

Viterbi training analysis

� The algorithm converges precisely

There are finitely many possible paths.

New parameters are uniquely determined by the current π*.

There may be several paths for x with the same probability, hence must compare the new π* with all previous paths having highest probability.

� Does not maximize the likelihood Πx P(x | Θ) but the contribution to the likelihood of the most probable path Πx

P(x | Θ, π*)

� In general performs less well than Baum-Welch

Page 9: Hidden Markov Models - Autenticação · Hidden Markov Models Slides revised and adapted to Bioinformática 55 Engª Biomédica/IST 2005 Ana Teresa Freitas Forward Algorithm • For

9

An Introduction to Bioinformatics Algorithms www.bioalgorithms.info

Unknown paths: Baum-Welch

Idea:

1. Guess initial values for parameters.

art and experience, not science

2. Estimate new (better) values for parameters.

how ?

3. Repeat until stopping criteria is met.

what criteria ?

An Introduction to Bioinformatics Algorithms www.bioalgorithms.info

Better values for parameters

Would need the Akl and Ek(b) values but cannot

count (the path is unknown) and do not want to

use a most probable path.

For all states k,l, symbol b and training sequence x

Compute Akl and Ek(b) as expected

values, given the current parameters

Page 10: Hidden Markov Models - Autenticação · Hidden Markov Models Slides revised and adapted to Bioinformática 55 Engª Biomédica/IST 2005 Ana Teresa Freitas Forward Algorithm • For

10

An Introduction to Bioinformatics Algorithms www.bioalgorithms.info

Notation

For any sequence of characters x emitted along some unknown path π, denote by πi = k the assumption that the state at position i (in which xi is emitted) is k.

An Introduction to Bioinformatics Algorithms www.bioalgorithms.info

Probabilistic setting for Ak,l

Given x1, … ,xm consider a discrete probability spacewith elementary events

εk,l, = “k → l is taken in x1, …, xm ”

For each x in {x1,…,xm} and each position i in x let Yx,i

be a random variable defined by

Define Y = Σx Σi Yx,i random var that counts # of times the event εk,l happens in x1,…,xm.

==

=+

otherwise

landkifY

ii

lkix,0

,1)(

1

,,

ππε

Page 11: Hidden Markov Models - Autenticação · Hidden Markov Models Slides revised and adapted to Bioinformática 55 Engª Biomédica/IST 2005 Ana Teresa Freitas Forward Algorithm • For

11

An Introduction to Bioinformatics Algorithms www.bioalgorithms.info

The meaning of Akl

Let Akl be the expectation of Y

E(Y) = Σx Σi E(Yx,i) = Σx Σi P(Yx,i = 1) =

ΣxΣi P({εk,l | πi = k and πi+1 = l}) =

ΣxΣi P(πi = k, πi+1 = l | x)

Need to compute P(πi = k, πi+1 = l | x)

An Introduction to Bioinformatics Algorithms www.bioalgorithms.info

Probabilistic setting for Ek(b)

Given x1, … ,xm consider a discrete probability

space with elementary events

εk,b = “b is emitted in state k in x1, … ,xm ”

For each x in {x1,…,xm} and each position i in x let

Yx,i be a random variable defined by

Define Y = Σx Σi Yx,i random var that counts # of

times the event εk,b happens in x1,…,xm.

==

=otherwise

kandbxifY

ii

bkix,0

,1)( ,,

πε

Page 12: Hidden Markov Models - Autenticação · Hidden Markov Models Slides revised and adapted to Bioinformática 55 Engª Biomédica/IST 2005 Ana Teresa Freitas Forward Algorithm • For

12

An Introduction to Bioinformatics Algorithms www.bioalgorithms.info

The meaning of Ek(b)

Let Ek(b) be the expectation of Y

E(Y) = Σx Σi E(Yx,i) = Σx Σi P(Yx,i = 1) =

ΣxΣi P({εk,b | xi = b and πi = k})

Need to compute P(πi = k | x)

∑ ∑∑ ∑==

====x bxi

i

x bxi

iibk

ii

xkPkbxP}|{}|{

, )|(}),|({ ππε

An Introduction to Bioinformatics Algorithms www.bioalgorithms.info

Computing new parameters

Consider x = x1…xn training sequence

Concentrate on positions i and i+1

Use the forward-backward values:

fki = P(x1 … xi , πi = k)

bki = P(xi+1 … xn | πi = k)

Page 13: Hidden Markov Models - Autenticação · Hidden Markov Models Slides revised and adapted to Bioinformática 55 Engª Biomédica/IST 2005 Ana Teresa Freitas Forward Algorithm • For

13

An Introduction to Bioinformatics Algorithms www.bioalgorithms.info

Compute Akl (1)

Prob k → l is taken at position i of x

P(πi = k, πi+1 = l | x1…xn) = P(x, πi = k, πi+1 = l) / P(x)

Compute P(x) using either forward or backward values

We’ll show that P(x, πi = k, πi+1 = l) = bli+1 ·el(xi+1) ·akl ·fki

Expected # times k → l is used in training sequences

Akl = Σx Σi (bli+1 ·el(xi+1) ·akl ·fki) / P(x)

An Introduction to Bioinformatics Algorithms www.bioalgorithms.info

Compute Akl (2)

P(x, πi = k, πi+1 = l) =

P(x1…xi, πi = k, πi+1 = l, xi+1…xn) =

P(πi+1 = l, xi+1…xn | x1…xi, πi = k)·P(x1…xi,πi =k)=

P(πi+1 = l, xi+1…xn | πi = k)·fki =

P(xi+1…xn | πi = k, πi+1 = l)·P(πi+1 = l | πi = k)·fki =

P(xi+1…xn | πi+1 = l)·akl ·fki =

P(xi+2…xn | xi+1, πi+1 = l) · P(xi+1 | πi+1 = l) ·akl ·fki =

P(xi+2…xn | πi+1 = l) ·el(xi+1) ·akl ·fki =

bli+1 ·el(xi+1) ·akl ·fki

Page 14: Hidden Markov Models - Autenticação · Hidden Markov Models Slides revised and adapted to Bioinformática 55 Engª Biomédica/IST 2005 Ana Teresa Freitas Forward Algorithm • For

14

An Introduction to Bioinformatics Algorithms www.bioalgorithms.info

Compute Ek(b)

Prob xi of x is emitted in state k

P(πi = k | x1…xn) = P(πi = k, x1…xn)/P(x)

P(πi = k, x1…xn) = P(x1…xi,πi = k,xi+1…xn) =

P(xi+1…xn | x1…xi,πi = k) · P(x1…xi,πi = k) =

P(xi+1…xn | πi = k) · fki = bki · fki

Expected # times b is emitted in state k

( )∑∑=

⋅=x bxi

kikik

i

xPbfbE:

)()(

An Introduction to Bioinformatics Algorithms www.bioalgorithms.info

Finally, new parameters

Can add pseudocounts as before.

=

=

'

'

'

)'(/)()(

/

b

kkk

l

klklkl

bEbEbe

AAa

Page 15: Hidden Markov Models - Autenticação · Hidden Markov Models Slides revised and adapted to Bioinformática 55 Engª Biomédica/IST 2005 Ana Teresa Freitas Forward Algorithm • For

15

An Introduction to Bioinformatics Algorithms www.bioalgorithms.info

Stopping criteria

Cannot actually reach maximum (optimization of continuous functions)

Therefore need stopping criteria

Compute the log likelihood of the model for current Θ

Compare with previous log likelihood

Stop if small difference

Stop after a certain number of iterations

∑ Θx

xP )|(log

An Introduction to Bioinformatics Algorithms www.bioalgorithms.info

The Baum-Welch algorithm

Initialization:

Pick the best-guess for model parameters

(or arbitrary)

Iteration:

1. Forward for each x

2. Backward for each x

3. Calculate Akl, Ek(b)

4. Calculate new akl, ek(b)

5. Calculate new log-likelihood

Until log-likelihood does not change much

Page 16: Hidden Markov Models - Autenticação · Hidden Markov Models Slides revised and adapted to Bioinformática 55 Engª Biomédica/IST 2005 Ana Teresa Freitas Forward Algorithm • For

16

An Introduction to Bioinformatics Algorithms www.bioalgorithms.info

Baum-Welch analysis

Log-likelihood is increased by iterations

Baum-Welch is a particular case of the EM

(expectation maximization) algorithm

Convergence to local maximum. Choice of

initial parameters determines local maximum to

which the algorithm converges

An Introduction to Bioinformatics Algorithms www.bioalgorithms.info

Finding Distant Members of a Protein Family

•• A distant cousin of functionally related sequences in A distant cousin of functionally related sequences in a protein family may have weak a protein family may have weak pairwisepairwise similarities similarities with each member of the family and thus fail with each member of the family and thus fail significance test. significance test.

•• However, they may have weak similarities with However, they may have weak similarities with manymany members of the family. members of the family.

•• The goal is to align a sequence to The goal is to align a sequence to allall members of members of the family at once.the family at once.

•• Family of related proteins can be represented by Family of related proteins can be represented by their multiple alignment and the corresponding their multiple alignment and the corresponding profile.profile.

Page 17: Hidden Markov Models - Autenticação · Hidden Markov Models Slides revised and adapted to Bioinformática 55 Engª Biomédica/IST 2005 Ana Teresa Freitas Forward Algorithm • For

17

An Introduction to Bioinformatics Algorithms www.bioalgorithms.info

Profile Representation of Protein Families

Aligned DNA sequences can be represented by a

4 ·n profile matrix reflecting the frequencies

of nucleotides in every aligned position.

Protein family can be represented by a Protein family can be represented by a 20·n profile profile

representing frequencies of amino acids.representing frequencies of amino acids.

An Introduction to Bioinformatics Algorithms www.bioalgorithms.info

Profiles and HMMs

•• HMMsHMMs can also be used for aligning a can also be used for aligning a

sequence against a profile representing sequence against a profile representing

protein family.protein family.

•• A A 2020··nn profile profile PP corresponds to corresponds to n n

sequentially linked sequentially linked matchmatch states states MM11,,……,,MMnn

in the in the profile HMMprofile HMM of of P.P.

Page 18: Hidden Markov Models - Autenticação · Hidden Markov Models Slides revised and adapted to Bioinformática 55 Engª Biomédica/IST 2005 Ana Teresa Freitas Forward Algorithm • For

18

An Introduction to Bioinformatics Algorithms www.bioalgorithms.info

Multiple Alignments and Protein

Family Classification

• Multiple alignment of a protein family shows

variations in conservation along the length of

a protein

• Example: after aligning many globin proteins,

the biologists recognized that the helices

region in globins are more conserved than

others.

An Introduction to Bioinformatics Algorithms www.bioalgorithms.info

What are Profile HMMs ?

• A Profile HMM is a probabilistic

representation of a multiple alignment.

• A given multiple alignment (of a protein

family) is used to build a profile HMM.

• This model then may be used to find and

score less obvious potential matches of new

protein sequences.

Page 19: Hidden Markov Models - Autenticação · Hidden Markov Models Slides revised and adapted to Bioinformática 55 Engª Biomédica/IST 2005 Ana Teresa Freitas Forward Algorithm • For

19

I

Start EndM1 M3 M4M2

Profile HMMsD D D D

I I I II

L W KM E

ILWKILMWKELMWKE

Match: a conserved position with

specialized emission probabilityInsert: an insertion with general

(background) emission probability

Delete: a deletion, a silent state

without any emission

An Introduction to Bioinformatics Algorithms www.bioalgorithms.info

Profile HMM

A profile HMMA profile HMM

Page 20: Hidden Markov Models - Autenticação · Hidden Markov Models Slides revised and adapted to Bioinformática 55 Engª Biomédica/IST 2005 Ana Teresa Freitas Forward Algorithm • For

20

An Introduction to Bioinformatics Algorithms www.bioalgorithms.info

Building a profile HMM

• Multiple alignment is used to construct the HMM model.

• Assign each column to a Match state in HMM. Add Insertion and Deletion state.

• Estimate the emission probabilities according to amino acid counts in column. Different positions in the protein will have different emission probabilities.

• Estimate the transition probabilities between Match, Deletion andInsertion states

• The HMM model gets trained to derive the optimal parameters.

An Introduction to Bioinformatics Algorithms www.bioalgorithms.info

States of Profile HMM

•• Match states Match states MM11……MMnn (plus (plus begin/endbegin/end states) states)

•• Insertion states Insertion states II00II11……IInn

•• Deletion states Deletion states DD11……DDnn

Page 21: Hidden Markov Models - Autenticação · Hidden Markov Models Slides revised and adapted to Bioinformática 55 Engª Biomédica/IST 2005 Ana Teresa Freitas Forward Algorithm • For

21

An Introduction to Bioinformatics Algorithms www.bioalgorithms.info

Transition Probabilities in Profile HMM

•• log(log(aaMIMI)+log()+log(aaIMIM) = ) = gap initiation penaltygap initiation penalty

•• log(log(aaIIII) = gap extension penaltygap extension penalty

An Introduction to Bioinformatics Algorithms www.bioalgorithms.info

Emission Probabilities in Profile HMM

•• ProbabiltyProbabilty of emitting a symbol of emitting a symbol a a at an at an

insertion stateinsertion state IIjj::

eeIjIj(a) = p(a)(a) = p(a)

where where p(a)p(a) is the frequency of the is the frequency of the

occurrence of the symbol occurrence of the symbol a a in all the in all the

sequences.sequences.

Page 22: Hidden Markov Models - Autenticação · Hidden Markov Models Slides revised and adapted to Bioinformática 55 Engª Biomédica/IST 2005 Ana Teresa Freitas Forward Algorithm • For

22

An Introduction to Bioinformatics Algorithms www.bioalgorithms.info

Profile HMM Alignment

•• Define Define vvMMjj (i)(i) as the logarithmic likelihood as the logarithmic likelihood

score of the best path for matching score of the best path for matching xx11..x..xii to to

profile HMM ending with profile HMM ending with xxii emitted by the emitted by the

state state MMjj..

•• vvIIjj (i) (i) andand vvDD

jj (i) (i) are defined similarly.are defined similarly.

An Introduction to Bioinformatics Algorithms www.bioalgorithms.info

Profile HMM Alignment: Dynamic Programming

vvMMjj--11(i(i--1) + log(a1) + log(aMMjj--1,1,MMj j ))

vvMMjj(i) = log ((i) = log (eeMMjj((xxii)/p()/p(xxii)) + max v)) + max vII

jj--11(i(i--1) + log(a1) + log(aIIjj--11,,MMj j ))

vvDDjj--11(i(i--1) + log(a1) + log(aDDjj--11,,MMj j ))

vvMMjj(i(i--1) + log(1) + log(aaMMjj, , IIjj))

vvIIjj(i) = log ((i) = log (eeIIjj((xxii)/p()/p(xxii)) + max v)) + max vII

jj(i(i--1) + log(1) + log(aaIIjj, , IIjj))

vvDDjj(i(i--1) + log(1) + log(aaDDjj, , IIjj))

Page 23: Hidden Markov Models - Autenticação · Hidden Markov Models Slides revised and adapted to Bioinformática 55 Engª Biomédica/IST 2005 Ana Teresa Freitas Forward Algorithm • For

23

An Introduction to Bioinformatics Algorithms www.bioalgorithms.info

Paths in Edit Graph and Profile HMM

A path through an edit graph and the corresponding path through a profile HMM