{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import dautil as dl\n",
    "import matplotlib.pyplot as plt\n",
    "from scipy.signal import savgol_filter\n",
    "import pandas as pd\n",
    "import numpy as np\n",
    "import seaborn as sns\n",
    "from IPython.display import HTML"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def error(data, fit):\n",
    "    return data - fit\n",
    "\n",
    "\n",
    "def win_rng():\n",
    "    return range(3, 25, 2)\n",
    "\n",
    "\n",
    "def calc_mape(i, j, pres):\n",
    "    return dl.stats.mape(pres, savgol_filter(pres, i, j))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "pres = dl.data.Weather.load()['PRESSURE'].dropna()\n",
    "pres = pres.resample('A')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "context = dl.nb.Context('eval_smooth')\n",
    "lr = dl.nb.LatexRenderer(chapter=6, start=6, context=context)\n",
    "lr.render(r'Y_j= \\sum _{i=-(m-1)/2}^{i=(m-1)/2}C_i\\, y_{j+i}\\qquad  \\frac{m+1}{2} \\le j \\le n-\\frac{m-1}{2}')"
   ]
  },
  {
   "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",
    "cp = dl.plotting.CyclePlotter(sp.ax)\n",
    "cp.plot(pres.index, pres, label='Pressure')\n",
    "cp.plot(pres.index, savgol_filter(pres, 11, 2), label='Poly order 2')\n",
    "cp.plot(pres.index, savgol_filter(pres, 11, 3), label='Poly order 3')\n",
    "cp.plot(pres.index, savgol_filter(pres, 11, 4), label='Poly order 4')\n",
    "sp.label(ylabel_params=dl.data.Weather.get_header('PRESSURE'))\n",
    "\n",
    "cp = dl.plotting.CyclePlotter(sp.next_ax())\n",
    "stds = [error(pres, savgol_filter(pres, i, 2)).std()\n",
    "        for i in win_rng()]\n",
    "cp.plot(win_rng(), stds, label='Filtered')\n",
    "stds = [error(pres, pd.rolling_mean(pres, i)).std()\n",
    "        for i in win_rng()]\n",
    "cp.plot(win_rng(), stds, label='Rolling mean')\n",
    "sp.label()\n",
    "\n",
    "sp.label(advance=True)\n",
    "sp.ax.boxplot([error(pres, savgol_filter(pres, i, 2))\n",
    "            for i in win_rng()])\n",
    "sp.ax.set_xticklabels(win_rng())\n",
    "\n",
    "sp.label(advance=True)\n",
    "df = dl.report.map_grid(win_rng()[1:], range(1, 5),\n",
    "                 ['win_size', 'poly', 'mape'], calc_mape, pres)\n",
    "sns.heatmap(df, cmap='Blues', ax=sp.ax)\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
}
