问题
I'm trying to write mpow(P, 18)
in vector form & matrix form. Can anyone help me with that?
Also, I'm trying to find the stationary distribution of each state.
Pi_0 = ?
Pi_1 = ?
Pi_2 = ?
...
Pi_5 = ?
Here is the code I've written:
P <- matrix(c(0, 0, 0, 0.5, 0, 0.5, 0.1, 0.1, 0, 0.4, 0, 0.4, 0, 0.2, 0.2, 0.3, 0, 0.3, 0, 0, 0.3, 0.5, 0, 0.2, 0, 0, 0, 0.4, 0.6, 0, 0, 0, 0, 0, 0.4, 0.6), nrow = 6, ncol = 6, byrow = TRUE)
mpow <- function(P, n) {
if (n == 0) diag(nrow(P))
else if (n == 1) P
else P %*% mpow(P, n - 1)
}
mpow(P, 18)
回答1:
In your question, the matrix P
is the transition probability. The probability that the current state is i
while the next state is j
is:
P[i, j] = Pr(k = j | k = i)
mpow(P, n)
computes the n-th power of the transition matrix. For example,
> mpow(P, 3)
[,1] [,2] [,3] [,4] [,5] [,6]
[1,] 0.000 0.030 0.105 0.250 0.280 0.335
[2,] 0.001 0.025 0.111 0.254 0.260 0.349
[3,] 0.006 0.032 0.113 0.266 0.224 0.359
[4,] 0.006 0.048 0.144 0.289 0.172 0.341
[5,] 0.000 0.024 0.156 0.400 0.248 0.172
[6,] 0.000 0.000 0.048 0.272 0.432 0.248
> mpow(P, 10)
[,1] [,2] [,3] [,4] [,5] [,6]
[1,] 0.002603379 0.02615891 0.1174816 0.3118660 0.2703684 0.2715217
[2,] 0.002591038 0.02612154 0.1175283 0.3121341 0.2705060 0.2711190
[3,] 0.002565915 0.02600925 0.1174628 0.3124644 0.2710401 0.2704575
[4,] 0.002523007 0.02573033 0.1169686 0.3125272 0.2725643 0.2696866
[5,] 0.002560361 0.02545419 0.1150961 0.3094197 0.2749053 0.2725643
[6,] 0.002708774 0.02649409 0.1171436 0.3096530 0.2690952 0.2749053
> mpow(P,50)
[,1] [,2] [,3] [,4] [,5] [,6]
[1,] 0.002590674 0.02590674 0.1165803 0.3108808 0.2720207 0.2720207
[2,] 0.002590674 0.02590674 0.1165803 0.3108808 0.2720207 0.2720207
[3,] 0.002590674 0.02590674 0.1165803 0.3108808 0.2720207 0.2720207
[4,] 0.002590674 0.02590674 0.1165803 0.3108808 0.2720207 0.2720207
[5,] 0.002590674 0.02590674 0.1165803 0.3108808 0.2720207 0.2720207
[6,] 0.002590674 0.02590674 0.1165803 0.3108808 0.2720207 0.2720207
As you can see, when n
is large, you reach a stationary distribution, where all rows are equal. In other words, regardless the initial state, the probability of ending up with a certain state is the same.
Once such convergence is reached, any row of this matrix is the stationary distribution. For example, you can extract the first row:
> mpow(P,50)[1, ]
[1] 0.002590674 0.025906736 0.116580311 0.310880829 0.272020725 0.272020725
来源:https://stackoverflow.com/questions/38103503/how-can-i-obtain-stationary-distribution-of-a-markov-chain-given-a-transition-pr