0% found this document useful (0 votes)
16 views8 pages

Heston Model - Montecarlo Simulation Quant Trading

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views8 pages

Heston Model - Montecarlo Simulation Quant Trading

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

5/20/25, 2:37 PM Heston Model - Montecarlo Simulation ~ Quant Trading

(https://quant-trading.co/)
a

Home (https://quant-trading.co/) / Tools (https://quant-trading.co/category/tools/) / Heston Model – Montecarlo Simulation

Subscribe to our n
Montercarlo Simulation - Heston Model
finance skills!

Name*

Email*

 I agree on data processing agreemen

HESTON MODEL – MONTERCARLO SIMULATION

MONTECARLO SIMULATION – HESTON PROCESS


¿How can you build a montercarlo simulation for Heston
Process using python?
In this notebook we are showing how you can run a montecarlo simulation for a Heston Process in python. Montecarlo simulation
is a powerful technique that allows you visualize different paths a financial asset could take in the future. You can also use this
technique for derivatives pricing. The Heston Process belongs to the category of square root diffusion process, similar to the COX
model for interest rates. If you would like to know more about the COX model please look here (https://quant-
trading.co/montecarlo-simulation-for-cox-ingersoll-ross-cir-process/).
(https://www.facebook.com/quanttrading.co?fref=ts)

(https://www.linkedin.com/company/quant-trading?
trk=vsrp_companies_res_name&trkInfo=VSRPsearchId%3A474811191394422056908%2CVSRPtargetId%3A3638588%2CVSRPcmpt%3Aprimary)
Please remember that we showed how to simulate Geometric Brownian Motion in this notebook (https://quant-
(https://twitter.com/QuantTradingCO)
trading.co/montercarlo-simulation-geometric-brownian-motion/). In that case, we can get an exact simulation of the
process. However, here in the Heston process we can only get a second order approximation.
(https://www.youtube.com/channel/UCp7X-TN5bgcHwwU5bteUCUg)
In [1]:

Privacy - Terms

https://quant-trading.co/heston-model/ 1/8
5/20/25, 2:37 PM Heston Model - Montecarlo Simulation ~ Quant Trading

import warnings
warnings.filterwarnings('ignore')
import numpy as np
from numpy.random import rand
import scipy.stats
import matplotlib.pyplot as plt
Subscribe to our n
Define all the required parameters finance skills!
We need to define some parameters such as: a) The number of simulations that we are going to run, b) The number of steps to
use in each path the asset will follow, c) The spot price of our financial asset, d) The time step in the simulation, e) The risk-free
interest rate, f) The volatility of the volatility our financial asset, g) The correlation between the financial asset and the volatility, h)
The mean reversion constant for the volatility process, i) The long run variance, j) The long run variance.

In [2]:

M = 100 #Number of paths


d = 250 #Number of steps
 I agree on data processing agreemen
spot = 100 #Spot
T=1 #Time to maturity
delta_t = T/d
r = 0.05 #Interest rate
sigma = 0.30 #Volatility of volatility
rho = -0.75 #Correlation spot-vol
sigma1 = rho*sigma #Relationship to generate two independent brownian motions
sigma2 = np.sqrt(sigma**2-sigma1**2)
kappa = 0.5 #Mean reversion constant for vol process
theta = 0.0725 #Long run variance
variance = 0.026 #Initial variance

Initialize the inverse normal function and normal


function
We need to transform uniform random variables into normal random variables. We can do that using the inverse normal
function.

In [3]:

norminv = scipy.stats.distributions.norm.ppf
norm = scipy.stats.distributions.norm.cdf

Generate uniformly distributed and normal random


variables
We can use the rand function to generate uniformly and normal distributed random variables as following

In [4]:

y1 = rand(M,d)
y2 = rand(M,d)
y3 = rand(M,d)
(https://www.facebook.com/quanttrading.co?fref=ts)
z1 = norminv(y1)
(https://www.linkedin.com/company/quant-trading?
z2 = norminv(y2)
trk=vsrp_companies_res_name&trkInfo=VSRPsearchId%3A474811191394422056908%2CVSRPtargetId%3A3638588%2CVSRPcmpt%3Aprimary)

Create a vector with the initial spot price and the initial
(https://twitter.com/QuantTradingCO)

variance
(https://www.youtube.com/channel/UCp7X-TN5bgcHwwU5bteUCUg)

We need to create a column vector that contains the spot price and another with the initial variance. The number of rows will be
equal to the number of simulations that we are going to run

In [5]:

https://quant-trading.co/heston-model/ 2/8
5/20/25, 2:37 PM Heston Model - Montecarlo Simulation ~ Quant Trading

initial_spot_asset=spot*np.ones((M,1))
initial_variance_asset=variance*np.ones((M,1))

Create a matrix
We need to store the results from our montecarlo simulation, so we create two matrices with M rows and d columns. Then we
append the previously created spot vectors to them Subscribe to our n
finance skills!
In [6]:

ASSET = np.zeros((M,d))
ASSET = np.append(initial_spot_asset,ASSET,axis=1)
ASSET_VARIANCE = np.zeros((M,d))
ASSET_VARIANCE = np.append(initial_variance_asset,ASSET_VARIANCE,axis=1)

Generate the simulation paths


Using the discretized equation of the heston process and the matrices of uniform and normal random variables we can create
the paths for our simulation as follows.  I agree on data processing agreemen

Please note that we are taking the absolute value of the variance to avoid taking the square root of a negative number or dividing by zero.
Remember that in this notebook we are showing how you can run a montecarlo simulation for a Heston Process in python.
In [7]:

for i in range(0,M):
for j in range(0,d):
if y3[i,j] > 0.5:
epsilon = delta_t
else:
epsilon = -delta_t

ASSET[i,j+1] = ASSET[i,j]*(1+r*delta_t + np.sqrt(abs(ASSET_VARIANCE[i,j]))*np.sqrt(delta_t)*z1[i,j]) \


+0.5*r**2*ASSET[i,j]*delta_t**2 \
+((r+0.25*(sigma1-kappa)*ASSET[i,j]*np.sqrt(abs(ASSET_VARIANCE[i,j])))+ (0.25*(kappa-theta) - 0.0625*sigma**2 )*(ASSET[
+0.5*ASSET[i,j]*(ASSET_VARIANCE[i,j]+0.5*sigma1)*(delta_t*z1[i,j]**2-delta_t) + 0.25*sigma2*ASSET[i,j]*(np.sqrt(delta_t

ASSET_VARIANCE[i,j+1] = kappa*theta*delta_t + (1 - kappa*delta_t)*abs(ASSET_VARIANCE[i,j]) \


+ np.sqrt(abs(ASSET_VARIANCE[i,j])) * (sigma1*np.sqrt(delta_t)*z1[i,j] + sigma2*np.sqrt(delta_t)*z2[i,j]) - 0.5
+ ( (0.25*kappa*theta -0.0625*sigma**2)/np.sqrt(abs(ASSET_VARIANCE[i,j])) - 1.5*kappa* np.sqrt(abs(ASSET_VARIA
+0.25*sigma1**2*(delta_t*z1[i,j]**2-delta_t) + 0.25* sigma2**2*(delta_t*z2[i,j]**2-delta_t) + 0.5*sigma1*sigma2

Plot the results


We can plot the results as follows

In [8]:

fig = plt.figure()
ax = fig.add_subplot(1,1,1)
ax.plot(ASSET[0:100,:].transpose(),c='silver')
ax.plot(ASSET[0:1,:].transpose(),c='darkblue')
ax.set_facecolor('white')
(https://www.facebook.com/quanttrading.co?fref=ts)
plt.yticks(fontname = "Tahoma",fontsize=15)
(https://www.linkedin.com/company/quant-trading?
plt.xticks(fontname = "Tahoma",fontsize=15)
trk=vsrp_companies_res_name&trkInfo=VSRPsearchId%3A474811191394422056908%2CVSRPtargetId%3A3638588%2CVSRPcmpt%3Aprimary)
plt.ylabel("Asset's price",fontsize=15)
(https://twitter.com/QuantTradingCO)
plt.xlabel("Business Days",fontsize=15)
plt.title("Heston Process - Montecarlo Simulation", size=25, family='Tahoma')
(https://www.youtube.com/channel/UCp7X-TN5bgcHwwU5bteUCUg)
plt.box(on=None)

plt.subplots_adjust(bottom=0.1, right=1.8, top=1.0)


plt.text(200, np.min(ASSET[0:100,:])-15,'www.quant-trading.co' , color='black', fontsize=15,family='Impact')
plt.show()

https://quant-trading.co/heston-model/ 3/8
5/20/25, 2:37 PM Heston Model - Montecarlo Simulation ~ Quant Trading

Subscribe to our n
finance skills!

 I agree on data processing agreemen

In this notebook we showed how you can run a montecarlo simulation for a Heston Process in python.
If this content is helpful and you want to make a donation please click on the button

(https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=29CVY97MEQ9BY)

Tags: Montecarlo Simulation - Tools (https://quant-trading.co/tag/simulacion-montecarlo/)

Related Post

How to calculate implied density function from options prices? (https://quant-


trading.co/how-to-calculate-implied-density-function-from-options-prices/)

Here we are showing how to calculate the implied density function from options prices using python code.

(https://www.facebook.com/quanttrading.co?fref=ts)

(https://www.linkedin.com/company/quant-trading?
trk=vsrp_companies_res_name&trkInfo=VSRPsearchId%3A474811191394422056908%2CVSRPtargetId%3A3638588%2CVSRPcmpt%3Aprimary)

(https://twitter.com/QuantTradingCO)

(https://www.youtube.com/channel/UCp7X-TN5bgcHwwU5bteUCUg)

https://quant-trading.co/heston-model/ 4/8
5/20/25, 2:37 PM Heston Model - Montecarlo Simulation ~ Quant Trading

Subscribe to our n
finance skills!

 I agree on data processing agreemen

(https://quant-trading.co/how-to-calculate-implied-density-function-from-options-prices/)

Read more

How to plot cumulative returns for multiple assets? (https://quant-trading.co/how-to-plot-


cumulative-returns-for-multiple-assets/)

In this post we will show how to plot multiple cumulative returns using python code.
(https://www.facebook.com/quanttrading.co?fref=ts)

(https://www.linkedin.com/company/quant-trading?
trk=vsrp_companies_res_name&trkInfo=VSRPsearchId%3A474811191394422056908%2CVSRPtargetId%3A3638588%2CVSRPcmpt%3Aprimary)

(https://twitter.com/QuantTradingCO)

(https://www.youtube.com/channel/UCp7X-TN5bgcHwwU5bteUCUg)

https://quant-trading.co/heston-model/ 5/8
5/20/25, 2:37 PM Heston Model - Montecarlo Simulation ~ Quant Trading

Subscribe to our n
finance skills!

 I agree on data processing agreemen

(https://quant-trading.co/how-to-plot-cumulative-returns-for-multiple-assets/)

Read more

How to make a dynamic chart for stock returns in python? (https://quant-trading.co/how-to-


make-a-dynamic-chart-for-stock-returns-using-python/)

In this post we will show how to create a dynamic chart for financial returns using python.

(https://www.facebook.com/quanttrading.co?fref=ts)

(https://www.linkedin.com/company/quant-trading?
trk=vsrp_companies_res_name&trkInfo=VSRPsearchId%3A474811191394422056908%2CVSRPtargetId%3A3638588%2CVSRPcmpt%3Aprimary)

(https://twitter.com/QuantTradingCO)

(https://www.youtube.com/channel/UCp7X-TN5bgcHwwU5bteUCUg)

https://quant-trading.co/heston-model/ 6/8
5/20/25, 2:37 PM Heston Model - Montecarlo Simulation ~ Quant Trading

Subscribe to our n
finance skills!

 I agree on data processing agreemen

(https://quant-trading.co/how-to-make-a-dynamic-chart-for-stock-returns-using-python/)

Read more

(https://www.facebook.com/quanttrading.co?fref=ts)
Courses
(https://www.facebook.com/quanttrading.co?fref=ts) (https://www.linkedin.com/company/quant-trading
(https://quant-
(https://www.linkedin.com/company/quant-trading?
trk=vsrp_companies_res_name&trkInfo=VSRPsearchId%3A474811191394422056908%2CVSRPtargetId%3A363
trading.co/courses/)
trk=vsrp_companies_res_name&trkInfo=VSRPsearchId%3A474811191394422056908%2CVSRPtargetId%3A3638588%2CVSRPcmpt%3Aprimary)
(https://twitter.com/QuantTradingCO) (https://www.youtube.com/channel/UCp7X-TN5bgcHwwU5bteUCUg
Blog (https://quant-
(https://twitter.com/QuantTradingCO)
trading.co/blog/)
(https://www.youtube.com/channel/UCp7X-TN5bgcHwwU5bteUCUg)
Contact
(https://quant-
trading.co/#contacto)

Copyright©2019 Quant Trading. Website Design by Eddi (https://eddi.com.co/)

https://quant-trading.co/heston-model/ 7/8
5/20/25, 2:37 PM Heston Model - Montecarlo Simulation ~ Quant Trading

Subscribe to our n
finance skills!

 I agree on data processing agreemen

(https://www.facebook.com/quanttrading.co?fref=ts)

(https://www.linkedin.com/company/quant-trading?
trk=vsrp_companies_res_name&trkInfo=VSRPsearchId%3A474811191394422056908%2CVSRPtargetId%3A3638588%2CVSRPcmpt%3Aprimary)

(https://twitter.com/QuantTradingCO)

(https://www.youtube.com/channel/UCp7X-TN5bgcHwwU5bteUCUg)

https://quant-trading.co/heston-model/ 8/8

You might also like