{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "from statsmodels.distributions.empirical_distribution import ECDF\n",
    "import dautil as dl\n",
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "from sklearn.utils import check_random_state\n",
    "from IPython.html.widgets.interaction import interact\n",
    "from IPython.core.display import HTML"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def slope(x, y):\n",
    "    return np.polyfit(x, y, 1)[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def simulate(x, years, rs, p):\n",
    "    N = len(years)\n",
    "    means = np.zeros(N)\n",
    "\n",
    "    for i in range(N):\n",
    "        sample = rs.choice(x, size=365, p=p)\n",
    "        means[i] = sample.mean()\n",
    "\n",
    "    return means, np.diff(means).mean(), slope(years, means)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def run_multiple(times, x, years, p):\n",
    "    sims = []\n",
    "    rs = check_random_state(20)\n",
    "\n",
    "    for i in range(times):\n",
    "        sims.append(simulate(x, years, rs, p))\n",
    "\n",
    "    return np.array(sims)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "%matplotlib inline\n",
    "dl.options.mimic_seaborn()\n",
    "context = dl.nb.Context('sampling_weights')\n",
    "dl.nb.RcWidget(context)\n",
    "dl.nb.LabelWidget(2, 1, context)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "def main(var='TEMP'):\n",
    "    df = dl.data.Weather.load().dropna()[var]\n",
    "    cdf = ECDF(df)\n",
    "    x = cdf.x[1:]\n",
    "    p = np.diff(cdf.y)\n",
    "\n",
    "    df = df.resample('A')\n",
    "    years = df.index.year\n",
    "    sims = run_multiple(500, x, years, p)\n",
    "\n",
    "    sp = dl.plotting.Subplotter(2, 1, context)\n",
    "    plotter = dl.plotting.CyclePlotter(sp.ax)\n",
    "    plotter.plot(years, df.values, label='Data')\n",
    "    plotter.plot(years, sims[0][0], label='Sim 1')\n",
    "    plotter.plot(years, sims[1][0], label='Sim 2')\n",
    "    header = dl.data.Weather.get_header(var)\n",
    "    sp.label(title_params=header, ylabel_params=header)\n",
    "    sp.ax.legend(loc='best')\n",
    "\n",
    "    sp.next_ax()\n",
    "    sp.label()\n",
    "    sp.ax.hist(sims.T[2], normed=True)\n",
    "    plt.figtext(0.2, 0.3, 'Slope of the Data {:.3f}'.format(slope(years, df.values)))\n",
    "    plt.tight_layout()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "interact(main, var=dl.data.Weather.get_headers())\n",
    "HTML(dl.report.HTMLBuilder().watermark())"
   ]
  },
  {
   "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
}
