import math import pandas as pd import feather from index_data import index_returns from arch import arch_model from statsmodels.graphics.tsaplots import plot_acf, plot_pacf returns = index_returns(index=['IG', 'HY'], tenor='5yr') returns = (returns. reset_index(level='series'). groupby(level=['date','index']). nth(-1)) ## lastest returns returns = (returns. set_index('series', append=True)['price_return']. unstack(level='index')) returns.columns = [col.lower() for col in returns.columns] feather.write_dataframe(returns.reset_index(), "/home/share/CorpCDOs/data/index_returns.fth") # three ways of computing the volatility # 20 days simple moving average vol_sma = returns.hy.rolling(20).std() * math.sqrt(252) vol_ewma = returns.hy.ewm(span=20).std() * math.sqrt(252) # GARCH(1,1) # we scale returns by 10 to help with the fitting scale = 10 am = arch_model(scale * returns.hy.dropna()) res = am.fit() vol_garch = res.conditional_volatility * math.sqrt(252)/scale vol = pd.concat([vol_sma, vol_ewma, vol_garch], axis=1) ## let's get the betas beta_ewma = (returns. ewm(span=20). cov(). apply(lambda df:df.at['ig', 'hy']/df.at['ig', 'ig'], ('minor', 'major'))) resids = returns.ig-beta_ewma*returns.hy # feather.write_dataframe(beta_ewma.reset_index(), # "/home/share/CorpCDOs/data/beta.fth")