Markov Chainsfin
Markov Chainsfin
Lecture 2
.
Markov Chain :
a process with a finite number of states (or outcomes, or events) in
which the probability of being in a particular state at step n + 1
depends only on the state occupied at step n.
2
A sequence of trials of an experiment is a Markov chain if:
1. the outcome of each experiment is one of a set of discrete states;
2. the outcome of an experiment depends only on the present state, and not on any
past states.
Categories
Time: 1, 2, 3, -- - k - -
RVs = Random Variables
RVs: X1, X2, - - - Xk -- -
4
If the system is in state i, the probability that it transitions to the
next state, j in the next time is given by:
5
6
7
…Definition 2
The initial distribution for the Markov chain is the sequence of probabilities
p ( x1 ) axi1xi
i 2
Similarly, (X1,…, Xi ,…) is a sequence of probability
distributions over D. There is a rich theory which studies the
properties of these sequences. A bit of it is presented next.
10
Markov Chain (cont.)
X1 X2 Xn-1 Xn
11
Suppose this person has an initial distribution among the shops as follows:
P(X0 = x0, X1 = x1, . . . , Xn-1 = xn-1, Xn = xn) = π(x0)p(x0, x1)p(x1, x2) · · · p(xn-1, xn)
which holds for all n ≥ 0 and all choices of states in S.
13
14
15
two states => n = 1 (0, 1)
P00 = P(X t+1 = 0 | X t = 0) = ¼ ; 0 0
P01 = P(X t+1 = 1 | Xt = 0) = ¾; 0 1
P10 = P(X t+1 = 0 | Xt = 1) = ½; 1 0
P11 = P(X t+1 = 1 | Xt = 1) = ½; 1 1
16
17
18
19
Random walk with borders (gambling)
20
21
22
Chapman-Kolmogorov equation
23
Chapman Kolmogorov equations
Multiple step transition probabilities
24
2-step transition probabilities
25
n-step, m-step and (m + n)-step
26
Interpretation
27
Matrix form
28
n-Step Transition Probabilities:
Consider a Markov chain {Xn, n = 0,1,2,...}, where Xn∈S. If X0= i, then X1=j with
probability pij. That is, pij gives us the probability of going from state i to state j in
one step. Now suppose that we are interested in finding the probability of going from
state i to state j in two steps, i.e.,
We can find this probability by applying the law of total probability. In particular, we
argue that X1 can take one of the possible values in S. Thus, we can write:
We conclude
29
Example: Happy-Sad
30
31
State Transition Matrix and Diagram
32
Ex2
Example:
On considère la matrice Q de taille 7 × 7 suivante :
33
34
35
36
37
38
39
40
41
42
43
Examples 1:Weather model.
This says that if it is sunny today, then the chance it will be sunny
tomorrow is 0.8, whereas if it is rainy today, then the chance it will be
sunny tomorrow is 0.4
44
Transition matrix features:
45
Matrix Representation
A B C D The transition probabilities
0.95 0 0.05 0 Matrix M =(ast)
A
M is a stochastic Matrix:
B 0.2 0.5 0 0.3
a t st 1
0 0.2 0 0.8
C The initial distribution vector
(u1…um) defines the distribution
0 0 1 0 of X1 (p(X1=si)=ui) .
D
46
Matrix Representation
A B C D
0.95 0 0.05 0
A
Example: if X1=(0, 1, 0, 0)
B 0.2 0.5 0 0.3 then X2=(0.2, 0.5, 0, 0.3)
47
Representation of a Markov Chain as a
Digraph
State Transition Diagram
0.95
A B C D
0.95 0 0.05 0
0.2 0.5 A
0.2 0.5 0 0.3
B
0.8 D
0 0 1 0
1
A state transition diagram.
48
80% of students who do maths work during one study period, will do the maths work at the
next study period. 30% of students who do english work during one study period, will do the
english work at the next study period.
Initially there were 60 students do maths work and 40 students do english work. Calculate,
1) The transition probability matrix
2) The number of students who do maths work, english work for the next subsequent 2 study
periods.
Solution
49
Example: Consider the Markov chain shown in Figure below:
50
Solution
51
Write any transition diagrams as transition matrices
a) b)
c)
a)
52
Probability Distributions: State Probability Distributions:
We can use the law of total probability. More specifically, for any j∈S,
we can write:
53
If we generally define
54
Our work thus far is summarized below:
Example
55
Solution
56
Solved example
Voting Trends At the end of June in a presidential election year, 40% of the voters
were registered as liberal, 45% as conservative, and 15% as independent. Over a one-
month period, the liberals retained 80% of their constituency, while 15% switched to
conservative and 5% to independent. The conservatives retained 70% and lost 20% to
the liberals. The independents retained 60% and lost 20% each to the conservatives
and liberals. Assume that these trends continue.
a) Write a transition matrix using this information.
b) Write a probability vector for the initial distribution.
Find the percent of each type of voter at the end of each of
the following months.
c). July
d). August
e). September
f. October Transition matrix
(at time, t+1)
Solution
(at time, t)
L I C
Liberal 0.8 0.15 0.05
Conservative 0.2 0.7 0.1
a) Independent 0.2 0.2 0.6
57
b) Initial distribution
40% of the voters were registered as liberal, 45% as conservative, and 15% as
independent:
b) The probability vector for the initial distribution:
X0 = π(0) = [0.4 0.45 0.15]
c) Percent of each type of voter at the end of July:
44% L; 40.5% C; 15.5% I
d) Percent of each type of voter at the end of August:
46.4% L; 38.05% C; 15.55% I
e) Percent of each type of voter at the end of August: Apply X0 *P(n)
47.84% L; 36.705% C; 15.3355% I
f) Percent of each type of voter at the end of September:
48.704% I; 35.9605% C; 15.3355% I
58
Regular Transition Matrices
One of the many applications of Markov chains is in finding long-range predictions.
It is not possible to make long-range predictions with all transition matrices, but for a
large set of transition matrices, long-range predictions are possible. Such predictions
are always possible with regular transition matrices.
Any transition matrix that has no zeros determines a regular Markov chain.
However, it is possible for a regular Markov chain to have a transition matrix that
has zeros, provided certain powers have no zeros.
An example of a non-regular Markov chain is an absorbing chain.
A Markov chain is a regular Markov chain if its transition matrix is regular.
59
NOTE
If a transition matrix P has some zero entries, and P2 does as well, you may wonder
how far you must compute Pk to be certain that the matrix is not regular.
The answer is that if zeros occur in the identical places in both Pk and Pk+1 for any k,
they will appear in those places for all higher powers of P, so P is not regular.
Suppose that v is any probability vector. It can be shown that for a regular Markov
chain with a transition matrix P, there exists a single vector V that does not depend on
v, such that gets v.Pn closer and closer to V as n gets larger and larger 60
61
PROPERTIES OF REGULAR MARKOV CHAINS
62
EQUILIBRIUM VECTOR OF A MARKOV CHAIN
If a Markov chain with transition matrix P is regular, then there is a unique vector V
such that, for any probability vector v and for large values of n
Vector V is called the equilibrium vector or the fixed vector of the Markov chain.
If a Markov chain with transition matrix P is regular, then there exists a probability
vector V such that
This vector V gives the long-range trend of the Markov chain. Vector V is found by
solving a system of linear equations, as shown in the next example.
Example:
63
64
65
Exercise 2
Suppose you eat one of three desserts every day :
1. Ice cream
2. Cake
3 . Chocolate
Assume the sequence of desserts you eat form a Markov chain with transition
matrix:
a) Transition diagram
b) what is the probability that in 2 day you seat , Ice cream given that
you ate cake today ?
c) what is the probability that you eat ice cream in 200 days given that
you ate cake today ?
Solution
a)
67
b)
68
c)
69
70
By Markov hypothesis,
71
Over the long run, with what frequency is state j visited?
Then is clear that it is possible to move from any state to any state, so the
chain is ergodic. However, if n is odd, then it is not possible to move from
state 0 to state 0 in n steps, and if n is even, then it is not possible to move
from state 0 to state 1 in n steps, so the chain is not regular. 72
Theorem: If a Markov chain is ergodic, then the limits
73
Example:
74
Example: Consider the Markov chain with transition matrix
Solving this 3 by 3 linear system we get w = x = [2/5, 1/5, 2/5] and hence
limn→∞ PN (x, y) = 2/5 for y = 1 and 3, and limn→∞ Pn (x, y) = 1/5 for y = 2.
75
Example
Consider a MC with the following transition matrix:
You are planning a two-day holiday to begin in seven days, i.e., you are away on day
7 and 8. A travel insurance deal will pay you $2500 if it rains on both days, nothing
if not, and the premium is $100. Should you buy this insurance if it is sunny today?
Solution
One way to make a decision would be to compare the expected pay-out
with the premium. The actual return is:
Exercise 2
78
Exercise 3
b) Show that
79
Classification of States
The first definition concerns the accessibility of states from each other: If it is
possible to go from state i to state j, we say that state j is accessible from state i. In
particular, we can provide the following definitions.
80
On dira que deux états i et j communiquent, noté,
81
Exemple.
Considérons une chaîne de Markov d’espace d’état S = {0, 1, 2, 3} de
matrice de transition
Solution:
{1, 2, 3}, {4, 5}.
State 2 leads to state 4, but state 4 does not lead back to state 2, so they are in
different communicating classes.
Definition: A communicating class of states is closed if it is not possible to
leave that class.
83
Example:
Consider the Markov chain shown in Figure that follows.
Notice that there are two kinds of classes. In particular, if at any time the Markov
chain enters Class 4, it will always stay in that class.
On the other hand, for other classes this is not true. For example, if X0=1, then the
Markov chain might stay in Class 1 for a while, but at some point, it will leave that
class and it will never return to that class again. The states in Class 4 are called
recurrent states, while the other states in this chain are called transient.
86
Recurrent states
In general, a state is said to be recurrent if, any time that we leave that state, we will
return to that state in the future with probability one. On the other hand, if the
probability of returning is less than one, the state is called transient. Here, we provide
a formal definition:
Consider a Markov chain and assume X0=i. If i is a recurrent state, then the
chain will return to state i any time it leaves that state. Therefore, the chain will
visit state i an infinite number of times. On the other hand, if i is a transient
state, the chain will return to state i with probability fii<1. Thus, in that case, the
total number of visits to state i will be a Geometric random variable with
parameter 1−fii.
87
Recurrent – Transient State
88
89
It follows from the above proposition that:
90
91
92
93
Example 1
Show that in a finite Markov chain, there is at least one recurrent class.
Solution
Then, starting from time 0, the chain might visit state 1 several times, but at some
point the chain will leave state 1 and will never return to it. That is, there exists an
integer M1>0 such that Xn≠1, for all n ≥ M1. Similarly, there exists an integer M2>0
such that Xn≠2, for all n≥M2, and so on. Now, if you choose
n ≥ max{M1,M2,⋯,Mr},
This is a contradiction, so we conclude that there must be at least one recurrent state,
which means that there must be at least one recurrent class.
94
Ex2
Sur l’ensemble S = {0, 1, . . . , n} on considère la chaîne de Markov de
matrice de transition P donnée pour 0 ≤ x ≤ n − 1 par:
b) Soit S = {1, . . . , 6}, compléter la matrice suivante pour qu’elle soit matrice de
transition d’une chaîne de Markov
Étudions l'état n -1
On a
P{Tn-1<∞|X0=n-1} ≤ P{X1=0|X0=n-1}
=1–p
<1
Les états 0 à n - 1 sont donc transients
96
… Solution
b) Dessiner le graphe de la chaîne de Markov.
????????????
Comme le graphe est ni, on sait que les états dans les feuilles sont
récurrents et les autres sont transitoires. Montrons le dans ce cas là.
97
… Solution
There is a periodic pattern in this chain. Starting from state 0, we only return to 0 at
times n=3,6,⋯. In other words, p(n)00= 0, if n is not divisible by 3. Such a state is
called a periodic state with period d(0)=3.
99
Periodic/aperiodic
A class is said to be periodic if its states are periodic. Similarly, a class is said to be
aperiodic if its states are aperiodic. Finally, a Markov chain is said to be aperiodic if
all of its states are aperiodic.
If i↔j, then d(i)=d(j).
100
Example 2
Consider the Markov chain
Solution
101
Irrudicible/Irréductible
105
Ex4
106
Temps d’atteinte, récurrence et transience
107
Absorbing Markov Chains
Not all Markov chains are regular. In fact, some of the most important life science
applications of Markov chains do not involve transition matrices that are regular. One
type of Markov chain that is widely used in the life sciences is called an absorbing
Markov chain.
When we use the ideas of Markov chains to model living organisms, a common state
is death. Once the organism enters that state, it is not possible to leave. In this
situation, the organism has entered an absorbing state.
108
The resulting transition diagram shows that it is not possible to leave
state 2.
109
Absorbing Markov Chains
Identify all absorbing states in the Markov chains having the following matrices.
Decide whether the Markov chain is absorbing
110
Solution
a) Since P11 = 1, and P33 =1 both state 1 and state 3 are absorbing states. (Once
these states are reached, they cannot be left.) The only non-absorbing state is state
2.
There is a 0.3 probability of going from state 2 to the absorbing state 1, and a 0.2
probability of going from state 2 to state 3, so that it is possible to go from the
nonabsorbing state to an absorbing state. This Markov chain is absorbing. The
transition diagram is shown in Figure.
111
b) States 2 and 4 are absorbing, with states 1 and 3 non-absorbing.
From state 1, it is possible to go only to states 1 or 3; from state 3 it is possible to go
only to states 1 or 3.
As the transition diagram in Figure below shows, neither non-absorbing state leads to
an absorbing state, so that this Markov chain is nonabsorbing.
112
Example 2:
A man walks along a three block portion of Main St. His house is at one end
of the three block section. A bar is at the other end of the three block section.
Each time he reaches a corner he randomly either goes forward one block or
turns around and goes back one block. If he reaches home or the bar, he stays
there. The four states are Home (H), Corner 1(C1), Corner 2 (C2) and Bar
(B). Write the transition matrix and identify the absorbing states. Find the
probabilities of ending up in each absorbing state depending on the initial
state.
Solution
Home and the Bar are absorbing states. If the man arrives home, he does not leave. If
the man arrives at the bar, he does not leave. Since it is possible to reach home or the
bar from each of the other two corners on his walk, this is an absorbing Markov
chain. 113
We can raise the transition matrix T to a high power, n. One we find a
power Tn that remains stable, it will tell us the probability of ending up
in each absorbing state depending on the initial state.
T91 = T90; the matrix does not change as we continue to examine higher powers. We
see that in the long-run, the Markov chain must end up in an absorbing state. In the
long run, the man must eventually end up at either his home or the bar.
The second row tells us that if the man is at corner C1, then there is a 2/3 chance he
will end up at home and a 1/3 chance he will end up at the bar.
The third row tells us that if the man is at corner C2, then there is a 1/3 chance he will
end up at home and a 2/3 chance he will end up at the bar. Once he reaches home or
the bar, he never leaves that absorbing state.
114
Note that while the matrix Tn for sufficiently large n has become stable
and is not changing, it does not represent an equilibrium matrix. The
rows are not all identical, as we found in the regular Markov chains that
reached an equilibrium.
We can write a smaller “solution matrix” by retaining only rows that
relate to the non-absorbing states and retaining only the columns that
relate to the absorbing states.
Then the solution matrix will have rows C1 and C2, and columns H and
B. The solution matrix is
The first row of the solution matrix shows that if the man is at corner C1, then there is
a 2/3 chance he will end up at home and a 1/3 chance he will end up at the bar.
The second row of the solution matrix shows that if the man is at corner C2, then there
is a 1/3 chance he will end up at home and a 2/3 chance he will end up at the bar. 115
Example 3:
A gambler has $3,000, and she decides to gamble $1,000 at a time at a Black Jack
table in a casino in Las Vegas. She has told herself that she will continue playing until
she goes broke or has $5,000. Her probability of winning at Black Jack is .40. Write
the transition matrix, identify the absorbing states, find the solution matrix, and
determine the probability that the gambler will be financially ruined at a stage when
she has $2,000.
Solution
The transition matrix is written below. Clearly the state 0 and state 5K are the
absorbing states. This makes sense because as soon as the gambler reaches 0, she is
financially ruined and the game is over. Similarly, if the gambler reaches $5,000, she
has promised herself to quit and, again, the game is over. The reader should note that
p00 = 1, and p55 = 1.
Further observe that since the gambler bets only $1,000 at a time, she can raise or
lower her money only by $1,000 at a time. In other words, if she has $2,000 now,
after the next bet she can have $3,000 with a probability of .40 and $1,000 with a
probability of .60.
116
To determine the long term trend, we raise the matrix to higher powers until all the
non-absorbing states are absorbed. This is the called the solution matrix.
The solution matrix is often written in the following form, where the non-absorbing
states are written as rows on the side, and the absorbing states as columns on the top.
117
The table lists the probabilities of getting absorbed in state 0 or state 5K
starting from any of the four non-absorbing states. For example, if at
any instance the gambler has $3,000, then her probability of financial
ruin is 135/211 and her probability reaching 5K is 76/211.
118
Canonical Form
where Im is an identity matrix, with m equal to the number of absorbing states, and O
is a matrix of all zeros.
The element in row i, column j of the fundamental matrix gives the number of
visits to state j that are expected to occur before absorption, given that the
current state is state i.
The product FR gives the matrix of probabilities that a particular initial
nonabsorbing state will lead to a particular absorbing state. 120
Example :
A gambler has $3,000, and she decides to gamble $1,000 at a time at a Black Jack
table in a casino in Las Vegas. She has told herself that she will continue playing until
she goes broke or has $5,000. Her probability of winning at Black Jack is .40. Write
the transition matrix, identify the absorbing states, find the solution matrix, and
determine the probability that the gambler will be financially ruined at a stage when
she has $2,000.
Solution
The canonical form is listed below:
The canonical form divides the
transition matrix into four sub-
matrices as listed below.
121
The matrix F=(In−B)−1 is called the fundamental matrix for the absorbing Markov
chain, where In is an identity matrix of the same size as B. The i, j-th entry of this
matrix tells us the average number of times the process is in the non-absorbing state j
before absorption if it started in the non-absorbing state i.
According to the matrix, the entry 1.78 in the row 3, column 2 position says that the
gambler will play the game 1.78 times before she goes from $3K to $2K. The entry
2.25 in row 3, column 3 says that if the gambler now has $3K, she will have $3K on
the average 2.25 times before the game is over.
We now address the question of how many bets will she have to make before she is
absorbed, if the gambler begins with $3K?
122
If we add the number of games the gambler plays in each non-absorbing state, we get
the average number of games before absorption from that state. Therefore, if the
gambler starts with $3K, the average number of Black Jack games she will play before
absorption is
1.07+1.78+2.25+0.90=6.0
That is, we expect the gambler will either have $5,000 or nothing on the 7th bet.
Lastly, we find the solution matrix without raising the transition matrix to higher
powers. The matrix FA gives us the solution matrix.
which is the same as the following matrix we obtained by raising the transition matrix
to higher powers.
123
Exercises
Students can be in one of 4 states: passed the class (P), enrolled in the class for the
first time (C), retaking the class (R) or failed twice and can not retake (F). Experience
shows 70% of students taking the class for the first time pass and 80% of students
taking the class for the second time pass.
Write the transition matrix and identify the absorbing states. Find the probability of
being absorbed eventually in each of the absorbing states.
124
125
2)
126
Probability of Absorption
In an absorbing Markov chain, the probability that the process will be
absorbed is 1 (i.e., Qn →0 as n→∞).
The ij-entry nij of the matrix N is the expected number of times the chain is
in state sj , given that it starts in state si.
The initial state is counted if I = j.
For an absorbing Markov chain P , the matrix
N = (I−Q)−1
is called the fundamental matrix for P.
The entry nij of N gives the expected number of times that the process is in
the transient state sj if it is started in the transient state si
127
Time to Absorption
t = Nc
where c is a column vector all of whose entries are 1.
Absorption Probabilities
Let bij be the probability that an absorbing chain will be absorbed in the
absorbing state sj if it starts in the transient state si.
Let B be the matrix with entries bij.
T T T T
x1 x2 XL-1 xL
L
A Markov chain (s1,…,sL): p( s1 , , sL ) p( si | si 1 )
i 1
and for each state s and a symbol x we have p(Xi=x|Si=s)
135
Hidden Markov Model
M M M M
S1 S2 SL-1 SL
T T T T
x1 x2 XL-1 xL
Notations:
Markov Chain transition probabilities: p(Si+1= t|Si = s) = ast
Emission probabilities: p(Xi = b| Si = s) = es(b)
L
For Markov Chains we know: p( s) p( s1 , , sL ) p( si | si 1 )
i 1
136
Hidden Markov Model
M M M M
S1 S2 SL-1 SL
T T T T
x1 x2 XL-1 xL
137