Skip to content

Markov chain simulation

3 messages · Philippe Hupé, Christian Schulz, Kjetil Halvorsen

#
Hi,

Does anybody know a function to simulate a Markov chain given a 
probability transition matrix and an initial state ?
Thanks.

Philippe
#
Is the Markov chain Monte Carlo (MCMC) Package
perhaps something for you!?

christian


----- Original Message -----
From: "Philippe Hup?" <Philippe.Hupe at curie.fr>
To: <r-help at stat.math.ethz.ch>
Sent: Wednesday, June 25, 2003 10:01 AM
Subject: [R] Markov chain simulation


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
#
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:
+               0.2, 0.1, 0.7), 3,3, byrow=TRUE)
+        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
+ }
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