Skip to content

Instantly share code, notes, and snippets.

@yogabonito
Created August 27, 2016 20:44
Show Gist options
  • Save yogabonito/eedd44be9b8a76de7773a1e43f961d45 to your computer and use it in GitHub Desktop.
Save yogabonito/eedd44be9b8a76de7773a1e43f961d45 to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 28,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"import pandas\n",
"import numpy as np\n",
"from statsmodels.tsa.vector_ar.var_model import VAR\n",
"import data as dta"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# 1. test Granger-causality between 2 variables in a system of 3 variables"
]
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"iidata = dta.load_pandas();\n",
"mdata = iidata.data\n",
"dates = mdata[['year', 'quarter']].astype(int).astype(str)\n",
"quarterly = dates[\"year\"] + \"Q\" + dates[\"quarter\"]\n",
"from statsmodels.tsa.base.datetools import dates_from_str\n",
"quarterly = dates_from_str(quarterly)\n",
"mdata = mdata[['realgdp','realcons','realinv']]\n",
"mdata.index = pandas.DatetimeIndex(quarterly)\n",
"data = mdata"
]
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"model = VAR(data)\n",
"var_res = model.fit(maxlags=4, trend=\"c\")"
]
},
{
"cell_type": "code",
"execution_count": 20,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Granger causality f-test\n",
"=============================================================\n",
" Test statistic Critical Value p-value df\n",
"-------------------------------------------------------------\n",
" 19.785068 2.387906 0.000 (4, 558)\n",
"=============================================================\n",
"H_0: ['realcons'] do not Granger-cause realgdp\n",
"Conclusion: reject H_0 at 5.00% significance level\n"
]
},
{
"data": {
"text/plain": [
"{'conclusion': 'reject',\n",
" 'crit_value': 2.3879062135285474,\n",
" 'df': (4, 558),\n",
" 'pvalue': 3.0302722513051643e-15,\n",
" 'signif': 0.05,\n",
" 'statistic': 19.785068360428099}"
]
},
"execution_count": 20,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"var_res.test_causality(variables='realcons', equation='realgdp')"
]
},
{
"cell_type": "code",
"execution_count": 29,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Granger causality f-test\n",
"=============================================================\n",
" Test statistic Critical Value p-value df\n",
"-------------------------------------------------------------\n",
" 5.953535 2.387906 0.000 (4, 558)\n",
"=============================================================\n",
"H_0: ['realgdp'] do not Granger-cause realcons\n",
"Conclusion: reject H_0 at 5.00% significance level\n"
]
},
{
"data": {
"text/plain": [
"{'conclusion': 'reject',\n",
" 'crit_value': 2.3879062135285474,\n",
" 'df': (4, 558),\n",
" 'pvalue': 0.00010721139549973668,\n",
" 'signif': 0.05,\n",
" 'statistic': 5.9535349329950709}"
]
},
"execution_count": 29,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"var_res.test_causality(variables='realgdp', equation='realcons')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## results: test statistics are 19.79 and 5.95"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# 2. test Granger-causality between 2 variables without further variables in the system"
]
},
{
"cell_type": "code",
"execution_count": 21,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"mdata2 = iidata.data\n",
"mdata2 = mdata2[['realgdp','realcons']]\n",
"mdata2.index = pandas.DatetimeIndex(quarterly)\n",
"data2 = mdata2"
]
},
{
"cell_type": "code",
"execution_count": 22,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"model2 = VAR(data2)\n",
"var_res2 = model2.fit(maxlags=4, trend=\"c\")"
]
},
{
"cell_type": "code",
"execution_count": 30,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Granger causality f-test\n",
"=============================================================\n",
" Test statistic Critical Value p-value df\n",
"-------------------------------------------------------------\n",
" 16.888324 2.395430 0.000 (4, 380)\n",
"=============================================================\n",
"H_0: ['realcons'] do not Granger-cause realgdp\n",
"Conclusion: reject H_0 at 5.00% significance level\n"
]
},
{
"data": {
"text/plain": [
"{'conclusion': 'reject',\n",
" 'crit_value': 2.3954299347613008,\n",
" 'df': (4, 380),\n",
" 'pvalue': 9.3501742398327963e-13,\n",
" 'signif': 0.05,\n",
" 'statistic': 16.888324307247704}"
]
},
"execution_count": 30,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"var_res2.test_causality(variables='realcons', equation='realgdp')"
]
},
{
"cell_type": "code",
"execution_count": 31,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Granger causality f-test\n",
"=============================================================\n",
" Test statistic Critical Value p-value df\n",
"-------------------------------------------------------------\n",
" 2.956067 2.395430 0.020 (4, 380)\n",
"=============================================================\n",
"H_0: ['realgdp'] do not Granger-cause realcons\n",
"Conclusion: reject H_0 at 5.00% significance level\n"
]
},
{
"data": {
"text/plain": [
"{'conclusion': 'reject',\n",
" 'crit_value': 2.3954299347613008,\n",
" 'df': (4, 380),\n",
" 'pvalue': 0.019942769805285872,\n",
" 'signif': 0.05,\n",
" 'statistic': 2.9560671453411649}"
]
},
"execution_count": 31,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"var_res2.test_causality(variables='realgdp', equation='realcons')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## results: test statistics are 16.88 and 2.96"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.4.3+"
}
},
"nbformat": 4,
"nbformat_minor": 0
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment