The \(ij\)th entry \(p^{(n)}_{ij}\) of the matrix \(P^n\) gives the probabilty that the Markov chain, starting in state \(s_i\) will be in state \(s_j\) after \(n\) steps.
library(expm)
labels <- c("H","Y","D")
T_mat <- matrix(c(0.8,0.2,0.0, # transition matrix
0.3,0.4,0.3,
0.2,0.1,0.7),
nrow=3,
byrow=TRUE,
dimnames=list(labels,labels))
T_mat
## H Y D
## H 0.8 0.2 0.0
## Y 0.3 0.4 0.3
## D 0.2 0.1 0.7
T_mat %*% T_mat
## H Y D
## H 0.70 0.24 0.06
## Y 0.42 0.25 0.33
## D 0.33 0.15 0.52
T_mat %^% 2
## H Y D
## H 0.70 0.24 0.06
## Y 0.42 0.25 0.33
## D 0.33 0.15 0.52
Grandson = 2nd step. The probability is 0.7.