To simulate geometric Brownian motion (GBM) in Python, draw a normal log return for each time interval, add the log returns along each path, then exponentiate to recover prices. With a constant drift and volatility, this gives exact draws at the dates you choose. If your payoff only depends on the final price, one draw per path is enough.
Here is a useful test: a stock starts at 100, with 5% annual drift and 20% annual volatility. After one year, should the middle simulated price be 105.13? No. That is the mean. The median is 103.05. Understanding why those numbers differ is a better check on your simulator than whether its paths look realistic.
The exact GBM step: what goes inside the exponential?
Write the stock dynamics as
. Here
is the drift of the proportional price change,
is the annual volatility, and time is measured in years. Over an interval
, the log return is normal:
![]()
where
.
The
comes from applying Itô’s lemma to the logarithm. There is also a quick way to check the sign: exponentiating the random part contributes a factor of
to the mean. The subtraction exactly cancels it. Without it, a simulation with these inputs produces a theoretical one-year mean of 107.25 instead of 105.13.
Python example: simulate several stock price paths
import numpy as np
def gbm_paths(s0, drift, vol, years, steps, paths, seed=7):
rng = np.random.default_rng(seed)
dt = years / steps
z = rng.standard_normal((paths, steps))
log_returns = (drift - 0.5 * vol**2) * dt + vol * np.sqrt(dt) * z
log_relative = np.cumsum(log_returns, axis=1)
levels = s0 * np.exp(log_relative)
return np.column_stack((np.full(paths, s0), levels))
# Six one-year paths, observed at the start and at 252 later dates
paths = gbm_paths(100, 0.05, 0.20, 1, 252, 6)
print(paths.shape) # (6, 253)
Each row is a path. Column zero is today’s price; column 252 is the one-year price. The cumulative sum must run across dates within each row. Summing down the rows instead quietly stitches together different scenarios. A quick deterministic check: set volatility to zero. Every row should then end at
, whatever seed you choose.
When one random draw is enough
A European call pays
. The payoff never looks at the intermediate prices, so generating 252 shocks per path spends memory without adding information about that payoff. Use one step of length
. For an Asian option sampled monthly, by contrast, keep the twelve observation dates: two paths with the same final price can have very different averages.
import numpy as np
s0, drift, vol, years = 100.0, 0.05, 0.20, 1.0
rng = np.random.default_rng(7)
z = rng.standard_normal(100_000)
terminal = s0 * np.exp((drift - 0.5 * vol**2) * years
+ vol * np.sqrt(years) * z)
se = terminal.std(ddof=1) / np.sqrt(terminal.size)
print(f"Mean: {terminal.mean():.2f}; median: {np.median(terminal):.2f}")
print(f"Theoretical mean: {s0 * np.exp(drift * years):.2f}; SE: {se:.3f}")
With the seed shown, the sample mean is 105.09 and the median is 103.00. The mean differs from its theoretical value by about 0.04, smaller than the estimated standard error of 0.067. Do not tune the seed until they match. The distribution has a long right tail: large outcomes pull up the average without moving the middle observation nearly as far.
Which drift should you use: expected return or risk-free rate?
That depends on what you want the number to mean. To explore investment scenarios, 5% might be your assumption about the stock’s expected return. A million paths can estimate the consequences of that assumption very precisely; they cannot tell you whether 5% was a good forecast.
For risk-neutral option pricing with constant risk-free rate
and continuous dividend yield
, pass drift=r-q and discount the expected option payoff at
. For a dividend-free stock with
, the discounted sample mean from the example is 99.97, close to the starting price of 100. This is a check on your pricing model. It does not imply the stock will earn 5% in the market.
To turn the terminal-price simulation into a pricer, follow Monte Carlo option pricing in Python. If the option price disagrees with Black–Scholes, use the pricing discrepancy checklist to distinguish a coding error from sampling noise.
Does a smaller time step make GBM more accurate?
For constant-parameter GBM, the exponential update already gives the exact distribution at each simulated date. Increasing the number of dates will not make a European terminal payoff more accurate in distribution. Increasing the number of independent paths reduces its sampling error. The distinction matters for a continuously monitored barrier: exact prices at daily dates can still miss a crossing between dates.
GBM is useful precisely because you know what the outputs should be. It assumes constant volatility and continuous paths; it cannot generate price jumps or volatility clustering. Before moving to a harder model, make this one pass three checks: the zero-volatility limit, the theoretical terminal mean, and the discounted-stock check under the pricing measure.
From simulation to derivation: if you can run the code but want to derive the drift correction, the Stochastic Calculus for Finance with Python course takes you from Brownian motion and Itô’s lemma to stochastic differential equations and their numerical checks. The syllabus shows where each piece of this example fits.
Get the Quant Reference Card — four pages covering stochastic calculus, measure change, the models you’ll actually meet, discretisation, rates, credit and XVA, with the practitioner notes that don’t make it into textbooks. Free.