了解R中的马尔可夫链源代码

时间:2019-05-09 14:23:45

标签: r statistics probability markov-chains markov

以下源代码来自一本书。我写评论是为了更好地理解代码。

#==================================================================
# markov(init,mat,n,states) = Simulates n steps of a Markov chain 
#------------------------------------------------------------------
# init = initial distribution 
# mat = transition matrix 
# labels = a character vector of states used as label of data-frame; 
#           default is 1, .... k
#-------------------------------------------------------------------
markov <- function(init,mat,n,labels) 
{ 
    if (missing(labels)) # check if 'labels' argument is missing
    {
        labels <- 1:length(init) # obtain the length of init-vecor, and number them accordingly.
    }

    simlist <- numeric(n+1) # create an empty vector of 0's
    states <- 1:length(init)# ???? use the length of initial distribution to generate states.
    simlist[1] <- sample(states,1,prob=init) # sample function returns a random permutation of a vector.
                        # select one value from the 'states' based on 'init' probabilities.

    for (i in 2:(n+1))
    { 
        simlist[i] <- sample(states, 1, prob = mat[simlist[i-1],]) # simlist is a vector.
                                                    # so, it is selecting all the columns 
                                                    # of a specific row from 'mat'
    }

    labels[simlist]
}
#==================================================================

我对这个源代码有些困惑。

为什么states <- 1:length(init)用于生成状态?如果状态如 S = {-1,0,1,2,...} 该怎么办?

1 个答案:

答案 0 :(得分:1)

各州的名称实际上不需要具有任何统计意义,只要它们是不同的即可。因此,在模拟状态之间的转换时,最好为它们选择states <- 1:length(init)或任何其他名称。最终,尽管如此,出于实际目的,我们通常会想到一些标签,例如-1、0,...,n,如您的示例所示。您可以提供这些名称作为labels参数,然后labels[simlist]1:length(init)重命名为labels,并逐元素重命名。也就是说,如果最初我们有c(1, 2, 3),而您以labels的形式提供了c(5, 10, 12),则输出将基于后一个向量。例如,

(states <- sample(1:3, 10, replace = TRUE))
# [1] 1 3 3 2 2 1 2 1 3 3
labels <- c(5, 10, 12)
labels[states]
# [1]  5 12 12 10 10  5 10  5 12 12