Let trans_m
be a n
by n
transition matrix of a first-order markov chain. In my problem, n
is large, say 10,000, and the matrix trans_m
is a sparse matrix constructed from Matrix
package. Otherwise, the size of trans_m
would be huge. My goal is to simulate a sequence of markov chain given a vector of initial states s1
and this transition matrix trans_m
. Consider the following concrete example.
n <- 5000 # there are 5,000 states in this case.
trans_m <- Matrix(0, nr = n, nc = n, sparse = TRUE)
K <- 5 # the maximal number of states that could be reached.
for(i in 1:n){
states_reachable <- sample(1:n, size = K) # randomly pick K states that can be reached with equal probability.
trans_m[i, states_reachable] <- 1/K
}
s1 <- sample(1:n, size = 1000, replace = TRUE) # generate 1000 inital states
draw_next <- function(s) {
.s <- sample(1:n, size = 1, prob = trans_m[s, ]) # given the current state s, draw the next state .s
.s
}
sapply(s1, draw_next)
Given the vector of initial states s1
as above, I used sapply(s1, draw_next)
to draw the next state. When n
is larger, sapply
becomes slow. Is there a better way?