From 438d56f3bfe3fa0390e22f11eab598066a0a137e Mon Sep 17 00:00:00 2001 From: Guillaume Horel Date: Tue, 2 Apr 2024 16:29:28 -0400 Subject: [PATCH 1/3] add default values --- quantlib/pricingengines/bondfunctions.pyx | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/quantlib/pricingengines/bondfunctions.pyx b/quantlib/pricingengines/bondfunctions.pyx index 319bd56de..314b0fb26 100644 --- a/quantlib/pricingengines/bondfunctions.pyx +++ b/quantlib/pricingengines/bondfunctions.pyx @@ -46,10 +46,10 @@ def bond_yield(Bond bond not None, DayCounter dayCounter not None, Compounding compounding, Frequency frequency, - Date settlementDate not None, - Real accuracy, - Size maxIterations, - Rate guess): + Date settlementDate=Date(), + Real accuracy=1e-10, + Size maxIterations=100, + Rate guess=0.05): return _bf.bf_yield( deref(bond.as_ptr()), @@ -68,7 +68,7 @@ def basisPointValue(Bond bond not None, DayCounter dayCounter not None, Compounding compounding, Frequency frequency, - Date settlementDate not None): + Date settlementDate=Date()): return _bf.basisPointValue( deref(bond.as_ptr()), yld, @@ -83,10 +83,10 @@ def zSpread(Bond bond, Real cleanPrice, DayCounter dayCounter not None, Compounding compounding, Frequency frequency, - Date settlementDate not None, - Real accuracy, - Size maxIterations, - Rate guess): + Date settlementDate=Date(), + Real accuracy=1e-10, + Size maxIterations=100, + Rate guess=0): return _bf.zSpread( deref(bond.as_ptr()), From 463a4aaa92f9cd5fca8ab8d39240922fa2f63117 Mon Sep 17 00:00:00 2001 From: Guillaume Horel Date: Wed, 3 Apr 2024 13:47:33 -0400 Subject: [PATCH 2/3] move bond engines inside folder --- quantlib/pricingengines/bond/__init__.py | 1 + quantlib/pricingengines/{ => bond}/_bondfunctions.pxd | 2 +- .../{_bond.pxd => bond/_discountingbondengine.pxd} | 4 +--- quantlib/pricingengines/{ => bond}/bondfunctions.pyx | 5 ++--- .../{bond.pyx => bond/discountingbondengine.pyx} | 11 +++-------- test/test_bondfunctions.py | 2 +- test/test_sensitivity_analysis.py | 2 +- 7 files changed, 10 insertions(+), 17 deletions(-) create mode 100644 quantlib/pricingengines/bond/__init__.py rename quantlib/pricingengines/{ => bond}/_bondfunctions.pxd (96%) rename quantlib/pricingengines/{_bond.pxd => bond/_discountingbondengine.pxd} (94%) rename quantlib/pricingengines/{ => bond}/bondfunctions.pyx (97%) rename quantlib/pricingengines/{bond.pyx => bond/discountingbondengine.pyx} (58%) diff --git a/quantlib/pricingengines/bond/__init__.py b/quantlib/pricingengines/bond/__init__.py new file mode 100644 index 000000000..66c6a96f9 --- /dev/null +++ b/quantlib/pricingengines/bond/__init__.py @@ -0,0 +1 @@ +from .discountingbondengine import DiscountingBondEngine diff --git a/quantlib/pricingengines/_bondfunctions.pxd b/quantlib/pricingengines/bond/_bondfunctions.pxd similarity index 96% rename from quantlib/pricingengines/_bondfunctions.pxd rename to quantlib/pricingengines/bond/_bondfunctions.pxd index 1bf062a49..5e04934bd 100644 --- a/quantlib/pricingengines/_bondfunctions.pxd +++ b/quantlib/pricingengines/bond/_bondfunctions.pxd @@ -1,4 +1,4 @@ -include '../types.pxi' +from quantlib.types cimport Rate, Real, Spread, Size, Time from quantlib.instruments._bond cimport Bond from quantlib.handle cimport shared_ptr diff --git a/quantlib/pricingengines/_bond.pxd b/quantlib/pricingengines/bond/_discountingbondengine.pxd similarity index 94% rename from quantlib/pricingengines/_bond.pxd rename to quantlib/pricingengines/bond/_discountingbondengine.pxd index fb3fc5796..197a59b5a 100644 --- a/quantlib/pricingengines/_bond.pxd +++ b/quantlib/pricingengines/bond/_discountingbondengine.pxd @@ -13,11 +13,9 @@ from quantlib.handle cimport Handle, optional from quantlib.termstructures._yield_term_structure cimport YieldTermStructure cdef extern from 'ql/pricingengines/bond/discountingbondengine.hpp' namespace \ - 'QuantLib': + 'QuantLib' nogil: cdef cppclass DiscountingBondEngine(PricingEngine): - - DiscountingBondEngine() DiscountingBondEngine(Handle[YieldTermStructure]& discountCurve) DiscountingBondEngine(Handle[YieldTermStructure]& discountCurve, optional[bool] includeSettlementDateFlows) diff --git a/quantlib/pricingengines/bondfunctions.pyx b/quantlib/pricingengines/bond/bondfunctions.pyx similarity index 97% rename from quantlib/pricingengines/bondfunctions.pyx rename to quantlib/pricingengines/bond/bondfunctions.pyx index 314b0fb26..5aa82ba22 100644 --- a/quantlib/pricingengines/bondfunctions.pyx +++ b/quantlib/pricingengines/bond/bondfunctions.pyx @@ -1,9 +1,8 @@ -include '../types.pxi' - +from quantlib.types cimport Rate, Real, Size from quantlib.time._date cimport Day, Month, Year, Date as QLDate from quantlib.time._period cimport Frequency from quantlib.time._daycounter cimport DayCounter as _DayCounter -cimport quantlib.pricingengines._bondfunctions as _bf +from . cimport _bondfunctions as _bf from quantlib.handle cimport shared_ptr, Handle from cython.operator cimport dereference as deref diff --git a/quantlib/pricingengines/bond.pyx b/quantlib/pricingengines/bond/discountingbondengine.pyx similarity index 58% rename from quantlib/pricingengines/bond.pyx rename to quantlib/pricingengines/bond/discountingbondengine.pyx index 317ddad56..b17565d86 100644 --- a/quantlib/pricingengines/bond.pyx +++ b/quantlib/pricingengines/bond/discountingbondengine.pyx @@ -5,15 +5,10 @@ ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the license for more details. """ -from cython.operator cimport dereference as deref +from . cimport _discountingbondengine as _dbe -from quantlib.handle cimport Handle, shared_ptr -from .cimport _pricing_engine as _pe -from . cimport _bond +from ..engine cimport PricingEngine -from .engine cimport PricingEngine - -cimport quantlib.termstructures._yield_term_structure as _yts from quantlib.termstructures.yield_term_structure cimport YieldTermStructure cdef class DiscountingBondEngine(PricingEngine): @@ -22,4 +17,4 @@ cdef class DiscountingBondEngine(PricingEngine): """ """ - self._thisptr.reset(new _bond.DiscountingBondEngine(discount_curve._thisptr)) + self._thisptr.reset(new _dbe.DiscountingBondEngine(discount_curve._thisptr)) diff --git a/test/test_bondfunctions.py b/test/test_bondfunctions.py index 49a3c4eb5..51ea9cfd6 100644 --- a/test/test_bondfunctions.py +++ b/test/test_bondfunctions.py @@ -34,7 +34,7 @@ from quantlib.math.interpolation import LogLinear from quantlib.quotes import SimpleQuote -import quantlib.pricingengines.bondfunctions as bf +import quantlib.pricingengines.bond.bondfunctions as bf class BondFunctionTestCase(unittest.TestCase): diff --git a/test/test_sensitivity_analysis.py b/test/test_sensitivity_analysis.py index 66d5ea73e..84e177f6c 100644 --- a/test/test_sensitivity_analysis.py +++ b/test/test_sensitivity_analysis.py @@ -33,7 +33,7 @@ AnalyticEuropeanEngine ) from quantlib.instruments.payoffs import PlainVanillaPayoff -import quantlib.pricingengines.bondfunctions as bf +import quantlib.pricingengines.bond.bondfunctions as bf from quantlib.experimental.risk.sensitivityanalysis import ( bucket_analysis, parallel_analysis, Centered) from numpy.testing import assert_allclose From 6598046bc02a7e65e289d7beba41fce02796ef6f Mon Sep 17 00:00:00 2001 From: Guillaume Horel Date: Wed, 3 Apr 2024 13:51:56 -0400 Subject: [PATCH 3/3] cleanups --- quantlib/instrument.pyx | 1 - quantlib/models/calibration_helper.pyx | 5 +---- quantlib/pricingengines/_pricing_engine.pxd | 2 +- quantlib/pricingengines/credit/isda_cds_engine.pyx | 5 ++--- quantlib/pricingengines/credit/midpoint_cds_engine.pyx | 1 - quantlib/pricingengines/engine.pxd | 2 -- quantlib/pricingengines/swap.pyx | 1 - quantlib/pricingengines/vanilla/mceuropeanhestonengine.pyx | 2 -- 8 files changed, 4 insertions(+), 15 deletions(-) diff --git a/quantlib/instrument.pyx b/quantlib/instrument.pyx index f0da250d9..3defea419 100644 --- a/quantlib/instrument.pyx +++ b/quantlib/instrument.pyx @@ -1,5 +1,4 @@ """Abstract instrument class""" -from cython.operator cimport dereference as deref from quantlib.time.date cimport date_from_qldate from quantlib.pricingengines.engine cimport PricingEngine from quantlib.handle cimport static_pointer_cast diff --git a/quantlib/models/calibration_helper.pyx b/quantlib/models/calibration_helper.pyx index 0f1980c3f..d9df4863a 100644 --- a/quantlib/models/calibration_helper.pyx +++ b/quantlib/models/calibration_helper.pyx @@ -7,12 +7,9 @@ FOR A PARTICULAR PURPOSE. See the license for more details. """ -include '../types.pxi' - +from quantlib.types cimport Real, Size, Volatility from quantlib.handle cimport shared_ptr -from cython.operator cimport dereference as deref -cimport quantlib.pricingengines._pricing_engine as _pe from quantlib.pricingengines.engine cimport PricingEngine diff --git a/quantlib/pricingengines/_pricing_engine.pxd b/quantlib/pricingengines/_pricing_engine.pxd index b8c11558f..c1646407c 100644 --- a/quantlib/pricingengines/_pricing_engine.pxd +++ b/quantlib/pricingengines/_pricing_engine.pxd @@ -1,4 +1,4 @@ -cdef extern from 'ql/pricingengine.hpp' namespace 'QuantLib': +cdef extern from 'ql/pricingengine.hpp' namespace 'QuantLib' nogil: cdef cppclass PricingEngine: pass diff --git a/quantlib/pricingengines/credit/isda_cds_engine.pyx b/quantlib/pricingengines/credit/isda_cds_engine.pyx index d0159215d..151d3aee6 100644 --- a/quantlib/pricingengines/credit/isda_cds_engine.pyx +++ b/quantlib/pricingengines/credit/isda_cds_engine.pyx @@ -57,9 +57,8 @@ cdef class IsdaCdsEngine(PricingEngine): <_ice.ForwardsInCouponPeriod>forwards_in_coupon_period) ) - cdef _ice.IsdaCdsEngine* _get_cds_engine(self): - cdef _ice.IsdaCdsEngine* ref = <_ice.IsdaCdsEngine*>(self._thisptr.get()) - return ref + cdef inline _ice.IsdaCdsEngine* _get_cds_engine(self) nogil: + return <_ice.IsdaCdsEngine*>(self._thisptr.get()) @property def isda_rate_curve(self): diff --git a/quantlib/pricingengines/credit/midpoint_cds_engine.pyx b/quantlib/pricingengines/credit/midpoint_cds_engine.pyx index 0ce63b048..33219b923 100644 --- a/quantlib/pricingengines/credit/midpoint_cds_engine.pyx +++ b/quantlib/pricingengines/credit/midpoint_cds_engine.pyx @@ -9,7 +9,6 @@ from cython.operator cimport dereference as deref from libcpp cimport bool from quantlib.handle cimport Handle, shared_ptr, optional -cimport quantlib.pricingengines._pricing_engine as _pe from . cimport _midpoint_cds_engine as _mce from quantlib.pricingengines.engine cimport PricingEngine diff --git a/quantlib/pricingengines/engine.pxd b/quantlib/pricingengines/engine.pxd index 9f5310378..e69139a2d 100644 --- a/quantlib/pricingengines/engine.pxd +++ b/quantlib/pricingengines/engine.pxd @@ -3,5 +3,3 @@ cimport quantlib.pricingengines._pricing_engine as _pe cdef class PricingEngine: cdef shared_ptr[_pe.PricingEngine] _thisptr - - diff --git a/quantlib/pricingengines/swap.pyx b/quantlib/pricingengines/swap.pyx index 3c73c1314..3946a30c0 100644 --- a/quantlib/pricingengines/swap.pyx +++ b/quantlib/pricingengines/swap.pyx @@ -6,7 +6,6 @@ from quantlib.pricingengines.vanilla.vanilla cimport PricingEngine from quantlib.termstructures.yield_term_structure cimport YieldTermStructure cimport quantlib.pricingengines._swap as _swap -cimport quantlib.pricingengines._pricing_engine as _pe cimport quantlib.termstructures._yield_term_structure as _yts from quantlib.time.date cimport Date diff --git a/quantlib/pricingengines/vanilla/mceuropeanhestonengine.pyx b/quantlib/pricingengines/vanilla/mceuropeanhestonengine.pyx index 3ed19f44b..f52b9abe8 100644 --- a/quantlib/pricingengines/vanilla/mceuropeanhestonengine.pyx +++ b/quantlib/pricingengines/vanilla/mceuropeanhestonengine.pyx @@ -1,9 +1,7 @@ from quantlib.types cimport BigNatural, Integer, Real, Size from libcpp cimport bool -from cython.operator cimport dereference as deref from quantlib.handle cimport shared_ptr, static_pointer_cast from quantlib.utilities.null cimport Null -cimport quantlib.pricingengines._pricing_engine as _pe from quantlib.processes.heston_process cimport HestonProcess cimport quantlib.processes._heston_process as _hp from .mcvanillaengine cimport MCVanillaEngine