From 85624fe9462e801b225b7f3c709c0c4dbc32264d Mon Sep 17 00:00:00 2001 From: Guillaume Horel Date: Thu, 25 Oct 2018 14:54:04 -0400 Subject: work in progress wip wip wip2 fix C++ --- c_layer/curve.cpp | 137 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 137 insertions(+) create mode 100644 c_layer/curve.cpp (limited to 'c_layer/curve.cpp') diff --git a/c_layer/curve.cpp b/c_layer/curve.cpp new file mode 100644 index 0000000..2f84d01 --- /dev/null +++ b/c_layer/curve.cpp @@ -0,0 +1,137 @@ +#include +#include "curve.hpp" +#include "isda/cxzerocurve.h" +#include + +namespace pyisda { + size_t Curve::size() { + return sizeof(int) + sizeof(TDate) + sizeof(double) + + sizeof(long) + sizeof(TRatePt) * ptr->fNumItems; + } + + unsigned char* Curve::serialize(unsigned char* const buf) { + unsigned char* cur = buf; + int num_items = ptr->fNumItems; + std::memcpy(cur, &num_items, sizeof(int)); + cur += sizeof(int); + memcpy(cur, ptr->fArray, sizeof(TRatePt) * num_items); + cur += sizeof(TRatePt) * num_items; + memcpy(cur, &(ptr->fBaseDate), sizeof(TDate)); + cur += sizeof(TDate); + memcpy(cur, &(ptr->fBasis), sizeof(double)); + cur += sizeof(double); + memcpy(cur, &(ptr->fDayCountConv), sizeof(long)); + return cur + sizeof(long); + } + + double Curve::zeroPrice(const TDate date) { + return JpmcdsZeroPrice(ptr, date); + } + + double Curve::zeroPrice(const TDate date1, const TDate date2) { + return JpmcdsForwardZeroPrice(ptr, date1, date2); + } + + double Curve::survivalProb(const TDate start_date, const TDate maturity_date, double eps) { + double lambda1, lambda2; + double t1, t2; + if(start_date == ptr->fBaseDate) { + lambda2 = JpmcdsZeroRate(ptr, maturity_date) * (1 + eps); + t2 = (maturity_date - ptr->fBaseDate) / 365.; + return std::exp(-lambda2 * t2); + } else { + lambda1 = JpmcdsZeroRate(ptr, start_date); + lambda2 = JpmcdsZeroRate(ptr, maturity_date); + t1 = start_date - ptr->fBaseDate; + t2 = maturity_date - ptr->fBaseDate; + return std::exp((lambda1 * t1 - lambda2 * t2) * (1 + eps) / 365.); + } + } + + double Curve::survivalProb(const TDate start_date, const TDate maturity_date) { + double lambda1, lambda2; + double t1, t2; + if(start_date == ptr->fBaseDate) { + lambda2 = JpmcdsZeroRate(ptr, maturity_date); + t2 = (maturity_date - ptr->fBaseDate) / 365.; + return std::exp(-lambda2 * t2); + } else { + lambda1 = JpmcdsZeroRate(ptr, start_date); + lambda2 = JpmcdsZeroRate(ptr, maturity_date); + t1 = start_date - ptr->fBaseDate; + t2 = maturity_date - ptr->fBaseDate; + return std::exp((lambda1 * t1 - lambda2 * t2) / 365.); + } + } + + void Curve::tweak(TCurve* const ptr, double epsilon) { + double h1, h2, t1, t2, c; + h1 = t1 = c = 0; + TRatePt cur; + for(int i = 0; i < ptr->fNumItems; i++) { + cur = ptr->fArray[i]; + h2 = cur.fRate; + t2 = (cur.fDate - ptr->fBaseDate) / 365.; + c += (h2 * t2 - h1 * t1 ) * (1 + epsilon); + cur.fRate = c / t2; + h1 = h2; + t1 = t2; + } + } + + void Curve::tweak(TCurve* const ptr, double epsilon, unsigned long mask) { + double h1, h2, t1, t2, c; + h1 = t1 = c = 0; + TRatePt cur; + for(int i = 0;i < ptr->fNumItems; i++) { + cur = ptr->fArray[i]; + h2 = cur.fRate; + t2 = (cur.fDate - ptr->fBaseDate) / 365.; + c += (h2 * t2 - h1 * t1 ) * (1 + epsilon * (( mask >> i) & 1)); + cur.fRate = c / t2; + h1 = h2; + t1 = t2; + } + } + + void Curve::tweak(double epsilon) { + Curve::tweak(ptr, epsilon); + } + + void Curve::tweak(double epsilon, unsigned long mask) { + Curve::tweak(ptr, epsilon, mask); + } + + size_t YieldCurve::size() { + return Curve::size() + sizeof(TDate) * dates.size(); + } + + unsigned char* YieldCurve::serialize(unsigned char* const buf) { + unsigned char* cur = buf; + cur = Curve::serialize(buf); + size_t size = dates.size(); + memcpy(cur, &size, sizeof(size_t)); + cur += sizeof(size_t); + memcpy(cur, dates.data(), sizeof(TDate) * size); + return cur + sizeof(TDate) * size; + } + + size_t SpreadCurve::size() { + return Curve::size() + recovery_rates.size() * sizeof(double) + 8; + } + + unsigned char* SpreadCurve::serialize(unsigned char* const buf) { + unsigned char* cur = buf; + cur = Curve::serialize(buf); + memcpy(cur, recovery_rates.data(), recovery_rates.size() * sizeof(double)); + cur += recovery_rates.size() * sizeof(double); + if (ticker.length() < 8) { + ticker.copy((char*)cur, ticker.length(), 0); + memset(cur + ticker.length(), 0, 8 - ticker.length()); + } else { + ticker.copy((char*)cur, 8, 0); + } + return cur + 8; + } + +} -- cgit v1.2.3-70-g09d2