*Introduction*

This first and basic article will show how to simulate a security following the Black & Scholes dynamic :

When solving this stochastic differential equation with Ito, you finally obtain:

The browian motion can be written as where . This will be usefull for our simulation!

Let’s simulate an asset over a year with , and . We discretize time daily so that we have .

The little program will use numpy package for its efficient data structure, math package for mathematical functions and plotly package for plotting. Let’s go into the code:

import numpy as np import math import plotly as plt import plotly.graph_objs as go def asset_price(S,drift,volatility,dt,X): return S*math.exp((drift-(volatility**2)/2)*dt + volatility*math.sqrt(dt)*X) S_0 = 35 drift = 0.05 volatility = 0.25 dt = 1/365 N_days = 365 path_matrix = np.zeros((N_days+1,1)) path_matrix[0,0] = S_0 #Initial value random_variable = np.random.randn(N_days) #365 independant values generated from standard normal random variables. for i in range(N_days): path_matrix[i+1,0] = asset_price(path_matrix[i,0],drift,volatility,dt,random_variable[i]) #Generate plot : asset = go.Scatter(y=path_matrix[:,0],name="Simulated asset",opacity = 0.8) layout = go.Layout( title='Simulated asset path') #Plot : plt.offline.plot({"data":[asset],"layout":layout})

When generating 100 random walks for this asset by adding a few lines of code, we obtain something such as this :

*Conclusion*

This article shows how to simulate one random path for an asset following a straightforward dynamic but the method can easily be extended to N assets. Here and are constant but stochastic process can be inserted. Following articles will deal with a Monte Carlo simulation of N correlated assets used to price exotic options for example.

*Alex Mouturat*

Pingback: Barrier option pricing with Monte Carlo | My financial markets

Can you mention the few lines of code that would lead to the final plot ?

LikeLike