Simulating Stochastic Processes#

1. Discrete time Markov Chain with finite state space#

Let \(\pi_t\) be the probability distribution at time t where \(t = 0, 1, 2, \dots\). Let \(\mathbb{P}\) be the \(n\times n\) one step transition probability matrix. By the Komolgorov-Chapman theorem, \(\pi_{t+1} = \pi_t\mathbb{P}\) and

\[\pi_t = \pi_0\mathbb{P}^t\]

The limiting probability \(\pi_\infty = \pi_\infty\mathbb{P}\). Thus, \(\pi_\infty\) is the eigen vector of \(\mathbb{P}^{'}\) for eigen value = 1.

Specify the states of the process#

states = c("ATLANTA","CHICAGO","DC","NYC")
nstates = length(states)

Initial Probability at time 0#

pi_0 = c(1,0,0,0)

Transition Probability Matrix#

tranP = matrix(runif(nstates*nstates),nrow=nstates,ncol=nstates)
for(i in 1:nstates) tranP[i,] = tranP[i,]/sum(tranP[i,])

The Probability distribution at time n#

ntime = 20
pi_n = matrix(0,nrow=ntime,ncol=nstates)
tranP_n = diag(nstates)

for(n in 1:ntime){
    tranP_n = tranP %*% tranP_n
    pi_n[n,] = pi_0 %*% tranP_n
}

print("the probablity distribution at time n")
pi_n

print("the n-step transition probability matrix")
tranP_n
[1] "the probablity distribution at time n"
A matrix: 20 × 4 of type dbl
0.30663540.021055620.33240780.3399012
0.26562040.295214310.24029480.1988705
0.29302230.306759940.18865560.2115622
0.30755570.295265460.18673870.2104401
0.30702630.289715120.19059450.2126641
0.30592950.290037450.19160320.2124299
0.30568830.290474240.19147080.2123666
0.30574590.290561290.19135860.2123342
0.30577570.290536350.19134620.2123417
0.30577760.290524880.19135340.2123441
0.30577530.290524370.19135600.2123444
0.30577470.290525320.19135590.2123441
0.30577470.290525540.19135560.2123441
0.30577480.290525510.19135560.2123441
0.30577480.290525490.19135560.2123441
0.30577480.290525480.19135560.2123441
0.30577480.290525480.19135560.2123441
0.30577480.290525490.19135560.2123441
0.30577480.290525490.19135560.2123441
0.30577480.290525490.19135560.2123441
[1] "the n-step transition probability matrix"
A matrix: 4 × 4 of type dbl
0.30577480.29052550.19135560.2123441
0.30577480.29052550.19135560.2123441
0.30577480.29052550.19135560.2123441
0.30577480.29052550.19135560.2123441

Limiting probability distribution#

pi = eigen(t(tranP))$vector[,1]
pi = pi/sum(pi)
pi
  1. 0.305774795884444+0i
  2. 0.290525485078612+0i
  3. 0.191355614662356+0i
  4. 0.212344104374588+0i

Simulation#

nsim = 50
x = rep(0,nsim)
x[1] = sample(1:nstates,1,prob = pi_0)
for(i in 2:nsim){
    x[i] = sample(1:nstates,1,prob = tranP[x[i-1],])
}

freq = table(states[x])
freq/sum(freq)

plot(1:nsim, x, xlab="time", ylab="state", type="l")
                  
ATLANTA CHICAGO      DC     NYC 
   0.24    0.34    0.24    0.18 
_images/d81fd1c60940b288249d44cd026325cd1c5cf7fa5dd4df90673226014be765c8.png

Animation#

data = rbind(c(1,1),c(2,2),c(2,0),c(3,1))
data = data[x,]
library(plotly)

df <- data.frame(
  x = data[,1], 
  y = data[,2], 
  f = 1:nsim,
  s = states[x]
)

fig <- df %>%
  plot_ly(
    x = ~x,
    y = ~y,
    frame = ~f,
    type = 'scatter',
    mode = 'markers',
    color = I("blue"),
    size = I(50)
  )

t <- list(
  family = "sans serif",
  size = 18,
  color = toRGB("red"))

fig <- fig %>% add_text(x = data[,1],y=data[,2],text = states[x],textfont=t,textposition="top right")
fig <- fig %>% layout(xaxis = list(range = c(0, 4)), yaxis = list(range = c(0,3)), showlegend = FALSE)

fig
Error in library(plotly): there is no package called ‘plotly’
Traceback:

1. library(plotly)

2. Ransom walks#

A random walk is a Markov chain with infinite state space. Let \(\pi_t\) be the probability distribution at time \(t\). Let be the \(n\times n\) one step transition probability matrix \(\mathbb{P}\). The Kolmogorov-Chapman theorem still holds, i.e.,

\[\pi_{t+1} = \pi_t\mathbb{P}\]

This indicates that \(\pi_{t+1}^i = \pi_t^{i-1}\times q + \pi_t^{i+1}\times p\)

Initial probability distribution#

x[1] = 0

Transition probability#

tranP = function(current,p){
    return(ifelse(runif(1)<=p,current-1,current+1))
}

y = 1:10000
for(i in 1:10000) y[i] = tranP(3,0.5)
sum(y==2)
4980

Simulation#

nsim = 1000
t = 100
p = 0.5
x = matrix(0, nsim, t)
for(j in 1:nsim){
    for(i in 2:t){
        x[j,i] = tranP(x[j,i-1],p)
    }
}
hist(x[,100])
_images/cffd85582202066db5d7e57c5809423b007692014bffe7dfd2e59a393576811d.png