Explainerai
Tracing A Random Walk Across The Screen
Building a geometric Brownian path from bare random increments.
Start with a coin. Flip it, and move a point one step right on heads and one step left on tails. Do that a few hundred times and you have a random walk: a path that wanders, with no plan and no memory of where it has been. The point is not where it ends. The point is that the path is a sequence of small, independent pushes, each one as likely to go up as down.
Stock prices are not coin flips, but they behave like a walk in one important way: the percentage change on any tiny stretch of time is roughly independent of the last one. You want a simulated price path that starts at a set value, drifts at a set rate, and jitters with a set amount of noise. The goal is a function that returns the whole path, so you can plot it and ask questions of it. Done means the output is a list of prices, one per time step, and the same starting inputs always produce a path with the right average drift.
Build the path one small step at a time. At each step, the price is multiplied by a growth factor: a steady drift term plus a random shock drawn from a normal distribution. Because you multiply rather than add, the path stays positive and its size scales with its current level, which is what real prices do.
Building the path in log space
import numpy as np
def gbm_path(s0, mu, sigma, t_end, steps, seed=None):
rng = np.random.default_rng(seed)
dt = t_end / steps
# One normal shock per step, scaled by sqrt(dt)
shocks = rng.standard_normal(steps)
log_increments = (mu - 0.5 * sigma**2) * dt + sigma * np.sqrt(dt) * shocks
# Accumulate in log space, then exponentiate
log_path = np.log(s0) + np.concatenate(([0.0], np.cumsum(log_increments)))
return np.exp(log_path)
path = gbm_path(s0=100.0, mu=0.08, sigma=0.20, t_end=1.0, steps=252, seed=7)
print(path[:5])Look at the drift term inside the log: it is mu minus one half of sigma squared, not just mu. This is the trap almost everyone falls into. If you put the raw drift mu into the exponent, your simulated paths drift upward too fast, and the average price comes out higher than the drift you asked for. The correction comes from the convexity of the exponential: the shock adds spread, and spread pulls the mean of the exponential up. Subtracting half the variance cancels that pull, so the expected price grows at exactly mu.
Description
The update rule the code implements: one step of geometric Brownian motion, with Z a standard normal shock.
Run the function many times with different seeds and average the final prices. For the inputs above, the average should sit close to 100 times e to the power of 0.08, about 108.3, and the spread should match the volatility. If your average lands noticeably above that, the minus half sigma squared term is missing or has the wrong sign.
This model assumes the volatility and drift are constant and that each shock is independent of the past. Real markets break both: volatility clusters, and crashes arrive in bunches. The same code still runs, but the paths it produces will understate the chance of large moves. For that you need models where sigma itself moves, which is the next step beyond this page.
Choose
