{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import matplotlib.pyplot as plt\n",
    "import numpy as np\n",
    "from statsmodels.tsa.stattools import adfuller"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "def generate_ar_process(lags, coefs, length):\n",
    "    \n",
    "    #cast coefs to np array\n",
    "    coefs = np.array(coefs)\n",
    "    \n",
    "    #initial values\n",
    "    series = [np.random.normal() for _ in range(lags)]\n",
    "    \n",
    "    for _ in range(length):\n",
    "        #get previous values of the series, reversed\n",
    "        prev_vals = series[-lags:][::-1]\n",
    "        \n",
    "        #get new value of time series\n",
    "        new_val = np.sum(np.array(prev_vals) * coefs) + np.random.normal()\n",
    "        \n",
    "        series.append(new_val)\n",
    "        \n",
    "    return np.array(series)"
   ]
  },
  {
   "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": "markdown",
   "metadata": {},
   "source": [
    "# AR(1) Process"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Stationary"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Text(0.5, 1.0, 'Stationary AR(1) Process')"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "[encoded data, 40784 characters]\n",
      "text/plain": [
       "<Figure size 720x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "ar_1_process = generate_ar_process(1, [.5], 100)\n",
    "plt.figure(figsize=(10,4))\n",
    "plt.plot(ar_1_process)\n",
    "plt.title('Stationary AR(1) Process', fontsize=18)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "ADF Statistic: -5.612850\n",
      "p-value: 0.000001\n"
     ]
    }
   ],
   "source": [
    "perform_adf_test(ar_1_process)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Non-Stationary"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Text(0.5, 1.0, 'Non-Stationary AR(1) Process')"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "[encoded data, 31200 characters]\n",
      "text/plain": [
       "<Figure size 720x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "ar_1_process_unit_root = generate_ar_process(1, [1], 100)\n",
    "plt.figure(figsize=(10,4))\n",
    "plt.plot(ar_1_process_unit_root)\n",
    "plt.title('Non-Stationary AR(1) Process', fontsize=18)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "ADF Statistic: -1.224748\n",
      "p-value: 0.662860\n"
     ]
    }
   ],
   "source": [
    "perform_adf_test(ar_1_process_unit_root)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# AR(2) Process"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Stationary"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Text(0.5, 1.0, 'Stationary AR(2) Process')"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "[encoded data, 41432 characters]\n",
      "text/plain": [
       "<Figure size 720x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "ar_2_process = generate_ar_process(2, [.5, .3], 100)\n",
    "plt.figure(figsize=(10,4))\n",
    "plt.plot(ar_2_process)\n",
    "plt.title('Stationary AR(2) Process', fontsize=18)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "ADF Statistic: -5.771185\n",
      "p-value: 0.000001\n"
     ]
    }
   ],
   "source": [
    "perform_adf_test(ar_2_process)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Non-Stationary"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Text(0.5, 1.0, 'Non-Stationary AR(2) Process')"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "[encoded data, 32664 characters]\n",
      "text/plain": [
       "<Figure size 720x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "ar_2_process_unit_root = generate_ar_process(2, [.7, .3], 100)\n",
    "plt.figure(figsize=(10,4))\n",
    "plt.plot(ar_2_process_unit_root)\n",
    "plt.title('Non-Stationary AR(2) Process', fontsize=18)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "ADF Statistic: -1.530086\n",
      "p-value: 0.518579\n"
     ]
    }
   ],
   "source": [
    "perform_adf_test(ar_2_process_unit_root)"
   ]
  },
  {
   "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
}
