Quantcast
Channel: CodeSection,代码区,Python开发技术文章_教程 - CodeSec
Viewing all articles
Browse latest Browse all 9596

Heston Model Calibration Using QuantLib Python and Scipy Optimize

$
0
0

In this post we do a deep dive on calibration of Heston model using QuantLib python and Scipy's Optimize package.

I have discussed parameter calibration in a couple of my earlier posts. In this post I want to show how you can use QuantLib Python and Scipy to do parameter calibration. In order to run this, you will need to build the QuantLib github master and the latest SWIG code with my pull request . Alternately, this should get merged into version 1.9 and you should be able to use it when it is released. This pull request adds some of the moethods of the CalibratedModel such as calibrationError that we will use in calibrating models using Scipy. QuantLib's strength is all financial models. Scipy's strength is all the solvers and numerical methods. So here, I will show you how you can make the best of both worlds. We will start as usual by importing the modules.

In[1]:

import QuantLib as ql from math import pow, sqrt import numpy as np from scipy.optimize import root

Let's construct some of the basic dependencies such as the yield and dividend term structures.

In[2]:

day_count = ql.Actual365Fixed() calendar = ql.UnitedStates() calculation_date = ql.Date(6, 11, 2015) spot = 659.37 ql.Settings.instance().evaluationDate = calculation_date risk_free_rate = 0.01 dividend_rate = 0.0 yield_ts = ql.YieldTermStructureHandle( ql.FlatForward(calculation_date, risk_free_rate, day_count)) dividend_ts = ql.YieldTermStructureHandle( ql.FlatForward(calculation_date, dividend_rate, day_count))

Following is a sample grid of volatilities for different expiration and strikes.

In[3]: expiration_dates = [ql.Date(6,12,2015), ql.Date(6,1,2016), ql.Date(6,2,2016), ql.Date(6,3,2016), ql.Date(6,4,2016), ql.Date(6,5,2016), ql.Date(6,6,2016), ql.Date(6,7,2016), ql.Date(6,8,2016), ql.Date(6,9,2016), ql.Date(6,10,2016), ql.Date(6,11,2016), ql.Date(6,12,2016), ql.Date(6,1,2017), ql.Date(6,2,2017), ql.Date(6,3,2017), ql.Date(6,4,2017), ql.Date(6,5,2017), ql.Date(6,6,2017), ql.Date(6,7,2017), ql.Date(6,8,2017), ql.Date(6,9,2017), ql.Date(6,10,2017), ql.Date(6,11,2017)] strikes = [527.50, 560.46, 593.43, 626.40, 659.37, 692.34, 725.31, 758.28] data = [ [0.37819, 0.34177, 0.30394, 0.27832, 0.26453, 0.25916, 0.25941, 0.26127], [0.3445, 0.31769, 0.2933, 0.27614, 0.26575, 0.25729, 0.25228, 0.25202], [0.37419, 0.35372, 0.33729, 0.32492, 0.31601, 0.30883

Viewing all articles
Browse latest Browse all 9596

Trending Articles