File: Chapter.6.2.R

package info (click to toggle)
r-cran-learnbayes 2.15.1-4
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 1,864 kB
  • sloc: sh: 15; makefile: 2
file content (22 lines) | stat: -rw-r--r-- 570 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
####################################################
# 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