格兰杰因果检验_Python实现
原理部分
代碼實現
步驟
#mermaid-svg-kEtkcqkpRzxjdoOv {font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}#mermaid-svg-kEtkcqkpRzxjdoOv .error-icon{fill:#552222;}#mermaid-svg-kEtkcqkpRzxjdoOv .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-kEtkcqkpRzxjdoOv .edge-thickness-normal{stroke-width:2px;}#mermaid-svg-kEtkcqkpRzxjdoOv .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-kEtkcqkpRzxjdoOv .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-kEtkcqkpRzxjdoOv .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-kEtkcqkpRzxjdoOv .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-kEtkcqkpRzxjdoOv .marker{fill:#333333;stroke:#333333;}#mermaid-svg-kEtkcqkpRzxjdoOv .marker.cross{stroke:#333333;}#mermaid-svg-kEtkcqkpRzxjdoOv svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-kEtkcqkpRzxjdoOv .label{font-family:"trebuchet ms",verdana,arial,sans-serif;color:#333;}#mermaid-svg-kEtkcqkpRzxjdoOv .cluster-label text{fill:#333;}#mermaid-svg-kEtkcqkpRzxjdoOv .cluster-label span{color:#333;}#mermaid-svg-kEtkcqkpRzxjdoOv .label text,#mermaid-svg-kEtkcqkpRzxjdoOv span{fill:#333;color:#333;}#mermaid-svg-kEtkcqkpRzxjdoOv .node rect,#mermaid-svg-kEtkcqkpRzxjdoOv .node circle,#mermaid-svg-kEtkcqkpRzxjdoOv .node ellipse,#mermaid-svg-kEtkcqkpRzxjdoOv .node polygon,#mermaid-svg-kEtkcqkpRzxjdoOv .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-kEtkcqkpRzxjdoOv .node .label{text-align:center;}#mermaid-svg-kEtkcqkpRzxjdoOv .node.clickable{cursor:pointer;}#mermaid-svg-kEtkcqkpRzxjdoOv .arrowheadPath{fill:#333333;}#mermaid-svg-kEtkcqkpRzxjdoOv .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-kEtkcqkpRzxjdoOv .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-kEtkcqkpRzxjdoOv .edgeLabel{background-color:#e8e8e8;text-align:center;}#mermaid-svg-kEtkcqkpRzxjdoOv .edgeLabel rect{opacity:0.5;background-color:#e8e8e8;fill:#e8e8e8;}#mermaid-svg-kEtkcqkpRzxjdoOv .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-kEtkcqkpRzxjdoOv .cluster text{fill:#333;}#mermaid-svg-kEtkcqkpRzxjdoOv .cluster span{color:#333;}#mermaid-svg-kEtkcqkpRzxjdoOv div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-kEtkcqkpRzxjdoOv :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;}數據導入單位根檢驗差分協整格蘭杰檢驗數據導入
數據使用開源數據集,便于實現
#數據集 import statsmodels.api as sm from statsmodels.tsa.stattools import grangercausalitytests import numpy as npdata = sm.datasets.macrodata.load_pandas() demo = data.data穩定性檢驗
方法與通用的時間序列方法相同
demo[['realgdp','realcons']].plot() from statsmodels.tsa.stattools import adfuller print(adfuller(demo['realgdp'])) (1.7504627967647173, 0.9982455372335032, 12, 190, {'1%': -3.4652439354133255, '5%': -2.8768752281673717, '10%': -2.574944653739612}, 2034.517123668382)差分
demo['diff1_realgdp'] = demo['realgdp'].diff(1).fillna(0) demo['diff1_realcons'] = demo['realcons'].diff(1).fillna(0)demo[['diff1_realgdp','diff1_realcons']].plot()再次檢驗單位根
print(adfuller(demo['diff1_realgdp'])) print(adfuller(demo['diff1_realcons']))(-6.287524437690158, 3.66693821207135e-08, 1, 201, {‘1%’: -3.4633090972761744, ‘5%’: -2.876029332045744, ‘10%’: -2.5744932593252643}, 2034.8610305811367)
(-4.206384895591229, 0.0006426930660370949, 3, 199, {‘1%’: -3.4636447617687436, ‘5%’: -2.8761761179270766, ‘10%’: -2.57457158581854}, 1793.118209620634)
協整檢驗
from statsmodels.tsa.stattools import coint print(coint(demo['diff1_realgdp'], demo['diff1_realcons']))(-14.507179284872553, 5.182509395533684e-26, array([-3.95147899, -3.36654523, -3.0655127 ]))p=5.182509395533684e-26 < 0.05,說明在長期是存在相關性的
格蘭杰檢驗
Granger causality test的思想
如果使用時間序列X和Y的歷史值來預測Y的當前值,比僅通過Y的歷史值來預測Y的當前值得到的誤差更小,并且通過了F檢驗,卡方檢驗,則X對Y的預測是有一定幫助的。
了解了Granger causality test的思想之后會發現,其實Granger causality test最多能推斷出X對Y的預測是有一定幫助的,至于是否能說X和Y是因果關系,則不一定。
Granger Causality
number of lags (no zero) 1
ssr based F test: F=0.1851 , p=0.6675 , df_denom=199, df_num=1
ssr based chi2 test: chi2=0.1879 , p=0.6646 , df=1
likelihood ratio test: chi2=0.1878 , p=0.6647 , df=1
parameter F test: F=0.1851 , p=0.6675 , df_denom=199, df_num=1
Granger Causality
number of lags (no zero) 2
ssr based F test: F=22.9218 , p=0.0000 , df_denom=196, df_num=2
ssr based chi2 test: chi2=47.0132 , p=0.0000 , df=2
likelihood ratio test: chi2=42.2456 , p=0.0000 , df=2
parameter F test: F=22.9218 , p=0.0000 , df_denom=196, df_num=2
函數解讀:
該方法接收一個包含2列的2維的數組作為主要參數:
- 第一列是當前要預測未來值的序列A,第二列是另一個序列B,該方法就是看B對A的預測是否有幫助。該方法的零假設是:B對A沒有幫助。如果所有檢驗下的P-Values都小于顯著水平0.05,則可以拒絕零假設,并推斷出B確實對A的預測有用。
- 第二個參數maxlag是設定測試用的lags的最大值。
我們使用realgdp的數據集做預測,并利用Granger causality檢測“realcons”這個序列是否對數據集的預測用。
df_num=1 即滯后一階時,p>0.05,不能拒絕原假設
df_num=2即滯后二階時,P值小于0.05,通過檢驗,也就是拒絕了realcons不是引起realgdp格蘭杰變化的原因,即realcons是引起realgdp格蘭杰變化的原因
同理可以查看
realgdp是否是引起realcons格蘭杰變化的原因
總結
以上是生活随笔為你收集整理的格兰杰因果检验_Python实现的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 基8266实现天气预报
- 下一篇: 服务器mtbf性能测试,云服务器 MTB