aboutsummaryrefslogtreecommitdiffstats
path: root/python/notebooks/Tranche calculator.ipynb
blob: 8649d43e2fd570f1167143f06d37939e59f4b2dc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import analytics.tranche_basket as bkt\n",
    "import pandas as pd\n",
    "import numpy as np\n",
    "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, ManualTrancheBasket\n",
    "from utils.db import dbconn\n",
    "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)})"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "index_type = 'HY'\n",
    "series = 35\n",
    "value_date = date.today()\n",
    "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"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#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"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#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"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "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
}