Skip to content

Commit 7c09d9c

Browse files
committed
add G2 model
1 parent 81c4cc2 commit 7c09d9c

File tree

7 files changed

+90
-0
lines changed

7 files changed

+90
-0
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
from quantlib.types cimport DiscountFactor, Rate, Real, Time
2+
from quantlib.math._array cimport Array
3+
from quantlib.models._model cimport ShortRateModel, AffineModel
4+
from quantlib.handle cimport shared_ptr
5+
from quantlib._stochastic_process cimport StochasticProcess1D
6+
7+
cdef extern from 'ql/models/shortrate/twofactormodel.hpp' namespace 'QuantLib' nogil:
8+
9+
cdef cppclass TwoFactorModel(ShortRateModel):
10+
cppclass ShortRateDynamics:
11+
ShortRateDynamics(shared_ptr[StochasticProcess1D]& xProcess,
12+
shared_ptr[StochasticProcess1D]& yProcess,
13+
Real correlation)
14+
Rate shortRate(Time t, Real x, Real y)
15+
shared_ptr[StochasticProcess1D]& xProcess()
16+
shared_ptr[StochasticProcess1D]& yProcess()
17+
shared_ptr[ShortRateDynamics] dynamics()
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
from quantlib.models.model cimport ShortRateModel
2+
from quantlib.handle cimport shared_ptr
3+
from . cimport _twofactor_model as _tfm
4+
5+
cdef class ShortRateDynamics:
6+
cdef shared_ptr[_tfm.TwoFactorModel.ShortRateDynamics] _thisptr
7+
8+
cdef class TwoFactorModel(ShortRateModel):
9+
pass
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
from quantlib.types cimport Real, Time
2+
3+
from quantlib.handle cimport static_pointer_cast
4+
from . cimport _twofactor_model as _tfm
5+
cimport quantlib._stochastic_process as _sp
6+
from quantlib.stochastic_process cimport StochasticProcess1D
7+
8+
cdef class ShortRateDynamics:
9+
10+
@property
11+
def process_x(self):
12+
cdef StochasticProcess1D sp = StochasticProcess1D.__new__(StochasticProcess1D)
13+
sp._thisptr = static_pointer_cast[_sp.StochasticProcess](self._thisptr.get().xProcess())
14+
return sp
15+
16+
@property
17+
def process_x(self):
18+
cdef StochasticProcess1D sp = StochasticProcess1D.__new__(StochasticProcess1D)
19+
sp._thisptr = static_pointer_cast[_sp.StochasticProcess](self._thisptr.get().yProcess())
20+
return sp
21+
22+
def short_rate(self, Time t, Real x, Real y):
23+
return self._thisptr.get().shortRate(t, x, y)
24+
25+
cdef class TwoFactorModel(ShortRateModel):
26+
27+
@property
28+
def dynamics(self):
29+
cdef ShortRateDynamics dyn = ShortRateDynamics.__new__(ShortRateDynamics)
30+
dyn._thisptr = (<_tfm.TwoFactorModel*>self._thisptr.get()).dynamics()
31+
return dyn

quantlib/models/shortrate/twofactormodels/__init__.py

Whitespace-only changes.
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
from quantlib.types cimport Rate, Real, Time
2+
from quantlib.handle cimport Handle
3+
from quantlib.termstructures._yield_term_structure cimport YieldTermStructure
4+
from .._twofactor_model cimport TwoFactorModel
5+
from ..._model cimport TermStructureConsistentModel, AffineModel
6+
7+
cdef extern from 'ql/models/shortrate/twofactormodels/g2.hpp' namespace 'QuantLib' nogil:
8+
cdef cppclass G2(TwoFactorModel, AffineModel, TermStructureConsistentModel):
9+
G2(
10+
Handle[YieldTermStructure]& termStructure,
11+
Real a, #0.1
12+
Real sigma, # = 0.01
13+
Real b, # = 0.1
14+
Real eta, # = 0.01
15+
Real rho, # = -0.75
16+
) except +
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
from ..twofactor_model cimport TwoFactorModel
2+
3+
cdef class G2(TwoFactorModel):
4+
pass
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
from quantlib.types cimport Real
2+
from quantlib.termstructures.yield_term_structure cimport HandleYieldTermStructure
3+
from . cimport _g2
4+
5+
cdef class G2(TwoFactorModel):
6+
def __init(self,
7+
HandleYieldTermStructure h,
8+
Real a=0.1,
9+
Real sigma=0.01,
10+
Real b=0.1,
11+
Real eta=0.01,
12+
Real rho=-0.75):
13+
self._thisptr.reset(new _g2.G2(h.handle, a, sigma, b, eta, rho))

0 commit comments

Comments
 (0)