Geometric Brownian Motion in Python; Predict the Bitcoin Prices

Roi Polanitzer
12 min readNov 27, 2021

“This article deals with simulation of random variables for the purpose of predicting the bitcoin prices. In order to understand this article the reader must have an in-depth understanding of probability and statistics, that is, how to generate random variables from known distributions and estimate distribution parameters from actual data.”

Such simulations, called Monte Carlo simulations, are central to data science. They allow data scientists to price assets. That is, they allow data scientists to build the distribution of assets that are too complex to model analytically.

Simulation methods are quite flexible and are becoming easier to implement with technological advances in computing. Their drawbacks should not be underestimated, however. For all their elegance, simulation results depend heavily on the model’s assumptions: the shape of the distribution, the parameters, and the pricing functions. Data scientists need to be keenly aware of the effect that errors in these assumptions can have on the results.

Simulations involve creating artificial random variables with properties similar to those of the risk factors which drive the asset price. These include cryptocurrency prices, stock prices, exchange rates, bond yields or prices, and commodity prices.

Simulating Markov Processes

In efficient markets, financial prices should display a random walk pattern. More precisely, prices are assumed to follow a Markov process, which is a particular stochastic process independent of its history — the entire distribution of the future price relies on the current price only. The past is irrelevant. These processes are built from the following components, described in order of increasing complexity.

The Wiener process

This describes a variable Δz, whose change is measured over the interval Δt such that its mean change is zero and variance is proportional to Δt:

If ε is a standard normal variable N(0, 1), this can be written as:

In addition, the increments Δz are independent across time.

The generalized Wiener process

This describes a variable Δx built up from a Wiener process, with in addition a constant trend a per unit time and volatility b:

A particular case is the martingale, which is a zero-drift stochastic process, a= 0, which leads to E(Δx) = 0. This has the convenient property that the expectation of a future value is the current value

The Ito process

This describes a generalized Wiener process whose trend and volatility depend on the current value of the underlying variable and time:

This is a Markov process because the distribution depends only on the current value of the random variable x, as well as time. In addition, the innovation in this process has a normal distribution.

The Geometric Brownian Motion

A particular example of Ito process is the geometric Brownian motion (GBM), which is described for the variable S as

The process is geometric because the trend and volatility terms are proportional to the current value of ΔS. This is typically the case for bitcoin prices, for which rates of returns appear to be more stationary than raw dollar returns, S.

It is also used for currencies. Because ΔS/S represents the capital appreciation only, abstracting from dividend payments, μ represents the expected total rate of return on the asset minus the rate of income payment, or dividend yield in the case of stocks.

Example №1 for A Bitcoin Price Process

Let’s assume the bitcoin has an expected return of 150% per annum, and volatility of 70% per annum. If the current price is $60,000, what is the process for the change in the bitcoin price over the next week? What if the current price is $20,000?

The process for the bitcoin price is

where ε is a random draw from a standard normal distribution. If the interval is one week, or Δt = 1/52 = 0.01923, the mean is

μΔt = 1.50 × 0.01923 =0.028846

and

σ√Δt= 0.70 × √0.01923 = 0.097073

The process is

ΔS = $60,000 × (0.028846 + 0.097073 × ε)

With an initial bitcoin price at $60,000, this gives

ΔS = 1,730.7692 + 5,824.3521 × ε

With an initial bitcoin price at $20,000 this gives

ΔS = 576.92 + 1,941.45 × ε

The trend and volatility are scaled down by a factor of three.

Normal distribution vs. Lognormal distribution

This model is particularly important because it is the underlying process for the Black-Scholes formula. The key feature of this distribution is the fact that the volatility is proportional to S.

This ensures that the bitcoin price will stay positive. Indeed, as the bitcoin price falls, its variance decreases, which makes it unlikely to experience a large down move that would push the price into negative values.

As the limit of this model is a normal distribution for dS/S = d ln(S), S follows a lognormal distribution.

This process implies that, over an interval T − t = τ, the logarithm of the ending price is distributed as

where ε is a standardized normal variable.

Whether a lognormal distribution is much better than the normal distribution depends on the horizon considered. If the horizon is one day only, the choice of the lognormal versus normal assumption does not really matter.

It is highly unlikely that the bitcoin price would drop below zero in one day, given typical volatilities. On the other hand, if the horizon is measured in years, the two assumptions do lead to different results.

The lognormal distribution is more realistic, as it prevents prices from turning negative.

Example №2 for A Bitcoin Price Process

Assume the bitcoin price in one week is given by S = $60,000 × exp(R), where R has annual expected value of 150% and volatility of 70%. Construct a 95% confidence interval for S.

The standard normal deviates that correspond to a 95% confidence interval are αMIN = −1.96 and αMAX = 1.96. In other words, we have 2.5% in each tail.

The 95% confidence band for R is then

RMIN = 0.028846 - 1.96 × 0.097073 = -0.161416

RMIN = 0.028846–1.96 × 0.097073 = 0.219108

Results:

SMIN = $60,000 × exp(-0.161416) = $51,056.28

SMAX = $60,000 × exp(0.219108) = $74,697.97

Simulating Bitcoin Price Paths

In simulations, the Geometric Brownian motion process is approximated by small steps with a normal distribution with mean and variance given by

To simulate the future price path for S, we start from the current price St and generate a sequence of independent standard normal variables ε, for i = 1, 2, . . . , n.

The next price St+1 is built as

The following price St+2 is taken as

and so on until we reach the target horizon, at which point the price

should have a distribution close to the lognormal.

Let’s illustrates simulation of a process with a drift (μ) of 0 percent and volatility (σ) of 70 percent over the total interval, which is divided into 100 steps.

The initial price is $60,000. The local expected return is μΔt = 0.0/100 = 0.0 and the volatility is 0.70 × √(1/100) = 0.07

First, let’s import the relevant libraries

import numpy as np
import pandas as pd
from scipy.stats import norm
from numpy.random import randn
from numpy import random as rn
import scipy.stats as si
from matplotlib import pyplot as plt
from IPython.display import Image
%matplotlib inline

Second, let’s create two functions. The first function, which I call it RAND(), generates pseudo-random random numbers from the Uniform distribution, where there is equal probability for all values that a random variable can take on.

def RAND():
d = rn.uniform(0, 1, 1)[0]
return (d)

let’s try it on

RAND()

0.9897635184955257

The second function, , which I call it NORMINV(),calculates the inverse of the Cumulative Normal Distribution Function for a supplied value of x, and a supplied distribution mean & standard deviation.

def NORMINV(x,mu,sigma):
d = si.norm.ppf(x, loc = mu, scale = sigma)
return (d)

let’s try it on for a probability of 95%, a distribution’s mean of 10% and a distribution’s standard deviation of 20%

NORMINV(0.95,0.1,0.2)

0.42897072539029446

Third, let’s set our initial price at $60,000.

S0 = 60000

Let’s set the number of steps on 100

M = 100
S = np.ones(M)
S

Let’s create an array of the integers from 0 to 100

step = np.arange(0,101)
step

Fourth, let’s create a dataframe

df = pd.DataFrame(step, columns=[‘Step i’])
df

Fifth, let’s create the second column, which will be the realization of a uniform U(0, 1) variable, with the corresponding RAND() function.

df[‘Uniform ui RAND(⋅)’]=0.0000
for i in range(1,len(df[‘Step i’])):
df[‘Uniform ui RAND(⋅)’][i]=RAND()
df

The value for the first step is u1 = 0.136923.

Fifth, let’s create the next column, which will transform this variable into a normal variable with mean 0.0 and volatility of 0.07, which gives -0.076597.

df[‘Normal ui NORMINV(ui,0.0,0.07)’]=0.0000
for i in range(1,len(df[‘Step i’])):
df[‘Normal ui NORMINV(ui,0.0,0.07)’][i] = NORMINV(df[‘Uniform ui RAND(⋅)’][i],0.0,0.07)
df

Sixth, let’s create an apriori column for the Price St+i, which later I will remove this column and rebuild it

df[‘Price St+i’]=0.000
df[‘Price St+i’][0]=S0
df

As we can see the St+i price at step 0 is our initial bitcoin price, $60,000.

Seventh, let’s create the GBM procces

for i in range(0,100):
df[‘Price St+i’][i+1] = df[‘Price St+i’][i]+ df[‘Price St+i’][i]*df[‘Normal ui NORMINV(ui,0.0,0.07)’][i+1]
df

The price increment is then obtained by multiplying the random variable by the previous price, which gives -$4,595.846392.

df[‘Price Increment’]=0.000
for i in range(1,100):
df[‘Price Increment’][i] = df[‘Price St+i’][i-1] * df[‘Normal ui NORMINV(ui,0.0,0.07)’][i]
df.drop([‘Price St+i’],axis=1,inplace=True)
df

Now, let’s build the posteriori column for the Price St+i. This generates a new value of S1 = $60,000 − $-4,595.846392 = $55,404.153608.

df[‘Price St+i’] = 0.000
df[‘Price St+i’][0] = S0
for i in range(0,100):
df[‘Price St+i’][i+1] = df[‘Price St+i’][i]+ df[‘Price Increment’][i+1]
df

The process is repeated until the final price of $65,481.34 is reached at the 100th step.

This experiment can be repeated as often as needed. Define K as the number of replications, or random trials.

import numpy as np
import pandas as pd
from scipy.stats import norm
from numpy.random import randn
from numpy import random as rn
from matplotlib import pyplot as plt
%matplotlib inline
S0 = 60000
mu = 0.00
sigma = 0.07
M = 3
N = 100
T = 1
h = T/N
Z = rn.randn(M,N)
S = S0*np.ones((M,N+1))
for i in range(0,N):
S[:,i+1] = S[:,i] + S[:,i]*( mu*h + sigma*np.sqrt(h)*Z[:,i] )
plt.figure(figsize=(17,10))
a = [ rn.randint(0,M) for j in range(1,20)]
for runer in a:
plt.plot(np.arange(0,T+h,h),S[runer])
Photo Credit: Intrinsic Value

The figure above displays the first three trials. Each leads to a simulated final value SkT. This generates a distribution of simulated prices ST.

With just one step n = 1, the distribution must be normal. As the number of steps n grows large, the distribution tends to a lognormal distribution.

While very useful to model cryptocurrency prices, this model has shortcomings. Price increments are assumed to have a normal distribution. In practice, we observe that price changes have fatter tails than the normal distribution. Returns may also experience changing variances.

In addition, as the time interval Δt shrinks, the volatility shrinks as well. This implies that large discontinuities cannot occur over short intervals. In reality, some assets experience discrete jumps, such as commodities. The stochastic process, therefore, may have to be changed to accommodate these observations.

Summary

In the geometric Brown motion process for a variable S, both dS/S or dln(S) are normally distributed. As a result, S is lognormally distributed.

If we consider that the bitcoin price S that follows a geometric Brownian motion dS = aSdt + bSdz, with b strictly positive. This means that the instantaneous rate of return on the bitcoin follows a normal distribution, the bitcoin price S follows a lognormal distribution, and that this model does not impose mean reversion. Since in our case the drift, a, is positive that means that the expected price (let’s say price a one year from now) is higher than today’s price, but certainly is not the price in all states of the world.

About the Author

Roi Polanitzer, FRM, F.IL.A.V.F.A., CFV

Roi Polanitzer, CFV, QFV, FEM, F.IL.A.V.F.A., FRM, CRM, PDS, is a well-known authority in Israel the field of business valuation and has written hundreds of papers that articulate many of the concepts used in modern business valuation around the world. Mr. Polanitzer is the Owner and Chief Appraiser of Intrinsic Value — Independent Business Appraisers, a business valuation firm headquartered in Rishon LeZion, Israel. He is also the Owner and Chief Data Scientist of Prediction Consultants, a consulting firm that specializes in advanced analysis and model development.

Over more than 17 years, he has performed valuation engagements for mergers and acquisitions, purchase price allocation (PPA) valuations, goodwill impairment test valuations, embedded option and real option valuations, employee stock option (ESOP) valuations, common stock valuations (409A), splitting equity components and complicated equity/liability instrument valuations (PWERM / CCM / OPM), contingent liability, guarantees and loan valuations, independent expert opinions for litigation purposes, damage quantifications, balancing resources between spouses due to divorce proceedings and many other kinds of business valuations. Mr. Polanitzer has testified in courts and tribunals across the country and from time to time participates in mediation proceedings between spouses.

Mr. Polanitzer holds an undergraduate degree in economics and a graduate degree in business administration, majoring in finance, both from the Ben-Gurion University of the Negev. He is a Full Actuary (Fellow), a Corporate Finance Valuator (CFV), a Quantitative Finance Valuator (QFV) and a Financial and Economic Modeler (FEM) from the Israel Association of Valuators and Financial Actuaries (IAVFA). Mr. Polanitzer is the Founder of the IAVFA and currently serves as its chairman.

Mr. Polanitzer’s professional recognitions include being designated a Financial Risk Manager (FRM) by the Global Association of Risk Professionals (GARP), a Certified Risk Manager (CRM) by the Israel Association of Risk Managers (IARM), as well as being designated a Python Data Analyst (PDA), a Machine Learning Specialist (MLS), an Accredited in Deep Learning (ADL) and a Professional Data Scientist (PDS) by the Professional Data Scientists’ Israel Association (PDSIA). Mr. Polanitzer is the Founder of the PDSIA and currently serves as its CEO.

He is the editor of IAVFA’s weekly newsletter since its inception (primarily for the professional appraisal community in Israel).

Mr. Polanitzer develops and teaches business valuation professional trainings and courses for the Israel Association of Valuators and Financial Actuaries, and frequently speaks on business valuation at professional meetings and conferences in Israel. He also developed IAVFA’s certification programs in the field of valuation and he is responsible for writing the IAVFA’s statement of financial valuation standards.

--

--

Roi Polanitzer

Chief Data Scientist at Prediction Consultants — Advanced Analysis and Model Development. https://polanitz8.wixsite.com/prediction/english