{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import dautil as dl\n",
    "import pandas as pd\n",
    "import matplotlib.pyplot as plt\n",
    "import numpy as np\n",
    "import seaborn as sns\n",
    "from IPython.display import HTML"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "context = dl.nb.Context('exp_smoothing')\n",
    "lr = dl.nb.LatexRenderer(chapter=6, start=3, context=context)\n",
    "lr.render(r's_{t}= \\alpha x_{t} + (1-\\alpha)s_{t-1},\\ t>0')\n",
    "lr.render(r's_{t} = \\alpha x_{t} + (1-\\alpha)(s_{t-1} + b_{t-1}')\n",
    "lr.render(r'b_{t} = \\beta (s_t - s_{t-1}) + (1-\\beta)b_{t-1}')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "def grid_mse(i, j, devs):\n",
    "    alpha = 0.1 * i\n",
    "    beta = 0.1 * j\n",
    "    cell = dl.ts.double_exp_smoothing(devs.values, alpha, beta)\n",
    "\n",
    "    return dl.stats.mse(devs, cell)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "wind = dl.data.Weather.load()['WIND_SPEED'].dropna()\n",
    "wind = dl.ts.groupby_year(wind).mean()\n",
    "devs = dl.ts.rolling_deviations(wind, 12).dropna()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "%matplotlib inline\n",
    "dl.nb.RcWidget(context)\n",
    "dl.nb.LabelWidget(2, 2, context)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "sp = dl.plotting.Subplotter(2, 2, context)\n",
    "sp.label(ylabel_params=dl.data.Weather.get_header('WIND_SPEED'))\n",
    "sp.ax.plot(wind.index, wind)\n",
    "\n",
    "cp = dl.plotting.CyclePlotter(sp.next_ax())\n",
    "cp.plot(devs.index, devs, label='Rolling Deviations')\n",
    "cp.plot(devs.index, dl.ts.exp_smoothing(devs.values, 0.7), label='Smoothing')\n",
    "sp.label()\n",
    "\n",
    "alphas = 0.01 * np.arange(1, 100)\n",
    "errors = [dl.stats.mse(devs, dl.ts.exp_smoothing(devs.values, alpha)) \n",
    "          for alpha in alphas]\n",
    "sp.label(advance=True)\n",
    "sp.ax.plot(alphas, errors)\n",
    "\n",
    "sp.label(advance=True)\n",
    "rng = range(1, 10)\n",
    "df = dl.report.map_grid(rng, rng, [\"alpha\", \"beta\", \"mse\"], grid_mse, devs) \n",
    "sns.heatmap(df, cmap='Blues', square=True, annot=True, fmt='.1f',\n",
    "            ax=sp.ax)\n",
    "\n",
    "HTML(sp.exit())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "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.4.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
