LessonThis is a lesson restated in new words. Any figure in it is an example from the source, not this desk's record.

The closed form for geometric Brownian motion turns into a short Python loop. Each new price is the previous price times an exponential. Inside the exponential is a drift adjustment times the step, plus volatility times a normal draw whose spread is the square root of the step. The lesson's demo starts at 100, uses 30 percent volatility, a one-year horizon, daily steps on a 252-day year, and 100 paths. A missing clock update makes that loop run forever. Adding the step to the clock fixes it.

Why it matters

The formula gives the price after a finite gap. A chart needs the prices in between. Stepping the same formula across small gaps produces a path, and repeating the path produces a fan. The lesson writes this in ordinary Python first, so each multiply is visible, and then points at a vectorized NumPy version on the geometric Brownian motion page of Wikipedia as the faster equivalent. This page is the slow, readable one. The title on an earlier session said arithmetic Brownian motion. The lesson corrects that: the arithmetic solution is the easy one, and this code is geometric.

The idea in plain words

The function takes a starting price, a drift, a volatility, a horizon, a step length, and a path count. For each path it starts a list at the starting price and a clock at zero. While the clock plus one full step is still inside the horizon, it appends the previous price times the exponential step and then advances the clock by the step. The exponential's contents are drift minus one half volatility squared, times the step, plus volatility times a normal draw of mean zero and standard deviation the square root of the step. NumPy supplies the exponential and the draw.

If the last full step would pass the horizon, the lesson does not pretend the leftover gap has the original length. It computes the horizon minus the clock and uses that shorter length both in the drift piece and inside the square root. A five-day horizon with a one-day step that has somehow landed at 4.5 days should take a half-day step, not another full day. After the leftover step, the finished list is stored and the next path begins. The function returns the collection of lists.

How to use it

Import NumPy, and import Matplotlib when you want the picture. Define the function with the six arguments above. In the demo, the starting price is 100, volatility is 30 percent, the horizon is one year, the step is one divided by 252, and the path count is 100. The drift is described as about 0.8 percent. Call the function, then plot every returned list on one axes. You should see 100 jagged paths leaving 100.

The bug the lesson hits is the clock. The while condition looks at the clock, and the body appends a price. If the body never adds the step to the clock, the condition stays true and the process does not return. The fix is one assignment: add the step to the clock on every full iteration, and set the clock to the horizon after the leftover step so the loop ends.

A 365-day step is a legitimate alternative the lesson mentions. The choice follows the calendar you are trying to mimic. Business days pair with 252. The Wikipedia version the lesson cites builds the same exponential with array operations instead of a Python loop. Use it when the path count grows and the picture is no longer the point.

What usually goes wrong

An infinite run is almost always a frozen clock. The loop condition moves only when the clock moves. Using a full step on a leftover gap gives the last shock too much variance. If the step divides the horizon exactly, that branch never runs.

The normal draw's spread is the square root of the step, not the step. Drift and volatility have to be yearly when the horizon is in years. The pure Python loop is clear and slow. The lesson points to a vectorized form when the path count grows past this demo of 100.

Questions people ask

What does one step multiply by?

The previous price, times an exponential of the variance-adjusted drift times the step, plus volatility times a normal shock of scale the square root of the step.

Which inputs does the demo pass?

Start at 100, drift near 0.8 percent, volatility 30 percent, horizon one year, step 1/252, and 100 paths.

Why did the first run never finish?

The clock was not advanced, so the loop condition never became false. Adding the step to the clock on each iteration lets the path reach the horizon.