{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "import seaborn as sns\n",
    "import matplotlib.pyplot as plt\n",
    "import dautil as dl\n",
    "from dautil.stats import zscores\n",
    "import statsmodels.api as sm\n",
    "from sklearn.neighbors import KernelDensity\n",
    "import numpy as np\n",
    "from scipy import stats\n",
    "from IPython.html import widgets\n",
    "from IPython.core.display import display\n",
    "from IPython.display import HTML"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "lr = dl.nb.LatexRenderer(chapter=3, start=10)\n",
    "lr.render(r'\\hat{f}_h(x) = \\frac{1}{n}\\sum_{i=1}^n K_h (x - x_i) \\quad = \\frac{1}{nh} \\sum_{i=1}^n K\\Big(\\frac{x-x_i}{h}\\Big)')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def plot(ax, a, b, c, xlabel, ylabel):\n",
    "    dl.plotting.scatter_with_bar(ax, 'Kernel Density', a.values, b.values, c=c, cmap='Blues')\n",
    "    ax.set_xlabel(xlabel)\n",
    "    ax.set_ylabel(ylabel)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "df = dl.data.Weather.load().resample('M').dropna()\n",
    "columns = [str(c) for c in df.columns.values]\n",
    "var1 = widgets.Dropdown(options=columns, selected_label='RAIN')\n",
    "display(var1)\n",
    "var2 = widgets.Dropdown(options=columns, selected_label='TEMP')\n",
    "display(var2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "x = df[var1.value]\n",
    "xlabel = dl.data.Weather.get_header(var1.value)\n",
    "y = df[var2.value]\n",
    "ylabel = dl.data.Weather.get_header(var2.value)\n",
    "X = [x, y]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "%matplotlib inline\n",
    "context = dl.nb.Context('kernel_density_estimation')\n",
    "dl.nb.RcWidget()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "# need to use zscores to avoid errors\n",
    "Z = [zscores(x), zscores(y)]\n",
    "kde = stats.gaussian_kde(Z)\n",
    "\n",
    "_, [[sp_ax, sm_ax], [sk_ax, sns_ax]] = plt.subplots(2, 2)\n",
    "plot(sp_ax, x, y, kde.pdf(Z), xlabel, ylabel)\n",
    "sp_ax.set_title('SciPy')\n",
    "\n",
    "sm_kde = sm.nonparametric.KDEMultivariate(data=X, var_type='cc',\n",
    "                                          bw='normal_reference')\n",
    "sm_ax.set_title('statsmodels')\n",
    "plot(sm_ax, x, y, sm_kde.pdf(X), xlabel, ylabel)\n",
    "\n",
    "XT = np.array(X).T\n",
    "sk_kde = KernelDensity(kernel='gaussian', bandwidth=0.2).fit(XT)\n",
    "sk_ax.set_title('Scikit Learn')\n",
    "plot(sk_ax, x, y, sk_kde.score_samples(XT), xlabel, ylabel)\n",
    "\n",
    "sns_ax.set_title('Seaborn')\n",
    "sns.kdeplot(x, y, ax=sns_ax)\n",
    "sns.rugplot(x, color=\"b\", ax=sns_ax)\n",
    "sns.rugplot(y, vertical=True, ax=sns_ax)\n",
    "sns_ax.set_xlabel(xlabel)\n",
    "sns_ax.set_ylabel(ylabel)\n",
    "\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
}
