diff options
Diffstat (limited to 'python/notebooks')
| -rw-r--r-- | python/notebooks/Realized Vol.ipynb | 25 | ||||
| -rw-r--r-- | python/notebooks/path.py | 5 | ||||
| -rw-r--r-- | python/notebooks/tranches numbers.ipynb | 488 |
3 files changed, 436 insertions, 82 deletions
diff --git a/python/notebooks/Realized Vol.ipynb b/python/notebooks/Realized Vol.ipynb index a67654c5..4fa97ede 100644 --- a/python/notebooks/Realized Vol.ipynb +++ b/python/notebooks/Realized Vol.ipynb @@ -6,18 +6,9 @@ "metadata": {}, "outputs": [], "source": [ - "import warnings\n", - "warnings.simplefilter('ignore')\n", - "\n", - "import os\n", - "import sys\n", - "sys.path.append(os.path.join(os.environ['CODE_DIR'], 'python', 'exploration'))\n", - "sys.path.append(os.path.join(os.environ['CODE_DIR'], 'python'))\n", - "\n", - "%matplotlib inline\n", - "import seaborn\n", - "seaborn.mpl.rcParams['figure.figsize'] = (10.0, 6.0)\n", - "seaborn.mpl.rcParams['savefig.dpi'] = 90" + "from option_trades import realized_vol\n", + "from matplotlib import pyplot as plt\n", + "plt.style.use('ggplot')" ] }, { @@ -26,7 +17,8 @@ "metadata": {}, "outputs": [], "source": [ - "import option_trades as ot" + "ig28, model_ig = realized_vol(\"IG\", 28, \"5yr\", years=3)\n", + "hy27, model_hy = realized_vol(\"HY\", 27, \"5yr\", years=3)" ] }, { @@ -35,8 +27,7 @@ "metadata": {}, "outputs": [], "source": [ - "HY28Vol = ot.realized_vol(\"HY\", 28,\"5yr\", years = 3)\n", - "IG28Vol = ot.realized_vol(\"IG\", 28,\"5yr\", years = 3)" + "ig28.plot()" ] }, { @@ -45,7 +36,7 @@ "metadata": {}, "outputs": [], "source": [ - "HY28Vol[0].plot()" + "hy27.plot()" ] }, { @@ -54,7 +45,7 @@ "metadata": {}, "outputs": [], "source": [ - "IG28Vol[0].plot()" + "model_ig.summary()" ] }, { diff --git a/python/notebooks/path.py b/python/notebooks/path.py new file mode 100644 index 00000000..9d22534b --- /dev/null +++ b/python/notebooks/path.py @@ -0,0 +1,5 @@ +import sys +import os +sys.path.append(os.path.join(os.environ['CODE_DIR'], 'python')) +sys.path.append(os.path.join(os.environ['CODE_DIR'], 'python', + 'exploration')) diff --git a/python/notebooks/tranches numbers.ipynb b/python/notebooks/tranches numbers.ipynb index 71cb44f8..cb45f6e2 100644 --- a/python/notebooks/tranches numbers.ipynb +++ b/python/notebooks/tranches numbers.ipynb @@ -3,34 +3,50 @@ { "cell_type": "code", "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "%matplotlib\n", - "import seaborn\n", - "seaborn.mpl.rcParams['figure.figsize'] = (12.0, 8.0)" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, + "metadata": { + "extensions": { + "jupyter_dashboards": { + "version": 1, + "views": { + "grid_default": { + "hidden": true + }, + "report_default": {} + } + } + } + }, "outputs": [], "source": [ "import pandas as pd\n", "from ipywidgets import interact\n", - "from collections import OrderedDict" + "from collections import OrderedDict\n", + "from matplotlib import pyplot as plt\n", + "plt.interactive(False)\n", + "plt.style.use('ggplot')" ] }, { "cell_type": "code", "execution_count": null, - "metadata": {}, + "metadata": { + "extensions": { + "jupyter_dashboards": { + "version": 1, + "views": { + "grid_default": { + "hidden": true + }, + "report_default": {} + } + } + } + }, "outputs": [], "source": [ - "SQL_CON = \"postgresql://serenitas_user@debian/serenitasdb\"\n", - "runs = (pd.\n", - " read_sql(\"SELECT DISTINCT index, series, tenor from risk_numbers ORDER BY index, series\", SQL_CON).\n", + "from db import dbengine\n", + "runs = (pd.read_sql_query(\"SELECT DISTINCT index, series, tenor from risk_numbers ORDER BY index, series\",\n", + " dbengine('serenitasdb')).\n", " itertuples(index=False, name='run'))\n", "runs = OrderedDict([(\"%s %s %s\" % (r.index, r.series, r.tenor), (r.index, r.series, r.tenor)) for r in runs])" ] @@ -38,7 +54,19 @@ { "cell_type": "code", "execution_count": null, - "metadata": {}, + "metadata": { + "extensions": { + "jupyter_dashboards": { + "version": 1, + "views": { + "grid_default": { + "hidden": true + }, + "report_default": {} + } + } + } + }, "outputs": [], "source": [ "df = pd.read_sql(\"SELECT * FROM risk_numbers\", SQL_CON, index_col=['date', 'index', 'series', 'tenor'])" @@ -47,37 +75,91 @@ { "cell_type": "code", "execution_count": null, - "metadata": {}, + "metadata": { + "extensions": { + "jupyter_dashboards": { + "version": 1, + "views": { + "grid_default": { + "col": 0, + "height": 57, + "hidden": false, + "row": 0, + "width": 10 + }, + "report_default": {} + } + } + } + }, "outputs": [], "source": [ "risk_numbers = ['skew', 'Dealer Deltas', 'Model Deltas', 'Forward Deltas', 'gammas', 'durations', 'thetas']\n", - "@interact(index=runs, what=risk_numbers)\n", + "\n", "def corrplot(index, what):\n", + " plt.close('all')\n", " selection = df.xs(index, level=[1,2,3], drop_level=True)\n", " cols = selection.attach.iloc[0]\n", " cols = [\"{}-{}\".format(a,d) for a, d in zip(cols[:-2], cols[1:-1])]\n", " selection = selection[what].apply(pd.Series)\n", " selection.drop(selection.columns[-1], axis=1, inplace=True)\n", " selection.columns = cols\n", - " selection.plot()" + " selection.plot()\n", + " plt.show()\n", + " \n", + "interact(corrplot, index=runs, what=risk_numbers)" ] }, { "cell_type": "code", "execution_count": null, - "metadata": {}, + "metadata": { + "extensions": { + "jupyter_dashboards": { + "version": 1, + "views": { + "grid_default": { + "hidden": true + }, + "report_default": {} + } + } + } + }, "outputs": [], "source": [ "def get_tranche_quotes(index, series, tenor):\n", " df = pd.read_sql(\"SELECT * FROM tranche_quotes WHERE index=%s and series=%s and tenor =%s\",\n", - " SQL_CON, params=(index, series, tenor), index_col='quotedate')\n", + " SQL_CON, params=(index, series, tenor), index_col=['quotedate'],\n", + " parse_dates=['quotedate'])\n", " df.sort_index(inplace=True)\n", " return df\n", + "\n", "def get_index_quotes(index, series, tenor):\n", " df = pd.read_sql(\"SELECT * FROM index_quotes WHERE index=%s and series=%s and tenor=%s\",\n", " SQL_CON, params = (index, series, tenor), index_col=['date'], parse_dates=['date'])\n", " df.sort_index(inplace=True)\n", - " return df\n", + " return df\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "extensions": { + "jupyter_dashboards": { + "version": 1, + "views": { + "grid_default": { + "hidden": true + }, + "report_default": {} + } + } + } + }, + "outputs": [], + "source": [ "ig25_tranches = get_tranche_quotes('IG', 25, '5yr')\n", "ig25 = get_index_quotes('IG', 25, '5yr')\n", "ig25_tranches = ig25_tranches[['attach', 'trancheupfrontmid','indexrefspread','tranchedelta']]" @@ -86,7 +168,19 @@ { "cell_type": "code", "execution_count": null, - "metadata": {}, + "metadata": { + "extensions": { + "jupyter_dashboards": { + "version": 1, + "views": { + "grid_default": { + "hidden": true + }, + "report_default": {} + } + } + } + }, "outputs": [], "source": [ "ig25_tranches = ig25_tranches.groupby([pd.TimeGrouper('D', level=0), 'attach']).last().dropna()\n", @@ -98,7 +192,23 @@ { "cell_type": "code", "execution_count": null, - "metadata": {}, + "metadata": { + "extensions": { + "jupyter_dashboards": { + "version": 1, + "views": { + "grid_default": { + "col": 0, + "height": 7, + "hidden": false, + "row": 57, + "width": 9 + }, + "report_default": {} + } + } + } + }, "outputs": [], "source": [ "ig25_data = ig25_data.assign(tranche_adjusted =\n", @@ -111,7 +221,19 @@ { "cell_type": "code", "execution_count": null, - "metadata": {}, + "metadata": { + "extensions": { + "jupyter_dashboards": { + "version": 1, + "views": { + "grid_default": { + "hidden": true + }, + "report_default": {} + } + } + } + }, "outputs": [], "source": [ "#compute the accrued\n", @@ -121,7 +243,19 @@ { "cell_type": "code", "execution_count": null, - "metadata": {}, + "metadata": { + "extensions": { + "jupyter_dashboards": { + "version": 1, + "views": { + "grid_default": { + "hidden": true + }, + "report_default": {} + } + } + } + }, "outputs": [], "source": [ "#compute the daily pnl\n", @@ -136,16 +270,49 @@ { "cell_type": "code", "execution_count": null, - "metadata": {}, + "metadata": { + "extensions": { + "jupyter_dashboards": { + "version": 1, + "views": { + "grid_default": { + "col": 0, + "height": 11, + "hidden": false, + "row": 64, + "width": 4 + }, + "report_default": {} + } + } + } + }, "outputs": [], "source": [ - "ig25_data.xs(3, level='attach').plot(x='index_pnl', y='tranche_pnl', kind='scatter')" + "ig25_data.xs(3, level='attach').plot(x='index_pnl', y='tranche_pnl', kind='scatter')\n", + "plt.show()" ] }, { "cell_type": "code", "execution_count": null, - "metadata": {}, + "metadata": { + "extensions": { + "jupyter_dashboards": { + "version": 1, + "views": { + "grid_default": { + "col": 4, + "height": 19, + "hidden": false, + "row": 64, + "width": 4 + }, + "report_default": {} + } + } + } + }, "outputs": [], "source": [ "import statsmodels.api as sm\n", @@ -159,7 +326,19 @@ { "cell_type": "code", "execution_count": null, - "metadata": {}, + "metadata": { + "extensions": { + "jupyter_dashboards": { + "version": 1, + "views": { + "grid_default": { + "hidden": true + }, + "report_default": {} + } + } + } + }, "outputs": [], "source": [ "def rolling_ols(df, formula, window=20):\n", @@ -175,19 +354,51 @@ { "cell_type": "code", "execution_count": null, - "metadata": {}, + "metadata": { + "extensions": { + "jupyter_dashboards": { + "version": 1, + "views": { + "grid_default": { + "col": 8, + "height": 11, + "hidden": false, + "row": 64, + "width": 4 + }, + "report_default": {} + } + } + } + }, "outputs": [], "source": [ - "rols = rolling_ols(ig25_data, 'tranche_pnl~0+index_pnl+np.square(index_pnl)')\n", - "rols.columns = ['delta','gamma']\n", - "test = rols.join(ig25_data)[['delta','tranchedelta']]\n", + "rols = rolling_ols(ig25_data.xs(15, level='attach'), 'tranche_pnl~0+index_pnl+np.square(index_pnl)')\n", + "rols.columns = ['delta', 'gamma']\n", + "test = rols.join(ig25_data.xs(15, level='attach'))[['delta', 'tranchedelta']]\n", "test.plot()" ] }, { "cell_type": "code", "execution_count": null, - "metadata": {}, + "metadata": { + "extensions": { + "jupyter_dashboards": { + "version": 1, + "views": { + "grid_default": { + "col": 0, + "height": 4, + "hidden": false, + "row": 75, + "width": 4 + }, + "report_default": {} + } + } + } + }, "outputs": [], "source": [ "ig25_data.closespread.apply(np.log).diff().std()*np.sqrt(250)" @@ -196,7 +407,23 @@ { "cell_type": "code", "execution_count": null, - "metadata": {}, + "metadata": { + "extensions": { + "jupyter_dashboards": { + "version": 1, + "views": { + "grid_default": { + "col": 8, + "height": 11, + "hidden": false, + "row": 75, + "width": 4 + }, + "report_default": {} + } + } + } + }, "outputs": [], "source": [ "ig25 = get_index_quotes('IG', 25, '5yr')\n", @@ -206,18 +433,49 @@ { "cell_type": "code", "execution_count": null, - "metadata": {}, + "metadata": { + "extensions": { + "jupyter_dashboards": { + "version": 1, + "views": { + "grid_default": { + "col": 0, + "height": 11, + "hidden": false, + "row": 79, + "width": 4 + }, + "report_default": {} + } + } + } + }, "outputs": [], "source": [ "ig25 = get_index_quotes('IG', 25, '5yr')\n", - "returns = ig25.closespread.pct_change().dropna()\n", - "returns.plot()" + "returns = ig25.closespread.pct_change().dropna()" ] }, { "cell_type": "code", "execution_count": null, - "metadata": {}, + "metadata": { + "extensions": { + "jupyter_dashboards": { + "version": 1, + "views": { + "grid_default": { + "col": 4, + "height": 32, + "hidden": false, + "row": 86, + "width": 5 + }, + "report_default": {} + } + } + } + }, "outputs": [], "source": [ "from arch import arch_model\n", @@ -229,7 +487,23 @@ { "cell_type": "code", "execution_count": null, - "metadata": {}, + "metadata": { + "extensions": { + "jupyter_dashboards": { + "version": 1, + "views": { + "grid_default": { + "col": 0, + "height": 12, + "hidden": false, + "row": 90, + "width": 4 + }, + "report_default": {} + } + } + } + }, "outputs": [], "source": [ "res.plot(annualize='D')" @@ -238,18 +512,51 @@ { "cell_type": "code", "execution_count": null, - "metadata": {}, + "metadata": { + "extensions": { + "jupyter_dashboards": { + "version": 1, + "views": { + "grid_default": { + "col": 0, + "height": 11, + "hidden": false, + "row": 102, + "width": 4 + }, + "report_default": {} + } + } + } + }, "outputs": [], "source": [ "import math\n", "annualized_vol = res.conditional_volatility * math.sqrt(252)\n", - "annualized_vol.plot()" + "annualized_vol.plot()\n", + "plt.show()" ] }, { "cell_type": "code", "execution_count": null, - "metadata": {}, + "metadata": { + "extensions": { + "jupyter_dashboards": { + "version": 1, + "views": { + "grid_default": { + "col": 0, + "height": 9, + "hidden": false, + "row": 113, + "width": 4 + }, + "report_default": {} + } + } + } + }, "outputs": [], "source": [ "returns[returns.abs()>0.04].groupby(pd.TimeGrouper('M')).count()" @@ -258,7 +565,23 @@ { "cell_type": "code", "execution_count": null, - "metadata": {}, + "metadata": { + "extensions": { + "jupyter_dashboards": { + "version": 1, + "views": { + "grid_default": { + "col": 4, + "height": 15, + "hidden": false, + "row": 118, + "width": 4 + }, + "report_default": {} + } + } + } + }, "outputs": [], "source": [ "returns['2016-03']" @@ -267,7 +590,23 @@ { "cell_type": "code", "execution_count": null, - "metadata": {}, + "metadata": { + "extensions": { + "jupyter_dashboards": { + "version": 1, + "views": { + "grid_default": { + "col": 0, + "height": 7, + "hidden": false, + "row": 133, + "width": 10 + }, + "report_default": {} + } + } + } + }, "outputs": [], "source": [ "ig25.head()" @@ -276,30 +615,49 @@ { "cell_type": "code", "execution_count": null, - "metadata": {}, + "metadata": { + "extensions": { + "jupyter_dashboards": { + "version": 1, + "views": { + "grid_default": { + "col": 8, + "height": 7, + "hidden": false, + "row": 118, + "width": 4 + }, + "report_default": {} + } + } + } + }, "outputs": [], "source": [ "ig25.index" ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "pd.read_sql?" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] } ], "metadata": { + "extensions": { + "jupyter_dashboards": { + "activeView": "grid_default", + "version": 1, + "views": { + "grid_default": { + "cellMargin": 10, + "defaultCellHeight": 20, + "maxColumns": 12, + "name": "grid", + "type": "grid" + }, + "report_default": { + "name": "report", + "type": "report" + } + } + } + }, "kernelspec": { "display_name": "Python 3", "language": "python", |
