# Is there a python code for estimating the parameters of geometric brownian motion?

I was trying to find the parameters of GBM but could not find a python code for the same.

The code below pulls AAPL time series from Yahoo Finance, computes mean/std and simulates 100 paths that are 20 days long.

Input:

import pandas as pd
import numpy as np
from numpy.random import normal

# bring data
ticker = 'AAPL'
url = 'http://real-chart.finance.yahoo.com/table.csv?s=%s' % ticker
data.sort_index(inplace=True)

# estimate parameters
sigma = np.std(r)
mu = np.mean(r) +0.5*sigma*sigma

# simulate paths
T = 20 # number of periods to simulate
N = 100 # number of scenarios
epsilon = normal(size=[T, N])
paths = data['Adj Close'][-1]*np.exp(np.cumsum(mu-0.5*sigma*sigma +sigma*epsilon, axis=0))

# output
print 'data from %s to %s' % (data.index[0].date(), data.index[-1].date())
print '%d scenarios of %d periods' % (N, T)
print paths[-1]


Output:

data from 1980-12-12 to 2015-11-27
100 scenarios of 20 periods
[ 110.05103396  115.22220256  102.15942834  137.8933195   128.98220659
123.78995167  119.78760965  111.55081804  101.28873804  150.01698323
118.17378031  129.26568517  116.89831894  120.14299291  100.92602175
129.08661341  126.11134726  131.05832164  105.40035237  148.56201625
117.6589326   135.80459977  116.3256132   118.11022374  138.24069944
130.23096841  121.44359248   93.36990366  120.40376295  113.86096665
112.79238568  104.29584358  154.99719687  133.25955192  108.20838712
120.11125973  104.21453197  112.78981396  108.77688605  118.40240356
156.99140878  130.91871886  111.0078855   139.71865207  145.2893878
135.05797455  122.57696229   93.85711338  148.3231574   105.5308185
98.42940505  106.28268823   99.49360753  118.41985903  152.10864408
100.48435239   96.49837802  124.98286066  112.95868168  134.59980791
132.01882861  109.43251849  151.64664262  116.37618346  112.04417492
119.18353134  120.73513746   96.41828595  107.16867675  121.18435982
90.45490766  110.88758748  126.11216937  106.48179123  147.52005583
121.25689013  118.6405851   103.84301241  120.88770293  110.99499809
119.6822996   112.84273236  131.80224893  124.77742484  135.99716652
141.02784531  117.44045644  110.02261302  128.75145159  115.82045737
129.60995183  115.7623887   133.83373679  116.89180599  103.1893998
138.25279665  106.56185885  138.59835512  101.29356076  152.75506114]


If you want to use GBM in the physical measure then just calculate the standard deviation of log returns to get $\sigma$. For $\mu$ there are many choices.

If you want to calibrate GBM to get option prices then you need the implied volatility and there is a package.

The OP said that he/she wants to simulate paths. If you don't want to price derivatives then you work in the "real world" measure.

Then $\sigma$ can be estimated as mentioned. $\mu$ is hard to assume (for the future). If you look at the literature of momentum strategies then some of them take the expectation of the last 3 to 6 months for the coming 3 to 6 months. I say this in the sense that there is the stylized fact that winners tend to win and losers tend to lose.

• Assuming stock price of a company to follow GBM, I need to estimate the parameters of GBM and simulate sample paths.
– upsc
Nov 28, 2015 at 16:42
• What do you do with the sample paths? If you want to study path properties then maybe you study $P$ the physical measure. see my edit. Nov 28, 2015 at 16:44