aboutsummaryrefslogtreecommitdiffstats
path: root/python/analytics
diff options
context:
space:
mode:
Diffstat (limited to 'python/analytics')
-rw-r--r--python/analytics/scenarios.py53
1 files changed, 53 insertions, 0 deletions
diff --git a/python/analytics/scenarios.py b/python/analytics/scenarios.py
index 1fc554fb..65f77563 100644
--- a/python/analytics/scenarios.py
+++ b/python/analytics/scenarios.py
@@ -97,3 +97,56 @@ def run_portfolio_scenarios(portf, date_range, spread_shock, vol_shock,
r.append([[date, s] + rec for rec in temp])
df = pd.DataFrame.from_records(chain(*r), columns=['date', 'spread', 'vol_shock'] + params)
return df.set_index('date')
+
+def run_tranche_scenarios(tranche, spread_range, date_range, corr_map=False):
+
+ """computes the pnl of a tranche for a range of spread scenarios
+
+ Parameters
+ ----------
+ tranche : TrancheBasket
+ spread_shock : `np.array`, spread range to run (different from swaption)
+ roll_corr: static correlation or mapped correlation
+ """
+
+ #create empty lists
+ shock_index_pv_calc = np.empty(len(spread_range))
+ shock_tranche_pv = np.empty((len(spread_range), tranche.K.size - 1))
+ shock_tranche_delta = np.empty((len(spread_range), tranche.K.size - 1))
+ shock_tranche_carry = np.empty((len(spread_range), tranche.K.size - 1))
+ results = pd.DataFrame()
+
+ tranche.build_skew()
+ temp_tranche = deepcopy(tranche)
+
+ for d in date_range:
+ temp_tranche.trade_date = d.date()
+ for i, shock in enumerate(spread_range):
+ temp_tranche.tweak(shock)
+ if corr_map is True:
+ temp_tranche.rho = tranche.map_skew(temp_tranche, 'TLP')
+ shock_index_pv_calc[i] = temp_tranche._snacpv(shock * 1e-4,
+ temp_tranche.coupon(temp_tranche.maturity), temp_tranche.recovery)
+ shock_tranche_pv[i] = temp_tranche.tranche_pvs().bond_price
+ shock_tranche_delta[i] = temp_tranche.tranche_deltas()['delta']
+ shock_tranche_carry[i] = temp_tranche.tranche_quotes.running
+ temp1 = pd.DataFrame(shock_tranche_pv, index=spread_range,
+ columns=[s + "_pv" for s in tranche._row_names])
+ temp2 = pd.DataFrame(shock_tranche_delta, index=spread_range,
+ columns=[s + "_delta" for s in tranche._row_names])
+ temp3 = pd.DataFrame(np.subtract(shock_tranche_pv,
+ tranche.tranche_pvs().bond_price), index=spread_range,
+ columns=[s + "_pnl" for s in tranche._row_names])
+ temp4 = pd.DataFrame(shock_index_pv_calc, index=spread_range,
+ columns=['index_price_snacpv'])
+ temp5 = pd.DataFrame(shock_tranche_carry, index=spread_range,
+ columns=[s + "_carry" for s in tranche._row_names])
+ df = pd.concat([temp1, temp2, temp3, temp4, temp5], axis=1)
+
+ df['date'] = d.date()
+ for column in [s + "_carry" for s in tranche._row_names]:
+ df[column] *= (d.date() - tranche.trade_date).days/365
+
+ results = results.append(df)
+
+ return results