{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import dautil as dl\n",
    "import numpy as np\n",
    "import pandas as pd\n",
    "import matplotlib.pyplot as plt\n",
    "import seaborn as sns\n",
    "import ch6util\n",
    "from scipy.signal import medfilt\n",
    "from IPython.display import HTML"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def shuffle(temp):\n",
    "    indices = np.random.choice(start, n/12)\n",
    "    sample = dl.collect.flatten([temp.values[i: i + 12] for i in indices])\n",
    "    sample = medfilt(sample)\n",
    "    df = pd.DataFrame({'TEMP': sample}, index=temp.index[:len(sample)])\n",
    "    df = df.resample('A', how=np.median)\n",
    "\n",
    "    return df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "temp = dl.data.Weather.load()['TEMP'].resample('M', how=np.median).dropna()\n",
    "n = len(temp)\n",
    "start = np.arange(n - 11)\n",
    "np.random.seed(2609787)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "%matplotlib inline\n",
    "context = dl.nb.Context('moving_boot')\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",
    "medians = []\n",
    "slopes = []\n",
    "\n",
    "for i in range(240):\n",
    "    df = shuffle(temp)\n",
    "    slopes.append(dl.ts.ar1(df.values.flatten())['slope'])\n",
    "    medians.append(ch6util.diff_median(df, 2))\n",
    "\n",
    "    if i < 5:\n",
    "        cp.plot(df.index, df.values)\n",
    "        \n",
    "sp.label(ylabel_params=dl.data.Weather.get_header('TEMP'))\n",
    "\n",
    "sns.distplot(medians, ax=sp.next_ax())\n",
    "sp.label()\n",
    "\n",
    "sns.distplot(slopes, ax=sp.next_ax())\n",
    "sp.label()\n",
    "\n",
    "mins = []\n",
    "tops = []\n",
    "xrng = range(30, len(medians))\n",
    "\n",
    "for i in xrng:\n",
    "    min, max = dl.stats.outliers(medians[:i])\n",
    "    mins.append(min)\n",
    "    tops.append(max)\n",
    "\n",
    "    \n",
    "cp = dl.plotting.CyclePlotter(sp.next_ax())\n",
    "cp.plot(xrng, mins, label='5 %')\n",
    "cp.plot(xrng, tops, label='95 %')\n",
    "sp.label()\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
}
