示例#1
0
# Exercise 1 & 3

data = pd.read_csv('data/Chapter8_Data.csv',
                   parse_dates=True,
                   index_col='date')

returns = data.apply(np.log) - data.apply(np.log).shift()
returns.dropna(inplace=True)
returns *= scale
returns.plot()

# FHS

fhs = ZeroMean(returns['Close'])
garch = GARCH(p=1, q=1)
fhs.distribution = StudentsT()
fhs.volatility = garch
rst = fhs.fit()

print(rst)

rst.plot(annualize='D')

sns.distplot(rst.std_resid, fit=stats.t)

forecast_variance_1day = rst.forecast(horizon=1).variance.iloc[-1, 0]

rs = np.random.RandomState(1234)


def Bootstrap(samples, random_state: np.random.RandomState, size: int):
示例#2
0
        cor0.loc[p, 'cor'] = cor_num[0]
    else:
        cut = returns.loc[(returns['sp'] > score_sp) &
                          (returns['tn'] > score_tn), ]

        cor_num = stats.pearsonr(cut['sp'], cut['tn'])

        cor0.loc[p, 'cor'] = cor_num[0]

cor0.plot()

tsm_sp = ZeroMean(returns['sp'])
garch = GARCH()
tsm_sp.volatility = garch
tsm_sp.distribution = StudentsT()
rst_sp = tsm_sp.fit()

filtered_sp = rst_sp.std_resid

tsm_tn = ZeroMean(returns['tn'])
garch = GARCH()
tsm_tn.volatility = garch
tsm_tn.distribution = StudentsT()
rst_tn = tsm_tn.fit()

filtered_tn = rst_tn.std_resid

filtered_returns = pd.DataFrame(dict(sp=filtered_sp, tn=filtered_tn),
                                index=returns.index)
示例#3
0
VaRtn = -stats.norm.ppf(q=0.01, scale=std_tn)
VaRp = -stats.norm.ppf(q=0.01, scale=std_p)

print(VaRp < (VaRsp + VaRtn) / 2)

# Exercise 4

omega = 1.5E-6 * scale**2
alpha = 0.05
beta = 0.8
theta = 1.25

tsm = ZeroMean(returns['sp500'])
ngarch = NGARCH11(np.array([omega, alpha, beta, theta]))
tsm.volatility = ngarch
tsm.distribution = StudentsT()
sp500_rst = tsm.fit()

print(sp500_rst)
sp500_rst.plot(annualize='D')

sns.distplot(sp500_rst.std_resid, fit=stats.t)

print(
    ngarch.is_valid(sp500_rst.params['alpha'], sp500_rst.params['beta'],
                    sp500_rst.params['theta']))

sm.graphics.qqplot(sp500_rst.std_resid, line='45')

returns['std_sp500'] = sp500_rst.std_resid