runSparseMarkovChain: Run a Markov Chain with a Sparse Transition Matrix

View source: R/sparseMarkov.R

runSparseMarkovChainR Documentation

Run a Markov Chain with a Sparse Transition Matrix

Description

Generate a simulated realisation of a discrete-time finite-state Markov chain, using a sparse matrix representation of the transition probability matrix.

Usage

  runSparseMarkovChain(P, x0, nsteps, ...,
        result = c("history", "last"), check = TRUE,
        method = c("C", "interpreted"))

Arguments

P

Transition matrix. A sparse matrix supported by the Matrix package, or a matrix in the base R package.

x0

Integer between 1 and nrow(P) specifying the initial state. Alternatively a vector of integers.

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 (result="history", the default) or just the final state of the chain (result="last").

check

Logical value specifying whether to check the validity of P and x0.

method

Character string (partially matched) indicating whether to use the C code implementation or an interpreted R implementation. For testing purposes only.

Details

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.

Value

Integer vector or matrix.

Author(s)

\adrian

Examples

  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")

spatstat.sparse documentation built on May 21, 2026, 9:07 a.m.