| runSparseMarkovChain | R Documentation |
Generate a simulated realisation of a discrete-time finite-state Markov chain, using a sparse matrix representation of the transition probability matrix.
runSparseMarkovChain(P, x0, nsteps, ...,
result = c("history", "last"), check = TRUE,
method = c("C", "interpreted"))
P |
Transition matrix. A sparse matrix supported by the
Matrix package, or a |
x0 |
Integer between |
nsteps |
Integer. Number of steps of the Markov chain. |
... |
Ignored. |
result |
Character string (partially matched) indicating whether to return
the entire trajectory of the chain ( |
check |
Logical value specifying whether to check the validity of |
method |
Character string (partially matched) indicating whether to use the C code implementation or an interpreted R implementation. For testing purposes only. |
If x0 is a single integer, the Markov chain
with transition probability matrix P will be
run for nsteps steps starting from initial state x0.
The result will be a vector of length nsteps+1 giving the
history of the chain after 0, 1, ..., nsteps steps
(if result="history", the default) or a
single integer giving the final state of the chain
(if result="last").
If x0 is a vector of integers, each entry of x0
will be taken as the starting state for an independent copy of the
Markov chain. Each copy will be run for nsteps steps.
The result will be a matrix with n=length(x0) rows
and nsteps+1 columns giving the history of each chain
(if result="history", the default) or a
vector of n integers giving the final state of each chain
(if result="last").
The matrix P should ideally be a sparse matrix
in row major form (class "dgRMatrix") but will be converted
to this form.
Integer vector or matrix.
M <- matrix(c(1, 0, 1, 0, 0,
0, 1, 1, 0, 0,
0, 1, 1, 1, 0,
1, 1, 1, 1, 1,
1, 1, 1, 1, 1),
5, 5)
P <- M/rowSums(M)
runSparseMarkovChain(P, 2, 20)
## random walk on { 1, ..., 1000 }
G <- diag(1000)
G[abs(row(G) - col(G)) == 1] <- 1
H <- G/rowSums(G)
plot(runSparseMarkovChain(H, 500, 1e4), type="l")
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.