An Introduction to Markov Chains

A Markov chain is a series of random variables, known as states, that satisfy the Markov property: the probability of the current state only depends on the state that preceded it. In other words, past and future states are stochastically independent. The “time” can be discrete, continuous or more generally, a totally ordered set.
Christopher DeFiglia

Example of Markov Chains

Imagine a mouse in a cage with two cells 1 and 2 containing fresh and stinky cheese. The mouse is in cell 1 at time n, then at time n + 1 it is either still in 1 or has moved to 2. Statistical observation shows that the mouse moves from cell 1 to cell 2 with probability α = 0.05 regardless of where it was at an earlier time. Similarly, it moves from 2 to 1 with probability β = 0.99. This can be represented by a transition diagram and/or a transition probability matrix.
transitionMatrix={{0.95,0.05},{0.99,0.01}};
g=Graph[DiscreteMarkovProcess[1,transitionMatrix]];​​Scan[(PropertyValue[{g,#},EdgeLabels]=PropertyValue[{g,#},"Probability"])&,EdgeList[g]];​​g
transitionMatrix//MatrixForm

0.95
0.05
0.99
0.01


Stationarity

Absorbing Markov Chains

Random Walk

A special kind of Markov chain is known as a random walk. This is a chain that presents spatial homogeneity in addition to time-homogeneous transition probabilities. In other words, at every state, the next step is an outgoing edge chosen according to an arbitrary distribution.
A simple random walk is symmetric if the moving “object” has the same probability for each of its neighbors.
Let’s define a better function to simulate Markov chains. simulateMChain simulates a chain of length n with initial distribution
p
0
and transition distribution p. Here p is a function returning the probabilities
p
i,j1
,...,
p
i,jk
, where j represents the states that belong to S that can be reached in one step from state i in S.
simulateMChain[p0_,p_,n_]:=​​Module[​​{init=p0,transP=p,chainL=n},​​chain=ConstantArray[RandomChoice[init],chainL+1];​​For[i=1,i≤chainL,i++,chain[[i+1]]=RandomChoice[transP[chain[[i]]]]];​​Return[chain[[2;;]]];​​]

Simple Symmetric Random Walk

Let’s create a random walk graph using
p:i
1
2
,
1
2
,(i-1+i+1)). randomWalkSymmetric plots 10 random walks of length 50, each one starting at a random point between {-10,..10}.
randomWalkSymmetric[prob_]:={.5,.5}{prob-1,prob+1};​​ListPlot[Table[simulateMChain[Range[-10,10],randomWalkSymmetric,100],{i,1,10}],JoinedTrue]
20
40
60
80
100
-20
-10
10
20

Bounded Symmetric Random Walk

The following is a simulation of a random walk using absorbing barriers between –10 and 10. As explained previously, if the walk reaches any of these states, it will stay there. This example uses the same transition distribution as before. This time, however, let’s start all walks at 0.
absorbingRandomWalk[prob_]:=If[prob-10||prob10,{prob},{.5,.5}{prob-1,prob+1}];​​ListPlot[Table[simulateMChain[{0},absorbingRandomWalk,100],{i,1,10}],JoinedTrue]
20
40
60
80
100
-10
-5
5
10

Asymmetric Random Walk

The following graph represents a random walk in which the initial distribution is asymmetric. Notice that even a slight change in probability drastically changes the general tendency of the trajectories.
randomWalkAsymmetric5[prob_]:={.45,.55}{prob-1,prob+1};​​randomWalkAsymmetric10[prob_]:={.40,.60}{prob-1,prob+1};​​{ListPlot[Table[simulateMChain[{0},randomWalkAsymmetric5,100],{i,1,10}],JoinedTrue],ListPlot[Table[simulateMChain[{0},randomWalkAsymmetric10,100],{i,1,10}],JoinedTrue]}

20
40
60
80
100
-10
0
10
20
30
,
20
40
60
80
100
0
10
20
30


Reluctant Random Walk

If a transition probability depends on the current position, the random walk is called reluctant. This is represented in the following graph. The closer we get to 0 or 10, the probability that any of these numbers are going to be reached decreases, thus we say the random walk is reluctant to end.
randomWalkReluct[prob_]:={prob/10,(10-prob)/10}{prob-1,prob+1};​​ListPlot[Table[simulateMChain[{5},randomWalkReluct,100],{i,1,10}],JoinedTrue]
20
40
60
80
100
2
4
6
8
10
FURTHER EXPLORATIONS
​
AUTHORSHIP
Christopher DeFiglia