diff options
| -rw-r--r-- | pyisda/curve.pxd | 2 | ||||
| -rw-r--r-- | pyisda/curve.pyx | 42 |
2 files changed, 23 insertions, 21 deletions
diff --git a/pyisda/curve.pxd b/pyisda/curve.pxd index 2ca1030..61d55de 100644 --- a/pyisda/curve.pxd +++ b/pyisda/curve.pxd @@ -83,6 +83,8 @@ cdef extern from "isda/tcurve.h" nogil: int numPts, double basis, long dayCountConv) + TCurve* JpmcdsCopyCurve(TCurve* curve) + TDate* JpmcdsDatesFromCurve(TCurve* curve) cdef extern from "isda/cxzerocurve.h" nogil: double JpmcdsZeroPrice(TCurve* curve, TDate date) diff --git a/pyisda/curve.pyx b/pyisda/curve.pyx index 6224b77..15f48a5 100644 --- a/pyisda/curve.pyx +++ b/pyisda/curve.pyx @@ -15,17 +15,6 @@ cdef extern from "numpy/arrayobject.h": cdef int SUCCESS = 0 -cdef inline TCurve* copy(const TCurve* curve): - cdef TCurve* newcurve = <TCurve*>malloc(sizeof(TCurve)) - newcurve.fNumItems = curve.fNumItems - newcurve.fBaseDate = curve.fBaseDate - newcurve.fDayCountConv = curve.fDayCountConv - newcurve.fArray = <TRatePt*>malloc(sizeof(TRatePt) * curve.fNumItems) - newcurve.fBasis = curve.fBasis - memcpy(newcurve.fArray, <char*>curve.fArray, - sizeof(TRatePt) * curve.fNumItems) - return newcurve - cpdef public enum BadDay: FOLLOW = <long>'F' PREVIOUS = <long>'P' @@ -72,7 +61,7 @@ cdef class Curve(object): def __deepcopy__(self, memo): cdef Curve sc = type(self).__new__(type(self)) - sc._thisptr = copy(self._thisptr) + sc._thisptr = JpmcdsCopyCurve(self._thisptr) return sc @property @@ -82,12 +71,23 @@ cdef class Curve(object): t1 = 0 h1 = 0 cdef double* data = <double*>malloc(self._thisptr.fNumItems * sizeof(double)) - for i in range(self._thisptr.fNumItems): - h2 = log1p(self._thisptr.fArray[i].fRate) - t2 = (self._thisptr.fArray[i].fDate - self._thisptr.fBaseDate)/365. - data[i] = (h2 * t2 - h1 * t1) / (t2 - t1) - h1 = h2 - t1 = t2 + if <Basis>self._thisptr.fBasis == Basis.CONTINUOUS: + for i in range(self._thisptr.fNumItems): + h2 = self._thisptr.fArray[i].fRate + t2 = (self._thisptr.fArray[i].fDate - self._thisptr.fBaseDate)/365. + data[i] = (h2 * t2 - h1 * t1) / (t2 - t1) + h1 = h2 + t1 = t2 + elif <Basis>self._thisptr.fBasis == Basis.ANNUAL_BASIS: + for i in range(self._thisptr.fNumItems): + h2 = log1p(self._thisptr.fArray[i].fRate) + t2 = (self._thisptr.fArray[i].fDate - self._thisptr.fBaseDate)/365. + data[i] = (h2 * t2 - h1 * t1) / (t2 - t1) + h1 = h2 + t1 = t2 + else: + raise ValueError("Can only convert CONTINUOUS and ANNUAL_BASIS") + cdef np.ndarray[np.float64_t] out = \ np.PyArray_SimpleNewFromData(1, &shape, np.NPY_DOUBLE, data) PyArray_ENABLEFLAGS(out, np.NPY_OWNDATA) @@ -396,7 +396,7 @@ cdef class SpreadCurve(Curve): cdef int num_items = self._thisptr.fNumItems if not inplace: sc = SpreadCurve.__new__(SpreadCurve) - sc._thisptr = copy(self._thisptr) + sc._thisptr = JpmcdsCopyCurve(self._thisptr) t1 = 0 h1 = 0 @@ -406,7 +406,7 @@ cdef class SpreadCurve(Curve): if mask.size != self._thisptr.fNumItems: raise ValueError("mask size need to be the same as the number of Items") for i in range(num_items): - h2 = log1p(self._thisptr.fArray[i].fRate) + h2 = self._thisptr.fArray[i].fRate t2 = T[i] = (self._thisptr.fArray[i].fDate - self._thisptr.fBaseDate)/365. h[i] = (h2 * t2 - h1 * t1) / (t2 - t1) if mask is None or mask[i]: @@ -423,7 +423,7 @@ cdef class SpreadCurve(Curve): for i in range(self._thisptr.fNumItems): c += (T[i] - t1) * h[i] - update[i].fRate = expm1(c/T[i]) + update[i].fRate = c/T[i] t1 = T[i] free(h) free(T) |
