我必须从隐马尔可夫模型生成数据。数据被建模为泊松过程,并根据隐马尔可夫链进行参数切换。 换句话说, 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]])
}

  ask by Spy93 translate from so

本文未有回复,本站智能推荐: