Markov Chain
http://iailab.kaist.ac.kr/
Industrial AI Lab at KAIST
Table of Contents
1. Markov Process (or Chain)¶
1.1. Sequential Processes¶
Most classifiers ignored the sequential aspects of data
Consider a system which can occupy one of NN discrete states or categories qt∈{S1,S2,⋯,SN}qt∈{S1,S2,⋯,SN}
We are interested in stochastic systems, in which state evolution is random
Any joint distribution can be factored into a series of conditional distributions
p(q0,q1,⋯,qT)=p(q0)p(q1∣q0)p(q2∣q1q0)p(q3∣q2q1q0)⋯p(q0,q1,⋯,qT)=p(q0)p(q1∣q0)p(q2∣q1q0)p(q3∣q2q1q0)⋯
p(q0,q1,⋯,qT)=p(q0)p(q1∣q0)p(q2∣q1)p(q3∣q2)⋯p(q0,q1,⋯,qT)=p(q0)p(q1∣q0)p(q2∣q1)p(q3∣q2)⋯
1.2. Markov Process¶
- (Assumption) for a Markov process, the next state depends only on the current state:
p(qt+1∣qt,⋯,q0)=p(qt+1∣qt)p(qt+1∣qt,⋯,q0)=p(qt+1∣qt)
- More clearly
p(qt+1=sj∣qt=si)=p(qt+1=sj∣qt=si,any earlier history)p(qt+1=sj∣qt=si)=p(qt+1=sj∣qt=si,any earlier history)
- Given current state, the past does not matter
- The state captures all relevant information from the history
- The state is a sufficient statistic of the future
1.3. State Transition Matrix¶
- For a Markov state 𝑠 and successor state 𝑠′, the state transition probability is defined by
Pss′=P[St+1=s′∣St=s]Pss′=P[St+1=s′∣St=s]
- State transition matrix PP defines transition probabilities from all states ss to all successor states s′s′.


Example: Markov chhain episodes
- sample episodes starting from S1S1
import numpy as np
P = [[0, 0, 1],
[1/2, 1/2, 0],
[1/3, 2/3, 0]]
print(P[1][:])
a = np.random.choice(3,1,p = P[1][:])
print(a)
# sequential processes
# sequence generated by Markov chain
# S1 = 0, S2 = 1, S3 = 2
# starting from 0
x = 0
S = []
S.append(x)
for i in range(50):
x = np.random.choice(3,1,p = P[x][:])[0]
S.append(x)
print(S)
1.4. Markov Chain Components¶
It represents passive stochastic behavior
1) a finite set of NN states, S={S1,⋯,SN}S={S1,⋯,SN}
2) a state transition probability, P={pij}M×M,1≤i,j≤MP={pij}M×M,1≤i,j≤M
3) an initial state probability distribution, π={πi}π={πi}
Example of Markov Chain
- Starting from S1S1 = Class 1

P = [[0, 0.5, 0, 0, 0, 0.5, 0],
[0, 0, 0.8, 0, 0, 0, 0.2],
[0, 0, 0, 0.6, 0.4, 0, 0],
[0, 0, 0, 0, 0, 0, 1],
[0.2, 0.4, 0.4, 0, 0, 0, 0],
[0.1, 0, 0, 0, 0, 0.9, 0],
[0, 0, 0, 0, 0, 0, 1]]
# sequential processes
# sequence generated by Markov chain
# [C1 C2 C3 Pass Pub FB Sleep] = [0 1 2 3 4 5 6]
name = ['C1','C2','C3','Pass','Pub','FB','Sleep']
# starting from 0
x = 0
S = []
S.append(x)
for i in range(5):
x = np.random.choice(len(P),1,p = P[x][:])[0]
S.append(x)
print(S)
episode = []
for i in S:
episode.append(name[i])
print(episode)
2. Chapman-Kolmogorov Equation¶
- (1-step transition probabilities) For a Markov chain on a finite state space, S={S1,⋯,SN}S={S1,⋯,SN}, with transition probability matrix PP and initial distribution π={π0i}π={π0i} (row vector) then the distribution of X(1)X(1) is given by

- (2-step transition probabilities) For a Markov chain on a finite state space, S={S1,⋯,SN}S={S1,⋯,SN}, with transition probability matrix PP and initial distribution π={π0i}π={π0i} (row vector) then the distribution of X(2)X(2) is given by

- (n-step transition probabilities) For a Markov chain on a finite state space, S={S1,⋯,SN}S={S1,⋯,SN}, with transition probability matrix PP and initial distribution π={π0i}π={π0i} (row vector) then the distribution of X(n)X(n) is given by

PnPn: n-step transition probabilities
Key recursion
pij(n)=N∑k=1pik(n−1)pkj(1),i→kandk→jimplyi→jpij(n)=N∑k=1pik(n−1)pkj(1),i→kandk→jimplyi→j


# state probability distribution
P = [[0, 0, 1],
[1/2, 1/2, 0],
[1/3, 2/3, 0]]
u = [0, 1, 0]
P = np.asmatrix(P)
u = np.asmatrix(u)
for i in range(10):
u = u*P
print(u)
u = [0, 1, 0]
u = u*P**10
print(u)
3. Stationary Distribution¶
Steady-state behavior
Does pij(n)=P[Xn=j∣X0=i]pij(n)=P[Xn=j∣X0=i] converge to some πjπj ?
Take the limit as n→∞n→∞
pij(n)=N∑k=1pik(n−1)pkjpij(n)=N∑k=1pik(n−1)pkj
πj=N∑k=1πkpkjπj=N∑k=1πkpkj
- Need also ∑jπj=1∑jπj=1
π=πPπ=πP
- How to compute (https://brilliant.org/wiki/stationary-distributions/)
- Eigen-analysis
- Fixed-point iteration (we already did)
# eigenvalue = 1 and associated eigenvector
d, v = np.linalg.eig(P.T)
print(d) # loof for eigenvalue = 1
print(v[:,2]/np.sum(v[:,2]))
%%javascript
$.getScript('https://kmahelona.github.io/ipython_notebook_goodies/ipython_notebook_toc.js')