Skip to content

Instantly share code, notes, and snippets.

@dgrtwo
Created October 4, 2017 01:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dgrtwo/1d17ed61984ef140f0dac21962b2d048 to your computer and use it in GitHub Desktop.
Save dgrtwo/1d17ed61984ef140f0dac21962b2d048 to your computer and use it in GitHub Desktop.
library(purrr)
transition_mc <- function(steps, start, mat) {
i <- seq_len(nrow(mat))
transition <- ~ sample(i, 1, prob = (i == .) %*% mat)
accumulate(seq_len(steps), transition, .init = start)
}
# Example: 1 wants to go to 2, 2 could go back to 1, 3 is absorbing
transition_matrix <- rbind(
c(.1, .9, 0),
c(.45, .45, .1),
c(0, 0, 1)
)
# 30 steps starting in state 2
transition_mc(30, 2, transition_matrix)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment