rfmc: Simulate a finite state space Markov chain

Description Usage Arguments Value See Also Examples

View source: R/rfmc.R

Description

This function simulates a single realisation from a discrete time Markov chain having a finite state space based on a given transition matrix.

Usage

1
rfmc(n,P,pi0)

Arguments

n

The number of states to be sampled from the Markov chain, including the initial state, which will be sampled using pi0.

P

The transition matrix of the Markov chain. This is assumed to be a stochastic matrix, having non-negative elements and rows summing to one, though in fact, the rows will in any case be normalised by the sampling procedure.

pi0

A vector representing the probability distribution of the initial state of the Markov chain. If this vector is of length r, then the transition matrix P is assumed to be r x r. The elements of this vector are assumed to be non-negative and sum to one, though in fact, they will be normalised by the sampling procedure.

Value

An R ts object containing the sampled values from the Markov chain.

See Also

rcfmc, ts

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
# example for sampling a finite Markov chain
P = matrix(c(0.9,0.1,0.2,0.8),ncol=2,byrow=TRUE)
pi0 = c(0.5,0.5)
samplepath = rfmc(200,P,pi0)
plot(samplepath)
summary(samplepath)
table(samplepath)
table(samplepath)/length(samplepath) # empirical distribution
# now compute the exact stationary distribution...
e = eigen(t(P))$vectors[,1]
e/sum(e)

Example output

Loading required package: abind
Loading required package: parallel
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
   1.00    1.00    1.00    1.14    1.00    2.00 
samplepath
  1   2 
172  28 
samplepath
   1    2 
0.86 0.14 
[1] 0.6666667 0.3333333

smfsb documentation built on May 2, 2019, 5:13 a.m.