{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "jupyter": {
     "source_hidden": true
    }
   },
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "import seaborn as sns\n",
    "from matplotlib import pyplot as plt\n",
    "from statsmodels.tsa.stattools import adfuller"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "series=pd.read_csv(\"airline-passengers.csv\",header=0, index_col=0,parse_dates=True,squeeze=True )"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "series"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "a) Check Stationarity By looking at plots "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "series.plot(figsize=(12,8))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The time series is not stationary"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "plt.figure(figsize=(12,8))\n",
    "plt.hist(series, edgecolor='black')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "b) Using Statistical Measures ( Mean & Variance)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "X=series.values\n",
    "split=int(len(X)/2)\n",
    "X1,X2=X[0:split],X[split:]\n",
    "mean1, mean2=X1.mean(),X2.mean()\n",
    "var1, var2= X1.var(),X2.var()\n",
    "print('mean1=%f,mean2=%f'%(mean1,mean2))\n",
    "print('var1=%f,var2=%f'%(var1,var2))\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The time sereis non-stationary"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "c)  Using AFFuller Test "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "X=series.values\n",
    "result=adfuller(X)\n",
    "print('ADF Statistic: %f'%result[0])\n",
    "print('p-value : %f'%result[1])\n",
    "print(' Citical Values:')\n",
    "for key , value in result[4].items():\n",
    "    print('\\t%s: %.3f' % (key, value))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Result : Accept HO: Time Sereis is non-stationary "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Achieving Stationarity "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Power Transforms "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Lof Transformation"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "X=series.values\n",
    "X=np.log(X)\n",
    "result=adfuller(X)\n",
    "print('ADF Statistic: %f'%result[0])\n",
    "print('p-value : %f'%result[1])\n",
    "print(' Citical Values:')\n",
    "for key , value in result[4].items():\n",
    "    print('\\t%s: %.3f' % (key, value))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "plt.figure(figsize=(12,8))\n",
    "plt.plot(X)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "plt.figure(figsize=(12,8))\n",
    "plt.plot(X)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Square-root Transformation "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "X=series.values\n",
    "X=np.sqrt(X)\n",
    "result=adfuller(X)\n",
    "print('ADF Statistic: %f'%result[0])\n",
    "print('p-value : %f'%result[1])\n",
    "print(' Citical Values:')\n",
    "for key , value in result[4].items():\n",
    "    print('\\t%s: %.3f' % (key, value))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "time sereis is still non-stationary"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "plt.figure(figsize=(12,8))\n",
    "plt.plot(X)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "plt.figure(figsize=(12,8))\n",
    "plt.hist(X,edgecolor='black')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from scipy.stats import boxcox\n",
    "df=pd.DataFrame(series.values)\n",
    "df.columns=['passengers']\n",
    "df['passengers']=boxcox(df['passengers'],lmbda=-0.5)\n",
    "plt.figure(figsize=(12,8))\n",
    "plt.subplot(211)\n",
    "plt.plot(df['passengers'])\n",
    "plt.subplot(212)\n",
    "plt.hist(df['passengers'],edgecolor='black')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Differencing "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "X=series.values"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "X"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "diff=list()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "diff"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "for i in range(1, len(X)):\n",
    "    value = X[i] - X[i - 1]\n",
    "    diff.append(value)\n",
    "\n",
    "plt.figure(figsize=(20,6))\n",
    "plt.plot(diff)\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "diff"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "result=adfuller(diff)\n",
    "print('ADF Statistic: %f'%result[0])\n",
    "print('p-value : %f'%result[1])\n",
    "print(' Citical Values:')\n",
    "for key , value in result[4].items():\n",
    "    print('\\t%s: %.3f' % (key, value))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "diff2=list()\n",
    "for i in range(1, len(diff)):\n",
    "    value = diff[i] - diff[i - 1]\n",
    "    diff2.append(value)\n",
    "\n",
    "plt.figure(figsize=(20,6))\n",
    "plt.plot(diff2)\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "result=adfuller(diff2)\n",
    "print('ADF Statistic: %f'%result[0])\n",
    "print('p-value : %f'%result[1])\n",
    "print(' Citical Values:')\n",
    "for key , value in result[4].items():\n",
    "    print('\\t%s: %.3f' % (key, value))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Detrending by Model Fitting"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn.linear_model import LinearRegression\n",
    "X = [i for i in range(0, len(series))]\n",
    "X = np.reshape(X, (len(X), 1))\n",
    "y = series.values\n",
    "model = LinearRegression()\n",
    "model.fit(X, y)\n",
    "# calculate trend\n",
    "trend = model.predict(X)\n",
    "# plot trend\n",
    "plt.plot(y)\n",
    "plt.plot(trend)\n",
    "plt.show()\n",
    "# detrend\n",
    "detrended = [y[i]-trend[i] for i in range(0, len(series))]\n",
    "# plot detrended\n",
    "plt.plot(detrended)\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "trend"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "detrended"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "result=adfuller(detrended)\n",
    "print('ADF Statistic: %f'%result[0])\n",
    "print('p-value : %f'%result[1])\n",
    "print(' Citical Values:')\n",
    "for key , value in result[4].items():\n",
    "    print('\\t%s: %.3f' % (key, value))"
   ]
  },
  {
   "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.8.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
