diff options
| author | Guillaume Horel <guillaume.horel@gmail.com> | 2017-05-17 14:09:43 -0400 |
|---|---|---|
| committer | Guillaume Horel <guillaume.horel@gmail.com> | 2017-05-17 14:09:43 -0400 |
| commit | 80c57d8132f43cb734442aa2e852413ef7658c19 (patch) | |
| tree | 8e39d519336fb77bc2f9c0d6f42feea00080c36f | |
| parent | 9e70d178a3db4569e3ff8307ebe9e0bdfdd8b3b5 (diff) | |
| download | pyisda-80c57d8132f43cb734442aa2e852413ef7658c19.tar.gz | |
Various tweaks
- switch to bitset for mask
- add couple nogil
| -rw-r--r-- | pyisda/cdsone.pxd | 4 | ||||
| -rw-r--r-- | pyisda/curve.pxd | 5 | ||||
| -rw-r--r-- | pyisda/curve.pyx | 24 |
3 files changed, 16 insertions, 17 deletions
diff --git a/pyisda/cdsone.pxd b/pyisda/cdsone.pxd index b24d3b4..0f446ba 100644 --- a/pyisda/cdsone.pxd +++ b/pyisda/cdsone.pxd @@ -1,13 +1,13 @@ from date cimport TDateInterval from curve cimport TCurve -cdef extern from "isda/stub.h": +cdef extern from "isda/stub.h" nogil: ctypedef struct TStubMethod: pass int JpmcdsStringToStubMethod(char* name, TStubMethod* stub) -cdef extern from "isda/cdsone.h": +cdef extern from "isda/cdsone.h" nogil: ctypedef int TBoolean diff --git a/pyisda/curve.pxd b/pyisda/curve.pxd index 0937eed..14c881c 100644 --- a/pyisda/curve.pxd +++ b/pyisda/curve.pxd @@ -2,6 +2,7 @@ from cdsone cimport TStubMethod from legs cimport TContingentLeg, TFeeLeg from libcpp.vector cimport vector from libcpp cimport bool +from libcpp.string cimport string cdef extern from "isda/zerocurve.h" nogil: ctypedef int TBoolean @@ -162,9 +163,9 @@ cdef class YieldCurve(Curve): cdef size_t _ninstr cdef class SpreadCurve(Curve): - cdef public str ticker + cdef string ticker cdef fArray_to_list(TRatePt* fArray, int fNumItems) cdef void tweak_curve(TCurve* sc, TCurve* sc_tweaked, double epsilon, - vector[double]& h, const vector[double]& T, bint* mask) + vector[double]& h, const vector[double]& T, unsigned long mask) nogil diff --git a/pyisda/curve.pyx b/pyisda/curve.pyx index c9ce6f7..23e9fad 100644 --- a/pyisda/curve.pyx +++ b/pyisda/curve.pyx @@ -343,7 +343,7 @@ cdef class YieldCurve(Curve): @cython.cdivision(True) cdef void tweak_curve(TCurve* sc, TCurve* sc_tweaked, double epsilon, - vector[double]& h, const vector[double]& T, bint* mask): + vector[double]& h, const vector[double]& T, unsigned long mask) nogil: ## We want to tweak in the forward space, so we convert the hazard rates ## into forward rates and then back cdef double h1, h2, t1, t2 @@ -353,7 +353,7 @@ cdef void tweak_curve(TCurve* sc, TCurve* sc_tweaked, double epsilon, h2 = sc.fArray[i].fRate t2 = T[i] h[i] = (h2 * t2 - h1 * t1) / (t2 - t1) - if mask is NULL or (mask is not NULL and mask[i]): + if mask == 0 or (mask >> i) & 1: h[i] *= (1 + epsilon) h1 = h2 t1 = t2 @@ -390,7 +390,7 @@ cdef class SpreadCurve(Curve): cash_settle_date, end_dates, double[:] coupon_rates, double[:] upfront_rates, double[:] recovery_rates, bint pay_accrued_on_default=True, - ticker=None): + str ticker=None): cdef TDate today_c = pydate_to_TDate(today) cdef TDate step_in_date_c = pydate_to_TDate(step_in_date) @@ -446,7 +446,7 @@ cdef class SpreadCurve(Curve): raise ValueError("Didn't init the survival curve properly") else: self._thisptr = make_shared(curve) - self.ticker = ticker + self.ticker = ticker.encode() survival_probability = Curve.__forward_zero_price @@ -485,7 +485,7 @@ cdef class SpreadCurve(Curve): @cython.boundscheck(False) def tweak_curve(self, double epsilon, bint multiplicative=True, - bint[::1] mask=None, bint inplace=False): + unsigned long mask=0, bint inplace=False): """ Tweak the survival curve in place. @@ -506,7 +506,6 @@ cdef class SpreadCurve(Curve): vector[double] h vector[double] T size_t i - bint* mask_ptr if not inplace: sc = SpreadCurve.__new__(SpreadCurve) @@ -521,13 +520,8 @@ cdef class SpreadCurve(Curve): for i in range(num_items): T[i] = (curve_tweaked.fArray[i].fDate - curve_tweaked.fBaseDate) / 365. - if mask is not None: - if mask.shape[0] != self._thisptr.get().fNumItems: - raise ValueError("mask size need to be the same as the number of Items") - mask_ptr = &mask[0] - else: - mask_ptr = NULL - tweak_curve(self._thisptr.get(), curve_tweaked, epsilon, h, T, mask_ptr) + if mask != 0: + tweak_curve(self._thisptr.get(), curve_tweaked, epsilon, h, T, mask) return sc @cython.boundscheck(False) @@ -575,6 +569,10 @@ cdef class SpreadCurve(Curve): free(par_spreads) return r + @property + def ticker(self): + return self.ticker.decode() + @cython.boundscheck(False) @cython.wraparound(False) @cython.initializedcheck(False) |
