{"id":5157,"date":"2022-11-28T14:59:02","date_gmt":"2022-11-28T06:59:02","guid":{"rendered":"https:\/\/seitlab.com\/?p=5157"},"modified":"2022-11-28T15:01:03","modified_gmt":"2022-11-28T07:01:03","slug":"5-7-%e8%ae%a1%e9%87%8f%e5%88%86%e6%9e%907%ef%bc%9a%e6%97%b6%e9%97%b4%e5%ba%8f%e5%88%97%e5%88%86%e6%9e%90","status":"publish","type":"post","link":"https:\/\/www.seitlab.com\/?p=5157","title":{"rendered":"5.7 \u8ba1\u91cf\u5206\u67907\uff1a\u65f6\u95f4\u5e8f\u5217\u5206\u6790"},"content":{"rendered":"<h3>\u4f8b\u5b50<\/h3>\n<p><img data-recalc-dims=\"1\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/seitlab.com\/wp-content\/uploads\/2020\/05\/%E5%AE%9E%E9%AA%8C5-1.png?w=750&#038;ssl=1\" alt=\"IMG\" \/><\/p>\n<h3>\u4ee3\u7801\u90e8\u5206<\/h3>\n<pre><code>\n# lec07\n# \u8ba1\u91cf\u5b9e\u9a8c5-\u4ee3\u7801\u90e8\u5206 \u81ea\u76f8\u5173\u7684\u8bca\u65ad\u4e0e\u5904\u7406\n\n# \u5bfc\u5165\u7a0b\u5e8f\u5305\nimport pandas as pd\nimport statsmodels.api as sm\nimport matplotlib.pyplot as plt\nfrom statsmodels.tsa.stattools import adfuller\nfrom statsmodels.tsa.stattools import coint\nfrom statsmodels.stats.diagnostic import acorr_breusch_godfrey\nimport ssl\n\n# \u7981\u7528SSL\u8bc1\u4e66\u6821\u9a8c\nssl._create_default_https_context = ssl._create_unverified_context\n\n# \u5bfc\u5165(\u5728\u7ebf)\u6570\u636e\ndf = pd.read_excel(r&quot;https:\/\/cdn.seitlab.com\/data\/econometrics\/exp3.1.xlsx&quot;)\n\n# \u5c55\u793a\u6570\u636e\nprint(&quot;\u6570\u636e\u5c55\u793a\u5982\u4e0b\uff1a&quot;)\nprint(df, &quot;\\n&quot;)\n\n# \u7b80\u8981\u7edf\u8ba1\u63cf\u8ff0\nprint(&quot;\u7b80\u8981\u7edf\u8ba1\u63cf\u8ff0\uff1a&quot;)\nprint(df.describe(), &quot;\\n&quot;)\n\n# \u540d\u4e49\u503c\u8f6c\u6362\u4e3a\u5b9e\u9645\u503c\ndf01 = pd.DataFrame()\ndf01[&quot;cons&quot;] = df[&quot;cons&quot;].div(df[&quot;cpi&quot;].div(100))\ndf01[&quot;dpi&quot;] = df[&quot;dpi&quot;].div(df[&quot;cpi&quot;].div(100))\n\n# \u63cf\u8ff0\u6027\u7edf\u8ba1\u5206\u6790\nprint(&quot;\u6570\u636e\u7684\u7b80\u8981\u7edf\u8ba1\u63cf\u8ff0\uff1a&quot;)\nprint(df01.describe(), &quot;\\n&quot;)\n\n# \u6298\u7ebf\u56fe\n# \u8bbe\u7f6e\u7ed8\u56fe\u683c\u5f0f,\u652f\u6301\u6c49\u5b57\nplt.rcParams[&#039;font.sans-serif&#039;] = [&#039;SimHei&#039;]\nplt.rcParams[&#039;axes.unicode_minus&#039;] = False\n# \u5b9a\u4e49\u53d8\u91cf\nx = df[&quot;year&quot;]\ny01 = df[&quot;cons&quot;]\ny02 = df[&quot;dpi&quot;]\ny1 = df01[&quot;cons&quot;]\ny2 = df01[&quot;dpi&quot;]\n# \u7ed8\u56fe\uff1a\u519c\u6751\u5c45\u6c11\u4eba\u5747\u6d88\u8d39\u4e0e\u652f\u51fa\nplt.plot(x, y01, c=&quot;blue&quot;, label=&quot;\u519c\u6751\u5c45\u6c11\u4eba\u5747\u6d88\u8d39&quot;)\nplt.plot(x, y02, c=&quot;green&quot;, label=&quot;\u519c\u6751\u5c45\u6c11\u4eba\u5747\u7eaf\u6536\u5165&quot;)\nplt.title(&quot;1985-2012\u519c\u6751\u5c45\u6c11\u4eba\u5747\u6d88\u8d39\u4e0e\u7eaf\u6536\u5165(\u6309\u5f53\u5e74\u4ef7\u683c)&quot;, fontsize=&quot;x-large&quot;, fontweight=&quot;bold&quot;)\nplt.legend()\nplt.show()\n# \u7ed8\u56fe\uff1a\u5b9e\u9645\u519c\u6751\u5c45\u6c11\u4eba\u5747\u6d88\u8d39\u4e0e\u652f\u51fa\nplt.plot(x, y1, c=&quot;blue&quot;, label=&quot;\u519c\u6751\u5c45\u6c11\u4eba\u5747\u6d88\u8d39&quot;)\nplt.plot(x, y2, c=&quot;green&quot;, label=&quot;\u519c\u6751\u5c45\u6c11\u4eba\u5747\u7eaf\u6536\u5165&quot;)\nplt.title(&quot;1985-2012\u5b9e\u9645\u519c\u6751\u5c45\u6c11\u4eba\u5747\u6d88\u8d39\u4e0e\u7eaf\u6536\u5165(\u63091978\u4ef7\u683c)&quot;, fontsize=&quot;x-large&quot;, fontweight=&quot;bold&quot;)\nplt.legend()\nplt.show()\n\n# XY\u6563\u70b9\u56fe\n# \u5b9a\u4e49\u53d8\u91cf\nx = df01[&quot;dpi&quot;]\ny = df01[&quot;cons&quot;]\nplt.scatter(x, y, c=&quot;green&quot;)\nplt.title(&quot;\u519c\u6751\u5c45\u6c11\u4eba\u5747\u6d88\u8d39\u4e0e\u7eaf\u6536\u5165\u6563\u70b9\u56fe&quot;, fontsize=&quot;x-large&quot;, fontweight=&quot;bold&quot;)\nplt.xlabel(&quot;\u519c\u6751\u5c45\u6c11\u4eba\u5747\u7eaf\u6536\u5165(\u5143)&quot;, fontsize=&quot;large&quot;, fontweight=&quot;bold&quot;)\nplt.ylabel(&quot;\u519c\u6751\u5c45\u6c11\u4eba\u5747\u6d88\u8d39(\u5143)&quot;, fontsize=&quot;large&quot;, fontweight=&quot;bold&quot;)\nplt.show()\n\n# \u76f8\u5173\u7cfb\u6570\nprint(&quot;\u76f8\u5173\u7cfb\u6570\uff1a&quot;)\nprint(df01.corr(), &quot;\\n&quot;)\n\n# \u5e73\u7a33\u6027\u68c0\u9a8c\u2014\u5355\u4f4d\u6839\u68c0\u9a8c\n# \u5355\u4f4d\u6839\u68c0\u9a8c\uff1ay\nadf_stat = adfuller(y, maxlag=0)\nadf_y = adf_stat[0]\nadf_pvalue_y = adf_stat[1]\n# \u5355\u4f4d\u6839\u68c0\u9a8c\uff1ay\u7684\u4e00\u9636\u5dee\u5206\ny_diff1 = y.diff(1)\ny_diff1 = y_diff1.dropna()\nadf_stat = adfuller(y_diff1, maxlag=0)\nadf_y_diff1 = adf_stat[0]\nadf_pvalue_y_diff1 = adf_stat[1]\n# \u5355\u4f4d\u6839\u68c0\u9a8c\uff1ay\u7684\u4e8c\u9636\u5dee\u5206\ny_diff2 = y_diff1.diff(1)\nprint(y_diff2)\ny_diff2 = y_diff2.dropna()\nadf_stat = adfuller(y_diff2, maxlag=0)\nadf_y_diff2 = adf_stat[0]\nadf_pvalue_y_diff2 = adf_stat[1]\n# \u5355\u4f4d\u6839\u68c0\u9a8c\uff1ax\nadf_stat = adfuller(x, maxlag=0)\nadf_x = adf_stat[0]\nadf_pvalue_x = adf_stat[1]\n# \u5355\u4f4d\u6839\u68c0\u9a8c\uff1ax\u7684\u4e00\u9636\u5dee\u5206\nx_diff1 = x.diff(1)\nx_diff1 = x_diff1.dropna()\nadf_stat = adfuller(x_diff1, maxlag=0)\nadf_x_diff1 = adf_stat[0]\nadf_pvalue_x_diff1 = adf_stat[1]\n# \u5355\u4f4d\u6839\u68c0\u9a8c\uff1ax\u7684\u4e8c\u9636\u5dee\u5206\nx_diff2 = x_diff1.diff(1)\nx_diff2 = x_diff2.dropna()\nadf_stat = adfuller(x_diff2, maxlag=0)\nadf_x_diff2 = adf_stat[0]\nadf_pvalue_x_diff2 = adf_stat[1]\n# \u5e73\u7a33\u6027\u6027-\u5355\u4f4d\u6839\u68c0\u9a8c\u7ed3\u679c\u6c47\u603b\ndf_adf = pd.DataFrame(columns=[&quot;\u53d8\u91cf&quot;, &quot;ADF\u503c&quot;, &quot;P\u503c&quot;, &quot;\u5e73\u7a33\u6027&quot;])\ndf_adf[&quot;\u53d8\u91cf&quot;] = [&quot;cons&quot;, &quot;cons_diff1&quot;, &quot;cons_diff2&quot;, &quot;dpi&quot;, &quot;dpi_diff1&quot;, &quot;dpi_diff2&quot;]\ndf_adf[&quot;ADF\u503c&quot;] = [adf_y, adf_y_diff1, adf_y_diff2, adf_x, adf_x_diff1, adf_x_diff2]\ndf_adf[&quot;P\u503c&quot;] = [adf_pvalue_y, adf_pvalue_y_diff1, adf_pvalue_y_diff2, adf_pvalue_x, adf_pvalue_x_diff1, adf_pvalue_x_diff2]\nadf_p = df_adf[&quot;P\u503c&quot;]\nfor i in range(6):\n    if df_adf.iloc[i-1, 2] &lt; 0.05:\n        df_adf.iloc[i-1, 3] = &quot;\u5e73\u7a33&quot;\n    else:\n        df_adf.iloc[i-1, 3] = &quot;\u4e0d\u5e73\u7a33&quot;\nprint(&quot;\u5e73\u7a33\u6027\u68c0\u9a8c\u7684\u6c47\u603b\u7ed3\u679c\uff1a&quot;)\nprint(df_adf.to_string(index=False), &quot;\\n&quot;)\n\n# \u56de\u5f52\u5206\u6790\n# \u5b9a\u4e49\u53d8\u91cf\ncons = y_diff2\ndpi = x_diff2\n# \u6dfb\u52a0\u5e38\u6570\u9879\nX = sm.add_constant(dpi)\n# \u56de\u5f52\nOLS_model = sm.OLS(cons, X).fit()\nprint(&quot;\u56de\u5f52\u7ed3\u679c\uff1a&quot;)\nprint(OLS_model.summary(), &quot;\\n&quot;)\n\n# \u6b8b\u5dee\u81ea\u76f8\u5173\u68c0\u9a8c\n# Breusch-Godfrey test\nstats_bg = acorr_breusch_godfrey(OLS_model, nlags=1)\nprint(&quot;\u6b8b\u5dee\u7684Breusch-Godfrey\u68c0\u9a8c\u7ed3\u679c\uff1a&quot;)\nif stats_bg[1] &lt; 0.05:\n    print(&quot;\u6b8b\u5dee\u5e8f\u5217\u5b58\u5728\u4e00\u9636\u81ea\u76f8\u5173\u3002&quot;)\nelse:\n    print(&quot;\u6b8b\u5dee\u5e8f\u5217\u4e0d\u5b58\u5728\u4e00\u9636\u81ea\u76f8\u5173\u3002&quot;)\n\n# \u5e7f\u4e49\u5dee\u5206\n# \u5b9a\u4e49\u53d8\u91cf\ny = OLS_model.resid\nx = y.shift(1)\n# \u8f85\u52a9\u56de\u5f52\nOLS_aux = sm.OLS(y, x, missing=&quot;drop&quot;).fit()\nprint(OLS_aux.summary(), &quot;\\n&quot;)\nrho = OLS_aux.params[0].round(4)\nprint(&quot;\u5e7f\u4e49\u5dee\u5206\u7684\u53c2\u6570rho\u503c\u4e3a\uff1a&quot;, rho, &quot;\\n&quot;)\n# \u5e7f\u4e49\u5dee\u5206\ncons_gdiff = cons - rho*(cons.shift(1))\ndpi_gdiff = dpi - rho*(dpi.shift(1))\n# \u56de\u5f52\u5206\u6790\uff1a\u5e7f\u4e49\u5dee\u5206\u6570\u636e\nX = sm.add_constant(dpi_gdiff)\nOLS_Gdiff = sm.OLS(cons_gdiff, X, missing=&quot;drop&quot;).fit()\nprint(&quot;\u5e7f\u4e49\u5dee\u5206\u540e\u7684\u56de\u5f52\u7ed3\u679c\uff1a&quot;)\nprint(OLS_Gdiff.summary(), &quot;\\n&quot;)\n\n# \u6b8b\u5dee\u81ea\u76f8\u5173\u68c0\u9a8c\n# Breusch-Godfrey test\nprint(&quot;\u5e7f\u4e49\u5dee\u5206\u6a21\u578b\u6b8b\u5dee\u68c0\u9a8c\u7ed3\u679c\uff1a&quot;)\nstats_bg = acorr_breusch_godfrey(OLS_Gdiff, nlags=1)\nif stats_bg[1] &lt; 0.05:\n    print(&quot;\u6b8b\u5dee\u5e8f\u5217\u5b58\u5728\u4e00\u9636\u81ea\u76f8\u5173\u3002&quot;, &quot;\\n&quot;)\nelse:\n    print(&quot;\u6b8b\u5dee\u5e8f\u5217\u4e0d\u5b58\u5728\u4e00\u9636\u81ea\u76f8\u5173\u3002&quot;, &quot;\\n&quot;)\n\n# \u534f\u6574\u68c0\u9a8c\n# \u68c0\u9a8c\u4e00\u9636\u5dee\u5206\u540e\u7684\u6570\u636e\u662f\u5426\u5177\u6709\u534f\u6574\u5173\u7cfb\nstats_coint = coint(y_diff1, x_diff1)\nprint(&quot;\u534f\u6574\u68c0\u9a8c\u7ed3\u679c\uff1a&quot;)\nif stats_coint[1] &lt; 0.05:\n    print(&quot;\u7ecf\u8fc7\u4e00\u9636\u5dee\u5206\u540e\u7684cons\u548cdpi\u4e4b\u95f4\u5b58\u5728\u534f\u6574\u5173\u7cfb\u3002&quot;, &quot;\\n&quot;)\nelse:\n    print(&quot;\u7ecf\u8fc7\u4e00\u9636\u5dee\u5206\u540e\u7684cons\u548cdpi\u4e4b\u95f4\u4e0d\u5b58\u5728\u534f\u6574\u5173\u7cfb\u3002&quot;, &quot;\\n&quot;)\n\n# \u57fa\u4e8e\u534f\u6574\u5173\u7cfb\u7684\u56de\u5f52\n# \u5b9a\u4e49\u53d8\u91cf\ncons = y_diff1\ndpi = x_diff1\nX = sm.add_constant(dpi)\nOLS_coint = sm.OLS(cons, X).fit()\nprint(&quot;\u57fa\u4e8e\u534f\u6574\u5173\u7cfb\u7684\u56de\u5f52\uff1a&quot;)\nprint(OLS_coint.summary(), &quot;\\n&quot;)\n# \u6b8b\u5dee\u68c0\u9a8c\n# \u6b8b\u5dee\u5e73\u7a33\u6027\u68c0\u9a8c\nprint(&quot;\u534f\u6574\u5173\u7cfb\u6a21\u578b\u6b8b\u5dee\u68c0\u9a8c\u7ed3\u679c\uff1a&quot;)\ne_coint = OLS_coint.resid\nadf_e_coint = adfuller(e_coint, maxlag=0)\nif adf_e_coint[1] &lt; 0.05:\n    print(&quot;\u6b8b\u5dee\u5e8f\u5217\u5e73\u7a33\u3002&quot;)\nelse:\n    print(&quot;\u6b8b\u5dee\u5e8f\u5217\u4e0d\u5e73\u7a33\u3002&quot;)\n# \u6b8b\u5dee\u81ea\u76f8\u5173\u68c0\u9a8c\ne_coint_bg = acorr_breusch_godfrey(OLS_coint, nlags=1)\nif e_coint_bg[1] &lt; 0.05:\n    print(&quot;\u6b8b\u5dee\u5e8f\u5217\u5b58\u5728\u4e00\u9636\u81ea\u76f8\u5173\u3002&quot;)\nelse:\n    print(&quot;\u6b8b\u5dee\u5e8f\u5217\u4e0d\u5b58\u5728\u4e00\u9636\u81ea\u76f8\u5173\u3002&quot;)\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u4f8b\u5b50 \u4ee3\u7801\u90e8\u5206 # lec07 # \u8ba1\u91cf\u5b9e\u9a8c5-\u4ee3\u7801\u90e8\u5206 \u81ea\u76f8\u5173\u7684\u8bca\u65ad\u4e0e\u5904\u7406 # \u5bfc\u5165\u7a0b\u5e8f\u5305 imp [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_feature_clip_id":0,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_post_was_ever_published":false},"categories":[29,30],"tags":[],"class_list":["post-5157","post","type-post","status-publish","format-standard","hentry","category-29","category-30"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.seitlab.com\/index.php?rest_route=\/wp\/v2\/posts\/5157","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.seitlab.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.seitlab.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.seitlab.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.seitlab.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=5157"}],"version-history":[{"count":0,"href":"https:\/\/www.seitlab.com\/index.php?rest_route=\/wp\/v2\/posts\/5157\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.seitlab.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=5157"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.seitlab.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=5157"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.seitlab.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=5157"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}