9.4.25 Generating a random walks: randmarkov
The randmarkov command generates random walks or creates
stochastic matrices.
To generate a random walk:
-
randmarkov takes two arguments:
-
M, a transition matrix for a Markov chain.
- i0, an initial state.
- n, a positive integer.
- randmarkov(M,i0,n) returns a
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.
Example.
Input:
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)
Output (for example):
⎡
⎣ | 2,3,2,0,3,2,2,0,3,2,0 | ⎤
⎦ |
To create a stochastic matrix:
-
randmarkov takes two arguments:
-
v, a vector of length p.
- i0, the number of transient states.
- randmatrix(v,i0) returns a stochastic matrix with
p recurrent loops (given by v) and i0 transient states.
Example.
Input:
randmarkov([1,2],2)
Output (for example):
⎡
⎢
⎢
⎢
⎢
⎢
⎣ | 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 |
| ⎤
⎥
⎥
⎥
⎥
⎥
⎦ |