Write a program to compute u(n) given u and P. Use this program to compute u(10) for the Land of Oz example, with u = (0,1,0), and with u = (1/3,1/3,1/3).
P <- matrix(c(.50, .25, .25, .50, 0, .50, .25, .25, .50), ncol=3,nrow=3, byrow = TRUE) # Transition MAtrix P from Land of Oz
P
## [,1] [,2] [,3]
## [1,] 0.50 0.25 0.25
## [2,] 0.50 0.00 0.50
## [3,] 0.25 0.25 0.50
u0 <- matrix(c(0,1,0), ncol=3,nrow = 1,byrow = TRUE)
u1 <- matrix(c(0.33,0.33,0.33), ncol=3,nrow = 1,byrow = TRUE)
#install.packages('expm')
library(expm)
## Loading required package: Matrix
##
## Attaching package: 'expm'
## The following object is masked from 'package:Matrix':
##
## expm
compute.u_n <- function(n,u,P){
Pn <- P %^% n #compute p^n
u_n <- u %*% Pn #compute uP^n
return(u_n)
}
compute.u_n(10,u0,P)
## [,1] [,2] [,3]
## [1,] 0.3999996 0.2000008 0.3999996
compute.u_n(10,u1,P)
## [,1] [,2] [,3]
## [1,] 0.3959999 0.1980001 0.3959999