Quantitative Finance Stack Exchange is a question and answer site for finance professionals and academics. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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))

enter image description here

share|improve this question
At a minimum, self is used for class methods in python. – John Apr 14 '14 at 22:37
@John: Yes, but this not change anything about the algorithm of the code ? – Alexis G Apr 14 '14 at 22:39
I just meant that if someone on this site tried to run the code themselves to figure out what's wrong with it, they would have problems because they couldn't get self.sig. So perhaps re-write it as a function and provide sufficient code to re-produce the chart (and make it clear what the problem is). Also, people who write python typically don't use int and float the way you're using them. Python's dynamically typed for a reason. – John Apr 14 '14 at 23:00
Modeling is good but must be seen in log plot since EDS works on return. Brownian part of BS is $B_t~\sigma~dW_t$ and not only $\sigma~dW_t$ like for OU process. !enter image description here – Alexis G Apr 14 '14 at 23:56

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.