{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "%matplotlib inline\n",
    "\n",
    "import numpy as np\n",
    "import pandas as pd\n",
    "import statsmodels.api as sm\n",
    "import matplotlib.pyplot as plt\n",
    "\n",
    "from pandas_datareader.data import DataReader"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from matplotlib import rcParams\n",
    "\n",
    "# Restore old behavior of rounding default axis ranges\n",
    "rcParams['axes.autolimit_mode'] = 'round_numbers'\n",
    "rcParams['axes.xmargin'] = 0\n",
    "rcParams['axes.ymargin'] = 0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "dta_pl = pd.read_stata('data/data_PL.dta')\n",
    "dta_pl.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "yy = (2000+(dta_pl['t']-1)//4).astype(str)\n",
    "qq = (1+(dta_pl['t']-1)%4).astype(str)\n",
    "date = yy+'Q'+qq\n",
    "\n",
    "from statsmodels.tsa.base.datetools import dates_from_str\n",
    "date = dates_from_str(date)\n",
    "\n",
    "dta_pl.index = date\n",
    "\n",
    "dta_pl.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "model_pl = sm.tsa.VAR(dta_pl[['GDP','HICP','WIB3M']])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "results_pl = model_pl.fit(4)\n",
    "results_pl.summary()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "irf_pl = results_pl.irf(20)\n",
    "irf_pl.plot(orth=True, impulse='WIB3M', signif=0.1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "fred = DataReader(['GDPC1','GDPDEF','FEDFUNDS','M2SL'], 'fred', start='1945', end='2020')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "temp = fred.resample('QS').mean()\n",
    "\n",
    "iacoviel = 100*np.log(temp).diff(4)\n",
    "iacoviel['FEDFUNDS'] = temp['FEDFUNDS']\n",
    "iacoviel = iacoviel.dropna()\n",
    "\n",
    "iacoviel.columns = ['Y','P','R','M']\n",
    "iacoviel.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "fig, ((ax1, ax2), (ax3, ax4)) = plt.subplots(2, 2, figsize=(14, 9))\n",
    "\n",
    "iacoviel['Y'].to_period('D').plot(ax=ax1)\n",
    "iacoviel['P'].to_period('D').plot(ax=ax2)\n",
    "iacoviel['M'].to_period('D').plot(ax=ax3)\n",
    "iacoviel['R'].to_period('D').plot(ax=ax4)\n",
    "\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "model = sm.tsa.VAR(iacoviel[['Y','P','R','M']])\n",
    "model.select_order(8)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": false
   },
   "outputs": [],
   "source": [
    "results = model.fit(6)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "results.irf(20).plot()\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "results.irf(20).plot(orth=True, impulse='R', signif=0.05)\n",
    "plt.savefig('Simple_VAR.pdf', transparent=True, bbox_inches='tight', pad_inches=0.05)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "results.plot_forecast(4)"
   ]
  }
 ],
 "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.6.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
