{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "9f9d82b7-e0ed-4098-bf03-679a538cf723",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "/home/jonas_h/ch4_py/notebooks/Time Series Analysis/dlm_helper\n"
     ]
    }
   ],
   "source": [
    "cd .."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "7a02cc62-6a31-4412-b6c2-5523b7b44c1a",
   "metadata": {},
   "outputs": [],
   "source": [
    "import statsmodels.api as sm\n",
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "\n",
    "import dlmhelper.data\n",
    "import dlmhelper.tools"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "6c362837-6665-406d-ac77-8f02ee561e45",
   "metadata": {},
   "outputs": [],
   "source": [
    "dataset = sm.datasets.co2.load()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "b63314e0-e222-4fc0-8326-9d5173a33c92",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Mauna Loa Weekly Atmospheric CO2 Data\n"
     ]
    }
   ],
   "source": [
    "print(sm.datasets.co2.TITLE)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "fb751886-fa6d-4b57-bbd7-a5f2c14ad5c3",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1958-03-29    316.1\n",
       "1958-04-05    317.3\n",
       "1958-04-12    317.6\n",
       "1958-04-19    317.5\n",
       "1958-04-26    316.4\n",
       "              ...  \n",
       "2001-12-01    370.3\n",
       "2001-12-08    370.8\n",
       "2001-12-15    371.2\n",
       "2001-12-22    371.3\n",
       "2001-12-29    371.5\n",
       "Freq: W-SAT, Name: co2, Length: 2284, dtype: float64"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dataset.data.co2"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1b069d03-d157-4be8-bd52-279280731c92",
   "metadata": {},
   "source": [
    "Now we create a time series object from this data using the following inputs:\n",
    "\n",
    "- **time64 & data** : provide the data with corresponding time (given as numpy.datetime64) values\n",
    "- **product_type** : allows to save some identifier with the data\n",
    "- **time_unit** : the time unit declares what a single time step corresponds to\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "c467431f-7715-4d5a-bac0-1d5192552041",
   "metadata": {},
   "outputs": [],
   "source": [
    "time64 = np.asarray(dataset.data.index)\n",
    "data = np.asarray(dataset.data.co2)\n",
    "\n",
    "product_type=\"Mauna_Loa_CO2\"\n",
    "time_unit = \"week\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "434d0279-41b8-4a2f-ac0e-3cda05e6a5c6",
   "metadata": {},
   "outputs": [],
   "source": [
    "#Uncomment to see the docstring for TimeSeries\n",
    "#?dlmhelper.data.TimeSeries"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "7ede2ec2-5a5f-40fd-abb1-c41e5d7c4857",
   "metadata": {},
   "outputs": [],
   "source": [
    "timeseries = dlmhelper.data.TimeSeries(data, time64 = time64, time_unit=time_unit, product_type=product_type)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fe992115-a7fc-4e80-b96b-1119553af847",
   "metadata": {},
   "source": [
    "The timeseries can be quickly plotted"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "719ff329-6660-45e3-9d05-a2ec50d697b3",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 24272 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "_fig, _ax = timeseries.plot()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "df18053b-7eca-4cf5-b06e-c201f3966a5e",
   "metadata": {},
   "source": [
    "Next we perform a single dynamic linear fit on the time series"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "4241900c-2c53-4a92-9344-cacc5daaa6af",
   "metadata": {},
   "outputs": [],
   "source": [
    "result = dlmhelper.tools.dlm_fit(timeseries, \"Mauna_Loa_CO2_test\",level=True, variable_level=False,trend=True, variable_trend=True,\n",
    "                    seasonal=True, autoregressive=1, irregular=True, seasonal_period = [52], \n",
    "                       )"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "ff2facce-f3c2-4bf5-8505-0110475caec8",
   "metadata": {},
   "outputs": [],
   "source": [
    "#Uncomment to see the docstring for dlm_fit\n",
    "#?dlmhelper.tools.dlm_fit"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "af27a45e-de85-4b88-8f70-85287f5f253d",
   "metadata": {},
   "source": [
    "We can quicky visualize an overview of the results"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "68c51b09-140a-4fb0-acb1-514562903234",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/home/jonas_h/ch4_py/notebooks/Time Series Analysis/dlm_helper/dlmhelper/data.py:866: RuntimeWarning: invalid value encountered in sqrt\n",
      "  ax2.plot(time, self.trend+np.sqrt(self.trend_cov),color=CF,ls='--')\n",
      "/home/jonas_h/ch4_py/notebooks/Time Series Analysis/dlm_helper/dlmhelper/data.py:867: RuntimeWarning: invalid value encountered in sqrt\n",
      "  ax2.plot(time, self.trend-np.sqrt(self.trend_cov),color=CF,ls='--')\n",
      "/home/jonas_h/ch4_py/notebooks/Time Series Analysis/dlm_helper/dlmhelper/data.py:871: RuntimeWarning: invalid value encountered in sqrt\n",
      "  _ymax = np.nanpercentile(self.trend+np.sqrt(self.trend_cov),99)\n",
      "/home/jonas_h/ch4_py/notebooks/Time Series Analysis/dlm_helper/dlmhelper/data.py:872: RuntimeWarning: invalid value encountered in sqrt\n",
      "  _ymin = np.nanpercentile(self.trend-np.sqrt(self.trend_cov),1)\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 143844 characters]\n",
      "text/plain": [
       "<Figure size 1152x576 with 5 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "fig = result.plot_summary(figsize=(16,8))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c686c61c-f070-49ff-b5ba-9e1a31ae8bfc",
   "metadata": {},
   "source": [
    "It is also possible to perform an ensemble fit on the data using a single command. We keep the standard values for the level and trend component and "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "af27e773-63da-4483-8319-8cf326806014",
   "metadata": {},
   "outputs": [],
   "source": [
    "seasonal = [True, False]\n",
    "\n",
    "seasonal_period = [\n",
    "                [52]\n",
    "                ]\n",
    "seasonal_harmonics = [\n",
    "                    [[1,2]]\n",
    "                    ]\n",
    "\n",
    "variable_seasonal = [\n",
    "                    [[False]]\n",
    "                    ]\n",
    "\n",
    "autoregressive = [None,1]\n",
    "irregular = [True, False]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "06453da7-ec50-4c81-aca5-c615e27344a5",
   "metadata": {},
   "outputs": [],
   "source": [
    "result_list = dlmhelper.tools.dlm_ensemble(timeseries, \"CO2_ensemble_test\",\n",
    "                                           seasonal=seasonal, seasonal_period=seasonal_period,\n",
    "                                           seasonal_harmonics=seasonal_harmonics,variable_seasonal = variable_seasonal,\n",
    "                                           autoregressive=autoregressive, irregular=irregular)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "32dec29c-3442-4f42-8729-edc20101ebbc",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "| Model           | converged   |     aic |       ll |      ssr |      mse |   cov_level |   cov_trend |   cov_seas |    cov_ar |   cv_amse |   agg |\n",
      "|:----------------|:------------|--------:|---------:|---------:|---------:|------------:|------------:|-----------:|----------:|----------:|------:|\n",
      "| LTs_SP52H2_A1_I | True        | 2278.22 | -1135.11 | 1818.92  | 0.819704 |   0.0551702 | 3.89396e-06 | 0.00496236 | 0.0893292 |       nan |   nan |\n",
      "| LTs_SP52H2_A1   | True        | 2398.29 | -1196.14 | 1607.61  | 0.724476 |   0.0372615 | 6.95113e-06 | 0.00616    | 0.0493256 |       nan |   nan |\n",
      "| LTs_SP52H1_A1_I | True        | 2529.56 | -1260.78 | 1150.43  | 0.51798  |   0.0628057 | 3.96323e-06 | 0.00570997 | 0.0942135 |       nan |   nan |\n",
      "| LTs_SP52H1_A1   | True        | 2566.73 | -1280.37 | 1086.47  | 0.489179 |   0.0503762 | 4.39114e-06 | 0.00612512 | 0.0633981 |       nan |   nan |\n",
      "| LTs_SP52H2_I    | True        | 2619.49 | -1307.75 | 2100.31  | 0.946514 |   0.0356785 | 0.00348678  | 0.0181953  | 0         |       nan |   nan |\n",
      "| LTs_SP52H1_I    | True        | 2822.17 | -1409.09 | 1414.38  | 0.636821 |   0.0636471 | 0.00660217  | 0.0413192  | 0         |       nan |   nan |\n",
      "| LTs_A1          | True        | 2913    | -1453.5  |  518.629 | 0.233301 |   0.0333737 | 0.00848057  | 0          | 0.0305135 |       nan |   nan |\n",
      "| LTs_A1_I        | True        | 2915    | -1453.5  |  518.629 | 0.233302 |   0.0333699 | 0.00847988  | 0          | 0.0305099 |       nan |   nan |\n",
      "| LTs_I           | True        | 2943.34 | -1469.67 |  528.314 | 0.237658 |   0.0255248 | 0.00809624  | 0          | 0         |       nan |   nan |\n",
      "| LTs             | True        | 4564.37 | -2281.18 | 1090.67  | 0.490629 |   0.0817381 | 0.00931267  | 0          | 0         |       nan |   nan |\n",
      "| LTs_SP52H1      | True        | 4576.15 | -2287.07 | 1489.99  | 0.670864 |   1.90428   | 0.0358904   | 1.82383    | 0         |       nan |   nan |\n",
      "| LTs_SP52H2      | True        | 4588.11 | -2293.06 | 1723.12  | 0.776532 |   2.016     | 0.0424603   | 1.9361     | 0         |       nan |   nan |\n"
     ]
    }
   ],
   "source": [
    "result_list.summary()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "e0c4852f-1d99-4ef2-8c47-c2f074c774af",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 121088 characters]\n",
      "text/plain": [
       "<Figure size 1152x576 with 8 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "result_list.plot_summary(num=8, figsize=(16,8))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4ddb8281-bc7f-4ce3-afea-3963a52c62e8",
   "metadata": {},
   "source": [
    "The best fit (according to some chose measure) can be also easily extracted:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "c9699795-31bb-4c88-bb3f-6508428eb0d1",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/home/jonas_h/ch4_py/notebooks/Time Series Analysis/dlm_helper/dlmhelper/data.py:866: RuntimeWarning: invalid value encountered in sqrt\n",
      "  ax2.plot(time, self.trend+np.sqrt(self.trend_cov),color=CF,ls='--')\n",
      "/home/jonas_h/ch4_py/notebooks/Time Series Analysis/dlm_helper/dlmhelper/data.py:867: RuntimeWarning: invalid value encountered in sqrt\n",
      "  ax2.plot(time, self.trend-np.sqrt(self.trend_cov),color=CF,ls='--')\n",
      "/home/jonas_h/ch4_py/notebooks/Time Series Analysis/dlm_helper/dlmhelper/data.py:871: RuntimeWarning: invalid value encountered in sqrt\n",
      "  _ymax = np.nanpercentile(self.trend+np.sqrt(self.trend_cov),99)\n",
      "/home/jonas_h/ch4_py/notebooks/Time Series Analysis/dlm_helper/dlmhelper/data.py:872: RuntimeWarning: invalid value encountered in sqrt\n",
      "  _ymin = np.nanpercentile(self.trend-np.sqrt(self.trend_cov),1)\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 120692 characters]\n",
      "text/plain": [
       "<Figure size 864x576 with 5 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "best_fit = result_list.get_best_result(sort='aic')\n",
    "fig = best_fit.plot_summary(figsize=(12,8))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "a0d8c346-885a-4d43-b1a5-fcf545cbe60d",
   "metadata": {},
   "outputs": [],
   "source": [
    "gr = []\n",
    "gr_err = []\n",
    "years = np.arange(1960,2000,1)\n",
    "for y in years:\n",
    "    _gr, _gr_err = dlmhelper.tools.annual_level_increase(best_fit, y, tolerance=np.timedelta64(5,'D'))\n",
    "    gr.append(_gr)\n",
    "    gr_err.append(_gr_err)\n",
    "    \n",
    "gr = np.array(gr)\n",
    "gr_err = np.array(gr_err)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "c2025136-fea0-4225-a93f-3127914d6ec7",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0.72804934, 0.72523133, 0.72795212, 0.7444936 , 0.7812283 ,\n",
       "       0.83658487, 0.89798806, 0.95853535, 1.01737439, 1.0646211 ,\n",
       "       1.10207009, 1.13917022, 1.17570719, 1.20198633, 1.22675634,\n",
       "       1.26716471, 1.32033231, 1.37477305, 1.4173855 , 1.44996736,\n",
       "       1.47211566, 1.4904921 , 1.51390615, 1.54085844, 1.59271137,\n",
       "       1.57935891, 1.58934032, 1.58200519, 1.54426396, 1.48554089,\n",
       "       1.4294814 , 1.39581171, 1.39998833, 1.44726061, 1.51769531,\n",
       "       1.58506939, 1.63876459, 1.67601254, 1.68740625, 1.67240821])"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "gr"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "054a8bdd-b8f1-4967-aa6e-b7275a27d6fb",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<BarContainer object of 40 artists>"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "[encoded data, 14892 characters]\n",
      "text/plain": [
       "<Figure size 1008x1008 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "fig = plt.figure(figsize=(14,14))\n",
    "ax = fig.add_subplot(111)\n",
    "\n",
    "ax.bar(years/\n",
    "/dlm_helper/examples/\n",
    "/\n",
    "/dlm_helper/examples/\n",
    ",gr,width=1, edgecolor=\"black\", yerr = gr_err)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "dd28cdd2-a6ff-4290-838c-51a59a34b5d1",
   "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.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
