{"id":101,"date":"2024-01-22T22:04:24","date_gmt":"2024-01-22T14:04:24","guid":{"rendered":"https:\/\/www.vincedeck.top\/?p=101"},"modified":"2024-01-30T21:13:45","modified_gmt":"2024-01-30T13:13:45","slug":"python%e6%97%b6%e9%97%b4%e5%ba%8f%e5%88%9702-ar%e6%a8%a1%e5%9e%8b","status":"publish","type":"post","link":"https:\/\/www.vincedeck.top\/?p=101","title":{"rendered":"Python\u65f6\u95f4\u5e8f\u5217#02 &#8211; AR\u6a21\u578b"},"content":{"rendered":"\n<!--more-->\n\n\n\n<h1 class=\"wp-block-heading\">\u81ea\u56de\u5f52\u6a21\u578b<\/h1>\n\n\n\n<p>\u81ea\u56de\u5f52\u6a21\u578b\uff08Autoregression Model, AR\uff09\u4f7f\u7528\u65f6\u95f4\u5e8f\u5217\u8fc7\u53bb\u7684\u6570\u636e\u9884\u6d4b\u5176\u672a\u6765\u3002$AR(p)$ \u6a21\u578b\u7684\u5f62\u5f0f\u4e3a\uff1a$$r_t = \\phi_0 + \\phi_1 r_{t-1} + &#8230; + \\phi_p r_{t-p} + a_t ,$$ \u5176\u4e2d $p$ \u662f\u975e\u8d1f\u6574\u6570\uff0c$\\{a_t\\}$ \u662f\u5747\u503c\u4e3a $0$ \u7684\u767d\u566a\u58f0\u5e8f\u5217\u3002\u8fd9\u4e2a\u6a21\u578b\u8868\u793a\uff0c\u7ed9\u5b9a\u8fc7\u53bb\u7684\u6570\u636e\u65f6\uff0c\u8fc7\u53bb\u7684 $p$ \u4e2a\u503c $r_{t-1}, r_{t-2} &#8230; ,r_{t-p}$ \u8054\u5408\u51b3\u5b9a $r_t$ \u7684\u6761\u4ef6\u671f\u671b\u3002<\/p>\n\n\n\n<p>AR\u6a21\u578b\u4e2d\uff0c$p$ \u8868\u793a\u6a21\u578b\u7684\u9636\u6570\u3002\u5f53 $p=1$ \u65f6\uff0c\u5f97\u5230\u4e00\u9636\u81ea\u56de\u5f52\u6a21\u578b\uff0c\u5373 $AR(1)$ \u6a21\u578b\uff0c\u5176\u5f62\u5f0f\u4e3a\uff1a$$r_t = \\phi_0 + \\phi_1 r_{t-1} + a_t .$$<\/p>\n\n\n\n<p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0cAR\u6a21\u578b\u7684\u9636\u6570 $p$ \u662f\u672a\u77e5\u7684\uff0c\u5fc5\u987b\u6839\u636e\u5b9e\u9645\u6570\u636e\u6765\u51b3\u5b9a\u3002\u6c42\u89e3 $p$ \u7684\u7684\u95ee\u9898\u53eb\u505aAR\u6a21\u578b\u7684\u5b9a\u9636\uff0c\u4e00\u822c\u53ef\u4ee5\u4f7f\u7528\u504f\u76f8\u5173\u51fd\u6570\u6216\u8005\u4fe1\u606f\u51c6\u6d4b\u51fd\u6570\u6765\u51b3\u5b9a\u3002<\/p>\n\n\n\n<h1 class=\"wp-block-heading\">\u5728Python\u4e2d\u751f\u6210 AR(1) \u5e8f\u5217<\/h1>\n\n\n\n<p>\u6839\u636e\u5b9a\u4e49\uff0c$AR(1)$ \u5e8f\u5217\u7684\u5f62\u5f0f\u4e3a\uff1a$$r_t = \\phi_0 + \\phi_1 r_{t-1}+ a_t,$$\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 <code>numpy<\/code> \u5e93\u751f\u6210 $AR(1)$ \u5e8f\u5217\uff1a<\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-python\" data-lang=\"Python\"><code>import numpy as np\nimport matplotlib.pyplot as plt\n\n# generate data\nnp.random.seed(1) # \u8bbe\u7f6e\u968f\u673a\u6570\u79cd\u5b50\nn = 1000\na = np.random.randn(n) # \u751f\u6210\u767d\u566a\u58f0\u5e8f\u5217\n\ny = np.ones(n) # \u521d\u59cb\u5316 `y`\n\n# \u8bbe\u7f6e\u53c2\u6570 `phi_0` \u548c `phi_1` \u7684\u503c\nphi_0 = 0.4\nphi_1 = 0.75\n\n# \u751f\u6210 `y[i]`, \u5e8f\u5217\u4e2d\u7684\u6bcf\u4e00\u4e2a\u6570\u636e\u7531 AR(1) \u5e8f\u5217\u7684\u516c\u5f0f\u7ed9\u51fa\nfor i in range(1, n):\n    y[i] = phi_0 + phi_1 * y[i-1] + a[i]<\/code><\/pre><\/div>\n\n\n\n<p>\u4f7f\u7528 <code>matplotlib<\/code> \u5e93\u753b\u56fe\u5c55\u793a\u5e8f\u5217 <code>y<\/code> \uff1a<\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-python\" data-lang=\"Python\"><code>plt.plot(y)<\/code><\/pre><\/div>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/s2.loli.net\/2024\/01\/22\/Heq5PnY7ECRLVyZ.png\" alt=\"image.png\"\/><\/figure>\n\n\n\n<h1 class=\"wp-block-heading\">ACF\u548cPACF<\/h1>\n\n\n\n<p>\u81ea\u76f8\u5173\u7cfb\u6570\uff08ACF\uff09\u548c\u504f\u76f8\u5173\u7cfb\u6570\uff08PACF\uff09\u662f\u4e24\u4e2a\u5e38\u7528\u7684\u7edf\u8ba1\u6307\u6807\uff0c\u53ef\u4ee5\u901a\u8fc7ACF\u548cPACF\u56fe\u5224\u65ad\u5f85\u56de\u5f52\u7684\u5e8f\u5217\u6240\u5c5e\u7684\u6a21\u578b\uff0c\u4ee5\u53ca\u5224\u65ad\u6a21\u578b\u7684\u53c2\u6570\u3002<\/p>\n\n\n\n<p>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528 <code>statsmodels<\/code> \u5e93\u4e2d\u7684\u51fd\u6570\u65b9\u4fbf\u5730\u751f\u6210ACF\u548cPACF\u56fe\uff1a<\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-python\" data-lang=\"Python\"><code>import statsmodels.api as sm\n\ndef draw_acf_pacf(y, lags=20):\n    fig, ax = plt.subplots(2, 1, figsize=(8, 7))\n    sm.graphics.tsa.plot_acf(y, lags=lags, ax=ax[0])\n    sm.graphics.tsa.plot_pacf(y, lags=lags, ax=ax[1])\n    # set ylim automatically\n    for i in range(2):\n        ylim = ax[i].get_ylim()\n        ax[i].set_ylim(ylim[0], ylim[1] + 0.1)\n    plt.show()<\/code><\/pre><\/div>\n\n\n\n<p>\u4e0a\u9762\u7684\u4ee3\u7801\u5b9a\u4e49\u4e86 <code>draw_acf_pacf()<\/code> \u51fd\u6570\uff0c\u5bf9 <code>statsmodels<\/code> \u5e93\u4e2d\u7ed8\u5236ACF\u548cPACF\u56fe\u7684\u51fd\u6570\u505a\u4e86\u8fdb\u4e00\u6b65\u5c01\u88c5\u3002\u4e0b\u9762\u7ed8\u5236\u4e0a\u4e00\u8282\u4e2d\u751f\u6210\u7684\u5e8f\u5217 <code>y<\/code> \u7684ACF\u548cPACF\u56fe\uff1a<\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-python\" data-lang=\"Python\"><code>draw_acf_pacf(y, lags=20)<\/code><\/pre><\/div>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/s2.loli.net\/2024\/01\/22\/WZdV8n4hcEHGiw2.png\" alt=\"image.png\"\/><\/figure>\n\n\n\n<p>\u53ef\u4ee5\u770b\u51fa\uff0c\u5e8f\u5217 <code>y<\/code> \u5bf9\u5e94\u7684ACF\u56fe\u662f\u62d6\u5c3e\u7684\uff0cPACF\u56fe\u662f1\u9636\u622a\u5c3e\u7684\uff0c\u6240\u4ee5\u53ef\u4ee5\u4f7f\u7528 $AR(1)$ \u6a21\u578b\u62df\u5408\u5e8f\u5217 <code>y<\/code> \u3002<\/p>\n\n\n\n<p>\u4e0b\u9762\u4f7f\u7528 $AR(1)$ \u6a21\u578b\u5bf9\u5e8f\u5217 <code>y<\/code> \u8fdb\u884c\u62df\u5408\uff1a<\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-python\" data-lang=\"Python\"><code># \u7528AR(1)\u6a21\u578b\u62df\u5408\u6570\u636e\nmodel = sm.tsa.AutoReg(y, lags=1).fit()\nprint(model.summary())<\/code><\/pre><\/div>\n\n\n\n<p>\u4f7f\u7528\u51fd\u6570 <code>sm.tsa.AutoReg()<\/code> \u8c03\u7528AR\u6a21\u578b\uff0c\u7b2c\u4e00\u4e2a\u53c2\u6570\u4e3a\u5f85\u62df\u5408\u7684\u5e8f\u5217\uff0c\u53c2\u6570 <code>lag=1<\/code> \u8868\u793a\u6a21\u578b\u7684\u9636\u6570\u4e3a1\u3002<\/p>\n\n\n\n<p>\u8f93\u51fa\u7ed3\u679c\u4e3a\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/s2.loli.net\/2024\/01\/22\/L2gSyznY4DXIClv.png\" alt=\"image.png\"\/><\/figure>\n\n\n\n<p>\u53ef\u4ee5\u770b\u51fa\u62df\u5408\u7ed3\u679c\u4e3a $ \\hat{y}_t = 0.4698 + 0.7314 y_{t-1}$\uff0c\u4e0e\u7ed9\u5b9a\u7684\u53c2\u6570\uff08<code>phi_0 = 0.4<\/code> , <code>phi_1 = 0.75<\/code>\uff09\u76f8\u6bd4\uff0c\u6a21\u578b\u62df\u5408\u7684\u6548\u679c\u8fd8\u662f\u4e0d\u9519\u7684\u3002<\/p>\n\n\n\n<h1 class=\"wp-block-heading\">\u4f7f\u7528AR\u6a21\u578b\u62df\u5408\u771f\u5b9e\u6570\u636e<\/h1>\n\n\n\n<p>\u4f7f\u7528AR\u6a21\u578b\u62df\u5408\u6807\u666e500\u7684\u6eda\u52a812\u4e2a\u6708\u6536\u76ca\u7387\u6570\u636e\uff0c\u6570\u636e\u9891\u7387\u4e3a\u6708\u5ea6\u3002\u9996\u5148\u5bfc\u5165\u6570\u636e\u5e76\u5c55\u793a\u524d\u4e94\u884c\uff1a<\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-python\" data-lang=\"Python\"><code>data_url = &#39;https:\/\/github.com\/peisong13\/dataset\/blob\/1666949c3cb7e7908d6c9e7fb26cf1213b5d7bd6\/SP500_Rolling_12_Month_Return\/SP500_Rolling_12_Month_Return.csv?raw=true&#39;\ndf = pd.read_csv(data_url, index_col=0, parse_dates=True)\ndf.head()<\/code><\/pre><\/div>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/s2.loli.net\/2024\/01\/22\/EmWakATL5h9vwBj.png\" alt=\"image.png\"\/><\/figure>\n\n\n\n<p>\u753b\u51faACF\u548cPACF\u56fe\uff1a<\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-python\" data-lang=\"Python\"><code># ACF and PACF\ndraw_acf_pacf(df[&#39;SP500_Rolling_12_Month_Return&#39;], lags=20)<\/code><\/pre><\/div>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/s2.loli.net\/2024\/01\/22\/EsdhF6cp5oayGCU.png\" alt=\"image.png\"\/><\/figure>\n\n\n\n<p>ACF\u62d6\u5c3e\uff0cPACF 1\u9636\u622a\u5c3e\uff0c\u56e0\u6b64\u4f7f\u7528 $AR(1)$ \u5bf9\u6570\u636e\u8fdb\u884c\u62df\u5408\uff1a<\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-python\" data-lang=\"Python\"><code># use AR(1) model\nmodel = sm.tsa.AutoReg(df[&#39;SP500_Rolling_12_Month_Return&#39;], lags=1).fit()\nprint(model.summary())<\/code><\/pre><\/div>\n\n\n\n<img decoding=\"async\" src=\"https:\/\/s2.loli.net\/2024\/01\/22\/CniyXkTzRdtSNVj.png\" alt=\"image.png\" style=\"object-fit:cover\" class=\"\">\n\n\n\n<p>\u4f7f\u7528\u8be5\u6a21\u578b\u5bf9\u5e8f\u52172022\u5e741-12\u6708\u7684\u503c\u8fdb\u884c\u9884\u6d4b\uff1a<\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-python\" data-lang=\"Python\"><code># predict\npred = model.predict(start=pd.to_datetime(&#39;2022-01-01&#39;), end=pd.to_datetime(&#39;2022-12-01&#39;))\nprint(pred)<\/code><\/pre><\/div>\n\n\n\n<p>\u8f93\u51fa\u7ed3\u679c\u4e3a\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>2022-01-01    25.491367\r\n2022-02-01    24.215782\r\n2022-03-01    23.052421\r\n2022-04-01    21.991409\r\n2022-05-01    21.023743\r\n2022-06-01    20.141209\r\n2022-07-01    19.336319\r\n2022-08-01    18.602242\r\n2022-09-01    17.932746\r\n2022-10-01    17.322152\r\n2022-11-01    16.765276\r\n2022-12-01    16.257392\r\nFreq: MS, dtype: float64<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[14,7],"tags":[25,23],"class_list":["post-101","post","type-post","status-publish","format-standard","hentry","category-14","category-7","tag-pacf","tag-23"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.vincedeck.top\/index.php?rest_route=\/wp\/v2\/posts\/101","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.vincedeck.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.vincedeck.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.vincedeck.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.vincedeck.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=101"}],"version-history":[{"count":49,"href":"https:\/\/www.vincedeck.top\/index.php?rest_route=\/wp\/v2\/posts\/101\/revisions"}],"predecessor-version":[{"id":260,"href":"https:\/\/www.vincedeck.top\/index.php?rest_route=\/wp\/v2\/posts\/101\/revisions\/260"}],"wp:attachment":[{"href":"https:\/\/www.vincedeck.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=101"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.vincedeck.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=101"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.vincedeck.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=101"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}