{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import matplotlib.pyplot as plt\n",
    "from scipy.stats import tmean\n",
    "from scipy.stats.mstats import trima\n",
    "import numpy as np\n",
    "import dautil as dl\n",
    "import seaborn as sns\n",
    "from IPython.display import HTML"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "context = dl.nb.Context('central_tendency')\n",
    "lr = dl.nb.LatexRenderer(chapter=4, context=context)\n",
    "lr.render(r'TM= \\frac{Q_1 + 2Q_2 + Q_3}{4}')\n",
    "lr.render(r'x_\\mathrm{IQM} = {2 \\over n} \\sum_{i=\\frac{n}{4}+1}^{\\frac{3n}{4}}{x_i}')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def iqm(a):\n",
    "    return truncated_mean(a, 25)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "def plotdists(var, ax):\n",
    "    displot_label = 'From {0} to {1} percentiles'\n",
    "    cyc = dl.plotting.Cycler()\n",
    "\n",
    "    for i in range(1, 9, 3):\n",
    "        limits = dl.stats.outliers(var, method='percentiles', \n",
    "                               percentiles=(i, 100 - i))\n",
    "        truncated = trima(var, limits=limits).compressed()\n",
    "        sns.distplot(truncated, ax=ax, color=cyc.color(),\n",
    "                     hist_kws={'histtype': 'stepfilled', 'alpha': 1/i,\n",
    "                               'linewidth': cyc.lw()},\n",
    "                     label=displot_label.format(i, 100 - i))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "def truncated_mean(a, percentile):\n",
    "    limits = dl.stats.outliers(a, method='percentiles', \n",
    "                               percentiles=(percentile, 100 - percentile))\n",
    "    \n",
    "    return tmean(a, limits=limits)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "df = dl.data.Weather.load().resample('M').dropna()\n",
    "x = range(9)\n",
    "temp_means = [truncated_mean(df['TEMP'], i) for i in x]\n",
    "ws_means = [truncated_mean(df['WIND_SPEED'], i) for i in x]"
   ]
  },
  {
   "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",
    "cp = dl.plotting.CyclePlotter(sp.ax)\n",
    "cp.plot(x, temp_means, label='Truncated mean')\n",
    "cp.plot(x, dl.stats.trimean(df['TEMP']) * np.ones_like(x), label='Trimean')\n",
    "cp.plot(x, iqm(df['TEMP']) * np.ones_like(x), label='IQM')\n",
    "sp.label(ylabel_params=dl.data.Weather.get_header('TEMP'))\n",
    "\n",
    "cp = dl.plotting.CyclePlotter(sp.next_ax())\n",
    "cp.plot(x, ws_means, label='Truncated mean')\n",
    "cp.plot(x, dl.stats.trimean(df['WIND_SPEED']) * np.ones_like(x),\n",
    "        label='Trimean')\n",
    "cp.plot(x, iqm(df['WIND_SPEED']) * np.ones_like(x), label='IQM')\n",
    "sp.label(ylabel_params=dl.data.Weather.get_header('WIND_SPEED'))\n",
    "\n",
    "plotdists(df['TEMP'], sp.next_ax())\n",
    "sp.label(xlabel_params=dl.data.Weather.get_header('TEMP'))\n",
    "\n",
    "plotdists(df['WIND_SPEED'], sp.next_ax())\n",
    "sp.label(xlabel_params=dl.data.Weather.get_header('WIND_SPEED'))\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
}
