{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "from scipy.stats.distributions import expon\n",
    "import matplotlib.pyplot as plt\n",
    "import dautil as dl\n",
    "from IPython.display import HTML"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "lr = dl.nb.LatexRenderer(chapter=3)\n",
    "lr.render(r'f(x;\\lambda) = \\begin{cases} \\lambda e^{-\\lambda x} &amp; x \\ge 0, \\\\ 0 &amp; x < 0. \\end{cases}')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "rain = dl.data.Weather.load()['RAIN'].dropna()\n",
    "dist = dl.stats.Distribution(rain, expon)\n",
    "dl.options.set_pd_options()\n",
    "html_builder = dl.report.HTMLBuilder()\n",
    "html_builder.h1('Fitting Data to the Exponential Distribution')\n",
    "loc, scale = dist.fit()\n",
    "table = dl.report.DFBuilder(['loc', 'scale'])\n",
    "table.row([loc, scale])\n",
    "html_builder.h2('Distribution Parameters')\n",
    "html_builder.add_df(table.build())\n",
    "\n",
    "\n",
    "pdf = dist.pdf(loc, scale)\n",
    "html_builder.h2('Residuals of the Fit')\n",
    "residuals = dist.describe_residuals()\n",
    "html_builder.add(residuals.to_html())\n",
    "\n",
    "table2 = dl.report.DFBuilder(['Mean_AD', 'RMSE'])\n",
    "table2.row([dist.mean_ad(), dist.rmse()])\n",
    "html_builder.h2('Fit Metrics')\n",
    "html_builder.add_df(table2.build())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "%matplotlib inline\n",
    "dl.options.mimic_seaborn()\n",
    "dl.nb.RcWidget()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "plt.hist(rain, bins=dist.nbins, normed=True, label='Rain')\n",
    "plt.plot(dist.x, pdf, label='PDF')\n",
    "plt.title('Fitting to the exponential 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",
    "plt.ylabel('Probability')\n",
    "plt.legend(loc='best')\n",
    "HTML(html_builder.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
}
