aboutsummaryrefslogtreecommitdiffstats
path: root/python/notebooks
diff options
context:
space:
mode:
Diffstat (limited to 'python/notebooks')
-rw-r--r--python/notebooks/Dispersion.ipynb80
1 files changed, 77 insertions, 3 deletions
diff --git a/python/notebooks/Dispersion.ipynb b/python/notebooks/Dispersion.ipynb
index 84701ba8..e8e07f4a 100644
--- a/python/notebooks/Dispersion.ipynb
+++ b/python/notebooks/Dispersion.ipynb
@@ -8,10 +8,12 @@
"source": [
"import pandas as pd\n",
"import numpy as np\n",
+ "import itertools\n",
"import datetime\n",
"import exploration.dispersion as disp\n",
"import matplotlib.pyplot as plt\n",
"import statsmodels.formula.api as smf\n",
+ "import analytics.tranche_data as tdata\n",
"\n",
"from analytics.basket_index import MarkitBasketIndex\n",
"from analytics import on_the_run\n",
@@ -50,10 +52,10 @@
"outputs": [],
"source": [
"#Get Gini factor\n",
- "date_range = pd.bdate_range(end=value_date, freq='1B',periods=52*4)\n",
+ "date_range = pd.bdate_range(end=value_date, freq='5B',periods=52*4)\n",
"risk = disp.get_tranche_data(index_type, serenitas_engine)\n",
"risk = risk[risk.index.get_level_values(0).isin(date_range)]\n",
- "gini_model, gini_calc = disp.create_models(risk, use_gini=True, use_log=False)"
+ "gini_model, gini_calc = disp.create_models(risk, use_gini=True, use_log=True)"
]
},
{
@@ -62,6 +64,7 @@
"metadata": {},
"outputs": [],
"source": [
+ "#Plot Gini if (use gini=True, use_log=False)\n",
"to_plot_gini = gini_calc.xs(0, level='attach').groupby(['date', 'series']).nth(-1)\n",
"to_plot_gini['dispersion'].unstack().plot()"
]
@@ -72,6 +75,7 @@
"metadata": {},
"outputs": [],
"source": [
+ "#Today's mispricing\n",
"today = gini_calc.xs([value_date,33], level=['date','series'])\n",
"today[['exp_percentage', 'predict_N', 'predict_preN', 'mispricing']]"
]
@@ -82,7 +86,21 @@
"metadata": {},
"outputs": [],
"source": [
- "to_plot = gini_calc.xs(0, level='attach')['mispricing']\n",
+ "#plot mispricing of a tranche through time \n",
+ "attach = 0\n",
+ "to_plot = gini_calc.xs(attach, level='attach')['mispricing']\n",
+ "to_plot.reset_index(['index','tenor'], drop=True).unstack().plot()"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "#plot mispricing of series through time \n",
+ "series = 33\n",
+ "to_plot = gini_calc.xs(series, level='series')['mispricing']\n",
"to_plot.reset_index(['index','tenor'], drop=True).unstack().plot()"
]
},
@@ -101,6 +119,53 @@
"metadata": {},
"outputs": [],
"source": [
+ "gini_calc.to_csv('/home/serenitas/edwin/Python/' + index_type+ '_tranche_model.csv')"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "#compare to realized delta-adjusted return\n",
+ "tranche_returns = tdata.get_tranche_quotes(index=index_type)\n",
+ "tranche_returns = tdata.tranche_returns(df=tranche_returns)\n",
+ "attach = 0\n",
+ "t = tranche_returns['deladj_return'].reset_index(['index', 'tenor'], drop=True).xs(attach, level='attach')\n",
+ "temp={}\n",
+ "for i,g in t.groupby('series'):\n",
+ " temp[i] = (g.dropna()+1).cumprod()\n",
+ "t = pd.concat(temp).reset_index(0, drop=True)\n",
+ "t.unstack(level='series').plot()"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "#compare models\n",
+ "a = [True, False]\n",
+ "for years in [1,2,3,4,5,6]:\n",
+ " date_range = pd.bdate_range(end=value_date, freq='5B',periods=52*years)\n",
+ " risk = disp.get_tranche_data(index_type, serenitas_engine)\n",
+ " risk = risk[risk.index.get_level_values(0).isin(date_range)]\n",
+ " for x, y in list(itertools.product(a,a)):\n",
+ " gini_model, gini_calc = disp.create_models(risk, use_gini=x, use_log=y)\n",
+ " for i, m in gini_model.items():\n",
+ " print (years, x, y, i, m.rsquared)\n",
+ " today = gini_calc.xs([value_date,33], level=['date','series'])\n",
+ " print (today[['exp_percentage', 'predict_N', 'predict_preN', 'mispricing']])"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "metadata": {},
+ "outputs": [],
+ "source": [
"#Run a particular gini scenario\n",
"scenario = gini_calc.loc(axis=0)[value_date,33,'HY','5yr',0]\n",
"scenario['dispersion'] = .6\n",
@@ -118,6 +183,15 @@
"metadata": {},
"outputs": [],
"source": [
+ "gini_calc.loc(axis=0)[:,33,'HY','5yr',0]['mispricing']"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "metadata": {},
+ "outputs": [],
+ "source": [
"#Let's use a GAM model instead?\n",
"#only use the 5yr point for modeling\n",
"equity = gini_calc.loc(axis=0)[:,:,[25,27,29,31,33],'5yr',0]\n",