diff options
Diffstat (limited to 'python/notebooks')
| -rw-r--r-- | python/notebooks/Single Names Monitoring.ipynb | 33 |
1 files changed, 17 insertions, 16 deletions
diff --git a/python/notebooks/Single Names Monitoring.ipynb b/python/notebooks/Single Names Monitoring.ipynb index 8c3c8be1..0c32e617 100644 --- a/python/notebooks/Single Names Monitoring.ipynb +++ b/python/notebooks/Single Names Monitoring.ipynb @@ -8,14 +8,12 @@ "source": [ "import pandas as pd\n", "import numpy as np\n", - "import analytics.basket_index as bkt\n", + "from analytics.basket_index import MarkitBasketIndex\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')" + "engine = dbengine('serenitasdb')" ] }, { @@ -49,8 +47,8 @@ "metadata": {}, "outputs": [], "source": [ - "trade_date = (pd.datetime.today() - pd.offsets.BDay(1)).normalize().date()\n", - "index = w.value\n", + "trade_date = (pd.datetime.today() - pd.offsets.BDay(1)).date()\n", + "index_type = w.value\n", "series = w_1.value" ] }, @@ -61,8 +59,8 @@ "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'])" + "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'])" ] }, { @@ -91,13 +89,15 @@ "metadata": {}, "outputs": [], "source": [ - "dp_top = pd.DataFrame()\n", "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", - " 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)" + " 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)" ] }, { @@ -107,9 +107,10 @@ "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]" + "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]}')" ] }, { |
