diff options
Diffstat (limited to 'python/notebooks')
| -rw-r--r-- | python/notebooks/Tranche calculator.ipynb | 118 |
1 files changed, 65 insertions, 53 deletions
diff --git a/python/notebooks/Tranche calculator.ipynb b/python/notebooks/Tranche calculator.ipynb index 3b55e4a7..8649d43e 100644 --- a/python/notebooks/Tranche calculator.ipynb +++ b/python/notebooks/Tranche calculator.ipynb @@ -12,11 +12,40 @@ "import matplotlib.pyplot as plt\n", "\n", "from analytics.scenarios import run_tranche_scenarios, run_portfolio_scenarios, run_tranche_scenarios_rolldown\n", - "from analytics import DualCorrTranche, TrancheBasket\n", + "from analytics import DualCorrTranche, TrancheBasket, ManualTrancheBasket\n", "from utils.db import dbconn\n", - "from datetime import date\n", - "\n", - "value_date = (date.today() - pd.offsets.BDay(1)).date()" + "from datetime import date" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "def build_tranche_quotes(index_type, ref, quotes):\n", + " if index_type == 'HY':\n", + " detach = [15, 25, 35, 100] \n", + " elif index_type == 'IG':\n", + " detach = [3, 7, 15, 100] \n", + " elif index_type == 'EU':\n", + " detach = [3, 6, 12, 100] \n", + " else:\n", + " detach = [10, 20, 35, 100]\n", + " coupon = 500 if (index_type == 'HY' or index_type == 'XO') else 100\n", + " if index_type == 'HY':\n", + " ref_type1 = 'indexrefprice'\n", + " ref_type2 = 'indexrefspread'\n", + " ref_2 = None\n", + " else:\n", + " ref_type1 = 'indexrefspread'\n", + " ref_type2 = 'indexrefprice'\n", + " ref_2 = None\n", + " return pd.DataFrame({\"detach\": np.array(detach), \n", + " \"trancheupfrontmid\": np.array(quotes), \n", + " \"trancherunningmid\": np.full(4, coupon),\n", + " ref_type1: np.full(4, ref),\n", + " ref_type2: np.full(4, ref_2)})" ] }, { @@ -27,17 +56,18 @@ "source": [ "index_type = 'HY'\n", "series = 35\n", - "tenor = '5yr'\n", "value_date = date.today()\n", - "price = 103.875\n", - "at_det = [0, 15, 25, 35, 100] if index_type == 'HY' else ['0', '3', '7', '15', '100']\n", - "tranche_prices= [41.4, 90.6, 109.6, 119.7]\n", - "\n", - "#Build another skew to price this new series\n", - "base_index = TrancheBasket(\"HY\", 33, \"5yr\")\n", - "base_index.tweak()\n", - "base_index.build_skew()\n", - "skew=base_index.skew" + "df = build_tranche_quotes(index_type, 104.625, [43, 92.5, 110, 120.27])\n", + "new_index = ManualTrancheBasket(index_type, series, \"5yr\", value_date=value_date, tranche_quotes=df)\n", + "new_index.tweak()\n", + "new_index.build_skew()\n", + "result = pd.concat([pd.DataFrame(new_index.rho[0:4], index=new_index.tranche_thetas().index, columns=['att_corr']),\n", + " pd.DataFrame(new_index.tranche_pvs().bond_price, index=new_index.tranche_thetas().index, columns=['price']),\n", + " new_index.tranche_deltas(),\n", + " new_index.tranche_thetas()],\n", + " axis=1)\n", + "result['net_theta'] = result.theta - new_index.theta()[0] * result.delta\n", + "result" ] }, { @@ -46,29 +76,12 @@ "metadata": {}, "outputs": [], "source": [ - "results = []\n", - "for i in range(3):\n", - " #set up\n", - " rho_floor = tranche.rho[1] if i > 0 else 0.2\n", - " rho_min = rho_floor\n", - " rho_max = rho_floor + 0.4\n", - " tranche = DualCorrTranche(index_type, series, tenor, attach=at_det[i], detach=at_det[i+1], corr_attach = rho_floor, corr_detach = rho_min + (rho_max -rho_min)/2, tranche_running = 500, value_date=value_date)\n", - " tranche._index.tweak([price])\n", - " #now loop to find it\n", - " for j in range(20):\n", - " if tranche.price <= tranche_prices[i]:\n", - " rho_min = tranche.rho[1]\n", - " else:\n", - " rho_max = tranche.rho[1]\n", - " tranche.rho[1] = rho_min + (rho_max - rho_min)/2\n", - " results.append([tranche.rho[1], tranche.price, tranche.delta, tranche.gamma, tranche.theta(skew=skew), tranche.delta * float(tranche._index.theta())])\n", - "ss_corr = tranche.rho[1]\n", - "tranche = DualCorrTranche(index_type, series, tenor, attach=at_det[i+1], detach=100, corr_attach = ss_corr, corr_detach = .999, tranche_running = 500, value_date=value_date)\n", - "tranche._index.tweak([price])\n", - "results.append([tranche.rho[1], tranche.price, tranche.delta, tranche.gamma, tranche.theta(skew=skew), tranche.delta * float(tranche._index.theta())])\n", - "results = pd.DataFrame(results, columns = ['corr', 'price', 'delta', 'gamma', 'theta', 'delta * index_theta'])\n", - "results['theta_per_delta'] = results['theta'] / results['delta']\n", - "results" + "#Implied SS\n", + "implied_ss = ((new_index.index_pv().bond_price - new_index.accrued()) -\n", + " ((new_index.K[1]-new_index.K[0]) * result.price[0] +\n", + " (new_index.K[2]-new_index.K[1]) * result.price[1] +\n", + " (new_index.K[3]-new_index.K[2]) * result.price[2]))/(new_index.K[4] - new_index.K[3])\n", + "implied_ss" ] }, { @@ -77,20 +90,19 @@ "metadata": {}, "outputs": [], "source": [ - "#Using another skew\n", - "mapped_results = []\n", - "for i in range(3):\n", - " tranche = DualCorrTranche(index_type, series, tenor, attach=at_det[i], detach=at_det[i+1], corr_attach = np.nan, corr_detach = 0.1, tranche_running = 500, value_date=value_date)\n", - " tranche._index.tweak([price])\n", - " tranche.mark(skew=skew)\n", - " mapped_results.append([tranche.rho[1], tranche.price, tranche.delta, tranche.gamma, tranche.theta(skew=skew), tranche.corr01/tranche.notional])\n", - "ss_corr = tranche.rho[1]\n", - "tranche = DualCorrTranche(index_type, series, tenor, attach=at_det[i+1], detach=100, corr_attach = ss_corr, corr_detach = .999, tranche_running = 500, value_date=value_date)\n", - "tranche._index.tweak([price])\n", - "mapped_results.append([tranche.rho[1], tranche.price, tranche.delta, tranche.gamma, tranche.theta(skew=skew), np.nan])\n", - "mapped_results = pd.DataFrame(mapped_results, columns = ['corr', 'price', 'delta', 'gamma', 'theta', 'corr01'])\n", - "mapped_results['theta_per_delta'] = mapped_results['theta'] / mapped_results['delta']\n", - "mapped_results" + "#Build previous series skew to price this new series\n", + "base_index = TrancheBasket(index_type, series-2, \"5yr\")\n", + "base_index.tweak()\n", + "base_index.build_skew()\n", + "\n", + "new_index.rho = base_index.map_skew(new_index)\n", + "result = pd.concat([pd.DataFrame(new_index.rho[0:4], index=new_index.tranche_thetas().index, columns=['att_corr']),\n", + " pd.DataFrame(new_index.tranche_pvs().bond_price, index=new_index.tranche_thetas().index, columns=['price']),\n", + " new_index.tranche_deltas(),\n", + " new_index.tranche_thetas()],\n", + " axis=1)\n", + "result['net_theta'] = result.theta - new_index.theta()[0] * result.delta\n", + "result" ] }, { @@ -103,9 +115,9 @@ ], "metadata": { "kernelspec": { - "display_name": "Python 3.8.1 64-bit", + "display_name": "Python 3", "language": "python", - "name": "python38164bitc40c8740e5d542d7959acb14be96f4f3" + "name": "python3" }, "language_info": { "codemirror_mode": { |
