Generating a random walks: randmarkov

### 7.4.25 Generating a random walks: randmarkov

Given the transition matrix M for a Markov chain and an initial state i0, the command randmarkov(M,i0,n) will generate a random walk (given as a vector) starting at i0 and taking n random steps, where each step is a transition with probabilities given by M. For example, if you enter

randmarkov([[0,1/2,0,1/2],[0,1,0,0],[1/4,1/4,1/4,1/4],[0,0,1/2,1/2]],2,10)

you might get

[2,3,2,0,3,2,2,0,3,2,0]

Alternatively, given a vector v = [n1,…,np], the command randmatrix(v,i0) will create a stochastic matrix with p recurrent loops (given by v) and i0 transient states. If you enter

randmarkov([1,2],2)

you might get

[[1.0,0.0,0.0,0.0,0.0], [0.0,0.289031975209,0.710968024791,0.0,0.0], [0.0,0.46230383289,0.53769616711,0.0,0.0], [0.259262238137,0.149948861946,0.143448150524,0.242132758802,0.205207990592], [0.231568633749,0.145429586345,0.155664673778,0.282556511895,0.184780594232]]