summaryrefslogtreecommitdiffstats
path: root/c_layer
diff options
context:
space:
mode:
Diffstat (limited to 'c_layer')
-rw-r--r--c_layer/curve.cpp137
-rw-r--r--c_layer/curve.hpp61
2 files changed, 198 insertions, 0 deletions
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 <cstring>
+#include "curve.hpp"
+#include "isda/cxzerocurve.h"
+#include <cmath>
+
+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;
+ }
+
+}
diff --git a/c_layer/curve.hpp b/c_layer/curve.hpp
new file mode 100644
index 0000000..5479e72
--- /dev/null
+++ b/c_layer/curve.hpp
@@ -0,0 +1,61 @@
+#pragma once
+
+#include <vector>
+#include <string>
+#include "isda/bastypes.h"
+#include "isda/cdate.h"
+#include "isda/tcurve.h"
+
+namespace pyisda {
+ class Curve {
+
+ public:
+ Curve(TCurve* const curve) : ptr(curve) {};
+ Curve(const Curve &curve2) :
+ ptr(JpmcdsCopyCurve(curve2.ptr)) {};
+
+ ~Curve() {
+ JpmcdsFreeTCurve(ptr);
+ }
+ explicit operator TCurve*() const { return ptr; }
+ unsigned char* serialize(unsigned char* buf);
+ double zeroPrice(const TDate date);
+ double zeroPrice(const TDate date1, const TDate date2);
+ double survivalProb(const TDate start_date, const TDate maturity_date);
+ double survivalProb(const TDate start_date, const TDate maturity_date, double eps);
+ static void tweak(TCurve* ptr, double epsilon);
+ static void tweak(TCurve* ptr, double epsilon, unsigned long mask);
+ void tweak(double epsilon);
+ void tweak(double epsilon, unsigned long mask);
+ size_t size();
+ private:
+ TCurve* ptr;
+ };
+
+ class YieldCurve : public Curve {
+ public:
+ YieldCurve(TCurve* const curve, const std::vector<TDate>& dates);
+ YieldCurve(const YieldCurve &curve2) :
+ Curve(curve2),
+ dates(curve2.dates) {};
+ size_t size();
+ unsigned char* serialize(unsigned char* buf);
+ std::vector<TDate> dates;
+ };
+
+ class SpreadCurve : public Curve {
+ public:
+ SpreadCurve(TCurve* const curve, const std::vector<double>& recovery_rates,
+ std::string ticker);
+ SpreadCurve(const SpreadCurve &curve2) :
+ Curve(curve2),
+ recovery_rates(curve2.recovery_rates),
+ ticker(curve2.ticker) {};
+
+ std::vector<double> recovery_rates;
+ std::string ticker;
+ size_t size();
+ unsigned char* serialize(unsigned char* buf);
+ };
+
+}