diff options
| author | Guillaume Horel <guillaume.horel@serenitascapital.com> | 2016-10-25 11:30:50 -0400 |
|---|---|---|
| committer | Guillaume Horel <guillaume.horel@serenitascapital.com> | 2016-10-25 11:30:50 -0400 |
| commit | d1f5259a5682346e0cb7ad342fe39581999f868c (patch) | |
| tree | 1cd2331b6af794d61195c6a7131691081c40708c | |
| parent | 37631371d32a93052202f3d3755d2c1752be4e0c (diff) | |
| download | pyisda-d1f5259a5682346e0cb7ad342fe39581999f868c.tar.gz | |
make function more useful
| -rw-r--r-- | pyisda/flat_hazard.pyx | 67 |
1 files changed, 35 insertions, 32 deletions
diff --git a/pyisda/flat_hazard.pyx b/pyisda/flat_hazard.pyx index ecd2c6a..83a0d4a 100644 --- a/pyisda/flat_hazard.pyx +++ b/pyisda/flat_hazard.pyx @@ -6,22 +6,24 @@ from legs cimport (JpmcdsCdsContingentLegMake, JpmcdsCdsFeeLegMake, from curve cimport TCurve, YieldCurve, SpreadCurve, JpmcdsCleanSpreadCurve from date cimport pydate_to_TDate from cdsone cimport JpmcdsStringToStubMethod, TStubMethod -from cython.parallel cimport parallel, prange cimport cython +import numpy as np + +np.import_array() @cython.boundscheck(False) -def snac_pv_vec(trade_date, cash_settle_date, exercise_date, YieldCurve yc, - start_date, end_date, double[:] spreads, double recovery_rate): - """vectorized and parallel version of snac_pv""" +def 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""" cdef TDate trade_date_c = pydate_to_TDate(trade_date) cdef TDate step_in_date_c = trade_date_c + 1 - cdef TDate cash_settle_date_c = pydate_to_TDate(cash_settle_date) + 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 TCurve* sc - cdef double[:] coupon_leg_pv = np.empty_like(spreads) - cdef double[:] default_leg_pv = np.empty_like(spreads) + 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 TStubMethod stub_type JpmcdsStringToStubMethod(b"f/s", &stub_type) @@ -42,28 +44,29 @@ def snac_pv_vec(trade_date, cash_settle_date, exercise_date, YieldCurve yc, <long>'M', b'NONE', 1) # protect_start = True - cdef int i - with nogil, parallel(num_threads=4): - for i in prange(spreads.shape[0]): - sc = JpmcdsCleanSpreadCurve(trade_date_c, - yc._thisptr, - start_date_c, - step_in_date_c, - cash_settle_date_c, - 1, # length of end_dates_c - &end_date_c, - &spreads[i], - NULL, - recovery_rate, - 1, # pay_accrued_on_default True - NULL, - 3, # JPMCDS_ACT_360 - &stub_type, - <long>'M', - b'NONE') - JpmcdsContingentLegPV(default_leg, trade_date_c, cash_settle_date_c, - step_in_date_c, yc._thisptr, sc, - recovery_rate, &default_leg_pv[i]) - JpmcdsFeeLegPV(fee_leg, trade_date_c, step_in_date_c, cash_settle_date_c, - yc._thisptr, sc, True, &coupon_leg_pv[i]) - return coupon_leg_pv, default_leg_pv + cdef size_t i + for i in range(spreads.shape[0]): + sc = JpmcdsCleanSpreadCurve(trade_date_c, + yc._thisptr, + start_date_c, + step_in_date_c, + value_date_c, + 1, # length of end_dates_c + &end_date_c, + &spreads[i], + NULL, + recovery_rate, + 1, # pay_accrued_on_default True + NULL, + 3, # JPMCDS_ACT_360 + &stub_type, + <long>'M', + b'NONE') + if JpmcdsContingentLegPV(default_leg, trade_date_c, value_date_c, + step_in_date_c, yc._thisptr, sc, + recovery_rate, &default_leg_pv[i]) != 0: + raise ValueError("Something went wrong") + if JpmcdsFeeLegPV(fee_leg, trade_date_c, step_in_date_c, value_date_c, + yc._thisptr, sc, True, &coupon_leg_pv[i]) != 0: + raise ValueError("Something went wrong") + return default_leg_pv, coupon_leg_pv |
