Model answers 3 state Markov Chain
Set up a 3x3 transition matrix:
= matrix(c(0.7, 0.2, 0.1,
transitionMatrix 0.3, 0.3, 0.4,
0.6, 0.2, 0.2), nrow=3, ncol=3, byrow=TRUE)
# Check matrix set-up correctly
print(transitionMatrix)
## [,1] [,2] [,3]
## [1,] 0.7 0.2 0.1
## [2,] 0.3 0.3 0.4
## [3,] 0.6 0.2 0.2
Note the ordering of the states is arbitrary but here we have used the convention that State 1 is Sunny, State 2 is Rainy and State 3 is Cloudy which means the probabilities are completed in that order in the transition matrix. We just need to be consistent.
<- 1 # initial state - it is [1] sunny, [2] rainy and [3] cloudy
state <- rep(0, 30) # vector to store simulated values
weather_sequence
# simulate for 30 days
for (day in 1:30) {
<- transitionMatrix[state, ] # select the row of transition probabilities
pr
# sample [1-3] based on the probs pr
<- sample(c(1, 2, 3), size = 1, prob = pr)
state <- state # store the sampled state
weather_sequence[day]
}print(weather_sequence)
## [1] 1 1 2 1 1 1 3 2 1 1 1 2 3 2 2 3 2 1 3 2 2 2 3 1 1 2 3 1 1 2