summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGuillaume Horel <guillaume.horel@gmail.com>2017-12-12 11:16:11 -0500
committerGuillaume Horel <guillaume.horel@gmail.com>2017-12-12 11:16:11 -0500
commit94d164973708051270527355b62d242698640873 (patch)
tree5b631ecf329392ed2b857a2456ec02a9041bf321
parent0b3e1597946065b59b567050adb8082ea5b9421c (diff)
downloadpyisda-94d164973708051270527355b62d242698640873.tar.gz
Fixes to CreditIndex
- switch to map for tickers (order is guaranteed) - add tickers methods, and return list of tickers in survival_matrix
-rw-r--r--pyisda/credit_index.pxd4
-rw-r--r--pyisda/credit_index.pyx36
2 files changed, 26 insertions, 14 deletions
diff --git a/pyisda/credit_index.pxd b/pyisda/credit_index.pxd
index 56267f9..e3e1de1 100644
--- a/pyisda/credit_index.pxd
+++ b/pyisda/credit_index.pxd
@@ -2,7 +2,7 @@ from legs cimport TContingentLeg, TFeeLeg
from date cimport TDate
from curve cimport TCurve, TRatePt, shared_ptr
from libcpp.vector cimport vector
-from libcpp.unordered_map cimport unordered_map
+from libcpp.map cimport map
from libcpp.string cimport string
cdef class CurveList:
@@ -10,7 +10,7 @@ cdef class CurveList:
cdef vector[double] _weights
cdef vector[double] T
cdef vector[shared_ptr[TCurve]] _curves
- cdef unordered_map[string, size_t] tickers
+ cdef map[string, size_t] tickers
cdef vector[shared_ptr[double]] recovery_rates
cdef class CreditIndex(CurveList):
diff --git a/pyisda/credit_index.pyx b/pyisda/credit_index.pyx
index d25e05a..6731ca9 100644
--- a/pyisda/credit_index.pyx
+++ b/pyisda/credit_index.pyx
@@ -52,7 +52,7 @@ cdef class CurveList:
cdef:
SpreadCurve sc
size_t i
- unordered_map[string, size_t].iterator it
+ map[string, size_t].iterator it
size_t n = len(curves)
if isinstance(curves[0], SpreadCurve):
@@ -91,7 +91,7 @@ cdef class CurveList:
def __getitem__(self, str ticker):
cdef:
string ticker_cpp = ticker.encode()
- unordered_map[string, size_t].iterator got = \
+ map[string, size_t].iterator got = \
self.tickers.find(ticker_cpp)
SpreadCurve sc
@@ -120,13 +120,23 @@ cdef class CurveList:
@property
def weights(self):
cdef np.npy_intp shape = self._weights.size()
- cdef np.ndarray out = \
+ cdef np.ndarray[np.float64_t, ndim=1] out = \
np.PyArray_SimpleNewFromData(1, &shape, np.NPY_DOUBLE, self._weights.data())
out.base = <PyObject*>self
Py_INCREF(self)
return out
@property
+ def tickers(self):
+ cdef np.npy_intp shape = self.tickers.size()
+ cdef pair[string, size_t] p
+ cdef np.ndarray out = np.PyArray_EMPTY(1, &shape, np.NPY_OBJECT, 1)
+ for p in self.tickers:
+ out[p.second] = p.first.decode('utf-8')
+ return out
+
+
+ @property
def curves(self):
"""returns the list of curves inside the porfolio.
@@ -150,7 +160,7 @@ cdef class CurveList:
size_t len_l = len(l)
size_t i
SpreadCurve sc
- unordered_map[string, size_t].iterator it
+ map[string, size_t].iterator it
self._curves.clear()
self.tickers.clear()
@@ -310,7 +320,6 @@ cdef class CreditIndex(CurveList):
h = Hash64WithSeed(<char*>self._weights.data(), sizeof(double) * self._weights.size(), h)
return h
-
#@cython.initializedcheck(False)
def pv_vec(self, step_in_date, value_date, YieldCurve yc, double recovery_rate):
cdef:
@@ -483,17 +492,20 @@ cdef class CreditIndex(CurveList):
def survival_matrix(self, TDate[:] schedule, d1=None):
cdef:
shared_ptr[TCurve] sc
+ pair[string, size_t] p
TDate start_date
- size_t i, j
+ size_t i
np.npy_intp[2] n = [self._curves.size(), schedule.shape[0]]
- np.ndarray[np.float64_t,ndim=2] sp = np.PyArray_EMPTY(2, n, np.NPY_DOUBLE, 1)
- j = 0
- for sc in self._curves:
+ np.ndarray[np.float64_t, ndim=2] sp = np.PyArray_EMPTY(2, n, np.NPY_DOUBLE, 1)
+ np.ndarray tickers = np.PyArray_EMPTY(1, n, np.NPY_OBJECT, 1)
+
+ for p in self.tickers:
+ sc = self._curves[p.second]
+ tickers[p.second] = p.first.decode('utf-8')
start_date = sc.get().fBaseDate
for i in range(n[1]):
- sp[j,i] = JpmcdsForwardZeroPrice(sc.get(), start_date, schedule[i])
- j += 1
- return sp
+ sp[p.second, i] = JpmcdsForwardZeroPrice(sc.get(), start_date, schedule[i])
+ return sp, tickers
cdef unsigned long fill_mask(const TDate maturity, const vector[TDate]& maturities,
const shared_ptr[TCurve]& sc) nogil: