aboutsummaryrefslogtreecommitdiffstats
path: root/python/notebooks/Risk Management.ipynb
blob: fe0457f3272c8dbfb95113af9da57e04bcbe5545 (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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import portfolio_var as port\n",
    "from analytics import Swaption, BlackSwaption, Index, VolatilitySurface, Portfolio\n",
    "from analytics.scenarios import run_swaption_scenarios, run_index_scenarios, run_portfolio_scenarios\n",
    "import datetime\n",
    "import pandas as pd\n",
    "from pandas.tseries.offsets import BDay, BMonthEnd\n",
    "\n",
    "#import exploration.swaption_calendar_spread as spread\n",
    "import exploration.swaption_calendar_spread as spread"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "#Oct ME Bond HY Equiv\n",
    "report_date = (datetime.date.today() + BMonthEnd(-1)).date()\n",
    "bond_HY_equiv = -.12088\n",
    "percentile = .95"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "#The 95%tile \n",
    "df, spread, dur = port.rel_spread_diff(report_date)\n",
    "stress = pd.DataFrame()\n",
    "stress.at[('2SD_widen', 'spread')] = df.quantile(.975) \n",
    "stress.at[('2SD_tighten', 'spread')] = df.quantile(.025) \n",
    "stress.at[('worst_widen', 'spread')] = df.max()\n",
    "stress['pts'] = -stress * spread * dur/100\n",
    "stress['nav_impact'] = bond_HY_equiv * stress['pts']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>spread</th>\n",
       "      <th>pts</th>\n",
       "      <th>nav_impact</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2SD_widen</th>\n",
       "      <td>0.157884</td>\n",
       "      <td>-2.208036</td>\n",
       "      <td>0.266907</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2SD_tighten</th>\n",
       "      <td>-0.163480</td>\n",
       "      <td>2.286301</td>\n",
       "      <td>-0.276368</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>worst_widen</th>\n",
       "      <td>0.359386</td>\n",
       "      <td>-5.026082</td>\n",
       "      <td>0.607553</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "               spread       pts  nav_impact\n",
       "2SD_widen    0.157884 -2.208036    0.266907\n",
       "2SD_tighten -0.163480  2.286301   -0.276368\n",
       "worst_widen  0.359386 -5.026082    0.607553"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "stress"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>level_1</th>\n",
       "      <th>notional</th>\n",
       "      <th>factor</th>\n",
       "      <th>coupon</th>\n",
       "      <th>duration</th>\n",
       "      <th>theta</th>\n",
       "      <th>price</th>\n",
       "      <th>closespread</th>\n",
       "      <th>clean_nav</th>\n",
       "      <th>accrued</th>\n",
       "      <th>onTR_notional</th>\n",
       "      <th>widen</th>\n",
       "      <th>tighten</th>\n",
       "      <th>total</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>strategy</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>HEDGE_MBS</th>\n",
       "      <td>3</td>\n",
       "      <td>-30500000.0</td>\n",
       "      <td>2.92</td>\n",
       "      <td>0.15</td>\n",
       "      <td>9.579586</td>\n",
       "      <td>0.091530</td>\n",
       "      <td>323.911577</td>\n",
       "      <td>728.775311</td>\n",
       "      <td>-2.454496e+06</td>\n",
       "      <td>-173891.666667</td>\n",
       "      <td>-2.271120e+07</td>\n",
       "      <td>-392951.746260</td>\n",
       "      <td>439165.979817</td>\n",
       "      <td>439165.979817</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>SER_IGCURVE</th>\n",
       "      <td>1</td>\n",
       "      <td>-11000000.0</td>\n",
       "      <td>2.00</td>\n",
       "      <td>0.02</td>\n",
       "      <td>4.202955</td>\n",
       "      <td>0.005495</td>\n",
       "      <td>203.166063</td>\n",
       "      <td>38.112262</td>\n",
       "      <td>-2.220374e+04</td>\n",
       "      <td>-12833.333333</td>\n",
       "      <td>1.006705e+06</td>\n",
       "      <td>3787.727425</td>\n",
       "      <td>-3681.218467</td>\n",
       "      <td>3787.727425</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>SER_ITRXCURVE</th>\n",
       "      <td>1</td>\n",
       "      <td>39300000.0</td>\n",
       "      <td>2.00</td>\n",
       "      <td>0.02</td>\n",
       "      <td>14.638235</td>\n",
       "      <td>0.021951</td>\n",
       "      <td>203.615045</td>\n",
       "      <td>138.590158</td>\n",
       "      <td>2.027082e+06</td>\n",
       "      <td>53396.910000</td>\n",
       "      <td>-3.481913e+06</td>\n",
       "      <td>-15658.882722</td>\n",
       "      <td>16533.674519</td>\n",
       "      <td>16533.674519</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "               level_1    notional  factor  coupon   duration     theta  \\\n",
       "strategy                                                                  \n",
       "HEDGE_MBS            3 -30500000.0    2.92    0.15   9.579586  0.091530   \n",
       "SER_IGCURVE          1 -11000000.0    2.00    0.02   4.202955  0.005495   \n",
       "SER_ITRXCURVE        1  39300000.0    2.00    0.02  14.638235  0.021951   \n",
       "\n",
       "                    price  closespread     clean_nav        accrued  \\\n",
       "strategy                                                              \n",
       "HEDGE_MBS      323.911577   728.775311 -2.454496e+06 -173891.666667   \n",
       "SER_IGCURVE    203.166063    38.112262 -2.220374e+04  -12833.333333   \n",
       "SER_ITRXCURVE  203.615045   138.590158  2.027082e+06   53396.910000   \n",
       "\n",
       "               onTR_notional          widen        tighten          total  \n",
       "strategy                                                                   \n",
       "HEDGE_MBS      -2.271120e+07 -392951.746260  439165.979817  439165.979817  \n",
       "SER_IGCURVE     1.006705e+06    3787.727425   -3681.218467    3787.727425  \n",
       "SER_ITRXCURVE  -3.481913e+06  -15658.882722   16533.674519   16533.674519  "
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "port.cleared_cds_margins(report_date, percentile)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "120816.55576340854"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "port.index_curve_margins(report_date)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "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.6.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}