问题描述
我有以下用于模拟随机系统的 Gillespie 算法的变体:
gillespie_counter_two <- function(N,n,...) {
tt = 0
x = N$M
S = t(N$Post - N$Pre)
u = nrow(S)
v = ncol(S)
count_matrix=matrix(0,nrow=n,ncol=ncol(N$Pre))
tvec = vector("numeric",n)
count= vector("numeric",v)
xmat = matrix(ncol=u,nrow=n+1)
xmat[1,] = x
for (i in 1:n){
h = N$h(x,tt,...)
if (h==0){
k=1
while (k <= n){
xmat[i+k-1,]=xmat[i,]
count_matrix[i+k-1,]=0
tvec[i+k-1]=tvec[i-1] + (tvec[i-2]-tvec[i-3])*(k)
k=k+1
}
tvec[n]= tvec[i-1] + (tvec[i-2]-tvec[i-3])*(n)
xmat[n,]=xmat[n-1,]
xmat[n,]=xmat[n,]
break
}
else{
tt = tt+rexp(1,sum(h))
j = sample(v,1,prob=h)
x = x+S[,j]
can= vector("numeric",ncol(N$Pre))
can[j]=1
count=count + can
count_matrix[i,]=count
tvec[i] = tt
xmat[i+1,] = x
}
}
return(list(t=tvec,x=xmat,r=count_matrix))
}
当我有以下输入时
N=list()
N$M= c(x1=100,x2=1,x3=0)
N$Pre= matrix(c(1,0),nrow=2,byrow=TRUE)
N$Post=matrix(c(0,2,1),byrow=TRUE)
N$h= function(x,t,th= c(th1=0.05,th2=1)) {
with(as.list(c(x,th)),{
return(c(th1*x1*x2,th2*x2))
})
}
所以我运行 Gillespie_counter_two(N,1000),它产生以下错误
Error in `[<-`(`*tmP*`,i + k - 1,value = 0) : subscript out of bounds
In addition: There were 50 or more warnings (use warnings() to see the first 50)
这是怎么回事??
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)