{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "import dautil as dl\n",
    "from scipy import stats\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,
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "lr = dl.nb.LatexRenderer(chapter=3, start=7)\n",
    "lr.render(r'f(k;n,p) = \\Pr(X = k) = {n\\choose k}p^k(1-p)^{n-k}')\n",
    "lr.render(r'{n\\choose k}=\\frac{n!}{k!(n-k)!}')\n",
    "proposition9 = r'P(a < p < b\\mid m;n) = \\frac {\\int_a^b {n+m \\choose m} p^m (1-p)^n\\,dp} {\\int_0^1 {n+m \\choose m} p^m (1-p)^n\\,dp}\\!'\n",
    "lr.render(proposition9)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def load():\n",
    "    rainy = dl.data.Weather.rain_values() > 0\n",
    "    nrains = np.cumsum(rainy)\n",
    "\n",
    "    return len(rainy), nrains"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def posterior(i, u, data):\n",
    "    return stats.binom(i, u).pmf(data[i])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def set_ylabel(ax):\n",
    "    ax.set_ylabel('Probability rain')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def plot_posterior(ax, day, u, nrains):\n",
    "    ax.set_title('Posterior distribution for day {}'.format(day))\n",
    "    ax.plot(posterior(day, u, nrains),\n",
    "            label='rainy days in period={}'.format(nrains[day]))\n",
    "    ax.set_xlabel('Uniform prior parameter')\n",
    "    ax.set_ylabel('Probability rain')\n",
    "    ax.legend(loc='best')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "%matplotlib inline\n",
    "dl.options.mimic_seaborn()\n",
    "context = dl.nb.Context('determining_bias')\n",
    "dl.nb.RcWidget(context)\n",
    "n, nrains = load()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def plot(day1=1, day2=30):\n",
    "    fig, [[upleft, upright], [downleft, downright]] = plt.subplots(2, 2)\n",
    "    plt.suptitle('Determining bias of rain data')\n",
    "    x = np.arange(n) + 1\n",
    "    upleft.set_title('Frequentist Approach')\n",
    "    upleft.plot(x, nrains/x, label='Probability rain')\n",
    "    upleft.set_xlabel('Days')\n",
    "    set_ylabel(upleft)\n",
    "\n",
    "    max_p = np.zeros(n)\n",
    "    u = np.linspace(0, 1, 100)\n",
    "\n",
    "    for i in x - 1:\n",
    "        max_p[i] = posterior(i, u, nrains).argmax()/100\n",
    "\n",
    "    downleft.set_title('Bayesian Approach')\n",
    "    downleft.plot(x, max_p)\n",
    "    downleft.set_xlabel('Days')\n",
    "    set_ylabel(downleft)\n",
    "\n",
    "    plot_posterior(upright, day1, u, nrains)\n",
    "    plot_posterior(downright, day2, u, nrains)\n",
    "    plt.tight_layout()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "interact(plot, day1=(1, n), day2=(1, n))\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
}
