aboutsummaryrefslogtreecommitdiffstats
path: root/python/beta_trade.py
blob: 34058090be964de3a02ccdac9823604415e0b05f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
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")