{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import pandas as pd\n", "import numpy as np\n", "import analytics.basket_index as bkt\n", "import matplotlib.pyplot as plt\n", "\n", "from quantlib.time.api import Schedule, Rule, Date, Period, WeekendsOnly\n", "from quantlib.settings import Settings\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)).normalize().date()\n", "index = 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 + str(series), trade_date), index_col=['markit_ticker'])\n", "df1 = pd.read_sql_query(sql_string, engine, params=(index + 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": [ "dp_top = pd.DataFrame()\n", "date_range = pd.bdate_range(trade_date - 52 * pd.offsets.Week(), trade_date, freq='5B')\n", "for d in date_range:\n", " temp = bkt.MarkitBasketIndex(index, series, ['5yr',], value_date=d.date())\n", " temp1 = temp.survival_matrix(np.array([np.datetime64(temp.maturities[0]).view('int') + 134774]))\n", " temp1 = pd.DataFrame(1 - temp1[0], index=temp1[1], columns=[d.date()])\n", " dp_top = pd.concat([dp_top, temp1], axis=1)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#Top 20 highest cumulative\n", "dp_top = dp_top.sort_values(trade_date, ascending=False)[0:20]\n", "dp_top = dp_top.join(df1.name).set_index('name', drop=True)\n", "dp_top.iloc[:,-1]" ] }, { "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 }