Frog and Lily Pad Demo

Define the transition matrix:

tm={{1/2,1/6,1/2},{1/4,1/3,1/2},{1/4,1/2,0}}

MatrixForm[tm]

Simulate one hop:

current=1 (* starting location of the frog *)

tm[[All,current]] (* a column of matrix tm *)

RandomChoice[tm[[All,current]]->Range[3]]

Simulate many hops:

current=1
numSteps=10
Do[
current=RandomChoice[tm[[All,current]]->Range[3]];
Print[current],
numSteps
]

Modify the code to keep track of locations visited.

current=1;
numSteps=10000;
chain={current}; (* a list to hold the locations *)
Do[
current=RandomChoice[tm[[All,current]]->Range[3]];
AppendTo[chain,current],
numSteps
]
chain;

Count[chain,1]

Tally[chain]

Length[chain]

Compute percentage of the time the frog is on each lily pad:

(Count[chain,#]& /@Range[3])/Length[chain]//N

Questions

After  a long time (say, thousands of steps), how frequently is the frog on each lily pad? What are the proportions?

Do these frequencies depend on the starting state?

Modify  to avoid appending:

current=1;
numSteps=1000000;
chain=Table[0,numSteps+1]; (* a list to hold the locations of size numSteps + 1 *)
chain[[1]]=current;
Do[
current=RandomChoice[tm[[All,current]]->Range[3]];
chain[[i]]=current,
{i,2,numSteps+1}
]
(Count[chain,#]& /@Range[3])/Length[chain]//N

Simulate  LOTS  of  frogs, distributed according to the steady-state distribution, each taking one hop

startCounts={38462,34615,26923};
endCounts={0,0,0};
Do[(* loop over lily pads*)
Do[ (* loop over all frogs on lily pad j*)
choice=RandomChoice[tm[[All,j]]->Range[3]];
endCounts[[choice]]+=1
,
startCounts[[j]]]
,
{j,1,3}
]
endCounts

Compute  the eigenvector

Eigensystem[tm]

Get  the  eigenvector corresponding to eigenvalue 1:

v=Eigensystem[tm][[2,1]]

(v/(Total[v]))//N