library("markovchain") library("igraph") # Part A stateNames = c("1","2","3","4","5","6","7","8","9") MC = new("markovchain",transitionMatrix=matrix(c(0,1/3,0,1/3,1/3,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,1/2,1/2,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,1,0,0,0,0,0,1/3,0,0,0,0,1/3,1/3,1/2,0,0,0,1/2,0,0,0,0,0,0,0,0,0,1/2,0,0,1/2,0,0,0,0,0,0,0,0,1),byrow=TRUE,nrow=9,dimnames=list(stateNames,stateNames))) plot(MC) # Part B summary(MC) #Unnamed Markov chain Markov chain that is composed by: #Closed classes: #9 #Transient classes: #1 4 7 #2 #3 5 6 8 #The Markov chain is not irreducible #The absorbing states are: 9 #Part C canonicForm(MC) #Q # 1 2 3 4 5 6 7 8 #1 0.0 0.3333333 0.0000000 0.3333333 0.3333333 0.0 0 0.0000000 #2 0.0 0.0000000 1.0000000 0.0000000 0.0000000 0.0 0 0.0000000 #3 0.0 0.0000000 0.0000000 0.0000000 0.5000000 0.5 0 0.0000000 #4 0.0 0.0000000 0.0000000 0.0000000 0.0000000 0.0 1 0.0000000 #5 0.0 0.0000000 0.0000000 0.0000000 0.0000000 1.0 0 0.0000000 #6 0.0 0.0000000 0.3333333 0.0000000 0.0000000 0.0 0 0.3333333 #7 0.5 0.0000000 0.0000000 0.0000000 0.5000000 0.0 0 0.0000000 #8 0.0 0.0000000 0.0000000 0.0000000 0.0000000 0.5 0 0.0000000 #Part D steadyStates(MC) # 1 2 3 4 5 6 7 8 9 #[1,] 0 0 0 0 0 0 0 0 1 #these are normalized (left) eigenvectors to eigenvalue 1. Since the MC from Problem 7 has only one recurrent class, the stationary distribution #should be only zeroes and a 1 for the one recurrent state (state 9). #Part E simulation = rmarkovchain(n=100,object=MC, t0="1") simulation # [1] "5" "6" "9" "9" "9" "9" "9" "9" "9" "9" "9" "9" "9" "9" "9" "9" "9" "9" # [19] "9" "9" "9" "9" "9" "9" "9" "9" "9" "9" "9" "9" "9" "9" "9" "9" "9" "9" # [37] "9" "9" "9" "9" "9" "9" "9" "9" "9" "9" "9" "9" "9" "9" "9" "9" "9" "9" # [55] "9" "9" "9" "9" "9" "9" "9" "9" "9" "9" "9" "9" "9" "9" "9" "9" "9" "9" # [73] "9" "9" "9" "9" "9" "9" "9" "9" "9" "9" "9" "9" "9" "9" "9" "9" "9" "9" # [91] "9" "9" "9" "9" "9" "9" "9" "9" "9" "9" # Trajectory ends up, as expected, in the recurrent class {9}