aboutsummaryrefslogtreecommitdiffstats
path: root/python/notebooks/VaR.ipynb
blob: fd48ded2b1560b75d0143856301476b6ad826b80 (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
{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from analytics.curve_trades import curve_pos\n",
    "from analytics import Index, Portfolio\n",
    "\n",
    "import datetime\n",
    "import exploration.VaR as var\n",
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "date = (datetime.date.today() - pd.tseries.offsets.BDay(1)).date()\n",
    "report_date = (date + pd.tseries.offsets.BMonthEnd(-1)).date()\n",
    "index_type = \"IG\"\n",
    "quantile = .025"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#IG Curve VaR\n",
    "portf = curve_pos(date, index_type)\n",
    "ig_curve_var = abs(var.hist_var(portf, quantile=quantile))\n",
    "ig_curve_var"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#EU Curve VaR\n",
    "index_type = \"EU\"\n",
    "portf = curve_pos(date, index_type)\n",
    "eu_curve_var = abs(var.hist_var(portf, quantile=quantile))\n",
    "eu_curve_var"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#Mortgage Hedge VaR - use IG spread relative move for VaR\n",
    "df = var.get_pos(date)\n",
    "df = df[df.strategy == 'HEDGE_MBS']\n",
    "portf = Portfolio([Index.from_name(row.p_index, row.p_series, row.tenor,\n",
    "                                       report_date, -row.notional)\n",
    "                      for row in df[['p_index', 'tenor', 'p_series', 'notional']].\n",
    "                       itertuples(index=False)])\n",
    "portf.mark()\n",
    "mort_hedge_var = abs(var.hist_var(portf, index_type = \"IG\", quantile=quantile, years=3))\n",
    "mort_hedge_var"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#Import the IM at the FCM account: calculate the IM share of different strategies as a share of VaR\n",
    "filename = date.strftime('%Y%m%d') + \"_OTC_MARGIN_EX_DEF.csv\"\n",
    "margin_df = pd.read_csv(\"/home/serenitas/Daily/SG_reports/\" + filename, index_col='Currency')\n",
    "morg_hedge_im = mort_hedge_var + mort_hedge_var/(mort_hedge_var + ig_curve_var) * margin_df.loc[('USD', 'SG IMR')]\n",
    "morg_hedge_im"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#Oct ME Bond HY Equiv\n",
    "bond_HY_equiv = -.12088\n",
    "percentile = .95"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#95%tile \n",
    "df, spread, dur = var.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']\n",
    "stress"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "port.cleared_cds_margins(report_date, percentile)"
   ]
  },
  {
   "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.6.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}