diff options
Diffstat (limited to 'python')
| -rw-r--r-- | python/exploration/hybb.py | 45 |
1 files changed, 45 insertions, 0 deletions
diff --git a/python/exploration/hybb.py b/python/exploration/hybb.py new file mode 100644 index 00000000..062f91fb --- /dev/null +++ b/python/exploration/hybb.py @@ -0,0 +1,45 @@ +from analytics import on_the_run +from analytics.basket_index import BasketIndex +from analytics.utils import bus_day +from analytics.index_data import index_returns +import datetime +import numpy as np +import pandas as pd + +dr = pd.date_range("2015-09-27", "2019-11-25", freq=bus_day) +prices = {} +s = -1 +for d in dr: + otr = on_the_run("HY", d) + if otr > s: + s = otr + basket_index = BasketIndex("HYBB", s, ["5yr"], value_date=d) + basket_index.value_date = d + prices[(s, d)] = 100 * (1 - basket_index.pv()["5yr"]) +hybb = pd.DataFrame.from_dict(prices, orient="index") +hybb.index = pd.MultiIndex.from_tuples(hybb.index) + +hybb.columns = ["HYBB"] +hybb = hybb.pct_change().reset_index(level=0, drop=True) +df = index_returns( + index="HY", + tenor="5yr", + series=[25, 26, 27, 28, 29, 30, 31, 32, 33], + from_date=datetime.date(2015, 9, 27), + years=None, +) +hy_returns = df["price_return"].groupby("date").nth(-1) +hy_returns.name = "HY" + +returns = pd.concat([hybb["HYBB"], hy_returns], axis=1) +# weekly +beta_weekly = ( + returns.ewm(span=5).cov().groupby(level=0).apply(lambda df: df / np.diag(df)) +) +beta_weekly = beta_weekly.xs("HY", level=1)["HYBB"] + +## monthly +beta_monthly = ( + returns.ewm(span=20).cov().groupby(level=0).apply(lambda df: df / np.diag(df)) +) +beta_monthly = beta_monthly.xs("HY", level=1)["HYBB"] |
