{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "from scipy import stats\n",
    "import dautil as dl\n",
    "from dautil.stats import jackknife\n",
    "import pandas as pd\n",
    "import matplotlib.pyplot as plt\n",
    "import numpy as np\n",
    "from IPython.html.widgets.interaction import interact\n",
    "from IPython.display import HTML"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "def set_labels(ax, var):\n",
    "    ax.set_xlabel('Day of year')\n",
    "    ax.set_ylabel(dl.data.Weather.get_header(var))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def plot_bayes(ax, metric, var, df):\n",
    "    vals = np.array([[v.statistic, v.minmax[0], v.minmax[1]] for v in\n",
    "                     df[metric].values])\n",
    "\n",
    "    ax.set_title('Bayes {}'.format(metric))\n",
    "    ax.errorbar(np.arange(len(vals)), vals.T[0], yerr=(vals.T[1], vals.T[2]))\n",
    "    set_labels(ax, var)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def plot_jackknife(ax, metric, func, var, df):\n",
    "    vals = df.apply(lambda x: jackknife(x, func, alpha=0.95))\n",
    "    vals = np.array([[v[0], v[1], v[2]] for v in vals.values])\n",
    "\n",
    "    ax.set_title('Jackknife {}'.format(metric))\n",
    "    ax.errorbar(np.arange(len(vals)), vals.T[0], yerr=(vals.T[1], vals.T[2]))\n",
    "    set_labels(ax, var)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "def confidence_interval(var='TEMP'):\n",
    "    df = dl.data.Weather.load().dropna()\n",
    "    df = dl.ts.groupby_yday(df)\n",
    "    \n",
    "    def f(x):\n",
    "        return stats.bayes_mvs(x, alpha=0.95)\n",
    "\n",
    "    bayes_df = pd.DataFrame([[v[0], v[1], v[2]] for v in\n",
    "                             df[var].apply(f).values], columns=['Mean', 'Var',\n",
    "                                                                'Std'])\n",
    "\n",
    "    fig, axes = plt.subplots(2, 2)\n",
    "    fig.suptitle('Confidence Intervals')\n",
    "\n",
    "    plot_bayes(axes[0][0], 'Mean', var, bayes_df)\n",
    "    plot_bayes(axes[0][1], 'Var', var, bayes_df)\n",
    "    plot_jackknife(axes[1][0], 'Mean', np.mean, var, df[var])\n",
    "    plot_jackknife(axes[1][1], 'Mean', np.var, var, df[var])\n",
    "\n",
    "    plt.tight_layout()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "%matplotlib inline\n",
    "dl.options.mimic_seaborn()\n",
    "context = dl.nb.Context('bayes_confidence')\n",
    "dl.nb.RcWidget(context)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "interact(confidence_interval, var=dl.data.Weather.get_headers())\n",
    "HTML(dl.report.HTMLBuilder().watermark())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  },
  {
   "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
}
