summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--pyisda/curve.pyx43
1 files changed, 19 insertions, 24 deletions
diff --git a/pyisda/curve.pyx b/pyisda/curve.pyx
index 0d35aa5..b903281 100644
--- a/pyisda/curve.pyx
+++ b/pyisda/curve.pyx
@@ -465,7 +465,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,
- str ticker=None):
+ str ticker=None, bint fill_curve=True):
cdef TDate today_c = pydate_to_TDate(today)
cdef TDate step_in_date_c
@@ -491,6 +491,7 @@ cdef class SpreadCurve(Curve):
cdef TDate* end_dates_c = NULL
cdef double* tenors_c = NULL
cdef TCurve* curve = NULL
+ cdef TCurve* new_curve = NULL
cdef unsigned int includes = 0
cdef size_t i
cdef bint freeup = False
@@ -551,7 +552,13 @@ cdef class SpreadCurve(Curve):
if curve is NULL:
raise ValueError("Didn't init the survival curve properly")
else:
- self._thisptr = make_shared(curve)
+ if fill_curve and curve.fNumItems != n_dates:
+ new_curve = _fill_curve(curve, end_dates_c, n_dates)
+ self._thisptr = make_shared(new_curve)
+ JpmcdsFreeTCurve(curve)
+ curve = new_curve
+ else:
+ self._thisptr = make_shared(curve)
self.recovery_rates = shared_ptr[double](
<double*>malloc(curve.fNumItems * sizeof(double)),
double_free)
@@ -774,30 +781,18 @@ cdef class SpreadCurve(Curve):
self.recovery_rates.get())
return out
-@cython.boundscheck(False)
-@cython.wraparound(False)
-@cython.initializedcheck(False)
@cython.cdivision(True)
-def fill_curve(SpreadCurve sc, TDate[:] end_dates):
-
- cdef size_t n_dates = end_dates.shape[0]
- cdef size_t i
- cdef TDate base_date = sc._thisptr.get().fBaseDate
- cdef double t
- cdef TCurve* curve = JpmcdsNewTCurve(sc._thisptr.get().fBaseDate,
- n_dates,
- 5000.,
- 2)
- cdef TRatePt* it = curve.fArray
+@cython.boundscheck(False)
+cdef TCurve* _fill_curve(TCurve* sc, TDate* end_dates, int n_dates):
+ cdef:
+ size_t i
+ TDate base_date = sc.fBaseDate
+ double t
+ TCurve* curve = JpmcdsNewTCurve(base_date, n_dates, 5000., 2)
+ TRatePt* it = curve.fArray
for i in range(n_dates):
t = (end_dates[i] - base_date)/365.
it[i].fDate = end_dates[i]
- it[i].fRate = -log(JpmcdsForwardZeroPrice(sc._thisptr.get(), base_date,
- end_dates[i])) / t
-
- cdef SpreadCurve sc_new = SpreadCurve.__new__(SpreadCurve)
- sc_new._thisptr = make_shared(curve)
- sc_new.ticker = sc.ticker
- sc_new.recovery_rates = sc.recovery_rates
- return sc_new
+ it[i].fRate = -log(JpmcdsForwardZeroPrice(sc, base_date, end_dates[i])) / t
+ return curve