{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 152,
   "metadata": {},
   "outputs": [],
   "source": [
    "from statsmodels.tsa.stattools import grangercausalitytests\n",
    "import numpy as np\n",
    "import pandas as pd\n",
    "import matplotlib.pyplot as plt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 153,
   "metadata": {},
   "outputs": [],
   "source": [
    "#build the time series, just a simple AR(1)\n",
    "t1 = [0.1*np.random.normal()]\n",
    "for _ in range(100):\n",
    "    t1.append(0.5*t1[-1] + 0.1*np.random.normal())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 154,
   "metadata": {},
   "outputs": [],
   "source": [
    "#build the time series that is granger caused by t1\n",
    "t2 = [item + 0.1*np.random.normal() for item in t1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 155,
   "metadata": {},
   "outputs": [],
   "source": [
    "#adjust t1 and t2\n",
    "t1 = t1[3:]\n",
    "t2 = t2[:-3]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 156,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<matplotlib.legend.Legend at 0x16e4277dfc8>"
      ]
     },
     "execution_count": 156,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "[encoded data, 47164 characters]\n",
      "text/plain": [
       "<Figure size 720x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "plt.figure(figsize=(10,4))\n",
    "plt.plot(t1, color='b')\n",
    "plt.plot(t2, color='r')\n",
    "\n",
    "plt.legend(['t1', 't2'], fontsize=16)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 157,
   "metadata": {},
   "outputs": [],
   "source": [
    "ts_df = pd.DataFrame(columns=['t2', 't1'], data=zip(t2,t1))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 158,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>t2</th>\n",
       "      <th>t1</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0.155416</td>\n",
       "      <td>0.098322</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>0.022086</td>\n",
       "      <td>0.095749</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>-0.065441</td>\n",
       "      <td>-0.036119</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>0.239062</td>\n",
       "      <td>-0.105903</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>-0.076154</td>\n",
       "      <td>-0.034917</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>93</th>\n",
       "      <td>0.130000</td>\n",
       "      <td>0.042752</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>94</th>\n",
       "      <td>-0.105530</td>\n",
       "      <td>0.047093</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>95</th>\n",
       "      <td>0.171263</td>\n",
       "      <td>0.030124</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>96</th>\n",
       "      <td>-0.014759</td>\n",
       "      <td>-0.093222</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>97</th>\n",
       "      <td>0.078611</td>\n",
       "      <td>-0.076502</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>98 rows × 2 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "          t2        t1\n",
       "0   0.155416  0.098322\n",
       "1   0.022086  0.095749\n",
       "2  -0.065441 -0.036119\n",
       "3   0.239062 -0.105903\n",
       "4  -0.076154 -0.034917\n",
       "..       ...       ...\n",
       "93  0.130000  0.042752\n",
       "94 -0.105530  0.047093\n",
       "95  0.171263  0.030124\n",
       "96 -0.014759 -0.093222\n",
       "97  0.078611 -0.076502\n",
       "\n",
       "[98 rows x 2 columns]"
      ]
     },
     "execution_count": 158,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ts_df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 159,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "Granger Causality\n",
      "number of lags (no zero) 1\n",
      "ssr based F test:         F=0.3711  , p=0.5439  , df_denom=94, df_num=1\n",
      "ssr based chi2 test:   chi2=0.3830  , p=0.5360  , df=1\n",
      "likelihood ratio test: chi2=0.3822  , p=0.5364  , df=1\n",
      "parameter F test:         F=0.3711  , p=0.5439  , df_denom=94, df_num=1\n",
      "\n",
      "Granger Causality\n",
      "number of lags (no zero) 2\n",
      "ssr based F test:         F=0.7496  , p=0.4754  , df_denom=91, df_num=2\n",
      "ssr based chi2 test:   chi2=1.5816  , p=0.4535  , df=2\n",
      "likelihood ratio test: chi2=1.5688  , p=0.4564  , df=2\n",
      "parameter F test:         F=0.7496  , p=0.4754  , df_denom=91, df_num=2\n",
      "\n",
      "Granger Causality\n",
      "number of lags (no zero) 3\n",
      "ssr based F test:         F=43.0289 , p=0.0000  , df_denom=88, df_num=3\n",
      "ssr based chi2 test:   chi2=139.3549, p=0.0000  , df=3\n",
      "likelihood ratio test: chi2=85.7812 , p=0.0000  , df=3\n",
      "parameter F test:         F=43.0289 , p=0.0000  , df_denom=88, df_num=3\n"
     ]
    }
   ],
   "source": [
    "gc_res = grangercausalitytests(ts_df, 3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "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.7.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
