Generating Random Paths Using Geometric Brownian Motion With Python

As developers or researchers, we need sometimes to compute timeseries for different purposes. In some cases we have specific conditions on the path the series follow. In financial analyses, we want them to behave as the underlying assets. While parts of their behaviour can be modelled, other parts may rely on unsystomatic motion or randomness. This article is intended to illustrate an approach how to simulate random paths given customizable parametrization using a concept from the area of physics: the (geometric) brownian motion.

The Theory: What is Brownian Motion?

The Brownian Motion is referred to be discovered by the botanist Robert Brown in 1927. It describes the random motion from particles in a fluid resulting from collision with its molecules. So orignally it has nothing to do with data analysis, but some creative and smart people applyied it and it turned out as a really cool instrument for simulation of time series. As you now data scientists love stochastic processes. One of the most important properties is that it just need cross-sectional data to work. That means regarding stock course simulation we just need the data of a single period what makes it very easy to implement.

The equation for the value of the path S in period t is as follows

\(S_t = S_0 exp((\mu -\frac{\sigma^2 }{2})t-\sigma W_t)\)

with µ denoting the mean growth rate, sigma the standard deviation, t the period and \(W_t\) the Wiener random variable or process for our path. We can assume that \(W_t\) follows a standard normal distribution

  1. \(W_t = \mathcal{N}(0,1)\).

Now we can continue with the fun part implementing these equation writing some lines of python code 😉

The application: Generating and plotting random paths in python

Although there is a way to get it done with the scipy package we will implement it manually as it is not that much harder and gives a better understanding of what you are doing. Before we start with coding I will briefly sum up the steps that we will follow (these cookbook recipes helped me a lot with in statistics exams):

  1. Importing needed packages
  2. Parametrization
  3. Defining a helper function to generate brownian motion
  4. Computing and plotting N random paths

So lets start with the packages. Nothing special – just the usual suspects for data analysis. Check out Scipy if you are uncertain.

# Load Packages
import matplotlib.pyplot as plt
import numpy as np
%matplotlib inline # just needed if you want to plot inline in jupyter

Next, we go for parametrisation. Note that we use the variable dt to define our step size and the resulting length of our paths will be T/dt. We also use  np.random.seed() to make sure that our random generator return reproducable results.

# State Variables
T = 10
N = 10
mu = 0.01*dt # Rendite pro Zeiteinheit
sigma = 0.1
S0 = 100
dt = 0.01

Now we can start with the interesting part. We will code the Brownian Motion into a helper function so we can later generate as much paths we need through iterations.

def genBrownPath (T, mu, sigma, S0, dt):
    n = round(T/dt)
    t = np.linspace(0, T, n)
    W = [0] + np.random.standard_normal(size = n) 
    W = np.cumsum(W)*np.sqrt(dt) # == standard brownian motion
    X = (mu-0.5*sigma**2)*t + sigma*W 
    S = S0*np.exp(X) # == geometric brownian motion
    plt.plot(t, S)
    return S

Here we define the number of total steps np.array(T/dt) with that we then generate our time variable in linear space. For the Wiener Process we first generate an standard normal distribution of size n and taking the cumulative sum of it mutliplied with the root of our step size.  Note that we set period zero respectively to zero. Now we can just implement equation (I) to obtain our path and thats it! We already plotting it here to show it later.

paths = []
for i in range(0,N-1):
    paths.append(genBrownPath(T, mu, sigma, S0, dt))

Because it makes often sense to use todays computational power on generating thousends of paths, we make use of an iterator to get N paths and store them in an array. After this we can plot our results (with N sufficiantly small) to get an insight of what we have done. It did that so here is the final result with N = 10 paths:

10 Paths generated through geometric brownian motion in python
10 Paths generated through geometric brownian motion in python


I hope this short tutorial helps you with simulations. Geometric Brownian Motion delivers not just an approach with beautiful and customizable curves – it is also easy to implement and very popular. It can also be included in models as a factor. So, whether you are going for complex data analysis or just to generate some randomness to play around: the brownian motion is a simple and powerful tool.

If you have questions or recommendations feel free to leave a comment – I am always open for exchange and to improve code. So have a wonderful day and fun playing around with your random paths!

Schreibe einen Kommentar

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert.