Random walk

Interior states move up with probability p and down with probability 1 − p. The boundaries reflect by holding: state 1 moves to 2 with probability p and stays otherwise; in a finite chain, the top state N moves to N − 1 with probability 1 − p and stays otherwise. A semi-infinite chain has no upper boundary.

How it works and what to try

How it works: Choose p, a starting state, the number of steps, and either a finite chain on 1 … N or a semi-infinite chain on 1, 2, 3, …. Each press of Run adds a new trajectory in a new colour, so runs can be compared side by side. For the semi-infinite chain, a dashed line shows the expected drift s0 + (2p − 1)t.

Try this: On the semi-infinite chain, compare p = 0.4, 0.5 and 0.6. For p > ½ the walk escapes and tracks the drift line (transient); for p ≤ ½ it keeps returning to state 1 and the line is misleading (recurrent). On a finite chain, watch where the walk spends its time as p moves away from ½.

Semi-infinite runs only
How the dashed line is derived

Each step Xk is +1 with probability p and −1 with probability 1 − p, so E[Xk] = p − (1 − p) = 2p − 1. With no boundary, St = s0 + X1 + … + Xt, so by linearity of expectation E[St] = s0 + (2p − 1) t. That is the dashed line: a straight line from the starting state with slope 2p − 1.

The boundary at 1. From state 1 the step is +1 or 0, with mean p rather than 2p − 1, so each visit to state 1 adds 1 − p: E[St] = s0 + (2p − 1) t + (1 − p) E[visits to 1 before t]. For p > ½ the chain is transient, the expected number of visits is finite, the correction is bounded, and the line is the correct asymptote; indeed St/t → 2p − 1 almost surely. For p ≤ ½ the walk keeps returning to state 1, the correction grows without bound, and the line is misleading.

Spread. Each step has variance 4p(1 − p), so trajectories scatter about the line with standard deviation 2 √(p(1 − p) t): about 31 states at p = 0.6 and t = 1000. The spread grows like √t while the drift grows like t.

Ready. Choose p and an initial state, then press Run.

Each run is computed in full and drawn at once. Each press of Run adds a new trajectory in a new colour; earlier trajectories stay on the graph until you press Reset. Changes to p, chain type, number of states, initial state or step count apply to the next run. For semi-infinite runs, the dashed line in the run's colour shows the expected drift start + (2p − 1)t, ignoring the boundary at state 1.