R中马尔可夫链的手动仿真(三)

Manual simulation of Markov Chain in R (3)

我努力改进 so that I can incorporate

源代码

states <- c(1, 2)
alpha <- c(1, 1)/2
mat <- matrix(c(0.5, 0.5, 
                0, 1), nrow = 2, ncol = 2, byrow = TRUE) 

# this function calculates the next state, if present state is given. 
# X = present states
# pMat = probability matrix
nextX <- function(X, pMat)
{
    #set.seed(1)

    probVec <- vector() # initialize vector

    if(X == states[1]) # if the present state is 1
    {
        probVec <- pMat[1,] # take the 1st row
    }

    if(X==states[2]) # if the prsent state is 2
    {
        probVec <- pMat[2,] # take the 2nd row
    }

    return(sample(states, 1, replace=TRUE, prob=probVec)) # calculate the next state
}

# this function simulates 5 steps 
steps <- function(alpha1, mat1, n1)
{
    vec <- vector(mode="numeric", length = n1+1) # initialize an empty vector

    X <- sample(states, 1, replace=TRUE, prob=alpha1) # initial state
    vec[1] <- X

    for (i in 2:(n1+1))
    {
        X <- nextX(X, mat1)
        vec[i] <- X
    }

    return (vec)
}

# this function repeats the simulation n1 times.
# steps(alpha1=alpha, mat1=mat, n1=5)
simulate <- function(alpha1, mat1, n1)
{
    mattt <- matrix(nrow=n1, ncol=6, byrow=T);

    for (i in 1:(n1)) 
    {
        temp <- steps(alpha1, mat1, 5)
        mattt[i,] <- temp
    }

    return (mattt)
}    

执行

我创建了这个函数,以便它可以处理任何条件概率:

prob <- function(simMat, fromStep, toStep, fromState, toState)
{
    mean(simMat[toStep+1, simMat[fromStep+1, ]==fromState]==toState) 
}

sim <- simulate(alpha, mat, 10)

p <- prob(sim, 0,1,1,1) # P(X1=1|X0=1)
p

输出

NaN

为什么这个源代码给出 NaN

我该如何更正它?

我没有检查你的其余代码,但似乎只有 prob 有错误;你把行和列混在一起了,应该是

prob <- function(simMat, fromStep, toStep, fromState, toState)
  mean(simMat[simMat[, fromStep + 1] == fromState, toStep + 1] == toState) 

那么NaN仍然是有效的可能性,原因如下。我们正在查看 条件 概率 P(X1=1|X0=1) 其中,根据定义,仅当 P(X0=1)>0 时才定义良好。样本估计也是如此:如果不存在 X0=1 的情况,则 prob 内部均值中的 "denominator" 为零。因此,它不能也不应该被修复(即在这些情况下返回 0 是错误的)。