Diagnosing Time-Step and Indexing Errors in Black–Scholes Paths
Summary
The document shows Python code intended to generate sample paths under a Black–Scholes diffusion, then reports seeing jumps and an off-center path. The path formula combines cumulative Gaussian increments, scaled by the square root of the time step, with the model’s drift term. As written, however, the code has implementation problems that can distort or prevent the intended simulation: the time grid includes both endpoints while the time step is calculated as the horizon divided by the number of points, and the first random increment is forcibly set to zero. The plotting loop is also indented incorrectly, and the displayed code uses functions without showing their imports.
These issues make the snippet unsuitable as a reliable demonstration without correction. In a properly indexed discretization, the grid spacing and increment scaling must agree, and each Brownian increment should be generated consistently with that spacing. The document provides code but no corrected version, path diagnostics, or statistical checks against the theoretical distribution.
Key ideas
- The code intends to simulate geometric Brownian motion using cumulative Gaussian increments.
- The time grid spacing does not match the time step used to scale the increments.
- Setting the first increment to zero changes the generated path unnecessarily.
- The plotting loop’s indentation and missing function imports are additional code issues.
- The snippet provides no corrected implementation or verification of the simulated distribution.
Tags
Full text
# Black Scholes diffusion well coded in Python
# Black Scholes diffusion well coded in Python
I have some trouble with the following code. Some jump and a decentered path are present but it's not the case, normally for Black Scholes diffusion!
Is anyone see a problem in my code ?
```
import numpy as np
import matplotlib.pyplot as plt
mu = 0
sig = 0.5
def generate(S0, T, nt, sig, mu):
nt = int(nt)
T = float(T)
dt = T / nt
St = [S0] * nt
t = linspace(0,T,nt)
dWt = np.random.normal(0, 1, nt)
dWt[0] = 0.
Wt = dWt.cumsum()
return S0 * np.exp(sig * sqrt(float(dt)) * Wt + (mu - sig**2/2.) * t)
for i in range(100):
plt.plot(generate(180., 2., 252*2, sig, mu))
```Shown in full with attribution under the source's licence. Licence: CC BY-SA 4.0 (Stack Exchange)
This summary was written by Stratmill's research agent from the original; it is not a copy of the source.