DATA 605 Week10 Discussion BKvarnstrom
Grinstead and Snell’s Introduction to Probability - Exercise 15, Page 415)
Write a program to simulate the outcomes of a Markov chain after n steps, given the initial starting state and the transition matrix P as data (see Example 11.12). Keep this program for use in later problems.
#simulates the outcomes of a Markov chain after n steps, given the initial starting state and the transition matrix P
markov_chain_sim <- function(start_state, P, n) {
current_state <- start_state
for (i in 1:n) {
next_state_probabilities <- current_state %*% P
next_state <- sample(1:length(next_state_probabilities), size = 1, prob = next_state_probabilities)
current_state <- matrix(0, nrow = 1, ncol = length(current_state))
current_state[1, next_state] <- 1
}
return(current_state)
}# Define the transition probability matrix
P <- matrix(c(0.45, 0.25, 0.1, 0.2, 0.6, 0.4, 0.55, 0.15, 0.5), nrow = 3, byrow = TRUE)
rownames(P) <- c("Professional", "Skilled Laborer", "Unskilled Laborer")
colnames(P) <- c("Professional", "Skilled Laborer", "Unskilled Laborer")
# Define the initial probability distribution
start_state <- c(1, 0, 0) # 100% chance of being a professional man
# Simulate the outcomes after 10 steps
markov_chain_sim(start_state, P, n = 10)## [,1] [,2] [,3]
## [1,] 1 0 0