randomMarkovChain: Random Markov chain

View source: R/random.R

randomMarkovChainR Documentation

Random Markov chain

Description

Generates a Markov chain whose transition probabilities are drawn at random, optionally with a given number of zero probabilities and with some probabilities fixed in advance.

Usage

randomMarkovChain(
  n,
  states = NULL,
  zeros = 0L,
  mask = NULL,
  byrow = TRUE,
  seed = NULL,
  name = "Random Markov chain"
)

Arguments

n

The number of states. It can be omitted when states is given.

states

An optional character vector of n state names. Defaults to as.character(1:n).

zeros

The number of transition probabilities, among those not fixed by mask, that are set to zero. Every row whose probabilities are not all fixed keeps at least one positive free entry, which bounds zeros from above; a larger value is an error.

mask

An optional n x n matrix of fixed transition probabilities: NA marks the entries to draw at random, any other value (in [0, 1]) is kept as it is. In each row the fixed values must not sum to more than one. A row whose fixed values sum to one gets zero in its NA entries; in any other row the free entries share the remaining probability. With byrow = FALSE the mask is read by columns, like the transition matrix.

byrow

Whether the transition matrix of the result is stored by rows (the default) or by columns.

seed

An optional whole number. When given, the chain is generated after set.seed(seed) and the caller's random number stream is restored afterwards, so the result is reproducible without affecting later random draws; when NULL (the default), the current stream is used, so set.seed() beforehand works as usual.

name

The name slot of the result.

Details

The algorithm follows MarkovChain.random() of PyDTMC. In each row not completely fixed by mask, one free entry, chosen at random, is reserved to be positive. The zeros zero entries are then chosen at random among the remaining free entries of the whole matrix, the other free entries are drawn from a uniform distribution on (0, 1), and the free entries of each row are rescaled so that, together with the fixed ones, they sum to one. The rows are normalised uniforms, which is not the uniform distribution on the simplex; use dirichletChain or draw the rows yourself if that matters.

Value

A markovchain object with n states.

See Also

dirichletChain, identityChain

Examples

randomMarkovChain(4, seed = 1)
# 6 of the 16 transition probabilities are zero
sum(randomMarkovChain(4, zeros = 6, seed = 1)@transitionMatrix == 0)
# state "b" moves to "a" with probability 0.5; the rest is random
m <- matrix(NA, 3, 3)
m[2, 1] <- 0.5
randomMarkovChain(states = c("a", "b", "c"), mask = m, seed = 1)


markovchain documentation built on Oct. 10, 2026, 9:07 a.m.