{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import dautil as dl\n",
    "import matplotlib.pyplot as plt\n",
    "import statsmodels.api as sm\n",
    "import numpy as np\n",
    "from IPython.display import HTML"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "temp = dl.data.Weather.load()['TEMP'].dropna()\n",
    "temp = dl.ts.groupby_yday(temp).mean()\n",
    "\n",
    "# Outlier added by malicious person, because noone\n",
    "# laughs at his jokes.\n",
    "temp.values[0] = 100"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "ntemp = len(temp)\n",
    "x = np.arange(1, ntemp + 1)\n",
    "factor = 2 * np.pi/365.25\n",
    "cos_x = sm.add_constant(np.cos(-factor * x - factor * 337))\n",
    "ols_model = sm.OLS(temp, cos_x)\n",
    "ols_results = ols_model.fit()\n",
    "hb = dl.report.HTMLBuilder()\n",
    "hb.h1('Taking variance into account with weighted least squares')\n",
    "hb.h2('Ordinary least squares')\n",
    "hb.add(ols_results.summary().tables[1].as_html())\n",
    "ols_preds = ols_results.predict()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "box = dl.stats.Box(temp)\n",
    "iqrs = box.iqr_from_box()\n",
    "# Adding 1 to avoid div by 0\n",
    "weights = 1./(iqrs + 1)\n",
    "wls_model = sm.WLS(temp, cos_x, weights=weights)\n",
    "wls_results = wls_model.fit()\n",
    "\n",
    "hb.h2('Weighted least squares')\n",
    "hb.add(wls_results.summary().tables[1].as_html())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "%matplotlib inline\n",
    "dl.options.mimic_seaborn()\n",
    "context = dl.nb.Context('weighted_ls')\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",
    "\n",
    "sp.ax.plot(x[1:], temp[1:], 'o', label='Data')\n",
    "sp.ax.plot(x[1:], ols_preds[1:], label='Fit')\n",
    "sp.label(ylabel_params=dl.data.Weather.get_header('TEMP'))\n",
    "\n",
    "sp.label(advance=True)\n",
    "sp.ax.plot(x, iqrs, 'o')\n",
    "\n",
    "sp.next_ax().plot(x[1:], temp[1:], 'o', label='Data')\n",
    "sp.ax.plot(x[1:], wls_results.predict()[1:], label='Fit')\n",
    "sp.label(ylabel_params=dl.data.Weather.get_header('TEMP'))\n",
    "\n",
    "sp.label(advance=True)\n",
    "sp.ax.plot(x, weights, 'o')\n",
    "plt.tight_layout()\n",
    "HTML(hb.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
}
