Markov chain simulation
On 25 Jun 2003 at 10:01, Philippe Hup? wrote:
For small transistion matrices (which seems to be what you ask for), something like this:
P <- matrix( c(0.2, 0.5, 0.5, 0.1, 0.8, 0.1,
+ 0.2, 0.1, 0.7), 3,3, byrow=TRUE)
simMarkov <- function( P, len=1000) {
+ n <- NROW(P)
+ result <- numeric(len)
+ result[1] <- 1
+ for (i in 2:len) {
+ result[i] <- sample(1:n, 1, prob=P[ result[i-1], ])
+ }
+ result
+ }
muestra <- simMarkov(P)
Note that this assumes the initial state to be 1, easy to modify wuth an extra argument to simMarkov. Whe seeing a for-loop solution as above, it is natural to ask for a vectorized solution. But whith the FUNction within the loop using earlier results, that seems difficult. Kjetil Halvorsen
Hi, Does anybody know a function to simulate a Markov chain given a probability transition matrix and an initial state ? Thanks. Philippe -- -------------------------------------------------- Philippe Hup? Institut Curie - Equipe Bioinformatique 26, rue d'Ulm - 75005 PARIS France +33 (0)1 42 34 65 29 Philippe.Hupe at curie.fr <mailto:Philippe.Hupe at curie.fr>
______________________________________________ R-help at stat.math.ethz.ch mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help