{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "from datetime import datetime\n",
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "from statsmodels.graphics.tsaplots import plot_acf, plot_pacf\n",
    "from statsmodels.tsa.arima_model import ARMA\n",
    "from statsmodels.tsa.stattools import adfuller"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "def parser(s):\n",
    "    return datetime.strptime(s, '%Y-%m-%d')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "def perform_adf_test(series):\n",
    "    result = adfuller(series)\n",
    "    print('ADF Statistic: %f' % result[0])\n",
    "    print('p-value: %f' % result[1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "#get data\n",
    "series = pd.read_csv('catfish.csv', parse_dates=[0], index_col=0, squeeze=True, date_parser=parser)\n",
    "series = series.asfreq(pd.infer_freq(series.index))\n",
    "series = series.loc[datetime(2004,1,1):]\n",
    "series = series.diff().diff().dropna()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "ADF Statistic: -7.162321\n",
      "p-value: 0.000000\n"
     ]
    }
   ],
   "source": [
    "#check stationarity\n",
    "perform_adf_test(series)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[<matplotlib.lines.Line2D at 0x1c8313fcf48>]"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "[encoded data, 40464 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "plt.plot(series)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 9724 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "plot_pacf(series, lags=10)\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Either AR(1), AR(4), AR(6), or AR(10)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 312708 characters]\n",
      "text/plain": [
       "<Figure size 864x864 with 4 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "plt.figure(figsize=(12,12))\n",
    "\n",
    "ar_orders = [1, 4, 6, 10]\n",
    "fitted_model_dict = {}\n",
    "\n",
    "for idx, ar_order in enumerate(ar_orders):\n",
    "    \n",
    "    #create AR(p) model\n",
    "    ar_model = ARMA(series, order=(ar_order,0))\n",
    "    ar_model_fit = ar_model.fit()\n",
    "    fitted_model_dict[ar_order] = ar_model_fit\n",
    "    plt.subplot(4,1,idx+1)\n",
    "    plt.plot(series)\n",
    "    plt.plot(ar_model_fit.fittedvalues)\n",
    "    plt.title('AR(%s) Fit'%ar_order, fontsize=16)\n",
    "\n",
    "plt.tight_layout()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Each model has: \n",
    "\n",
    "## a log likelihood ($l$)\n",
    "## a number of parameters ($k$)\n",
    "## a number of samples used for fitting ($n$)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# AIC = 2$k$ - 2$l$\n",
    "\n",
    "## Lower AIC via higher log likelihood or less parameters\n",
    "\n",
    "# BIC = $\\ln(n)k$ - 2$l$\n",
    "\n",
    "## Lower BIC via higher log likelihood or less parameters or less samples used in fitting"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "AIC for AR(1): 1980.8563105422684\n",
      "AIC for AR(4): 1927.4507590389182\n",
      "AIC for AR(6): 1899.630265985839\n",
      "AIC for AR(10): 1902.2647912124996\n"
     ]
    }
   ],
   "source": [
    "#AIC comparison\n",
    "for ar_order in ar_orders:\n",
    "    print('AIC for AR(%s): %s'%(ar_order, fitted_model_dict[ar_order].aic))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Based on AIC criteria, pick AR(6)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "BIC for AR(1): 1988.8466278246046\n",
      "BIC for AR(4): 1943.4313936035905\n",
      "BIC for AR(6): 1920.9377787387357\n",
      "BIC for AR(10): 1934.2260603418445\n"
     ]
    }
   ],
   "source": [
    "#BIC comparison\n",
    "for ar_order in ar_orders:\n",
    "    print('BIC for AR(%s): %s'%(ar_order, fitted_model_dict[ar_order].bic))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Based on BIC criteria, pick AR(6)"
   ]
  },
  {
   "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": 2
}
