{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import pandas as pd\n", "import numpy as np\n", "from analytics.basket_index import MarkitBasketIndex\n", "import matplotlib.pyplot as plt\n", "\n", "from db import dbengine\n", "from ipywidgets import widgets\n", "engine = dbengine('serenitasdb')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "w = widgets.Dropdown(\n", " options=['IG', 'HY', 'EU'],\n", " value='IG',\n", " description='Index:',\n", " disabled=False,\n", ")\n", "w" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "w_1 = widgets.IntSlider(value=30, min=22, max=30, description = 'Series')\n", "w_1" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "trade_date = (pd.datetime.today() - pd.offsets.BDay(1)).date()\n", "index_type = w.value\n", "series = w_1.value" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "sql_string = \"select * from index_members(%s, %s)\"\n", "df = pd.read_sql_query(sql_string, engine, params=(index_type + str(series), trade_date), index_col=['markit_ticker'])\n", "df1 = pd.read_sql_query(sql_string, engine, params=(index_type + str(series-1), trade_date), index_col=['markit_ticker'])" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#Removals\n", "df1.loc[df1.index.difference(df.index)]" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#Additions\n", "df.loc[df.index.difference(df1.index)]" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "date_range = pd.bdate_range(trade_date - 52 * pd.offsets.Week(), trade_date, freq='5B')\n", "index = MarkitBasketIndex(index_type, series, ['5yr'])\n", "default_prob = {}\n", "maturity = np.array([np.datetime64(index.maturities[0]).view('int') + 134774])\n", "for d in date_range:\n", " index.value_date = d\n", " surv_prob, tickers = index.survival_matrix(maturity)\n", " default_prob[d] = pd.Series(1 - np.ravel(surv_prob), index=tickers)\n", "default_prob = pd.concat(default_prob)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#Top 20 highest cumulative\n", "top20 = default_prob.unstack(-1)[default_prob[trade_date].nlargest(20).index]\n", "top20.index.name='date'\n", "top20.columns.name='tickers'\n", "top20.plot(title=f'market implied default probabilities to {index.maturities[0]}')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "title = 'market implied default probabilities to ' + str(temp.maturities[0])\n", "fig, ax = plt.subplots()\n", "ax.plot(dp_top.T)\n", "ax.set(xlabel='date', ylabel='probability',\n", " title=title)\n", "ax.legend(dp_top.index, loc='upper center', bbox_to_anchor=(1.3, 1), ncol=1)\n", "fig.set_size_inches(10, 6)\n", "fig.tight_layout()" ] } ], "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 }