import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from arch import arch_model
from statsmodels.stats.diagnostic import het_arch
data = pd.read_csv('SSEC_daily.csv', encoding='GB2312', usecols=[2, 6])
data
| 交易日期 | 收盘 | |
|---|---|---|
| 0 | 1997/1/2 | 919.44 |
| 1 | 1997/1/3 | 899.61 |
| 2 | 1997/1/6 | 876.50 |
| 3 | 1997/1/7 | 898.17 |
| 4 | 1997/1/8 | 896.41 |
| ... | ... | ... |
| 5086 | 2017/12/29 | 3307.17 |
| 5087 | 2018/1/2 | 3348.33 |
| 5088 | 2018/1/3 | 3369.11 |
| 5089 | 2018/1/4 | 3385.71 |
| 5090 | 2018/1/5 | 3391.75 |
5091 rows × 2 columns
data.columns = ['date', 'close']
p = data.loc[:, ['close']].values
r = np.log(p[1:]) - np.log(p[:-1])
plt.plot(r)
[<matplotlib.lines.Line2D at 0x2055742b400>]
# mu常数,波动率 GARCH(1, 1),分布 正态
am_garch = arch_model(r, mean='constant', vol='garch', p=1, q=1, dist='normal')
res_garch = am_garch.fit()
print(res_garch.summary())
Iteration: 1, Func. Count: 6, Neg. LLF: 1430893525100.7856
Iteration: 2, Func. Count: 18, Neg. LLF: -14474.835667164152
Optimization terminated successfully (Exit mode 0)
Current function value: -14474.835659373894
Iterations: 6
Function evaluations: 18
Gradient evaluations: 2
Constant Mean - GARCH Model Results
==============================================================================
Dep. Variable: y R-squared: 0.000
Mean Model: Constant Mean Adj. R-squared: 0.000
Vol Model: GARCH Log-Likelihood: 14474.8
Distribution: Normal AIC: -28941.7
Method: Maximum Likelihood BIC: -28915.5
No. Observations: 5090
Date: Thu, May 25 2023 Df Residuals: 5089
Time: 18:21:52 Df Model: 1
Mean Model
============================================================================
coef std err t P>|t| 95.0% Conf. Int.
----------------------------------------------------------------------------
mu 2.2201e-04 2.409e-05 9.215 3.125e-20 [1.748e-04,2.692e-04]
Volatility Model
============================================================================
coef std err t P>|t| 95.0% Conf. Int.
----------------------------------------------------------------------------
omega 5.2878e-06 1.591e-12 3.323e+06 0.000 [5.288e-06,5.288e-06]
alpha[1] 0.1000 1.077e-02 9.283 1.653e-20 [7.889e-02, 0.121]
beta[1] 0.8800 9.380e-03 93.813 0.000 [ 0.862, 0.898]
============================================================================
Covariance estimator: robust
C:\ProgramData\Anaconda3\lib\site-packages\arch\univariate\base.py:310: DataScaleWarning: y is poorly scaled, which may affect convergence of the optimizer when estimating the model parameters. The scale of y is 0.0002644. Parameter estimation work better when this value is between 1 and 1000. The recommended rescaling is 100 * y. This warning can be disabled by either rescaling y before initializing the model or by setting rescale=False. warnings.warn(
# mu AR,波动率 GARCH(1, 1),分布 正态
am_ar_garch = arch_model(r, mean='ar', lags=1, vol='garch', p=1, q=1, dist='normal')
res_am_ar_garch = am_ar_garch.fit()
print(res_am_ar_garch.summary())
Iteration: 1, Func. Count: 7, Neg. LLF: 887808619677.113
Iteration: 2, Func. Count: 20, Neg. LLF: -14472.862358762366
Optimization terminated successfully (Exit mode 0)
Current function value: -14472.86235098874
Iterations: 6
Function evaluations: 20
Gradient evaluations: 2
AR - GARCH Model Results
==============================================================================
Dep. Variable: y R-squared: 0.000
Mean Model: AR Adj. R-squared: -0.000
Vol Model: GARCH Log-Likelihood: 14472.9
Distribution: Normal AIC: -28935.7
Method: Maximum Likelihood BIC: -28903.1
No. Observations: 5089
Date: Thu, May 25 2023 Df Residuals: 5087
Time: 18:31:10 Df Model: 2
Mean Model
=============================================================================
coef std err t P>|t| 95.0% Conf. Int.
-----------------------------------------------------------------------------
Const 2.1909e-04 4.828e-05 4.538 5.683e-06 [1.245e-04,3.137e-04]
y[1] 0.0114 1.581e-02 0.720 0.472 [-1.961e-02,4.238e-02]
Volatility Model
============================================================================
coef std err t P>|t| 95.0% Conf. Int.
----------------------------------------------------------------------------
omega 5.2862e-06 6.250e-11 8.458e+04 0.000 [5.286e-06,5.286e-06]
alpha[1] 0.1000 1.108e-02 9.024 1.817e-19 [7.828e-02, 0.122]
beta[1] 0.8800 9.690e-03 90.812 0.000 [ 0.861, 0.899]
============================================================================
Covariance estimator: robust
C:\ProgramData\Anaconda3\lib\site-packages\arch\univariate\base.py:310: DataScaleWarning: y is poorly scaled, which may affect convergence of the optimizer when estimating the model parameters. The scale of y is 0.0002643. Parameter estimation work better when this value is between 1 and 1000. The recommended rescaling is 100 * y. This warning can be disabled by either rescaling y before initializing the model or by setting rescale=False. warnings.warn(
sim_garch = arch_model(None, mean='constant', vol='garch', dist='normal', p=1, q=1)
# print(res_garch.params)
# sim_paras = res_garch.params
sim_paras = pd.Series([0.0006, 0.00008, 0.15, 0.8], index=['mu', 'omega', 'alpha[1]', 'beta[1]'])
sim_garch_data = sim_garch.simulate(sim_paras, 1000)
plt.plot(sim_garch_data)
[<matplotlib.lines.Line2D at 0x20559042460>, <matplotlib.lines.Line2D at 0x20559042520>, <matplotlib.lines.Line2D at 0x205590425e0>]
am_egarch = arch_model(r*100, mean='constant', vol='egarch', p=1, q=1, o=1)
res_egarch = am_egarch.fit()
print(res_egarch.summary())
Iteration: 1, Func. Count: 7, Neg. LLF: 41310.24007067039
Iteration: 2, Func. Count: 20, Neg. LLF: 41311.930866633185
Iteration: 3, Func. Count: 31, Neg. LLF: 1053056504.8062321
Iteration: 4, Func. Count: 41, Neg. LLF: 9326.272080588187
Iteration: 5, Func. Count: 49, Neg. LLF: 69534.02742630933
Iteration: 6, Func. Count: 57, Neg. LLF: 8932.101523765676
Iteration: 7, Func. Count: 64, Neg. LLF: 9020.736319561376
Iteration: 8, Func. Count: 72, Neg. LLF: 8930.874073790517
Iteration: 9, Func. Count: 79, Neg. LLF: 8907.632705122136
Iteration: 10, Func. Count: 85, Neg. LLF: 8907.626055527973
Iteration: 11, Func. Count: 91, Neg. LLF: 8907.625999106054
Iteration: 12, Func. Count: 97, Neg. LLF: 8907.626001698038
Iteration: 13, Func. Count: 104, Neg. LLF: 8907.625987108073
Iteration: 14, Func. Count: 109, Neg. LLF: 8907.625987105723
Optimization terminated successfully (Exit mode 0)
Current function value: 8907.625987108073
Iterations: 14
Function evaluations: 109
Gradient evaluations: 14
Constant Mean - EGARCH Model Results
==============================================================================
Dep. Variable: y R-squared: 0.000
Mean Model: Constant Mean Adj. R-squared: 0.000
Vol Model: EGARCH Log-Likelihood: -8907.63
Distribution: Normal AIC: 17825.3
Method: Maximum Likelihood BIC: 17857.9
No. Observations: 5090
Date: Thu, May 25 2023 Df Residuals: 5089
Time: 18:46:27 Df Model: 1
Mean Model
============================================================================
coef std err t P>|t| 95.0% Conf. Int.
----------------------------------------------------------------------------
mu 0.0145 3.658e-03 3.969 7.232e-05 [7.347e-03,2.169e-02]
Volatility Model
==============================================================================
coef std err t P>|t| 95.0% Conf. Int.
------------------------------------------------------------------------------
omega 0.0181 4.295e-03 4.219 2.451e-05 [9.703e-03,2.654e-02]
alpha[1] 0.1737 2.338e-02 7.431 1.080e-13 [ 0.128, 0.220]
gamma[1] -0.0232 9.989e-03 -2.326 2.002e-02 [-4.281e-02,-3.657e-03]
beta[1] 0.9886 3.916e-03 252.442 0.000 [ 0.981, 0.996]
==============================================================================
Covariance estimator: robust
result_arch_test = het_arch(r, nlags=10)
print(result_arch_test)
(452.0287706945414, 7.713522349298394e-91, 49.51054630896014, 2.8996625106266254e-95)