简体   繁体   English

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

[英]Manual simulation of Markov Chain in R

Consider the Markov chain with state space S = {1, 2} , transition matrix 考虑状态空间为S = {1,2}的马尔可夫链,转移矩阵

在此处输入图片说明

and initial distribution α = (1/2, 1/2) . 和初始分布α=(1/2,1/2)

  1. Simulate 5 steps of the Markov chain (that is, simulate X 0 , X 1 , . . . , X 5 ). 模拟马尔可夫链的5个步骤(即模拟X 0X 1 ,..., X 5 )。 Repeat the simulation 100 times. 重复模拟100次。 Use the results of your simulations to solve the following problems. 使用模拟结果来解决以下问题。

    • Estimate P(X 1 = 1|X 0 = 1) . 估计P(X 1 = 1 | X 0 = 1) Compare your result with the exact probability. 将您的结果与确切的可能性进行比较。

My solution: 我的解决方案:

# returns Xn 
func2 <- function(alpha1, mat1, n1) 
{
  xn <- alpha1 %*% matrixpower(mat1, n1+1)

  return (xn)
}

alpha <- c(0.5, 0.5)
mat <- matrix(c(0.5, 0.5, 0, 1), nrow=2, ncol=2)
n <- 10


for (variable in 1:100) 
{
   print(func2(alpha, mat, n))
}

What is the difference if I run this code once or 100 times (as is said in the problem-statement)? 如果我运行一次或100次此代码有什么区别(如问题陈述中所述)?

How can I find the conditional probability from here on? 从这里开始如何找到条件概率?

Let

alpha <- c(1, 1) / 2
mat <- matrix(c(1 / 2, 0, 1 / 2, 1), nrow = 2, ncol = 2) # Different than yours

be the initial distribution and the transition matrix. 是初始分布和过渡矩阵。 Your func2 only finds n-th step distribution, which isn't needed, and doesn't simulate anything. 您的func2仅找到第n步分布,这是不需要的,并且不模拟任何东西。 Instead we may use 相反,我们可以使用

chainSim <- function(alpha, mat, n) {
  out <- numeric(n)
  out[1] <- sample(1:2, 1, prob = alpha)
  for(i in 2:n)
    out[i] <- sample(1:2, 1, prob = mat[out[i - 1], ])
  out
}

where out[1] is generated using only the initial distribution and then for subsequent terms we use the transition matrix. 其中, out[1]仅使用初始分布生成,然后对于后续项,我们使用过渡矩阵。

Then we have 那我们有

set.seed(1)
# Doing once
chainSim(alpha, mat, 1 + 5)
# [1] 2 2 2 2 2 2

so that the chain initiated at 2 and got stuck there due to the specified transition probabilities. 因此链条从2开始并由于指定的转移概率而卡在那里。

Doing it for 100 times we have 做一百遍

# Doing 100 times
sim <- replicate(chainSim(alpha, mat, 1 + 5), n = 100)
rowMeans(sim - 1)
# [1] 0.52 0.78 0.87 0.94 0.99 1.00

where the last line shows how often we ended up in state 2 rather than 1. That gives one (out of many) reasons why 100 repetitions are more informative: we got stuck at state 2 doing just a single simulation, while repeating it for 100 times we explored more possible paths. 最后一行显示了我们在状态2而不是状态1中出现的频率。这给出了一个(为什么)100个重复信息更丰富的原因:我们陷入状态2只是进行了一次模拟,而重复了100次我们探索了更多可能的途径。

Then the conditional probability can be found with 然后可以找到条件概率

mean(sim[2, sim[1, ] == 1] == 1)
# [1] 0.4583333

while the true probability is 0.5 (given by the upper left entry of the transition matrix). 而真实概率为0.5(由转换矩阵的左上角给出)。

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

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