s_rathwell — Mar 11, 2014, 5:14 PM
alpha<-c(1/8, 1/8, 5/8, 1/8)
states<-c(0, 1, 2, 3)
P<-matrix(c(1/2, 1/2, 0, 0, 1/2, 1/2, 0, 0, 1/4, 1/4, 1/4, 1/4, 0, 0, 0, 1), byrow=T, nrow=4)
Prow0<-P[1,]
Prow1<-P[2,]
Prow2<-P[3,]
Prow3<-P[4,]
chain<-matrix(NA, nrow=4, ncol= 51)
par(mfrow=c(2,2))
set.seed(123456)
for(i in 1:4){
start<-sample(states, size=1, prob=alpha)
step<-0
while(step<=50){
chain[i, step+1]<-start
if(start==0){end<-sample(states, size=1, prob=Prow0)}
if(start==1){end<-sample(states, size=1, prob=Prow1)}
if(start==2){end<-sample(states, size=1, prob=Prow2)}
if(start==3){end<-sample(states, size=1, prob=Prow3)}
step<-step+1
start<-end
}
plot(0:50, chain[i,], ylim=c(0,3), type="b", xlab="step", ylab="state")
}
mtext(outer=T, line=-2, text="4 sample paths of Markov chain in example 4.12")