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