diff options
Diffstat (limited to 'python/notebooks/risk_sabo.ipynb')
| -rw-r--r-- | python/notebooks/risk_sabo.ipynb | 129 |
1 files changed, 129 insertions, 0 deletions
diff --git a/python/notebooks/risk_sabo.ipynb b/python/notebooks/risk_sabo.ipynb new file mode 100644 index 00000000..dd2cb9f1 --- /dev/null +++ b/python/notebooks/risk_sabo.ipynb @@ -0,0 +1,129 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "import datetime\n", + "import globeop_reports as go\n", + "import pandas as pd\n", + "import analytics\n", + "import numpy as np\n", + "\n", + "from pandas.tseries.offsets import BDay, MonthEnd\n", + "from analytics.scenarios import run_portfolio_scenarios\n", + "from utils.db import dbconn\n", + "from risk.portfolio import build_portfolio, generate_vol_surface\n", + "from analytics.basket_index import BasketIndex" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "#Set dates\n", + "position_date = (datetime.date.today() - MonthEnd(1)).date()\n", + "spread_date = position_date\n", + "analytics._local = False\n", + "analytics.init_ontr(spread_date)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "################################### Run Credit Spread scenarios\n", + "spread_shock = np.array([-100., -25., 1., +25. , 100.])\n", + "spread_shock /= analytics._ontr['HY'].spread\n", + "portf, _ = build_portfolio(position_date, spread_date)\n", + "vol_surface = generate_vol_surface(portf, 5)\n", + "portf.reset_pv()\n", + "scens = run_portfolio_scenarios(portf, date_range=[pd.Timestamp(spread_date)], params=['pnl'],\n", + " spread_shock=spread_shock,\n", + " vol_shock=[0.0],\n", + " corr_shock=[0.0],\n", + " vol_surface=vol_surface)\n", + "\n", + "pnl = scens.xs('pnl', axis=1, level=2)\n", + "pnl = pnl.xs((0.0, 0.0), level=['vol_shock', 'corr_shock'])\n", + "\n", + "scenarios = (pnl.\n", + " reset_index(level=['date'], drop=True).\n", + " groupby(level=0, axis=1).sum())\n", + "\n", + "options = ['HYOPTDEL', 'HYPAYER', 'HYREC', 'IGOPTDEL', 'IGPAYER', 'IGREC']\n", + "tranches = ['HYMEZ', 'HYINX', 'HYEQY', 'IGMEZ', 'IGINX', 'IGEQY', 'IGSNR', 'IGINX', 'BSPK', 'XOMEZ', 'XOINX', 'EUMEZ']\n", + "hedges = ['HEDGE_CLO', 'HEDGE_MAC', 'HEDGE_MBS']\n", + "\n", + "synthetic =pd.DataFrame()\n", + "synthetic['options'] = scenarios[set(scenarios.columns).intersection(options)].sum(axis=1)\n", + "synthetic['tranches'] = scenarios[set(scenarios.columns).intersection(tranches)].sum(axis=1)\n", + "synthetic['curve_trades'] = scenarios['curve_trades']\n", + "synthetic['total'] = synthetic.sum(axis = 1)\n", + "nav = go.get_net_navs()\n", + "scenarios.sum(axis=1)\n", + "scenarios.sum(axis=1).to_clipboard()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "################################### JTD\n", + "_, portf = build_portfolio(position_date, spread_date)\n", + "jtd_i = []\n", + "for t in portf.indices:\n", + " bkt = BasketIndex(t.index_type, t.series, [t.tenor])\n", + " spreads = pd.DataFrame(bkt.spreads() * 10000, index=pd.Index(bkt.tickers, name='ticker'), columns=['spread'])\n", + " jump = pd.merge(spreads, bkt.jump_to_default() * t.notional, left_index=True, right_index=True)\n", + " jtd_i.append(jump.rename(columns={jump.columns[1]: 'jtd'}))\n", + "jtd_t = []\n", + "for t in portf.tranches:\n", + " jump = pd.concat([t.singlename_spreads().reset_index(['seniority', 'doc_clause'], drop=True), t.jump_to_default().rename('jtd')], axis=1)\n", + " jtd_t.append(jump.drop(['weight', 'recovery'], axis=1))\n", + "\n", + "ref_names = pd.read_sql_query(\"select ticker, referenceentity from refentity\", dbconn('serenitasdb'), index_col='ticker')\n", + "jump = pd.concat([pd.concat(jtd_t), pd.concat(jtd_i)])\n", + "jump = jump.merge(ref_names, left_index=True, right_index=True)\n", + "jump = jump.groupby('referenceentity').agg({'spread': np.mean, 'jtd': np.sum}).sort_values(by='jtd', ascending=True)\n", + "jump.to_clipboard()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3.8.1 64-bit", + "language": "python", + "name": "python38164bitc40c8740e5d542d7959acb14be96f4f3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.8.5" + } + }, + "nbformat": 4, + "nbformat_minor": 4 +} |
