user366312
user366312

Reputation: 16894

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?

How can I correct it?

Upvotes: 0

Views: 145

Answers (1)

Julius Vainora
Julius Vainora

Reputation: 48191

I didn't inspect the rest of your code, but it seems that only prob has a mistake; 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. We are looking at a conditional probability P(X1=1|X0=1) which, by definition, is well defined only when P(X0=1)>0. The same holds with sample estimates: if there are no cases where X0=1, then the "denominator" in the mean inside of prob is zero. Thus, it cannot and should not be fixed (i.e., returning 0 in those cases would be wrong).

Upvotes: 1

Related Questions