Manual simulation of Markov Chain in R (2)

六眼飞鱼酱① 提交于 2019-12-24 07:50:37

问题


Consider the Markov chain with state space S = {1, 2}, transition matrix

and initial distribution α = (1/2, 1/2).

  1. Simulate 5 steps of the Markov chain (that is, simulate X0, X1, . . . , X5). Repeat the simulation 100 times.

My solution:

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

nextX <- function(X, pMat)
{
    probVec <- vector()

    if(X == states[1])
    {
        probVec <- pMat[1,]
    }
    if(X==states[2])
    {
        probVec <- pMat[2,]
    }

    return(sample(states, 1, replace=TRUE, prob=probVec))
}

steps <- function(alpha1, mat1, n1)
{
    X0 <- sample(states, 1, replace=TRUE, prob=alpha1)

    if(n1 <=0)
    {
        return (X0)
    }
    else
    {
        vec <- vector(mode="numeric", length=n1)

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

        return (vec)
    }
}

# steps(alpha1=alpha, mat1=mat, n1=5)

simulate <- function(alpha1, mat1, n1)
{
    for (i in 1:n1) 
    {
        vec <- steps(alpha1, mat1, 5)
        print(vec)
    }
}

simulate(alpha, mat, 100)

Output

> simulate(alpha, mat, 100)
[1] 1 2 2 2 2
[1] 2 1 2 2 2
[1] 1 1 1 1 1
[1] 1 1 1 1 1
[1] 2 2 2 2 2
[1] 2 1 1 2 2
[1] 1 1 1 1 1
[1] 2 2 1 2 2
[1] 1 1 1 1 1
[1] 2 2 1 1 2
[1] 2 2 1 2 2
[1] 1 1 1 1 1
[1] 1 1 1 1 1
[1] 1 1 2 2 2
[1] 1 1 1 1 1
[1] 2 1 2 2 2
[1] 1 1 1 1 1
[1] 1 1 1 1 1
[1] 2 1 2 1 1
[1] 1 1 1 1 1
[1] 1 1 1 1 1
[1] 1 1 1 1 1
[1] 1 1 1 1 1
[1] 1 2 1 2 2
[1] 2 2 2 1 2
[1] 1 1 1 1 1
[1] 1 1 1 1 1
[1] 1 1 1 1 1
[1] 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I fix my code?


回答1:


There are two issues:

Transposed Matrix

If you check the matrix you have input, it is the transpose of what you wanted:

> mat
     [,1] [,2]
[1,]  0.5    0
[2,]  0.5    1

So, change that.

States are not Chained

In the step function, the returned state is not used to initiate the subsequent state. Instead, X0 just keeps getting passed in repeatedly:

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

Honestly, you don't need X0 at all. Just change all the X0s in the step function to X and it should work.



来源:https://stackoverflow.com/questions/55840140/manual-simulation-of-markov-chain-in-r-2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!