summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGuillaume Horel <guillaume.horel@serenitascapital.com>2016-11-10 18:17:10 -0500
committerGuillaume Horel <guillaume.horel@serenitascapital.com>2016-11-10 18:17:10 -0500
commit76064859c2d24ea1c44aa9839e09bfdada284000 (patch)
tree69339c6f0173a129a7c90f650edd053bbe40cde1
parent2a2290a51924bd36b57b977caf9f7affc593b820 (diff)
downloadpyisda-76064859c2d24ea1c44aa9839e09bfdada284000.tar.gz
rename to pv_vec and small fixes
-rw-r--r--pyisda/flat_hazard.pyx32
1 files changed, 17 insertions, 15 deletions
diff --git a/pyisda/flat_hazard.pyx b/pyisda/flat_hazard.pyx
index 2257098..daeac41 100644
--- a/pyisda/flat_hazard.pyx
+++ b/pyisda/flat_hazard.pyx
@@ -1,4 +1,5 @@
from date cimport dcc, TDate
+import numpy as np
cimport numpy as np
from legs cimport (JpmcdsCdsContingentLegMake, JpmcdsCdsFeeLegMake,
@@ -11,7 +12,6 @@ from cdsone cimport JpmcdsStringToStubMethod, TStubMethod
from libc.stdlib cimport free, malloc
from libc.stdio cimport printf
cimport cython
-import numpy as np
np.import_array()
@@ -109,26 +109,26 @@ cdef extern from "isda/rtbrent.h" nogil:
@cython.boundscheck(False)
@cython.cdivision(True)
-def faster_strike_vec(double[:] spreads, YieldCurve yc, trade_date, value_date,
- start_date, end_date, double recovery_rate):
- """Computes coupon and default leg for a vector of spreads"""
+def pv_vec(double[:] spreads, YieldCurve yc, trade_date, value_date,
+ start_date, end_date, double recovery_rate, double fixed_rate):
+ """Computes vector of cds clean pvs for a vector of spreads"""
cdef TDate trade_date_c = pydate_to_TDate(trade_date)
cdef TDate step_in_date_c = trade_date_c + 1
cdef TDate value_date_c = pydate_to_TDate(value_date)
cdef TDate start_date_c = pydate_to_TDate(start_date)
cdef TDate end_date_c = pydate_to_TDate(end_date)
- cdef np.ndarray[np.float_t] coupon_leg_pv = np.empty_like(spreads)
- cdef np.ndarray[np.float_t] default_leg_pv = np.empty_like(spreads)
- cdef int nspreads = spreads.shape[0]
- cdef double h, guess
+ cdef np.npy_intp nspreads = spreads.shape[0]
+ cdef double h, guess, hi, lo, coupon_leg_pv
+ cdef np.ndarray[np.float_t] pv = np.empty_like(spreads)
cdef TStubMethod stub_type
cdef TCurve* sc
cdef TContingentLeg* default_leg
cdef TFeeLeg* fee_leg
cdef cds_bootstrap_ctx* params
- cdef size_t i
+ cdef Py_ssize_t i
JpmcdsStringToStubMethod(b"f/s", &stub_type)
+ cdef double eta = 365./360.
with nogil:
h = spreads[0] / (1 - recovery_rate)
@@ -165,11 +165,13 @@ def faster_strike_vec(double[:] spreads, YieldCurve yc, trade_date, value_date,
for i in range(nspreads):
params.spread = spreads[i]
- guess = spreads[i] / ( 1 - recovery_rate) * 365/360
+ lo = guess * 0.9
+ guess = spreads[i] / ( 1 - recovery_rate) * eta
+ hi = guess * 1.1
if JpmcdsRootFindBrent(<TObjectFunc>cdsBootstrapPointFunction,
<void*> params,
- 0.0, # boundLo */
- 1e10, # boundHi */
+ lo, # boundLo */
+ hi, # boundHi */
100, # numIterations */
guess,
0.0005, # initialXstep */
@@ -180,11 +182,11 @@ def faster_strike_vec(double[:] spreads, YieldCurve yc, trade_date, value_date,
printf("failed to find the root")
sc.fArray[0].fRate = h
if JpmcdsFeeLegPV(fee_leg, trade_date_c, step_in_date_c, value_date_c,
- yc._thisptr, sc, True, &coupon_leg_pv[i]) != 0:
+ yc._thisptr, sc, True, &coupon_leg_pv) != 0:
printf("Something went wrong")
- default_leg_pv[i] = coupon_leg_pv[i] * spreads[i]
+ pv[i] = coupon_leg_pv * (spreads[i] - fixed_rate)
free(params)
JpmcdsFeeLegFree(fee_leg)
free(default_leg)
JpmcdsFreeTCurve(sc)
- return default_leg_pv, coupon_leg_pv
+ return pv