forked from enthought/pyql
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathheston_model.pyx
More file actions
128 lines (106 loc) · 4.27 KB
/
heston_model.pyx
File metadata and controls
128 lines (106 loc) · 4.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# Copyright (C) 2011, Enthought Inc
# Copyright (C) 2011, Patrick Henaff
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the license for more details.
""" Heston model for the stochastic volatility of an asset"""
from quantlib.types cimport Real
from libcpp cimport bool
from cython.operator cimport dereference as deref
from libcpp.vector cimport vector
from . cimport _heston_model as _hm
cimport quantlib.models._calibration_helper as _ch
cimport quantlib.processes._heston_process as _hp
cimport quantlib._stochastic_process as _sp
cimport quantlib.termstructures.yields._flat_forward as _ffwd
cimport quantlib.pricingengines._pricing_engine as _pe
from quantlib.handle cimport shared_ptr, static_pointer_cast
from quantlib.math.optimization cimport (Constraint,OptimizationMethod,
EndCriteria)
from quantlib.processes.heston_process cimport HestonProcess
from quantlib.pricingengines.engine cimport PricingEngine
from quantlib.quote cimport Quote
from quantlib.time.calendar cimport Calendar
from quantlib.time.date cimport Period
from quantlib.termstructures.yield_term_structure cimport (
HandleYieldTermStructure
)
from quantlib.models.calibration_helper cimport BlackCalibrationHelper, CalibrationErrorType
cdef class HestonModelHelper(BlackCalibrationHelper):
def __str__(self):
return 'Heston model helper'
def __init__(self,
Period maturity,
Calendar calendar,
Real s0,
Real strike_price,
Quote volatility,
HandleYieldTermStructure risk_free_rate,
HandleYieldTermStructure dividend_yield,
CalibrationErrorType error_type=_ch.RelativePriceError
):
self._thisptr = shared_ptr[_ch.CalibrationHelper](
new _hm.HestonModelHelper(
deref(maturity._thisptr),
calendar._thisptr,
s0,
strike_price,
volatility.handle(),
risk_free_rate.handle,
dividend_yield.handle,
error_type
)
)
cdef class HestonModel:
"""Heston model for the stochastic volatility of an asset
References
----------
Heston, Steven L., 1993. "A Closed-Form Solution for Options with Stochastic Volatility with Applications to Bond and Currency Options." *The review of Financial Studies, Volume 6, Issue 2, 327-343.*
"""
def __init__(self, HestonProcess process):
self._thisptr = shared_ptr[_hm.HestonModel](
new _hm.HestonModel(static_pointer_cast[_hp.HestonProcess](
process._thisptr))
)
def process(self):
"""underlying process"""
cdef HestonProcess process = HestonProcess.__new__(HestonProcess)
process._thisptr = static_pointer_cast[_sp.StochasticProcess](
self._thisptr.get().process())
return process
@property
def theta(self):
"""variance mean reversion level"""
return self._thisptr.get().theta()
@property
def kappa(self):
"""variance mean reversion speed"""
return self._thisptr.get().kappa()
@property
def sigma(self):
"""volatility of the volatility"""
return self._thisptr.get().sigma()
@property
def rho(self):
"""correlation"""
return self._thisptr.get().rho()
@property
def v0(self):
"""spot variance"""
return self._thisptr.get().v0()
def calibrate(self, list helpers, OptimizationMethod method, EndCriteria
end_criteria, Constraint constraint=Constraint(),
vector[Real] weights=[], vector[bool] fix_parameters=[]):
#convert list to vector
cdef vector[shared_ptr[_ch.CalibrationHelper]] helpers_vector
cdef shared_ptr[_ch.CalibrationHelper] chelper
for helper in helpers:
chelper = (<HestonModelHelper>helper)._thisptr
helpers_vector.push_back(chelper)
self._thisptr.get().calibrate(
helpers_vector,
deref(method._thisptr),
deref(end_criteria._thisptr),
deref(constraint._thisptr),
weights,
fix_parameters)