I have a cumulative transition matrix with probabilities for all the possible states from 1 to 5. Now the algorithm for simulating future states is following: the initial state is selected randomly, and a random value between 0 and 1 is then produced by uniform random number generator.To determine the next state in the Markov process the value of the random number is is compared with the elements of of the *i-*th row of the cumulative transition matrix determined from the preceding state. If the value of the random number is greater than the cumulative probability of the preceding state but less then or equal to the cumulative probability of the succeeding state, the succeeding state is chosen to represent the next state.
cum_trans <- matrix(c(0.1686747,0.4337349,0.6265060,0.7289157,1,
0.2765957,0.5053191,0.6648936,0.7659574,1,
0.2518519,0.4740741,0.6518519,0.7407407,1,
0.1911765,0.4705882,0.6617647,0.7941176,1,
0.2096774,0.4892473,0.6827957,0.7419355,1
), nrow = 5, ncol = 5, byrow = TRUE)
[,1] [,2] [,3] [,4] [,5]
[1,] 0.1686747 0.4337349 0.6265060 0.7289157 1
[2,] 0.2765957 0.5053191 0.6648936 0.7659574 1
[3,] 0.2518519 0.4740741 0.6518519 0.7407407 1
[4,] 0.1911765 0.4705882 0.6617647 0.7941176 1
[5,] 0.2096774 0.4892473 0.6827957 0.7419355 1
state<-matrix(NA, nrow=1,ncol=25)
i<-sample(1:5,1)
state[1]<-i
for (k in 2:25){
rr<-runif(1)
state[k]<-findInterval(rr,cum_trans[i,])+1
i<-state[k]
}
state
So I'm not sure if I implemented the algorithm correctly. Can someone suggest modifications or improvements.
findInterval
support vectors. \$\endgroup\$