-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgarch2.py
100 lines (53 loc) · 1.53 KB
/
garch2.py
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
#!/usr/bin/env python
# coding: utf-8
from IPython import get_ipython
# ## More from arch
# _This setup code is required to run in an IPython notebook_
# In[1]:
get_ipython().run_line_magic('matplotlib', 'auto')
import matplotlib.pyplot as plt
import seaborn
seaborn.set_style("darkgrid")
plt.rc("figure", figsize=(16, 6))
plt.rc("savefig", dpi=90)
plt.rc("font", family="sans-serif")
plt.rc("font", size=14)
# In[2]:
from sqlalchemy import create_engine
engine = create_engine('mysql+pymysql://localhost', connect_args={'read_default_file': '~/.mytest.cnf'})
# In[3]:
import pandas as pd
with engine.connect() as conn:
SQL_query = pd.read_sql_query(
'''select tradedate, pct_close from USEQ_HIST where symbol='MS'
and volume>0
order by tradedate''',
conn,
index_col='tradedate'
)
df = pd.DataFrame(SQL_query, columns=['pct_close'])
df.head()
# In[4]:
ax = df.plot()
xlim = ax.set_xlim(df.index.min(), df.index.max())
# In[5]:
from arch import arch_model
# ## GJR-Garch with Student's T
# In[6]:
am = arch_model(df, p=1, o=1, q=1, dist="StudentsT")
res = am.fit(update_freq=5)
print(res.summary())
# `plot()` can be used to quickly visualize the standardized residuals and conditional volatility.
#
# In[7]:
fig = res.plot(annualize="D")
# ## Parameters
#
# In[8]:
res.params
# ## Forecasts
# In[9]:
forecasts = res.forecast(reindex=False)
print(forecasts.mean.iloc[-3:])
forecasts = res.forecast(horizon=10, reindex=False)
print(forecasts.residual_variance.iloc[-3:])