考虑 5 个状态{1,2,3,4,5} 上的马尔可夫过程 Xt 以及相关的速率矩阵
Q <- matrix( c(-3,3,0,0,0,0,-2,2,0,0,0,1,-1,0,0,0,0,0,-3,3,1,0,2,0,-3),
nrow=5, ncol=5, byrow = TRUE)
如何生成 R 代码来估计这个过程从状态 4 开始到达状态 5 的概率?</p>
我试过这段代码,但不知道正确与否。
states<-5
trials<-1000
result<-0
Q <- matrix( c(-3,3,0,0,0,0,-2,2,0,0,0,1,-1,0,0,0,0,0,-3,3,1,0,2,0,-3),
nrow=5, ncol=5, byrow = TRUE)
P<-matrix( rep(0,states*states),
nrow=states, ncol=states, byrow = TRUE)
for( j in 1:states){
if(Q[j,j] ==0){
P[j,]= rep(0,states)
P[j,j]=1
}else{
P[j,] = -Q[j,]/Q[j,j]
P[j,j]=0
}
}
for(k in 1:trials){
X<-4 # initial state of the Markov Chain
i<-1
while(X[i]!= 5){
Y<-runif(1)
p<-P[X[i],]
p<-cumsum(p)
# update the chain
if(Y<=p[1]){
X[i+1]= 1
}else if(Y<=p[2]){
X[i+1]=2
}else if(Y<=p[3]){
X[i+1]=3
}else if(Y<=p[4]){
X[i+1]=4
}else {
X[i+1] = 5
}
i<-i+1
}
if(X[i] ==5){
result<-result+1
}
}
prob<-result/trials
prob