Markov Chain
Table of Contents
$$p(q_0,q_1,\cdots,q_T ) = p(q_0) \; p(q_1 \mid q_0) \; p( q_2 \mid q_1 q_0 ) \; p( q_3 \mid q_2 q_1 q_0 ) \cdots$$
$$p(q_0,q_1,\cdots,q_T ) = p(q_0) \; p(q_1 \mid q_0) \; p( q_2 \mid q_1 ) \; p( q_3 \mid q_2 ) \cdots$$
Example: Markov chhain episodes
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)
It represents passive stochastic behavior
$\;\;$ 1) a finite set of $N$ states, $S = \{ S_1, \cdots , S_N \}$
$\;\;$ 2) a state transition probability, $P = \{ p_{ij} \}_{M \times M},\quad 1 \leq i,j \leq M$
$\;\;$ 3) an initial state probability distribution, $\pi = \{ \pi_i\}$
Example of Markov Chain
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)
# 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)
# 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]))
%%html
<center><iframe src="https://www.youtube.com/embed/vW8WnPIPz2s?rel=0"
width="420" height="315" frameborder="0" allowfullscreen></iframe></center>
%%html
<center><iframe src="https://www.youtube.com/embed/lvCO2z3xYWM?rel=0"
width="420" height="315" frameborder="0" allowfullscreen></iframe></center>
%%javascript
$.getScript('https://kmahelona.github.io/ipython_notebook_goodies/ipython_notebook_toc.js')