Generate 10000 transitions of the Markov chain. What percent of times do you find your generated Markov chain in each of its 3 states?
#We generate/display the matrix
p = matrix(data = c(0.64,0.4,0.25,0.32,0.5,0.5,0.04,0.1,0.25), nrow = 3)
p
## [,1] [,2] [,3]
## [1,] 0.64 0.32 0.04
## [2,] 0.40 0.50 0.10
## [3,] 0.25 0.50 0.25
# We simulate the data
set.seed(6589)
p = matrix(data = c(0.64,0.4,0.25,0.32,0.5,0.5,0.04,0.1,0.25), nrow = 3)
all.states = c(0, 1, 2)
n = 10000
state = 0
transitions = c()
for (i in 1:n) {
if(state == 0){
state = sample(all.states, 1, prob = c(0.64,0.32,0.04))
}
if(state == 1){
state = sample(all.states, 1, prob = c(0.4,0.5,0.1))
}
if(state == 2){
state = sample(all.states, 1, prob = c(0.25,0.5,0.25))
}
transitions = c(transitions, state)
}
head(transitions)
## [1] 0 0 1 1 0 0
# We make/create an histogram
hist(transitions, prob=TRUE, main="State Frequencies", xlab = "States", ylab = "Frequency")
