{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import statsmodels.api as sm\n",
    "import matplotlib.pyplot as plt\n",
    "from scipy.stats import boxcox\n",
    "import seaborn as sns\n",
    "import dautil as dl\n",
    "from IPython.display import HTML"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "context = dl.nb.Context('normalizing_boxcox')\n",
    "lr = dl.nb.LatexRenderer(chapter=4, start=3, context=context)\n",
    "lr.render(r'y_i^{(\\lambda)} = \\begin{cases} \\dfrac{y_i^\\lambda - 1}{\\lambda} & \\text{if } \\lambda \\neq 0, \\\\[8pt] \\ln{(y_i)} & \\text{if } \\lambda = 0, \\end{cases} ')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "starsCYG = sm.datasets.get_rdataset(\"starsCYG\", \"robustbase\", cache=True).data\n",
    "\n",
    "var = 'log.Te'\n",
    "\n",
    "# Data must be positive\n",
    "transformed, _ = boxcox(starsCYG[var])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "%matplotlib inline\n",
    "dl.nb.RcWidget(context)\n",
    "dl.nb.LabelWidget(2, 2, context)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "sp = dl.plotting.Subplotter(2, 2, context)\n",
    "sp.label()\n",
    "sm.qqplot(starsCYG[var], fit=True, line='s', ax=sp.ax)\n",
    "\n",
    "sp.label(advance=True)\n",
    "sm.qqplot(transformed, fit=True, line='s', ax=sp.ax)\n",
    "\n",
    "sp.label(advance=True)\n",
    "sns.distplot(starsCYG[var], ax=sp.ax)\n",
    "\n",
    "sp.label(advance=True)\n",
    "sns.distplot(transformed, ax=sp.ax)                                       \n",
    "plt.tight_layout()\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
}
