Heston Model - Montecarlo Simulation Quant Trading
Heston Model - Montecarlo Simulation Quant Trading
(https://quant-trading.co/)
a
Subscribe to our n
Montercarlo Simulation - Heston Model
finance skills!
Name*
Email*
(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]:
In [3]:
norminv = scipy.stats.distributions.norm.ppf
norm = scipy.stats.distributions.norm.cdf
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)
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
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)
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!
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)
Related Post
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!
(https://quant-trading.co/how-to-calculate-implied-density-function-from-options-prices/)
Read more
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!
(https://quant-trading.co/how-to-plot-cumulative-returns-for-multiple-assets/)
Read more
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!
(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)
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!
(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