{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "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", "from statsmodels.graphics.regressionplots import plot_fit\n", "from scipy.special import logit, expit\n", "from pygam import LinearGAM, s, f, GAM\n", "from utils.db import dbengine, dbconn" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "%matplotlib inline" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "value_date = (datetime.datetime.today() - pd.offsets.BDay(1)).date()\n", "start = (datetime.datetime.today() - pd.offsets.BDay(1) * 365 *4).date()\n", "#end = (start + pd.offsets.BDay(1) * 365).date()\n", "end = datetime.datetime.today()\n", "gini_model, gini_results = {}, {}\n", "conn = dbconn(\"serenitasdb\")\n", "conn.autocommit = True\n", "for index_type in ['HY', 'IG', 'EU', 'XO']:\n", " risk = disp.get_tranche_data(dbconn(\"serenitasdb\"), index_type)\n", " risk = risk[risk.index_duration > .5] #filter out the short duration ones\n", " gini_results[index_type], gini_model[index_type] = disp.create_models_v2(conn, risk)\n", " fitted = gini_model[index_type].fit()\n", " w = 1/(expit(fitted.fittedvalues + fitted.resid) -expit(fitted.fittedvalues))**2\n", " gini_results[index_type], gini_model[index_type] = disp.create_models_v2(conn, risk, w)\n", "gini_model['HY'].fit().summary()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "fieldlist = ['exp_percentage','dispersion','gini','tranche_loss_per','mispricing']\n", "for index_type in ['HY', 'IG', 'EU', 'XO']:\n", " gini_results[index_type][fieldlist].to_csv('/home/serenitas/edwin/' + index_type + '_results.csv')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#Run a particular gini scenario\n", "scenario = gini_results['HY'].loc(axis=0)[value_date,'HY',33,:,'5yr',0]\n", "scenario['gini'].iloc[0] = .7\n", "scenario_disp = expit(gini_model['HY'].fit().predict(scenario))\n", "mispricing = scenario['tranche_loss_per'] - scenario_disp\n", "mispricing" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#plot the residuals\n", "fitted = gini_model['HY'].fit()\n", "plt.figure(figsize=(8,5))\n", "p=plt.scatter(x=expit(fitted.fittedvalues),y=expit(fitted.fittedvalues + fitted.resid) -expit(fitted.fittedvalues),edgecolor='k')\n", "xmin=min(expit(fitted.fittedvalues))\n", "xmax = max(expit(fitted.fittedvalues))\n", "plt.hlines(y=0,xmin=xmin*0.9,xmax=xmax*1.1,color='red',linestyle='--',lw=3)\n", "plt.xlabel(\"Fitted values\",fontsize=15)\n", "plt.ylabel(\"Residuals\",fontsize=15)\n", "plt.title(\"Fitted vs. residuals plot\",fontsize=18)\n", "plt.grid(True)\n", "plt.show()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "value_date = (datetime.datetime.today() - pd.offsets.BDay(1)).date()\n", "start = (datetime.datetime.today() - pd.offsets.BDay(1) * 365 *4).date()\n", "#end = (start + pd.offsets.BDay(1) * 365).date()\n", "end = datetime.datetime.today()\n", "index_type = 'IG'\n", "serenitasconn = dbconn(\"serenitasdb\")\n", "serenitasconn.autocommit = True\n", "risk = disp.get_tranche_data(serenitasconn, index_type)\n", "train_data = risk[start: end]\n", "gini_calc, gini_model = disp.create_models(serenitasconn, train_data)\n", "gini_model.fit().summary()" ] }, { "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['delhedged_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()\n", "tranche_returns.to_csv('/home/serenitas/edwin/Python/temp3.csv')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "attach = 0\n", "\n", "returns = tranche_returns.xs(['HY', 29, '5yr', attach], level = ['index', 'series', 'tenor','attach'])['delhedged_return']\n", "model = gini_calc.xs(['HY', 29, '5yr', attach], level = ['index', 'series', 'tenor','attach'])['mispricing']\n", "returns = pd.merge(returns, model, left_index=True, right_index=True)\n", "model_verification = smf.ols(\"delhedged_return ~ mispricing \", data=returns).fit()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "tranche_returns.xs(29, level='series').unstack(level='attach').to_csv('/home/serenitas/edwin/Python/temp1.csv')" ] } ], "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.1" } }, "nbformat": 4, "nbformat_minor": 4 }