I wrote code to simulate the stock price using geometric brownian motion.

My code is as follows:

    for i in range( N):
        S1=S*np.exp(((mu - sigma**2/2)*dt) + (sigma * np.sqrt(dt) * np.random.normal(0,1)))

However one thing that I notice is that the price always seems to follow an upward trend. If my starting point is 120, it never goes below 100 for some reason. Sure, there could be dips in the graph, but the trend is always upward.

Is that how geometric brownian motion is supposed to work or have I gotten something wrong?

  • 2
    $\begingroup$ This is going to be very dependent on your values of mu and sigma. Could you share the full code so that we can reproduce it? $\endgroup$
    – R. Rayl
    May 13 at 10:31
  • $\begingroup$ @R.Rayl you're right, that's what I figured too. The mean is 61% and the SD is 0.39! $\endgroup$
    – atastix
    May 13 at 16:27

To answer your question, few steps are necessary:

  • I assume your are working under the BS model (from your code). Therefore we have that $$dS_t = \mu S_t dt + \sigma S_t dW_t$$ where $W_t$ is a SBM.
  • Using Ito's lemma we can recover: $$\log(S_t) - \log(S_0) \sim N \left((\mu - \frac{1}{2}\sigma^2)t, \ \ \sigma^2t \right)$$
  • Now, observe how $\exp(\log(S_t) - \log(S_0)) = \frac{S_t}{S_0}$. What does this tells you? It tells you that $$\frac{S_t}{S_0} \sim Log N \left((\mu - \frac{1}{2}\sigma^2)t, \ \ \sigma^2t \right)$$
  • We plug in the parameters you have indicated $\mu = 0.61$ and $\sigma = 0.39$. What we obtain is then $\frac{S_t}{S_0} \sim Log N \left(0.53395t, \ \ 0.1521t\right)$. Choosing a value for $t$, you can plot the density using this tool. At the same link you can also ask for the CDF.

As an illustrative example, fix $t=1$. You are asking: why is my simulation never producing $S_t \leq 100$? Let's see what is $\mathbb{P}\left[S_t \leq 100\right]$. $$\left[S_t \leq 100\right] = \left[\frac{S_t}{S_0} \leq \frac{100}{S_0}\right] = \left[\frac{S_t}{S_0} \leq \frac{100}{120}\right] = \left[L \leq 0.8333\right]$$ where $L \sim Log N \left(0.53395, \ \ 0.1521\right)$. So that $\mathbb{P}\left[S_t \leq 100\right] = 0.03313$. This means that, choosing to simulate $n = 1000$ different values, around 33 of these should be less than 100. This is indeed the case. You can try with this code:

import numpy as np
ns = 1000
sum = 0
for i in range(100):
    S = np.zeros(ns)
    randomE = np.random.normal(0.0, 1.0, ns)
    S = 120*np.exp((0.61 - 0.5*0.39*0.39) + 0.39*randomE)
    boole3 = np.where(S < 100)
    sum += len(boole3[0])

Hope this clarifies!


Not the answer you're looking for? Browse other questions tagged or ask your own question.