demo/Chapter.6.2.R

####################################################
# Section 6.2 Introduction to Discrete Markov Chains
####################################################

 P=matrix(c(.5,.5,0,0,0,0,.25,.5,.25,0,0,0,0,.25,.5,.25,0,0,
           0,0,.25,.5,.25,0,0,0,0,.25,.5,.25,0,0,0,0,.5,.5),
           nrow=6,ncol=6,byrow=TRUE)
 P

 s=array(0,c(50000,1))

 s[1]=3
 for (j in 2:50000)
   s[j]=sample(1:6,size=1,prob=P[s[j-1],])

 m=c(500,2000,8000,50000)
 for (i in 1:4)
   print(table(s[1:m[i]])/m[i])

 w=matrix(c(.1,.2,.2,.2,.2,.1),nrow=1,ncol=6)
 w%*%P
bayesball/LearnBayes documentation built on May 11, 2019, 9:21 p.m.