R中的马尔可夫链的手动模拟(3)

时间:2019-06-04 17:02:03

标签: r statistics simulation probability markov-chains

我已尝试改进my previous code,以便可以合并conditional probability

源代码

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

我该如何纠正?

1 个答案:

答案 0 :(得分:1)

我没有检查您的其余代码,但似乎只有prob有错误;您正在将行与列混合在一起,应该是

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

然后,由于以下原因,NaN仍然是有效的可能性。我们正在研究有条件概率P(X 1 = 1 | X 0 = 1),根据定义,只有当P(X 0 = 1)> 0。样本估计值也是如此:如果没有X 0 = 1的情况,则prob内的均值中的“分母”为零。因此,它不能也不应该被修复(即在这种情况下返回0将是错误的)。