如何在R

时间:2017-07-08 15:04:20

标签: hidden-markov-models poisson

我必须从隐马尔可夫模型生成数据。数据被建模为泊松过程,其参数根据隐马尔可夫链进行切换。换句话说,y_(k)|y_(k-1),r_(k)分发为Poisson(lambda_(r_k))。 模型的参数为lambda=(0.5,2.5,5,8),初始分布和转移矩阵在下面的算法中分别表示为p0p1。我的问题是,我不知道下面的流程是否正确,或者是否有更好的方法来获取我的数据?

# -------Function RandomChoice------------
RandomChoice <- function( p){
  u=runif(1,0,1)
  if (u<=p[1]) return (1)
  else if (u<=p[1]+p[2]) return(2)
  else if (u<=p[1]+p[2]+p[3]) return(3)
  else  return(4)
}


# Create Data
TimeK = 100
NumState = 4
Iterations = 250
r=c()
y=c()
lambda=c(0.5,2.5,5,8)
p0=c(0.25,0.25,0.25,0.25)

p1=matrix(c(0.28,0.64,0.05,0.03,0.36,0.46,0.1,0.08,0.06,0.14,0.43,
0.37,0.01,0.02,0.64,0.33), byrow=TRUE, nrow=4,ncol=4)
r[1]=RandomChoice(p0)
y[1]=rpois(1,lambda[r[1]])
for (k in 2:TimeK)
{
  r[k]=RandomChoice(p1[r[k-1],])
  y[k]=rpois(1,lambda[r[k]])
}

0 个答案:

没有答案