简体   繁体   English

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

[英]Manual simulation of Markov Chain in R (3)

I have tried to improve my previous code so that I can incorporate conditional probability . 我已尝试改进以前的代码,以便可以合并条件概率

Source Code 源代码

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)
}    

Execution 执行

I created this function so that it can handle any conditional probability: 我创建了此函数,以便它可以处理任何条件概率:

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

Output 产量

NaN

Why is this source code giving NaN ? 为什么此源代码提供NaN

How can I correct it? 我该如何纠正?

I didn't inspect the rest of your code, but it seems that only prob has a mistake; 我没有检查其余的代码,但似乎只有prob有错误; you are mixing up rows with columns and instead it should be 您将行与列混合在一起,而是

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

Then NaN still remains a valid possibility for the following reason. 然后,由于以下原因, NaN仍然是有效的可能性。 We are looking at a conditional probability P(X 1 =1|X 0 =1) which, by definition, is well defined only when P(X 0 =1)>0. 我们正在研究一个条件概率P(X 1 = 1 | X 0 = 1),根据定义,只有当P(X 0 = 1)> 0时才可以很好地定义它。 The same holds with sample estimates: if there are no cases where X 0 =1, then the "denominator" in the mean inside of prob is zero. 样本估计值也是如此:如果没有X 0 = 1的情况,则prob内均值的“分母”为零。 Thus, it cannot and should not be fixed (ie, returning 0 in those cases would be wrong). 因此,它不能也不应该被修复(即,在这些情况下返回0将是错误的)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM