summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGuillaume Horel <guillaume.horel@gmail.com>2023-11-30 13:54:59 -0500
committerGuillaume Horel <guillaume.horel@gmail.com>2023-11-30 13:54:59 -0500
commitfca0caa31743af344ff53a1f7b7a5a7e2bc89c18 (patch)
treedef98d21bdc481d22e4582fbae50d9b2f4cc1b0d
parent6267aa32c891bb61d817ce2d44ac441e20a761a5 (diff)
downloadpyisda-fca0caa31743af344ff53a1f7b7a5a7e2bc89c18.tar.gz
add theta vec function
-rw-r--r--pyisda/credit_index.pyx68
1 files changed, 68 insertions, 0 deletions
diff --git a/pyisda/credit_index.pyx b/pyisda/credit_index.pyx
index 4401e21..1d7db78 100644
--- a/pyisda/credit_index.pyx
+++ b/pyisda/credit_index.pyx
@@ -491,6 +491,74 @@ cdef class CreditIndex(CurveList):
JpmcdsFeeLegFree(legs.second)
return r
+ def theta_vec(self, step_in_date, cash_settle_date, maturity, YieldCurve yc not None,
+ double fixed_rate):
+ cdef:
+ TDate step_in_date_c = pydate_to_TDate(step_in_date)
+ TDate cash_settle_date_c = pydate_to_TDate(cash_settle_date)
+ TDate maturity_c = pydate_to_TDate(maturity)
+ TDate maturity_short_c
+ pair[TContingentLeg_ptr, TFeeLeg_ptr] legs_short
+ pair[TContingentLeg_ptr, TFeeLeg_ptr] legs_long
+ TDateInterval ivl
+ int i
+ double carry
+ double cl_pv_long, cl_pv_short, fl_pv_long, fl_pv_short
+ np.npy_intp n = self._curves.size()
+ np.ndarray arr = np.PyArray_EMPTY(1, &n, np.NPY_DOUBLE, 1)
+
+ cdef double[::1] r = arr
+ JpmcdsMakeDateInterval(-1, b"Y", &ivl)
+ JpmcdsDtFwdAny(maturity_c, &ivl, &maturity_short_c)
+ carry = (1 - self.defaulted_weight) * fixed_rate * 365 / 360
+ if maturity_short_c < step_in_date_c:
+ return nan("")
+ legs_long = get_legs(maturity_c,
+ self._start_date, self.cal_file.c_str())
+ legs_short = get_legs(maturity_short_c, self._start_date, self.cal_file.c_str())
+
+ with nogil:
+ for i in prange(self._curves.size()):
+ curve = <TCurve*>(self._curves[i].get())
+ recovery_rate = <double*>(self._curves[i].get() + self.offset_recovery_rates[i])
+ JpmcdsContingentLegPV(legs_long.first,
+ self.base_date,
+ cash_settle_date_c,
+ step_in_date_c,
+ yc.get_TCurve(),
+ curve,
+ deref(recovery_rate),
+ &cl_pv_long)
+ JpmcdsContingentLegPV(legs_short.first,
+ self.base_date,
+ cash_settle_date_c,
+ step_in_date_c,
+ yc.get_TCurve(),
+ curve,
+ deref(recovery_rate),
+ &cl_pv_short)
+ JpmcdsFeeLegPV(legs_long.second,
+ self.base_date,
+ step_in_date_c,
+ cash_settle_date_c,
+ yc.get_TCurve(),
+ curve,
+ True,
+ &fl_pv_long)
+ JpmcdsFeeLegPV(legs_short.second,
+ self.base_date,
+ step_in_date_c,
+ cash_settle_date_c,
+ yc.get_TCurve(),
+ curve,
+ True,
+ &fl_pv_short)
+ r[i] = self._weights[i] * (cl_pv_long - cl_pv_short - (fl_pv_long - fl_pv_short) * fixed_rate)
+
+ return arr
+
+
+
def protection_leg(self, step_in_date, cash_settle_date, maturity, YieldCurve yc not None):
cdef:
TDate step_in_date_c = pydate_to_TDate(step_in_date)