diff options
| -rw-r--r-- | pyisda/credit_index.pxd | 4 | ||||
| -rw-r--r-- | pyisda/credit_index.pyx | 44 |
2 files changed, 25 insertions, 23 deletions
diff --git a/pyisda/credit_index.pxd b/pyisda/credit_index.pxd index 3da57f9..e4b2d37 100644 --- a/pyisda/credit_index.pxd +++ b/pyisda/credit_index.pxd @@ -16,7 +16,3 @@ cdef class CreditIndex(CurveList): cdef vector[TDate] maturities cdef TContingentLeg** contingent_legs cdef TFeeLeg** fee_legs - -cdef extern from "<algorithm>" namespace "std" nogil: - Iter find[Iter, T](Iter first, Iter last, const T& val) - Iter lower_bound[Iter, T](Iter first, Iter last, const T& value) diff --git a/pyisda/credit_index.pyx b/pyisda/credit_index.pyx index e8db882..66c475a 100644 --- a/pyisda/credit_index.pyx +++ b/pyisda/credit_index.pyx @@ -1,4 +1,5 @@ from libc.stdlib cimport malloc, free +from libc.math cimport nan from libcpp.pair cimport pair from cython.operator cimport dereference as deref @@ -163,8 +164,6 @@ cdef class CreditIndex(CurveList): TCurve* sc pair[string,shared_ptr[TCurve]] p - FeeLegAI(self.fee_legs[0], self.base_date, &accrued) - for maturity in self.maturities: d[j] = maturity - 134774 j += 1 @@ -234,7 +233,7 @@ cdef class CreditIndex(CurveList): return r def theta(self, step_in_date, value_date, maturity, YieldCurve yc not None, - double recovery_rate, double fixed_rate): + double recovery_rate, double fixed_rate, double old_pv=nan("")): cdef: TDate step_in_date_c = pydate_to_TDate(step_in_date) TDate value_date_c = pydate_to_TDate(value_date) @@ -245,11 +244,12 @@ cdef class CreditIndex(CurveList): self.contingent_legs, self.fee_legs, self.maturities) + if old_pv != old_pv: + old_pv = pv(self.curves, self.base_date, step_in_date_c, + value_date_c, yc._thisptr.get(), legs, + recovery_rate, fixed_rate, self.weights, + 0., h, self.T, 0) - cdef double old_pv = pv(self.curves, self.base_date, step_in_date_c, - value_date_c, yc._thisptr.get(), legs, - recovery_rate, fixed_rate, self.weights, - 0., h, self.T, 0) maturity_c = pydate_to_TDate(maturity - relativedelta(years=1)) legs = get_legs(maturity_c, self.start_date, @@ -323,22 +323,28 @@ cdef unsigned long fill_mask(const TDate maturity, const vector[TDate]& maturiti const shared_ptr[TCurve]& sc) nogil: cdef: TDate prev_maturity = 0 + TDate next_maturity = maturity size_t i - vector[TDate].const_iterator index = lower_bound(maturities.const_begin(), - maturities.const_end(), - maturity) + TRatePt* it = sc.get().fArray unsigned long mask = 0 - if index == maturities.end(): - return 0 - elif index != maturities.begin(): - prev_maturity = deref(index-1) - cdef TRatePt* it = sc.get().fArray - for i in range(sc.get().fNumItems): - if (it[i].fDate <= maturity ) and \ - (it[i].fDate > prev_maturity): - mask |= 1 << i + for i in range(maturities.size()): + if maturities[i] == maturity: + break + else: + return mask + + if i > 0: + prev_maturity = maturities[i-1] + i = 0 + while it[i].fDate < prev_maturity: + i += 1 + i += 1 + while it[i].fDate < maturity: + mask |= 1 << i + i += 1 + mask |= 1 << i return mask cdef pair[TContingentLeg,TFeeLeg] get_legs(TDate maturity, |
