diff options
| -rw-r--r-- | pyisda/credit_index.pyx | 91 | ||||
| -rw-r--r-- | setup.py | 30 |
2 files changed, 69 insertions, 52 deletions
diff --git a/pyisda/credit_index.pyx b/pyisda/credit_index.pyx index f058839..45bcc54 100644 --- a/pyisda/credit_index.pyx +++ b/pyisda/credit_index.pyx @@ -6,6 +6,7 @@ from libcpp.pair cimport pair from libcpp.memory cimport unique_ptr from cython.operator cimport dereference as deref from cpython cimport PyObject, Py_INCREF +from cython.parallel cimport prange cimport cython from legs cimport (JpmcdsCdsContingentLegMake, JpmcdsCdsFeeLegMake, @@ -506,12 +507,13 @@ cdef class CreditIndex(CurveList): cdef: TDate maturity_c = pydate_to_TDate(maturity) unsigned long mask = fill_mask(maturity_c, self._maturities, self._curves[0]) - shared_ptr[TCurve] sc - TCurve* sc_orig, *sc_copy + TCurve* sc_orig + TCurve *sc_copy size_t i if inplace: - for sc in self._curves: - tweak_curve(sc.get(), sc.get(), epsilon, mask) + for i in range(self._curves.size()): + sc_orig = self._curves[i].get() + tweak_curve(sc_orig, sc_orig, epsilon, mask) else: for i in range(self._curves.size()): sc_orig = self._curves[i].get() @@ -599,7 +601,7 @@ cdef pair[TContingentLeg_ptr, TFeeLeg_ptr] get_legs(TDate maturity, True) return r -cdef double pv(vector[shared_ptr[TCurve]]& curves, +cdef double pv(const vector[shared_ptr[TCurve]]& curves, TDate base_date, TDate step_in_date, TDate cash_settle_date, @@ -612,40 +614,53 @@ cdef double pv(vector[shared_ptr[TCurve]]& curves, unsigned long mask) nogil: cdef: double fl_pv, cl_pv, r = 0 - TCurve* tweaked_curve - shared_ptr[TCurve] c + TCurve *tweaked_curve + TCurve *orig_curve size_t i = 0 double recovery_rate - if epsilon != 0.: - tweaked_curve = JpmcdsCopyCurve(curves[0].get()) - - for c in curves: - if epsilon != 0.: - tweak_curve(c.get(), tweaked_curve, epsilon, mask) - else: - tweaked_curve = c.get() - # TODO: pick the actual recovery on the curve - # this only works for flat recovery curve - recovery_rate = recovery_rates[i].get()[0] - JpmcdsContingentLegPV(legs.first, - base_date, - cash_settle_date, - step_in_date, - yc, - tweaked_curve, - recovery_rate, - &cl_pv) - - JpmcdsFeeLegPV(legs.second, - base_date, - step_in_date, - cash_settle_date, - yc, - tweaked_curve, - True, - &fl_pv) - - r += weights[i] * (cl_pv - fl_pv * fixed_rate) - i += 1 + if epsilon == 0.: + for i in prange(curves.size()): + tweaked_curve = curves[i].get() + recovery_rate = recovery_rates[i].get()[0] + JpmcdsContingentLegPV(legs.first, + base_date, + cash_settle_date, + step_in_date, + yc, + tweaked_curve, + recovery_rate, + &cl_pv) + JpmcdsFeeLegPV(legs.second, + base_date, + step_in_date, + cash_settle_date, + yc, + tweaked_curve, + True, + &fl_pv) + r += weights[i] * (cl_pv - fl_pv * fixed_rate) + else: + tweaked_curve = JpmcdsCopyCurve(curves[0].get()) + for i in prange(curves.size()): + orig_curve = curves[i].get() + tweak_curve(orig_curve, tweaked_curve, epsilon, mask) + recovery_rate = recovery_rates[i].get()[0] + JpmcdsContingentLegPV(legs.first, + base_date, + cash_settle_date, + step_in_date, + yc, + tweaked_curve, + recovery_rate, + &cl_pv) + JpmcdsFeeLegPV(legs.second, + base_date, + step_in_date, + cash_settle_date, + yc, + tweaked_curve, + True, + &fl_pv) + r += weights[i] * (cl_pv - fl_pv * fixed_rate) return r @@ -4,22 +4,24 @@ from Cython.Build import cythonize import numpy all_extensions = Extension("*", ["pyisda/*.pyx"], - include_dirs = ['c_layer', numpy.get_include()], - libraries = ["cds", "farmhash"], - language = 'c++') + include_dirs=['c_layer', numpy.get_include()], + libraries=["cds", "farmhash"], + language='c++', + extra_compile_args=['-fopenmp'], + extra_link_args=['-fopenmp', '-Wl,--strip-all']) c_extension = Extension("pyisda.flat_hazard", - include_dirs = ['c_layer', numpy.get_include()], - sources = ['pyisda/flat_hazard.pyx', 'c_layer/cdsbootstrap.c'], - libraries = ['cds'], - language = 'c++') + include_dirs=['c_layer', numpy.get_include()], + sources=['pyisda/flat_hazard.pyx', 'c_layer/cdsbootstrap.c'], + libraries=['cds'], + language='c++') -all_extensions = cythonize([c_extension, all_extensions], nthreads = 4, - compiler_directives={'embedsignature':True}) +all_extensions = cythonize([c_extension, all_extensions], nthreads=4, + compiler_directives={'embedsignature': True}) setup( - name = "pyisda", - version = '0.1', - author = 'Guillaume Horel', - ext_modules = all_extensions, - packages = ['pyisda']) + name="pyisda", + version='0.1', + author='Guillaume Horel', + ext_modules=all_extensions, + packages=['pyisda']) |
