diff options
Diffstat (limited to 'python')
| -rw-r--r-- | python/analytics/scenarios.py | 70 |
1 files changed, 33 insertions, 37 deletions
diff --git a/python/analytics/scenarios.py b/python/analytics/scenarios.py index 52aadcfd..e9712947 100644 --- a/python/analytics/scenarios.py +++ b/python/analytics/scenarios.py @@ -99,54 +99,50 @@ def run_portfolio_scenarios(portf, date_range, spread_shock, vol_shock, 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 + spread_range : `np.array`, spread range to run (different from swaption) + corr_map: 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() + index_pv = np.empty_like(spread_range) + tranche_pv = np.empty((len(spread_range), tranche.K.size - 1)) + tranche_delta = np.empty((len(spread_range), tranche.K.size - 1)) tranche.build_skew() temp_tranche = deepcopy(tranche) - + results = [] 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.value_date = d.date() + for i, spread in enumerate(spread_range): + temp_tranche.tweak(spread) + if corr_map: 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) - + index_pv[i] = temp_tranche._snacpv(spread * 1e-4, + temp_tranche.coupon(temp_tranche.maturity), + temp_tranche.recovery) + tranche_pv[i] = temp_tranche.tranche_pvs().bond_price + tranche_delta[i] = temp_tranche.tranche_deltas()['delta'] + carry = temp_tranche.tranche_quotes.running * \ + (d.date() - tranche.value_date).days / 360 + df = pd.concat({'pv': pd.DataFrame(tranche_pv, index=spread_range, + columns=tranche._row_names), + 'delta': pd.DataFrame(tranche_delta, index=spread_range, + columns=tranche._row_names), + 'carry': pd.DataFrame( + np.tile(carry, (len(spread_range), 1)), + index=spread_range, columns=tranche._row_names)}, + axis=1) + df = df.join( + pd.concat({'pnl': df['pv'].sub(tranche.tranche_pvs().bond_price), + 'index_price_snac_pv': pd.Series(index_pv, index=spread_range, + name='pv')}, + axis=1)) + results.append(df) + results = pd.concat(results, keys=date_range) + results.index.names = ['date', 'spread_range'] return results |
