股票Garch模型python实现代码-计算结果

这份输出是一个GARCH(1,1)模型对股票收益率的拟合结果,下面是对关键部分的解释:

模型概述

  • Dep. Variable: 因变量是log_returns(对数收益率)
  • Mean Model: 使用了常数均值模型(Constant Mean),即假设收益率的均值为常数
  • Vol Model: 使用GARCH(1,1)模型拟合波动率
  • Distribution: 假设收益率服从正态分布
  • No. Observations: 338个观测值

均值模型参数

                 coef    std err          t      P>|t|  95.0% Conf. Int.
------------------------------------------------------------------------
mu            -0.0335  9.835e-02     -0.340      0.734 [ -0.226,  0.159]
  • mu: 收益率的均值估计值为-0.0335
  • t统计量p值显示该参数在统计上不显著(p=0.734 > 0.05)
  • 置信区间包含0,进一步验证了不显著性

波动率模型参数

                 coef    std err          t      P>|t|     95.0% Conf. Int.
---------------------------------------------------------------------------
omega          0.5262      0.926      0.569      0.570    [ -1.288,  2.340]
alpha[1]       0.0877  6.055e-02      1.449      0.147 [-3.095e-02,  0.206]
beta[1]        0.7641      0.283      2.703  6.873e-03    [  0.210,  1.318]
  • omega: 长期波动率水平参数,估计值0.5262,但不显著(p=0.570)
  • alpha[1]: ARCH项系数,反映过去收益率对当前波动率的影响,估计值0.0877(p=0.147)
  • beta[1]: GARCH项系数,反映过去波动率对当前波动率的影响,估计值0.7641(高度显著)

模型诊断

  • ARCH+GARCH系数之和: α+β = 0.0877 + 0.7641 = 0.8518,接近1,说明波动率具有较强的持续性
  • 信息准则: AIC=1377.55,BIC=1392.84,可用于模型比较
  • 对数似然值: -684.774,可用于模型比较

实际意义

  • 该GARCH模型表明,股票收益率的波动率具有明显的聚类效应(β显著)
  • 过去的波动对当前波动有较强的预测能力
  • 虽然ARCH项系数不显著,但仍保留在模型中可能是因为理论上的需要或样本量限制
  • 常数均值模型的不显著性可能暗示股票收益率存在时间序列相关性,需要考虑更复杂的均值模型

建议

  • 考虑使用t分布替代正态分布,更适合金融收益率的尖峰厚尾特性
  • 检查残差的自相关性和ARCH效应,验证模型拟合效果
  • 考虑加入外部变量或使用EGARCH等非对称模型捕捉杠杆效应

GARCH Model python代码

#volatility forcast波动性预测
#实现DARCH模型的python代码:
import akshare as ak
import pandas as pd
from arch import arch_model
import matplotlib.pyplot as plt

# 获取股票数据
def get_stock_data(stock_code, start_date, end_date):
    stock_df = ak.stock_zh_a_hist(symbol=stock_code, period="daily", start_date=start_date, end_date=end_date)
    stock_df['日期'] = pd.to_datetime(stock_df['日期'])
    stock_df.set_index('日期', inplace=True)
    return stock_df

# 计算对数收益率
def calculate_log_returns(stock_data):
    stock_data['log_returns'] = 100 * stock_data['收盘'].pct_change().dropna()
    return stock_data.dropna()

# 拟合 GARCH 模型
def fit_garch_model(returns):
    model = arch_model(returns, vol='Garch', p=1, q=1)
    results = model.fit(disp='off')
    return results

# 主函数
def main():
    stock_code = "000937"  # 股票代码,这里以平安银行为例
    start_date = "20240101"
    end_date = "20250530"

    # 获取数据
    stock_data = get_stock_data(stock_code, start_date, end_date)
    # 计算对数收益率
    stock_data = calculate_log_returns(stock_data)
    # 提取对数收益率
    returns = stock_data['log_returns']

    # 拟合 GARCH 模型
    results = fit_garch_model(returns)

    # 输出模型结果
    print(results.summary())

    # 计算历史波动率
    historical_volatility = results.conditional_volatility

    # 绘制波动率预测图
    forecasts = results.forecast(horizon=1)
    # 提取最后一个预测方差并转换为标量
    last_forecast_value = forecasts.variance.iloc[-1].values[0]
    # 获取最后一个观测日期的下一天作为预测日期
    forecast_date = returns.index[-1] + pd.DateOffset(days=1)

    plt.figure(figsize=(12, 6))
    # 绘制历史波动率
    plt.plot(historical_volatility, label='Historical Volatility')
    # 绘制预测波动率
    plt.plot([forecast_date], [last_forecast_value], marker='o', color='red', label='Forecast Volatility')
    plt.title('GARCH(1,1) Volatility Forecast')
    plt.xlabel('Date')
    plt.ylabel('Volatility')
    plt.grid(True)
    plt.legend()
    plt.show()

if __name__ == "__main__":
    main()

C:\Users\czliu> & “C:/Program Files/Python313/python.exe” c:/Users/czliu/Documents/python/stock_GARCH_model.py

Constant Mean – GARCH Model Results

Dep. Variable: log_returns R-squared: 0.000
Mean Model: Constant Mean Adj. R-squared: 0.000
Vol Model: GARCH Log-Likelihood: -684.774
Distribution: Normal AIC: 1377.55
Method: Maximum Likelihood BIC: 1392.84
No. Observations: 338
Date: Fri, May 30 2025 Df Residuals: 337
Time: 23:15:55 Df Model: 1

Mean Model

coef std err t P>|t| 95.0% Conf. Int.

mu -0.0335 9.835e-02 -0.340 0.734 [ -0.226, 0.159]

Volatility Model波动性模型

coef std err t P>|t| 95.0% Conf. Int.

omega 0.5262 0.926 0.569 0.570 [ -1.288, 2.340]
alpha[1] 0.0877 6.055e-02 1.449 0.147 [-3.095e-02, 0.206]

beta[1] 0.7641 0.283 2.703 6.873e-03 [ 0.210, 1.318]

Covariance estimator: robust