You are on page 1of 3

Code 1

install.packages("diagram")
install.packages("expm")
#specifying transition probability matrix
tm<-
matrix(c(0,1,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0.4,0.2,0.2,0.2,0,0,0,0,0.2,0.4,0.4,0.3,0,0,0.1,0.3,0.1,0.
2,0,0,0,0.2,0.2,0.3,0.3,0,0,0,0.5,0.2,0.2,0.1),
nrow=7,ncol=7,byrow=TRUE)
#transposing transition probability matrix
tm.tr <-t(tm)
#plotting diagram
library(diagram)
plotmat(tm.tr,pos=c(1,2,3,1),arr.length=0.7,arr.width=0.1,
box.col="lightblue",box.lwd=1,box.prop=0.5,box.size=0.125,
box.type="circle",cex.txt=0.8,lwd=1,self.cex=0.8,
self.shiftx=0.17,self.shifty=-0.01)
library(expm)
print(tm3<-tm%^%3)
#computing unconditional distribution after three steps
init.p<-c(1/7,1/7,1/7,1/7,1/7,1/7,1/7)
init.p%*%tm3
[,1] [,2] [,3] [,4] [,5] [,6] [,7]
[1,] 0.000 1.00 0 0.000 0.000 0.000 0.000
[2,] 1.000 0.00 0 0.000 0.000 0.000 0.000
[3,] 0.066 0.06 0 0.218 0.210 0.226 0.220
[4,] 0.066 0.06 0 0.166 0.210 0.248 0.250
[5,] 0.351 0.09 0 0.120 0.139 0.149 0.151
[6,] 0.066 0.06 0 0.192 0.210 0.237 0.235
[7,] 0.066 0.06 0 0.237 0.210 0.218 0.209

Conclusion:

According to the Chapman-Kolmogorov equations, this 7*7 matrix shows the transition matrix
after three transitions, where the Mij element represents the probability that we reach the state j
from state i after 3 transitions.

CODE 2:

install.packages("diagram")
install.packages("expm")
#specifying transition probability matrix
tm<-matrix(c(1.0,0,0,0,0,0.5,0,0,0,0.5,0.2,0,0,0,0.8,0,0,1,0,0,0,0,0,1,0),
nrow=5,ncol=5,byrow=TRUE)
#transposing transition probability matrix
tm.tr<-t(tm)
#plotting diagram
library(diagram)
plotmat(tm.tr,pos=c(1,2,2),arr.length=0.3,arr.width=0.1,
box.col="lightblue",box.lwd=1,box.prop=0.5,box.size=0.125,
box.type="square",cex.txt=0.8,lwd=1,self.cex=0.8,
self.shiftx=0.17,self.shifty=-0.01)
library(expm)
print(tm3<-tm%^%3)
#computing unconditional distribution after three steps
init.p<-c(1/5,1/5,1/5,1/5,1/5)
init.p%*%tm3
[,1] [,2] [,3] [,4] [,5]
[1,] 1.0 0 0.0 0.0 0.0
[2,] 0.5 0 0.5 0.0 0.0
[3,] 0.2 0 0.8 0.0 0.0
[4,] 0.2 0 0.0 0.8 0.0
[5,] 0.2 0 0.0 0.0 0.8

Conclusion:

According to the Chapman-Kolmogorov equations, this 5*5 matrix shows the transition matrix
after three transitions, where the Mij element represents the probability that we reach the state j
from state i after 3 transitions.

You might also like