{"id":5693,"date":"2025-05-30T23:22:31","date_gmt":"2025-05-30T15:22:31","guid":{"rendered":"http:\/\/cnliutz.pgrm.cc\/?p=5693"},"modified":"2025-05-31T10:47:47","modified_gmt":"2025-05-31T02:47:47","slug":"%e8%82%a1%e7%a5%a8garch%e6%a8%a1%e5%9e%8bpython%e5%ae%9e%e7%8e%b0%e4%bb%a3%e7%a0%81-%e8%ae%a1%e7%ae%97%e7%bb%93%e6%9e%9c","status":"publish","type":"post","link":"http:\/\/cnliutz.wicp.vip\/?p=5693","title":{"rendered":"\u80a1\u7968Garch\u6a21\u578bpython\u5b9e\u73b0\u4ee3\u7801-\u8ba1\u7b97\u7ed3\u679c"},"content":{"rendered":"\n<p>\u8fd9\u4efd\u8f93\u51fa\u662f\u4e00\u4e2aGARCH(1,1)\u6a21\u578b\u5bf9\u80a1\u7968\u6536\u76ca\u7387\u7684\u62df\u5408\u7ed3\u679c\uff0c\u4e0b\u9762\u662f\u5bf9\u5173\u952e\u90e8\u5206\u7684\u89e3\u91ca\uff1a<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u6a21\u578b\u6982\u8ff0<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Dep. Variable<\/strong>: \u56e0\u53d8\u91cf\u662f<code>log_returns<\/code>\uff08\u5bf9\u6570\u6536\u76ca\u7387\uff09<\/li>\n\n\n\n<li><strong>Mean Model<\/strong>: \u4f7f\u7528\u4e86\u5e38\u6570\u5747\u503c\u6a21\u578b\uff08Constant Mean\uff09\uff0c\u5373\u5047\u8bbe\u6536\u76ca\u7387\u7684\u5747\u503c\u4e3a\u5e38\u6570<\/li>\n\n\n\n<li><strong>Vol Model<\/strong>: \u4f7f\u7528GARCH(1,1)\u6a21\u578b\u62df\u5408\u6ce2\u52a8\u7387<\/li>\n\n\n\n<li><strong>Distribution<\/strong>: \u5047\u8bbe\u6536\u76ca\u7387\u670d\u4ece\u6b63\u6001\u5206\u5e03<\/li>\n\n\n\n<li><strong>No. Observations<\/strong>: 338\u4e2a\u89c2\u6d4b\u503c<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">\u5747\u503c\u6a21\u578b\u53c2\u6570<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>                 coef    std err          t      P&gt;|t|  95.0% Conf. Int.\n------------------------------------------------------------------------\nmu            -0.0335  9.835e-02     -0.340      0.734 &#91; -0.226,  0.159]<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>mu<\/strong>: \u6536\u76ca\u7387\u7684\u5747\u503c\u4f30\u8ba1\u503c\u4e3a-0.0335<\/li>\n\n\n\n<li><strong>t\u7edf\u8ba1\u91cf<\/strong>\u548c<strong>p\u503c<\/strong>\u663e\u793a\u8be5\u53c2\u6570\u5728\u7edf\u8ba1\u4e0a\u4e0d\u663e\u8457\uff08p=0.734 > 0.05\uff09<\/li>\n\n\n\n<li><strong>\u7f6e\u4fe1\u533a\u95f4<\/strong>\u5305\u542b0\uff0c\u8fdb\u4e00\u6b65\u9a8c\u8bc1\u4e86\u4e0d\u663e\u8457\u6027<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">\u6ce2\u52a8\u7387\u6a21\u578b\u53c2\u6570<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>                 coef    std err          t      P&gt;|t|     95.0% Conf. Int.\n---------------------------------------------------------------------------\nomega          0.5262      0.926      0.569      0.570    &#91; -1.288,  2.340]\nalpha&#91;1]       0.0877  6.055e-02      1.449      0.147 &#91;-3.095e-02,  0.206]\nbeta&#91;1]        0.7641      0.283      2.703  6.873e-03    &#91;  0.210,  1.318]<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>omega<\/strong>: \u957f\u671f\u6ce2\u52a8\u7387\u6c34\u5e73\u53c2\u6570\uff0c\u4f30\u8ba1\u503c0.5262\uff0c\u4f46\u4e0d\u663e\u8457\uff08p=0.570\uff09<\/li>\n\n\n\n<li><strong>alpha[1]<\/strong>: ARCH\u9879\u7cfb\u6570\uff0c\u53cd\u6620\u8fc7\u53bb\u6536\u76ca\u7387\u5bf9\u5f53\u524d\u6ce2\u52a8\u7387\u7684\u5f71\u54cd\uff0c\u4f30\u8ba1\u503c0.0877\uff08p=0.147\uff09<\/li>\n\n\n\n<li><strong>beta[1]<\/strong>: GARCH\u9879\u7cfb\u6570\uff0c\u53cd\u6620\u8fc7\u53bb\u6ce2\u52a8\u7387\u5bf9\u5f53\u524d\u6ce2\u52a8\u7387\u7684\u5f71\u54cd\uff0c\u4f30\u8ba1\u503c0.7641\uff08\u9ad8\u5ea6\u663e\u8457\uff09<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">\u6a21\u578b\u8bca\u65ad<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>ARCH+GARCH\u7cfb\u6570\u4e4b\u548c<\/strong>: \u03b1+\u03b2 = 0.0877 + 0.7641 = 0.8518\uff0c\u63a5\u8fd11\uff0c\u8bf4\u660e\u6ce2\u52a8\u7387\u5177\u6709\u8f83\u5f3a\u7684\u6301\u7eed\u6027<\/li>\n\n\n\n<li><strong>\u4fe1\u606f\u51c6\u5219<\/strong>: AIC=1377.55\uff0cBIC=1392.84\uff0c\u53ef\u7528\u4e8e\u6a21\u578b\u6bd4\u8f83<\/li>\n\n\n\n<li><strong>\u5bf9\u6570\u4f3c\u7136\u503c<\/strong>: -684.774\uff0c\u53ef\u7528\u4e8e\u6a21\u578b\u6bd4\u8f83<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">\u5b9e\u9645\u610f\u4e49<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u8be5GARCH\u6a21\u578b\u8868\u660e\uff0c\u80a1\u7968\u6536\u76ca\u7387\u7684\u6ce2\u52a8\u7387\u5177\u6709\u660e\u663e\u7684\u805a\u7c7b\u6548\u5e94\uff08\u03b2\u663e\u8457\uff09<\/li>\n\n\n\n<li>\u8fc7\u53bb\u7684\u6ce2\u52a8\u5bf9\u5f53\u524d\u6ce2\u52a8\u6709\u8f83\u5f3a\u7684\u9884\u6d4b\u80fd\u529b<\/li>\n\n\n\n<li>\u867d\u7136ARCH\u9879\u7cfb\u6570\u4e0d\u663e\u8457\uff0c\u4f46\u4ecd\u4fdd\u7559\u5728\u6a21\u578b\u4e2d\u53ef\u80fd\u662f\u56e0\u4e3a\u7406\u8bba\u4e0a\u7684\u9700\u8981\u6216\u6837\u672c\u91cf\u9650\u5236<\/li>\n\n\n\n<li>\u5e38\u6570\u5747\u503c\u6a21\u578b\u7684\u4e0d\u663e\u8457\u6027\u53ef\u80fd\u6697\u793a\u80a1\u7968\u6536\u76ca\u7387\u5b58\u5728\u65f6\u95f4\u5e8f\u5217\u76f8\u5173\u6027\uff0c\u9700\u8981\u8003\u8651\u66f4\u590d\u6742\u7684\u5747\u503c\u6a21\u578b<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">\u5efa\u8bae<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u8003\u8651\u4f7f\u7528t\u5206\u5e03\u66ff\u4ee3\u6b63\u6001\u5206\u5e03\uff0c\u66f4\u9002\u5408\u91d1\u878d\u6536\u76ca\u7387\u7684\u5c16\u5cf0\u539a\u5c3e\u7279\u6027<\/li>\n\n\n\n<li>\u68c0\u67e5\u6b8b\u5dee\u7684\u81ea\u76f8\u5173\u6027\u548cARCH\u6548\u5e94\uff0c\u9a8c\u8bc1\u6a21\u578b\u62df\u5408\u6548\u679c<\/li>\n\n\n\n<li>\u8003\u8651\u52a0\u5165\u5916\u90e8\u53d8\u91cf\u6216\u4f7f\u7528EGARCH\u7b49\u975e\u5bf9\u79f0\u6a21\u578b\u6355\u6349\u6760\u6746\u6548\u5e94<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">GARCH Model python\u4ee3\u7801<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code><strong>#volatility forcast\u6ce2\u52a8\u6027\u9884\u6d4b<\/strong>\n#\u5b9e\u73b0DARCH\u6a21\u578b\u7684python\u4ee3\u7801\uff1a\nimport akshare as ak\nimport pandas as pd\nfrom arch import arch_model\nimport matplotlib.pyplot as plt\n\n# \u83b7\u53d6\u80a1\u7968\u6570\u636e\ndef get_stock_data(stock_code, start_date, end_date):\n    stock_df = ak.stock_zh_a_hist(symbol=stock_code, period=\"daily\", start_date=start_date, end_date=end_date)\n    stock_df&#91;'\u65e5\u671f'] = pd.to_datetime(stock_df&#91;'\u65e5\u671f'])\n    stock_df.set_index('\u65e5\u671f', inplace=True)\n    return stock_df\n\n# \u8ba1\u7b97\u5bf9\u6570\u6536\u76ca\u7387\ndef calculate_log_returns(stock_data):\n    stock_data&#91;'log_returns'] = 100 * stock_data&#91;'\u6536\u76d8'].pct_change().dropna()\n    return stock_data.dropna()\n\n# \u62df\u5408 GARCH \u6a21\u578b\ndef fit_garch_model(returns):\n    model = arch_model(returns, vol='Garch', p=1, q=1)\n    results = model.fit(disp='off')\n    return results\n\n# \u4e3b\u51fd\u6570\ndef main():\n    stock_code = \"000937\"  # \u80a1\u7968\u4ee3\u7801\uff0c\u8fd9\u91cc\u4ee5\u5e73\u5b89\u94f6\u884c\u4e3a\u4f8b\n    start_date = \"20240101\"\n    end_date = \"20250530\"\n\n    # \u83b7\u53d6\u6570\u636e\n    stock_data = get_stock_data(stock_code, start_date, end_date)\n    # \u8ba1\u7b97\u5bf9\u6570\u6536\u76ca\u7387\n    stock_data = calculate_log_returns(stock_data)\n    # \u63d0\u53d6\u5bf9\u6570\u6536\u76ca\u7387\n    returns = stock_data&#91;'log_returns']\n\n    # \u62df\u5408 GARCH \u6a21\u578b\n    results = fit_garch_model(returns)\n\n    # \u8f93\u51fa\u6a21\u578b\u7ed3\u679c\n    print(results.summary())\n\n    # \u8ba1\u7b97\u5386\u53f2\u6ce2\u52a8\u7387\n    historical_volatility = results.conditional_volatility\n\n    # \u7ed8\u5236\u6ce2\u52a8\u7387\u9884\u6d4b\u56fe\n    forecasts = results.forecast(horizon=1)\n    # \u63d0\u53d6\u6700\u540e\u4e00\u4e2a\u9884\u6d4b\u65b9\u5dee\u5e76\u8f6c\u6362\u4e3a\u6807\u91cf\n    last_forecast_value = forecasts.variance.iloc&#91;-1].values&#91;0]\n    # \u83b7\u53d6\u6700\u540e\u4e00\u4e2a\u89c2\u6d4b\u65e5\u671f\u7684\u4e0b\u4e00\u5929\u4f5c\u4e3a\u9884\u6d4b\u65e5\u671f\n    forecast_date = returns.index&#91;-1] + pd.DateOffset(days=1)\n\n    plt.figure(figsize=(12, 6))\n    # \u7ed8\u5236\u5386\u53f2\u6ce2\u52a8\u7387\n    plt.plot(historical_volatility, label='Historical Volatility')\n    # \u7ed8\u5236\u9884\u6d4b\u6ce2\u52a8\u7387\n    plt.plot(&#91;forecast_date], &#91;last_forecast_value], marker='o', color='red', label='Forecast Volatility')\n    plt.title('GARCH(1,1) Volatility Forecast')\n    plt.xlabel('Date')\n    plt.ylabel('Volatility')\n    plt.grid(True)\n    plt.legend()\n    plt.show()\n\nif __name__ == \"__main__\":\n    main()\n<\/code><\/pre>\n\n\n\n<p>C:\\Users\\czliu&gt; &amp; &#8220;C:\/Program Files\/Python313\/python.exe&#8221; c:\/Users\/czliu\/Documents\/python\/stock_GARCH_model.py<\/p>\n\n\n\n<h1 class=\"wp-block-heading\">Constant Mean &#8211; GARCH Model Results<\/h1>\n\n\n\n<p>Dep. Variable: log_returns R-squared: 0.000<br>Mean Model: Constant Mean Adj. R-squared: 0.000<br>Vol Model: GARCH Log-Likelihood: -684.774<br>Distribution: Normal AIC: 1377.55<br>Method: Maximum Likelihood BIC: 1392.84<br>No. Observations: 338<br>Date: Fri, May 30 2025 Df Residuals: 337<br>Time: 23:15:55 Df Model: 1<\/p>\n\n\n\n<h1 class=\"wp-block-heading\">Mean Model<\/h1>\n\n\n\n<h2 class=\"wp-block-heading\">coef std err t P&gt;|t| 95.0% Conf. Int.<\/h2>\n\n\n\n<p>mu -0.0335 9.835e-02 -0.340 0.734 [ -0.226, 0.159]<\/p>\n\n\n\n<h1 class=\"wp-block-heading\">Volatility Model\u6ce2\u52a8\u6027\u6a21\u578b<\/h1>\n\n\n\n<h2 class=\"wp-block-heading\">coef std err t P&gt;|t| 95.0% Conf. Int.<\/h2>\n\n\n\n<p>omega 0.5262 0.926 0.569 0.570 [ -1.288, 2.340]<br>alpha[1] 0.0877 6.055e-02 1.449 0.147 [-3.095e-02, 0.206]<\/p>\n\n\n\n<h1 class=\"wp-block-heading\">beta[1] 0.7641 0.283 2.703 6.873e-03 [ 0.210, 1.318]<\/h1>\n\n\n\n<p>Covariance estimator: robust<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"566\" src=\"http:\/\/cnliutz.pgrm.cc\/wp-content\/uploads\/2025\/05\/image-4-1024x566.png\" alt=\"\" class=\"wp-image-5694\" srcset=\"http:\/\/cnliutz.wicp.vip\/wp-content\/uploads\/2025\/05\/image-4-1024x566.png 1024w, http:\/\/cnliutz.wicp.vip\/wp-content\/uploads\/2025\/05\/image-4-300x166.png 300w, http:\/\/cnliutz.wicp.vip\/wp-content\/uploads\/2025\/05\/image-4-768x424.png 768w, http:\/\/cnliutz.wicp.vip\/wp-content\/uploads\/2025\/05\/image-4-1536x849.png 1536w, http:\/\/cnliutz.wicp.vip\/wp-content\/uploads\/2025\/05\/image-4.png 1804w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>\u8fd9\u4efd\u8f93\u51fa\u662f\u4e00\u4e2aGARCH(1,1)\u6a21\u578b\u5bf9\u80a1\u7968\u6536\u76ca\u7387\u7684\u62df\u5408\u7ed3\u679c\uff0c\u4e0b\u9762\u662f\u5bf9\u5173\u952e\u90e8\u5206\u7684 <span class=\"readmore\"><a href=\"http:\/\/cnliutz.wicp.vip\/?p=5693\">Continue Reading<\/a><\/span><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2,24],"tags":[],"class_list":["post-5693","post","type-post","status-publish","format-standard","hentry","category-2","category-24"],"_links":{"self":[{"href":"http:\/\/cnliutz.wicp.vip\/index.php?rest_route=\/wp\/v2\/posts\/5693","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/cnliutz.wicp.vip\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/cnliutz.wicp.vip\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/cnliutz.wicp.vip\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/cnliutz.wicp.vip\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=5693"}],"version-history":[{"count":3,"href":"http:\/\/cnliutz.wicp.vip\/index.php?rest_route=\/wp\/v2\/posts\/5693\/revisions"}],"predecessor-version":[{"id":5701,"href":"http:\/\/cnliutz.wicp.vip\/index.php?rest_route=\/wp\/v2\/posts\/5693\/revisions\/5701"}],"wp:attachment":[{"href":"http:\/\/cnliutz.wicp.vip\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=5693"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/cnliutz.wicp.vip\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=5693"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/cnliutz.wicp.vip\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=5693"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}