R fills matrices columnwise by default. Specifying rowwise filling gives the requested order:
A <- matrix(c(1/3, 2/3, 0,
              1/4, 1/2, 1/4,
              0,   1/4, 3/4), nrow = 3, byrow = TRUE)
The result is a transition matrix, with nonnegative entries and every row summing to one.
Use inverse transform sampling for the categorical draw. Validate a finite nonnegative probability vector, allowing only a small rounding tolerance in its total, then compare a single uniform draw with the cumulative probabilities:
sim1 <- function(p) {
  if (!is.numeric(p) || length(p) == 0L ||
      any(!is.finite(p)) || any(p < 0) ||
      abs(sum(p) - 1) > 1e-12) {
    stop("p must be a nonempty finite probability vector")
  }
  p <- p / sum(p)
  cumulative <- cumsum(p)
  cumulative[length(p)] <- 1
  u <- runif(1)
  as.integer(which(u < cumulative)[1L])
}
The returned value is when falls in the interval between the previous cumulative sum and the th one. That interval has length , so the output has the required probabilities. Strict comparison skips zero-probability categories even if a simulated uniform value equals an interval boundary. Exactly one call to runif(1) is made.
The function below counts the initial uniformly chosen state as the first of the visited states, and then generates transitions using the appropriate row of :
simchain <- function(n, P) {
  if (!is.numeric(n) || length(n) != 1L || !is.finite(n) ||
      n < 1 || n != floor(n)) stop("n must be a positive integer")
  if (!is.matrix(P) || !is.numeric(P) || nrow(P) == 0L ||
      nrow(P) != ncol(P) || any(!is.finite(P)) ||
      any(P < 0) || any(abs(rowSums(P) - 1) > 1e-12)) {
    stop("P must be a finite square stochastic matrix")
  }
  k <- nrow(P)
  state <- sim1(rep(1/k, k))
  counts <- numeric(k)
  counts[state] <- 1
  if (n > 1) {
    for (step in seq_len(n - 1)) {
      state <- sim1(P[state, ])
      counts[state] <- counts[state] + 1
    }
  }
  counts / n
}
Conditional on the current state , sim1(P[i, ]) selects with probability , proving the Markov property with the desired transition matrix. Initialization is uniform by the same sampler. Dividing the visit counts by gives the requested Empirical occupation proportions of a finite Markov chain, including a valid result when .

Articles by others on the same topic (0)

There are currently no matching articles.