diff options
| -rw-r--r-- | cpp_layer/curve.hpp | 77 | ||||
| -rw-r--r-- | pyisda/credit_index.pxd | 3 | ||||
| -rw-r--r-- | pyisda/curve.pxd | 27 | ||||
| -rw-r--r-- | pyisda/curve.pyx | 6 | ||||
| -rw-r--r-- | setup.py | 2 |
5 files changed, 17 insertions, 98 deletions
diff --git a/cpp_layer/curve.hpp b/cpp_layer/curve.hpp deleted file mode 100644 index 5d1720d..0000000 --- a/cpp_layer/curve.hpp +++ /dev/null @@ -1,77 +0,0 @@ -#include <isda/tcurve.h> -#include <isda/cxzerocurve.h> -#include <vector> -#include <memory> - -typedef long TDate; - -class CurveObject { -public: - CurveObject(TDate baseDate, std::vector<TDate> dates, std::vector<double> rates, - double basis, long dayCountConv) { - _ptr = JpmcdsMakeTCurve(baseDate, dates.data(), rates.data(), dates.size(), - basis, dayCountConv); - } - CurveObject(TCurve* ptr) { - _ptr = ptr; - } - CurveObject(const CurveObject& other) { - _ptr = JpmcdsCopyCurve(other._ptr); - } - CurveObject(CurveObject&& other) : _ptr(other._ptr) { - other._ptr = nullptr; - } - CurveObject& operator=(const CurveObject& other) { - if( this != &other) { - JpmcdsFreeTCurve(_ptr); - _ptr = JpmcdsCopyCurve(other._ptr); - } - return *this; - } - CurveObject& operator=(CurveObject&& other) { - if( this != &other) { - JpmcdsFreeTCurve(_ptr); - _ptr = other._ptr; - other._ptr = nullptr; - } - return *this; - } - - TCurve* data() { - return _ptr; - } - typedef TRatePt* iterator; - typedef const TRatePt* const_iterator; - iterator begin() { return _ptr->fArray; } - iterator end() {return &_ptr->fArray[_ptr->fNumItems];} - int size() { - return _ptr->fNumItems; - } - TDate BaseDate() { - return _ptr->fBaseDate; - } - double Basis() { - return _ptr->fBasis; - } - double ForwardZeroPrice(TDate d2, TDate d1); - double ForwardZeroPrice(TDate d2); - ~CurveObject() { - if(_ptr != nullptr) { - JpmcdsFreeTCurve(_ptr); - } - } -private: - TCurve* _ptr; -}; - -double CurveObject::ForwardZeroPrice(TDate d2, TDate d1) { - return JpmcdsForwardZeroPrice(_ptr, d1, d2); -} - -double CurveObject::ForwardZeroPrice(TDate d2) { - return JpmcdsForwardZeroPrice(_ptr, _ptr->fBaseDate, d2); -} - -std::shared_ptr<TCurve>& make_shared(TCurve* ptr) { - return std::shared_ptr<TCurve>(ptr, jpmcdsFreeTCurve); -} diff --git a/pyisda/credit_index.pxd b/pyisda/credit_index.pxd index e4391e9..ed0d0b1 100644 --- a/pyisda/credit_index.pxd +++ b/pyisda/credit_index.pxd @@ -1,7 +1,6 @@ from legs cimport TContingentLeg, TFeeLeg from date cimport TDate -from curve cimport CurveObject, TCurve, TRatePt -from libcpp.memory cimport shared_ptr +from curve cimport TCurve, TRatePt, shared_ptr from libcpp.vector cimport vector cdef class CurveList: diff --git a/pyisda/curve.pxd b/pyisda/curve.pxd index d76a2af..50d9784 100644 --- a/pyisda/curve.pxd +++ b/pyisda/curve.pxd @@ -1,7 +1,7 @@ from cdsone cimport TStubMethod from legs cimport TContingentLeg, TFeeLeg -from libcpp.memory cimport shared_ptr from libcpp.vector cimport vector +from libcpp cimport bool cdef extern from "isda/zerocurve.h" nogil: ctypedef int TBoolean @@ -93,20 +93,6 @@ cdef extern from "isda/tcurve.h" nogil: double basis, # (I) Compounding periods/year long dayCountConv) -cdef extern from "../cpp_layer/curve.hpp" nogil: - cdef cppclass CurveObject: - CurveObject(TDate baseDate, vector[TDate] dates, vector[double] rates, - double basis, long dayCountConv) - TCurve* data() - double ForwardZeroPrice(TDate d2) - double ForwardZeroPrice(TDate d2, TDate d1) - int size() - TDate BaseDate() - ctypedef TRatePt* iterator - iterator begin() - iterator end() - shared_ptr[TCurve]& make_shared(TCurve* ptr) - cdef extern from "isda/cxzerocurve.h" nogil: double JpmcdsZeroPrice(TCurve* curve, TDate date) double JpmcdsForwardZeroPrice(TCurve* curve, TDate startDate, TDate maturityDate) @@ -123,6 +109,15 @@ cdef enum Basis: ANNUAL_BASIS = 1 DISCOUNT_FACTOR = -2 +cdef extern from "<memory>" namespace "std" nogil: + cdef cppclass shared_ptr[T]: + ctypedef void (*D)(T*) + shared_ptr() + shared_ptr(T*, D) + T* get() + bool operator!() + long use_count() + cdef class Curve: cdef shared_ptr[TCurve] _thisptr @@ -136,4 +131,4 @@ cdef class SpreadCurve(Curve): cdef fArray_to_list(TRatePt* fArray, int fNumItems) cdef void tweak_curve(TCurve* sc, TCurve* sc_tweaked, double epsilon, - const vector[double]& h, const vector[double]& T, bint* mask) + vector[double]& h, const vector[double]& T, bint* mask) diff --git a/pyisda/curve.pyx b/pyisda/curve.pyx index ab498bd..48298d1 100644 --- a/pyisda/curve.pyx +++ b/pyisda/curve.pyx @@ -1,7 +1,6 @@ from libc.stdlib cimport malloc, free from libc.string cimport memcpy from libc.math cimport log1p -from libcpp.memory cimport shared_ptr from libcpp.vector cimport vector from cython.operator cimport preincrement as preinc @@ -29,6 +28,9 @@ cpdef public enum BadDay: NONE = <long>'N' MODIFIED = <long>'M' +cdef inline shared_ptr[TCurve] make_shared(TCurve* ptr): + return shared_ptr[TCurve](ptr, JpmcdsFreeTCurve) + cdef class Curve(object): def __getstate__(self): @@ -320,7 +322,7 @@ cdef class YieldCurve(Curve): @cython.cdivision(True) cdef void tweak_curve(TCurve* sc, TCurve* sc_tweaked, double epsilon, - vector[double] h, const vector[double]& T, bint* mask): + vector[double]& h, const vector[double]& T, bint* mask): ## We want to tweak in the forward space, so we convert the hazard rates ## into forward rates and then back cdef double h1, h2, t1, t2 @@ -4,7 +4,7 @@ from Cython.Build import cythonize import numpy all_extensions = Extension("*", ["pyisda/*.pyx"], - include_dirs = ['cpp_layer', 'c_layer', numpy.get_include()], + include_dirs = ['c_layer', numpy.get_include()], libraries = ["cds"], language = 'c++') |
