From 1318b80b281814c4b3ba9c33972be9124c5c469d Mon Sep 17 00:00:00 2001 From: Guillaume Horel Date: Fri, 12 May 2017 14:04:21 -0400 Subject: allow to return forward rates --- pyisda/curve.pyx | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/pyisda/curve.pyx b/pyisda/curve.pyx index cb20d63..60f7bb4 100644 --- a/pyisda/curve.pyx +++ b/pyisda/curve.pyx @@ -15,6 +15,7 @@ cimport cython cimport numpy as np import numpy as np np.import_array() +import pandas as pd cdef extern from "numpy/arrayobject.h": void PyArray_ENABLEFLAGS(np.ndarray arr, int flags) @@ -83,15 +84,28 @@ cdef class Curve(object): 'data': fArray_to_list(self._thisptr.get().fArray, self._thisptr.get().fNumItems)} @cython.boundscheck(False) - def to_series(self): + def to_series(self, bint forward=True): cdef np.npy_intp n = self._thisptr.get().fNumItems cdef np.ndarray[np.float64_t,ndim=1] h = np.PyArray_EMPTY(1, &n, np.NPY_DOUBLE, 0) cdef np.ndarray[np.int64_t,ndim=1] d = np.PyArray_EMPTY(1, &n, np.NPY_INT64, 0) cdef size_t i cdef TRatePt* it = self._thisptr.get().fArray - for i in range(n): - h[i] = it[i].fRate - d[i] = it[i].fDate - 134774 + cdef double t1, h1, t2, h2 + t1 = 0 + h1 = 0 + cdef base_date = self._thisptr.get().fBaseDate + if forward: + for i in range(n): + h2 = it[i].fRate + t2 = (it[i].fDate - base_date)/365. + h[i] = (h2 * t2 - h1 * t1) / (t2 - t1) + d[i] = it[i].fDate -134774 + h1 = h2 + t1 = t2 + else: + for i in range(n): + h[i] = it[i].fRate + d[i] = it[i].fDate -134774 return pd.Series(h, index=d.view('M8[D]'), name='hazard_rates') def __iter__(self): -- cgit v1.2.3-70-g09d2