{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "from scipy.stats.distributions import gamma\n",
    "import matplotlib.pyplot as plt\n",
    "import dautil as dl\n",
    "import pandas as pd\n",
    "from IPython.display import HTML"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "lr = dl.nb.LatexRenderer(chapter=3, start=2)\n",
    "lr.render(r'f(x;k,\\theta) =  \\frac{x^{k-1}e^{-\\frac{x}{\\theta}}}{\\theta^k\\Gamma(k)} \\quad \\text{ for } x > 0 \\text{ and } k, \\theta > 0')\n",
    "lr.render(r'\\scriptstyle \\mathbf{E}[ X] = k \\theta')\n",
    "lr.render(r'\\scriptstyle\\operatorname{Var}[ X] = k \\theta^2')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "rain = dl.data.Weather.load()['RAIN'].resample('M').dropna()\n",
    "rain = dl.ts.groupby_month(rain)\n",
    "rain = rain.get_group(1)\n",
    "\n",
    "dist = dl.stats.Distribution(rain, gamma)\n",
    "\n",
    "a = (dist.mean() ** 2)/dist.var()\n",
    "shape, loc, scale = dist.fit(a)\n",
    "\n",
    "result = dl.report.HTMLBuilder()\n",
    "result.h1('Fitting Data to the Gamma Distribution')\n",
    "table = dl.report.DFBuilder(['loc', 'scale', 'shape'])\n",
    "table.row([loc, scale, shape])\n",
    "dl.options.set_pd_options()\n",
    "result.h2('Distribution Parameters')\n",
    "result.add_df(table.build())\n",
    "\n",
    "result.h2('Residuals of the Fit')\n",
    "table2 = dist.describe_residuals(a=shape, loc=loc, scale=scale)\n",
    "result.add(table2.to_html())\n",
    "\n",
    "result.h2('Fit Metrics')\n",
    "table3 = pd.DataFrame([{'RMSE': dist.rmse(), 'Mean_AD': dist.mean_ad()}])\n",
    "result.add_df(table3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "%matplotlib inline\n",
    "dl.options.mimic_seaborn()\n",
    "context = dl.nb.Context('fitting_gamma')\n",
    "dl.nb.RcWidget(context)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "fig, ax = plt.subplots()\n",
    "plt.title('Fitting to the gamma distribution')\n",
    "\n",
    "# Limiting the x-asis for a better plot\n",
    "plt.xlim([0, 15])\n",
    "plt.xlabel(dl.data.Weather.get_header('RAIN'))\n",
    "dist.plot(ax)\n",
    "HTML(result.html)"
   ]
  },
  {
   "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
}
