生生不息

生生不息

AI实现ARCH模型

2025-06-28
AI实现ARCH模型

提示词

现在有一个名叫df的DataFrame,index是日期,包含名叫收盘价的列,是期货每日的历史行情数据,请演示如何拟合ARCH模型。

答案

from statsmodels.stats.diagnostic import het_arch
import pandas as pd
import numpy as np
from arch import arch_model

# 检验收益率平方的自相关性
# returns

arch_test = het_arch(returns)
print(f"ARCH-LM统计量: {arch_test[0]:.2f}, p值: {arch_test[1]:.4f}")

if arch_test[1] < 0.05:
    print("存在显著ARCH效应(p<0.05),适合拟合ARCH模型")
else:
    print("无显著ARCH效应,建议改用其他波动率模型")
# 基础ARCH(1)模型
model = arch_model(
    returns, 
    mean='Constant',   # 假设收益率均值为常数(常见于金融数据)
    vol='ARCH',        # 指定ARCH模型
    p=1                # 滞后阶数
)
result = model.fit(update_freq=5)  # 每5次迭代输出进度
print(result.summary())

结果:

ARCH-LM统计量: 64.91, p值: 0.0000
存在显著ARCH效应(p<0.05),适合拟合ARCH模型
Iteration:      5,   Func. Count:     28,   Neg. LLF: 3999.956257378006
Optimization terminated successfully    (Exit mode 0)
            Current function value: 3999.956241808769
            Iterations: 7
            Function evaluations: 35
            Gradient evaluations: 7
                      Constant Mean - ARCH Model Results                  
==============================================================================
Dep. Variable:                    收盘价   R-squared:                       0.000
Mean Model:             Constant Mean   Adj. R-squared:                  0.000
Vol Model:                       ARCH   Log-Likelihood:               -3999.96
Distribution:                  Normal   AIC:                           8005.91
Method:            Maximum Likelihood   BIC:                           8023.29
                                        No. Observations:                 2425
Date:                Sat, Jun 28 2025   Df Residuals:                     2424
Time:                        21:46:57   Df Model:                            1
                                  Mean Model                             
=============================================================================
                 coef    std err          t      P>|t|       95.0% Conf. Int.
-----------------------------------------------------------------------------
mu            -0.0175  2.856e-02     -0.613      0.540 [-7.348e-02,3.847e-02]
                            Volatility Model                        
========================================================================
                 coef    std err          t      P>|t|  95.0% Conf. Int.
------------------------------------------------------------------------
omega          1.2762      0.138      9.270  1.854e-20 [  1.006,  1.546]
alpha[1]       0.2902  6.036e-02      4.809  1.518e-06 [  0.172,  0.409]
========================================================================

Covariance estimator: robust