How to do a geometric Brownian motion in Python

7-Day Challenge

Land Your First Data Science Job

A proven roadmap to prepare for $75K+ entry-level data roles. Perfect for Data Scientist ready to level up their career.

Build portfolios that hiring managers love
Master the Python and SQL essentials to be industry-ready
Practice with real interview questions from tech companies
Access to the $100k/y Data Scientist Cheatsheet

Join thousands of developers who transformed their careers through our challenge. Unsubscribe anytime.

Geometric Brownian Motion (GBM) is a stochastic process that describes the evolution of the price of a financial asset over time. The model assumes that the stock price follows a log-normal distribution and that the change in the stock price is proportional to the current stock price and a normally distributed random variable.

In Python, you can model GBM by using the following formula to update the stock price at each time step:

price[t] = price[t-1] * np.exp((mu - 0.5 * sigma**2) * dt + sigma * np.sqrt(dt) * e)

Where:

  • price[t] is the stock price at time t
  • price[t-1] is the stock price at time t-1
  • mu is the mean return of the stock
  • sigma is the standard deviation of the returns of the stock
  • dt is the time step (T/N, where T is the total time period and N is the number of time steps)
  • e is a random normal variable with mean 0 and standard deviation 1

Here's a simple Python script for GBM simulation:

import numpy as np

# Number of time steps
N = 252
# Total time period
T = 1
# Mean return
mu = 0.05
# Standard deviation of returns
sigma = 0.2
# Initial stock price
S = 100

# Time step
dt = T/N
# Initialize prices array
prices = np.zeros([N])
# First price is the initial price
prices[0] = S
# Loop through the remaining time steps
for i in range(1, N):
    # Generate random normal variable
    e = np.random.normal(0, 1)
    # Update the stock price using the geometric Brownian motion model
    prices[i] = prices[i-1] * np.exp((mu - 0.5 * sigma**2) * dt + sigma * np.sqrt(dt) * e)

This script generates a simulation of the stock price over N time steps. The stock price at each time step is updated using the formula for GBM, with the np.random.normal function generating a random normal variable for the change in the stock price at each time step.

7-Day Challenge

Land Your First Data Science Job

A proven roadmap to prepare for $75K+ entry-level data roles. Perfect for Data Scientist ready to level up their career.

Build portfolios that hiring managers love
Master the Python and SQL essentials to be industry-ready
Practice with real interview questions from tech companies
Access to the $100k/y Data Scientist Cheatsheet

Join thousands of developers who transformed their careers through our challenge. Unsubscribe anytime.

Free Newsletter

Master Data Science in Days, Not Months 🚀

Skip the theoretical rabbit holes. Get practical data science skills delivered in bite-sized lessons – Approach used by real data scientist. Not bookworms. 📚

Weekly simple and practical lessons
Access to ready to use code examples
Skip the math, focus on results
Learn while drinking your coffee

By subscribing, you agree to receive our newsletter. You can unsubscribe at any time.