-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathChapter4.py
More file actions
176 lines (127 loc) · 3.43 KB
/
Chapter4.py
File metadata and controls
176 lines (127 loc) · 3.43 KB
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
import numpy as np
import pandas as pd
from arch import arch_model
from arch.univariate import ConstantMean, GARCH, ZeroMean, StudentsT
import seaborn as sb
from scipy import stats
import statsmodels.api as sm
from archEx.NGARCH import NGARCH11, FixedNGARCH11
from archEx.GARCHX import GARCHX
scale = 100.0
# Exercise 1
spClose = pd.read_csv(
'data/Chapter4_Data1.csv', parse_dates=True,
index_col='Date', squeeze=True)
spClose.plot()
returns = spClose.apply(np.log) - spClose.shift(1).apply(np.log)
returns *= scale
returns.dropna(inplace=True)
returns.plot()
# Method 1
omega = 0.000005 * scale ** 2
alpha = 0.1
beta = 0.85
garch = arch_model(returns)
rst = garch.fit(
starting_values=np.array([0.0, omega, alpha, beta]))
print(rst)
rst.plot(annualize='D')
# Method 2
tsm = ConstantMean(returns)
garch = GARCH(p=1, q=1)
tsm.volatility = garch
rst = tsm.fit(
starting_values=np.array([0.0, omega, alpha, beta]))
print(rst)
rst.plot(annualize='D')
sb.distplot(rst.resid, fit=stats.norm)
# Exercise 2
spClose = pd.read_csv(
'data/Chapter4_Data1.csv', parse_dates=True,
index_col='Date', squeeze=True)
spClose.plot()
returns = spClose.apply(np.log) - spClose.shift(1).apply(np.log)
returns *= scale
returns.dropna(inplace=True)
returns.plot()
omega = 0.000005 * scale ** 2
alpha = 0.07
beta = 0.85
theta = 0.5
# using NGARCH11
tsm = ConstantMean(returns)
ngarch = NGARCH11(
np.array([omega, alpha, beta, theta]))
tsm.volatility = ngarch
rst = tsm.fit()
print(rst)
rst.plot(annualize='D')
sb.distplot(rst.resid, fit=stats.norm)
print(
ngarch.is_valid(
rst.params['alpha'],
rst.params['beta'],
rst.params['theta']))
returns2 = returns ** 2.0
filtered_returns2 = rst.std_resid ** 2.0
sm.graphics.tsa.plot_acf(returns2, lags=100)
sm.graphics.tsa.plot_acf(filtered_returns2, lags=100)
# using FixedNGARCH11
tsm = ConstantMean(returns)
fixed_ngarch = FixedNGARCH11(
1.373877, # author's result
np.array([omega, alpha, beta]))
tsm.volatility = fixed_ngarch
rst = tsm.fit()
print(rst)
rst.plot(annualize='D')
sb.distplot(rst.resid, fit=stats.norm)
returns2 = returns ** 2.0
filtered_returns2 = rst.std_resid ** 2.0
sm.graphics.tsa.plot_acf(returns2, lags=100)
sm.graphics.tsa.plot_acf(filtered_returns2, lags=100)
# Exercise 3
spClose = pd.read_csv(
'data/Chapter4_Data1.csv', parse_dates=True,
index_col='Date', squeeze=True)
vix = pd.read_csv(
'data/Chapter4_Data2.csv', parse_dates=True,
index_col='Date', squeeze=True)
vix *= scale
vix2 = vix ** 2.0 / 252.0
returns = spClose.apply(np.log) - spClose.shift(1).apply(np.log)
returns *= scale
returns.dropna(inplace=True)
# Correlation of sigma and vix
tsm = ConstantMean(returns)
garch = GARCH(p=1, q=1)
tsm.volatility = garch
tsm.distribution = StudentsT()
rst = tsm.fit()
sigma2 = rst.conditional_volatility ** 2.0
print(rst)
sb.regplot(
x=vix2,
y=sigma2,
marker='.')
print(stats.pearsonr(vix2, sigma2))
'''
The correlation of sigma2 and vix2 is really high.
Because of multicollinearity, vix2 is not a good explanatory
variable for (N)GARCHX model.
Maybe, we can replace sigma2 by vix2 in (N)GARCHX model.
'''
# using GARCHX(1,1)-normal
tsm = ZeroMean(returns)
garchx = GARCHX(vix2.values)
tsm.volatility = garchx
rst = tsm.fit()
print(rst)
rst.plot(annualize='D')
# using GARCHX(1,0)-normal
tsm = ZeroMean(returns)
garchx = GARCHX(vix2.values, q=0)
tsm.volatility = garchx
rst = tsm.fit()
print(rst)
rst.plot(annualize='D')