From b4128a6906e1d42946e8b1152ad87f7028209688 Mon Sep 17 00:00:00 2001 From: Guillaume Horel Date: Wed, 15 Feb 2017 17:01:44 -0500 Subject: better typing --- pyisda/curve.pyx | 34 +++++++++++++++++++++------------- 1 file changed, 21 insertions(+), 13 deletions(-) diff --git a/pyisda/curve.pyx b/pyisda/curve.pyx index 46dac79..1afb9f9 100644 --- a/pyisda/curve.pyx +++ b/pyisda/curve.pyx @@ -38,11 +38,17 @@ cdef class Curve(object): self._thisptr.fDayCountConv) def __setstate__(self, state): + cdef: + int num_items + char* rates + TDate base_date + double basis + long dcc num_items, rates, base_date, basis, dcc = state self._thisptr = malloc(sizeof(TCurve)) self._thisptr.fNumItems = num_items self._thisptr.fArray = malloc(sizeof(TRatePt) * num_items) - memcpy(self._thisptr.fArray, rates, sizeof(TRatePt) * num_items) + memcpy(self._thisptr.fArray, rates, sizeof(TRatePt) * num_items) self._thisptr.fBaseDate = base_date self._thisptr.fBasis = basis self._thisptr.fDayCountConv = dcc @@ -61,13 +67,13 @@ cdef class Curve(object): 'day_count_convention': dcc_tostring(self._thisptr.fDayCountConv), 'data': fArray_to_list(self._thisptr.fArray, self._thisptr.fNumItems)} - def __deepcopy__(self, memo): cdef Curve sc = type(self).__new__(type(self)) sc._thisptr = JpmcdsCopyCurve(self._thisptr) return sc @property + @cython.cdivision(True) def forward_hazard_rates(self): cdef double t1, h1, t2, h2 cdef np.npy_intp shape = self._thisptr.fNumItems @@ -170,8 +176,9 @@ cdef class YieldCurve(Curve): char* routine = 'zerocurve' TDate value_date = pydate_to_TDate(date) - self._dates = malloc(len(periods) * sizeof(TDate)) self._ninstr = len(periods) + self._dates = malloc(self._ninstr * sizeof(TDate)) + cdef TDate settle_date if JpmcdsDateFromBusDaysOffset(value_date, 2, "None", &settle_date)!= SUCCESS: @@ -186,16 +193,16 @@ cdef class YieldCurve(Curve): if types[i] == 'M': period_adjust = MODIFIED else: - priod_adjust = NONE + period_adjust = NONE if JpmcdsDateFwdThenAdjust(settle_date, &tmp, period_adjust, "None", &self._dates[i]) != SUCCESS: raise ValueError('Invalid interval') - fixed_bytes = fixed_swap_period.encode('utf-8') - float_bytes = float_swap_period.encode('utf-8') - types_bytes = types.encode('utf-8') + cdef bytes fixed_bytes = fixed_swap_period.encode('utf-8') + cdef bytes float_bytes = float_swap_period.encode('utf-8') + cdef bytes types_bytes = types.encode('utf-8') - if JpmcdsStringToDateInterval(fixed_bytes, routine, &ivl) != SUCCESS: + if JpmcdsStringToDateInterval(fixed_bytes, routine, &ivl) != SUCCESS: raise ValueError if JpmcdsDateIntervalToFreq(&ivl, &fixed_freq) != SUCCESS: raise ValueError @@ -206,7 +213,7 @@ cdef class YieldCurve(Curve): self._thisptr = JpmcdsBuildIRZeroCurve( value_date, types_bytes, self._dates, - &rates[0], len(periods), dcc(mm_dcc), fixed_freq, + &rates[0], self._ninstr, dcc(mm_dcc), fixed_freq, float_freq, dcc(fixed_swap_dcc), dcc(float_swap_dcc), bad_day_conv, b"None" ) @@ -233,10 +240,10 @@ cdef class YieldCurve(Curve): """ build a yield curve from a list of discount factors """ cdef TDate base_date_c = pydate_to_TDate(base_date) cdef YieldCurve yc = cls.__new__(cls) - yc._dates = malloc(sizeof(TDate) * len(dates)) - cdef size_t i - cdef double* rates = malloc(sizeof(double) * len(dfs)) yc._ninstr = len(dates) + yc._dates = malloc(sizeof(TDate) * yc._ninstr) + cdef size_t i + cdef double* rates = malloc(sizeof(double) * yc._ninstr) for i, d in enumerate(dates): yc._dates[i] = pydate_to_TDate(d) JpmcdsDiscountToRateYearFrac(dfs[i], (yc._dates[i]-base_date_c)/365., @@ -375,8 +382,9 @@ cdef class SpreadCurve(Curve): basis, dcc(day_count_conv)) return sc + @cython.cdivision(True) def tweak_curve(self, double epsilon, bint multiplicative=True, - bint[:] mask=None, inplace=True): + bint[:] mask=None, bint inplace=False): """ Tweak the survival curve in place. -- cgit v1.2.3-70-g09d2