微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

模拟马尔可夫链的步骤

如何解决模拟马尔可夫链的步骤

我试图通过马尔可夫链模拟一个步骤,目的是多次循环执行该过程直到满足条件。 (即,找出平均需要多少步才能达到特定状态)。

在这种情况下,一个状态只能走一种方式。例如,状态 4 可以向前转换到状态 5,但不能向后转换到状态 3。这意味着转换矩阵的左下半部分设置为零。这也是为什么下面的方法将任意大的值置于“先前”状态的原因。我试图通过检查转移矩阵的指定行中哪个概率最接近随机数来找到正确的新状态。

get_new_state <- function(current_state,trans_matrix)
{
  # generate a random number between 0-1 to compare to the transition matrix probabilities
  rand <- runif(1)
  
  # transition to where the 
  # random number falls within the transition matrix
  row <- current_state # initial condition determines the row of the trans_matrix
  col = current_state # start in the column 
  # loop thru all columns and find the correct state
  potential_states <- rep(0,each=ncol(trans_matrix)) # holds the value of the potential state it transitions to

  # in this case,we can't transition to a prevIoUs state so we set the prevIoUs state values arbitrarily high 
  # so they don't get selected in the which.min() function later
  potential_states[1:col] <- 999
  
  for(k in row:ncol(trans_matrix)) # loop thru non-zero matrix values
  {
    if(trans_matrix[row,k] > rand)
    {
      potential_states[k] <- trans_matrix[row,k] / rand
      potential_states[k] <- 1 - potential_states[k]
    }
  }
  
  # new state is equal to the index of the lowest value
  # lowest value = closest to random number
  new_state = which.min(potential_states)
  return(as.numeric(new_state))
}

我不确定这种方法是否合理。我假设有一种更好的模拟方法,无需在 potential_states[] 中放入任意大值的 kluge。

解决方法

这样的事情会更好吗(这是一个单线马尔可夫转换):

> new_state <- sample(1:number_states,size=1,prob=transition_matrix[old_state,])

然后将其放入(例如)带有计数器的 while() 循环中。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。