Simulate systems over timeLesson 37 of 76 · 8 min
Follow a random walk
Simulate complete paths and retain the distinction between steps and path outcomes.
A stochastic process describes how a random state changes over time. A random walk updates its position by adding a random step. One path is a complete sequence of steps, and one path outcome might be its final position or whether it ever crossed a boundary.
The matrix below stores one path per row and one time step per column. A cumulative sum creates the states. Final-position and ever-crossed events are different: a path can cross a threshold and return before the last step.
The simple walk is an authored mechanism with independent equally likely steps. It is useful for learning path bookkeeping. Applying it to real demand, physical movement or prices would require a domain argument about drift, dependence, time scale and boundary behaviour.
In the plot call, paths[:12] selects the first twelve paths. The transpose .T swaps rows and columns, so the plotting function draws each complete path as one line.
Can a path cross +10 and still finish below +10?
Write a prediction (optional)
Seeded teaching simulation
Select Run Python below with the code unchanged.
A saved draft is open. To run the published example, open Tools and select Restore example. Recover draft brings your code back.
Execution error
Technical error details
Your result
Published example output
This result comes from the original Python. Your runs appear above. Build environment ↗
P(final >= 10): 0.1783 P(ever >= 10): 0.33
Read the first result
The ever-crossed event includes paths that later return. Keep all states needed by the target event rather than only the final value.
Try an edit
Replace rng.choice([-1, 1], size=(3000, 100)) with rng.choice([-1, 1], size=(3000, 100), p=[.45, .55]). Select Run Python again. Compare P(final >= 10) and P(ever >= 10) with the first run. How does favouring +1 affect the two path events?
Scope. The walk is a finite illustrative process, not a validated forecast for a real time series.
Try a fresh case
For an inventory model, why can recording only the final stock be insufficient?
Need a hint?
Hint 1
Distinguish final state from events along the path.
Hint 2
Name which timestamps the service requirement covers.