import pandas as pd import feather from option_trades import index_price_returns from arch import arch_model returns = pd.concat([index_price_returns(index=i) for i in ['IG', 'HY']], axis=1) returns.columns = ['ig', 'hy'] feather.write_dataframe(returns.reset_index(), "/home/share/CorpCDOs/data/index_returns.fth") model = pd.ols(y = returns.hy, x=returns.ig) beta = model.beta.x am = arch_model(returns.ig.dropna()) res = am.fit() # 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.loc['ig', 'hy']/df.loc['ig', 'ig'], ('minor', 'major'))) feather.write_dataframe(beta_ewma.reset_index(), "/home/share/CorpCDOs/data/beta.fth")