diff options
| -rw-r--r-- | python/notebooks/Single Names Monitoring.ipynb | 66 |
1 files changed, 15 insertions, 51 deletions
diff --git a/python/notebooks/Single Names Monitoring.ipynb b/python/notebooks/Single Names Monitoring.ipynb index be3f2c47..5c25fb05 100644 --- a/python/notebooks/Single Names Monitoring.ipynb +++ b/python/notebooks/Single Names Monitoring.ipynb @@ -13,11 +13,11 @@ "import matplotlib.pyplot as plt\n", "import statsmodels.formula.api as smf\n", "\n", - "from analytics.basket_index import MarkitBasketIndex\n", - "from analytics import on_the_run\n", + "from serenitas.analytics.basket_index import MarkitBasketIndex\n", + "from serenitas.analytics.base import Trade\n", "from statsmodels.graphics.regressionplots import plot_fit\n", "from pygam import LinearGAM, s, f, GAM\n", - "from utils.db import dbengine\n", + "from serenitas.utils.db import dbengine\n", "\n", "serenitas_engine = dbengine('serenitasdb')" ] @@ -37,9 +37,9 @@ "metadata": {}, "outputs": [], "source": [ - "value_date = (pd.datetime.today() - pd.offsets.BDay(1)).date()\n", - "index_type = 'HY'\n", - "series = 33" + "value_date = (datetime.date.today() - pd.offsets.BDay(1)).date()\n", + "index_type = 'IG'\n", + "series = 37" ] }, { @@ -127,7 +127,8 @@ "outputs": [], "source": [ "#Top 20 highest cumulative\n", - "top20 = default_prob.unstack(-1)[default_prob[value_date].nlargest(20).index]\n", + "top20 = default_prob.unstack(-1)\n", + "top20 = top20[top20.iloc[-1].nlargest(25).index]\n", "top20.index.name='date'\n", "top20.columns.name='tickers'\n", "ax = top20.plot(title=f'market implied default probabilities to {index.maturities[0]}', figsize=(10,6))\n", @@ -186,40 +187,15 @@ "metadata": {}, "outputs": [], "source": [ - "####################### Get Gini on indices: this calc bombs a lot so let's do the ones that we were able to calc before (dropna)\n", - "df_gini_calc_temp = df.dropna().loc[datetime.date(2019,1,1):, :].reset_index('dist_on_the_run')[\n", - " ['index','series', 'tenor', 'duration', 'basis', 'closespread']]\n", - "temp = df_gini_calc_temp.apply(get_gini_spreadstdev, axis=1)\n", - "temp = pd.DataFrame(temp.values.tolist(), columns=['gini_spread','std_spread'], index=temp.index)\n", - "df_gini_calc = pd.concat([df_gini_calc_temp, temp], axis=1).dropna()" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "####################### Get Gini on the run only - same calc as above\n", - "df_gini_calc_temp = df.groupby(['date']).nth(-1).dropna()[\n", - " ['index','series', 'tenor', 'duration', 'basis', 'closespread']]\n", - "temp = df_gini_calc_temp.apply(get_gini_spreadstdev, axis=1)\n", - "temp = pd.DataFrame(temp.values.tolist(), columns=['gini_spread','std_spread'], index=temp.index)\n", - "df_gini_calc = pd.concat([df_gini_calc_temp, temp], axis=1).dropna()" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ "#######################OLS regression of NAV basis to spread/duration\n", "#basis_gini_model = smf.ols(\"basis ~ np.log(duration) + np.log(closespread) + np.log(gini_spread)\", data=df_gini_calc).fit()\n", "#basis_gini_model.summary()\n", "\n", "#Let's use a GAM model instead?\n", - "X = np.array(df_gini_calc[['duration', 'closespread', 'gini_spread']])\n", + "df_gini_calc = df.dropna().loc[datetime.date(2019,1,1):, :].reset_index('dist_on_the_run')[\n", + " ['index','series', 'tenor', 'duration', 'basis', 'closespread', 'gini']]\n", + "\n", + "X = np.array(df_gini_calc[['duration', 'closespread', 'gini']])\n", "y = np.array(df_gini_calc[['basis']])\n", "\n", "basis_model = GAM(s(0, constraints='concave') +\n", @@ -280,24 +256,12 @@ "execution_count": null, "metadata": {}, "outputs": [], - "source": [ - "## BBs \n", - "date_range = pd.bdate_range(end=value_date, freq='5B',periods=52*10)\n", - "index_spreads = {}\n", - "index_type = 'HYBB'\n", - "for d in date_range:\n", - " try:\n", - " index = MarkitBasketIndex(index_type, on_the_run(index_type, d), ['5yr'], value_date =d)\n", - " index_spreads[d] = index.spread()\n", - " except:\n", - " continue\n", - "index_spreads = pd.concat(index_spreads)" - ] + "source": [] } ], "metadata": { "kernelspec": { - "display_name": "Python 3", + "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, @@ -311,7 +275,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.8.0" + "version": "3.10.4" } }, "nbformat": 4, |
