{"id":41,"date":"2023-12-11T22:30:15","date_gmt":"2023-12-11T14:30:15","guid":{"rendered":"https:\/\/www.vincedeck.top\/?p=41"},"modified":"2024-01-30T21:16:06","modified_gmt":"2024-01-30T13:16:06","slug":"%e3%80%90python%e6%97%b6%e9%97%b4%e5%ba%8f%e5%88%97%e3%80%91%e5%b9%b3%e7%a8%b3%e5%ba%8f%e5%88%97%e7%94%9f%e6%88%90%e5%92%8c%e6%a3%80%e9%aa%8c","status":"publish","type":"post","link":"https:\/\/www.vincedeck.top\/?p=41","title":{"rendered":"Python\u65f6\u95f4\u5e8f\u5217#01 &#8211; \u5e73\u7a33\u5e8f\u5217\u751f\u6210\u548c\u68c0\u9a8c"},"content":{"rendered":"<p><!--more--><\/p>\n\n\n\n<h1 class=\"wp-block-heading\">\u5e73\u7a33\u6027<\/h1>\n\n\n\n<p>\u4e25\u5e73\u7a33\uff1a\u65f6\u95f4\u5e8f\u5217 ${r_t}$ \u662f\u4e25\u5e73\u7a33\u7684\uff0c\u5982\u679c\u5bf9\u4e8e\u6240\u6709\u7684  $t$ \u548c\u4efb\u610f\u6b63\u6574\u6570  $k$ \u548c\u4efb\u610f  $k$ \u4e2a\u6b63\u6574\u6570  $(t_1,\u2026,t_k)$\uff0c $(r_{t_1},\u2026,r_{t_k})$ \u7684\u8054\u5408\u5206\u5e03\u5728\u65f6\u95f4\u7684\u5e73\u79fb\u53d8\u6362\u4e0b\u4fdd\u6301\u4e0d\u53d8\u3002<\/p>\n\n\n\n<p>\u5f31\u5e73\u7a33\uff1a\u65f6\u95f4\u5e8f\u5217  ${r_t}$ \u662f\u5f31\u5e73\u7a33\u7684\uff0c\u5982\u679c  $r_t$ \u7684\u5747\u503c\u4e0e  $r_{t-l}$ \u7684\u534f\u65b9\u5dee\u4e0d\u968f\u65f6\u95f4\u800c\u6539\u53d8\uff0c\u5176\u4e2d  $l$ \u662f\u4efb\u610f\u6574\u6570\u3002<\/p>\n\n\n\n<h1 class=\"wp-block-heading\">\u5e73\u7a33\u6027\u5e8f\u5217\u7684\u751f\u6210<\/h1>\n\n\n\n<p>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528 <code>numpy<\/code> \u548c <code>pandas<\/code> \u751f\u6210\u5e73\u7a33\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 pandas as pd\nimport matplotlib.pyplot as plt\n\ndef generate_datapoint(params):\n    mu = params[0]\n    sigma = params[1]\n    return np.random.normal(mu, sigma)\n\n# \u5e8f\u5217A\nparams = (0, 1)\nT = 100\n\nA = pd.Series(index=range(T))\nA.name = &#39;A&#39;\n\nfor t in range(T):\n    A[t] = generate_datapoint(params)\n\nplt.plot(A)\nplt.xlabel(&#39;Time&#39;)\nplt.ylabel(&#39;Value&#39;)\nplt.legend([&#39;Series A&#39;])\nplt.show()<\/code><\/pre><\/div>\n\n\n\n<p>\u7528\u7c7b\u4f3c\u7684\u529e\u6cd5\uff0c\u751f\u6210\u4e00\u4e2a\u975e\u5e73\u7a33\u7684\u5e8f\u5217\u5e76\u547d\u540d\u4e3a <code>B<\/code> \uff1a<\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-python\" data-lang=\"Python\"><code># \u5e8f\u5217B\nT = 100\n\nB = pd.Series(index=range(T))\nB.name = &#39;B&#39;\n\nfor t in range(T):\n    # \u6ce8\u610f\u5230\u5e8f\u5217B\u7684\u5747\u503c\u4f1a\u968f\u65f6\u95f4\u53d8\u5316\n    params = (t*0.1, 1)\n    B[t] = generate_datapoint(params)\n\nplt.plot(B)\nplt.xlabel(&#39;Time&#39;)\nplt.ylabel(&#39;Value&#39;)\nplt.legend([&#39;Series B&#39;])\nplt.plot()<\/code><\/pre><\/div>\n\n\n\n<h1 class=\"wp-block-heading\">\u5e73\u7a33\u6027\u5e8f\u5217\u7684\u68c0\u9a8c<\/h1>\n\n\n\n<p>\u4f7f\u7528 <code>statsmodels<\/code> \u5e93\u4e2d\u7684ADF\u68c0\u9a8c\u5de5\u5177\u68c0\u9a8c\u5e8f\u5217\u7684\u5e73\u7a33\u6027\uff1a<\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-python\" data-lang=\"Python\"><code>from statsmodels.tsa.stattools import adfuller\n\ndef check_for_stationarity(X, cutoff=0.01):\n    pvalue = adfuller(X)[1]\n    if pvalue &lt; cutoff:\n        print(f&#39;p-value: {pvalue}. \u5e8f\u5217{X.name}\u53ef\u80fd\u4e3a\u5e73\u7a33\u5e8f\u5217\u3002&#39;)\n        return True\n    else:\n        print(f&#39;p-value: {pvalue}. \u5e8f\u5217{X.name}\u53ef\u80fd\u4e3a\u975e\u5e73\u7a33\u5e8f\u5217\u3002&#39;)\n        return False\n\ncheck_for_stationarity(A);\ncheck_for_stationarity(B);<\/code><\/pre><\/div>\n\n\n\n<p>\u4ee5\u4e0a\u4ee3\u7801\u5b9a\u4e49\u4e86 <code>check_for_stationarity()<\/code> \u51fd\u6570\uff0c\u8fd9\u4e2a\u51fd\u6570\u7684\u7b2c\u4e00\u4e2a\u53c2\u6570\u662f\u5f85\u68c0\u9a8c\u7684\u5e8f\u5217\uff0c\u7b2c\u4e8c\u4e2a\u53c2\u6570\u662fADF\u68c0\u9a8c\u7684\u9608\u503c\uff0c\u5982\u679c p-value \u5c0f\u4e8e\u9608\u503c\uff0c\u5219\u8ba4\u4e3a\u5e8f\u5217\u662f\u5e73\u7a33\u5e8f\u5217\u3002<\/p>\n\n\n\n<p>\u8f93\u51fa\u7ed3\u679c\u4e3a\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>p-value: 1.601227238375676e-14. \u5e8f\u5217A\u53ef\u80fd\u4e3a\u5e73\u7a33\u5e8f\u5217\u3002\np-value: 0.7343548250851961. \u5e8f\u5217B\u53ef\u80fd\u4e3a\u975e\u5e73\u7a33\u5e8f\u5217\u3002<\/code><\/pre>\n\n\n\n<p>\u8f93\u51fa\u7ed3\u679c\u8868\u660e\uff0c\u5e8f\u5217A\u7684 p-value \u975e\u5e38\u5c0f\uff0c\u5c0f\u4e8e\u4ee3\u7801\u4e2d\u8bbe\u5b9a\u7684\u9608\u503c\uff080.01\uff09\u3002\u56e0\u6b64\u5e8f\u5217A\u662f\u5e73\u7a33\u5e8f\u5217\uff0c\u800c\u5e8f\u5217B\u662f\u975e\u5e73\u7a33\u5e8f\u5217\u3002<\/p>\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":[16,15],"class_list":["post-41","post","type-post","status-publish","format-standard","hentry","category-14","category-7","tag-adf","tag-15"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.vincedeck.top\/index.php?rest_route=\/wp\/v2\/posts\/41","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=41"}],"version-history":[{"count":13,"href":"https:\/\/www.vincedeck.top\/index.php?rest_route=\/wp\/v2\/posts\/41\/revisions"}],"predecessor-version":[{"id":172,"href":"https:\/\/www.vincedeck.top\/index.php?rest_route=\/wp\/v2\/posts\/41\/revisions\/172"}],"wp:attachment":[{"href":"https:\/\/www.vincedeck.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=41"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.vincedeck.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=41"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.vincedeck.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=41"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}