{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "import dautil as dl\n",
    "from scipy import stats\n",
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "import pandas as pd\n",
    "from IPython.display import HTML"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "lr = dl.nb.LatexRenderer(chapter=3, start=21)\n",
    "lr.render(r'r_{pb} = \\frac{M_1 - M_0}{s_n} \\sqrt{ \\frac{n_1 n_0}{n^2}}')\n",
    "lr.render(r's_n = \\sqrt{\\frac{1}{n} \\sum_{i=1}^n (X_i - \\overline{X})^2}')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "df = dl.data.Weather.load().dropna()\n",
    "df['RAIN'] = df['RAIN'] > 0\n",
    "\n",
    "stats_corr = stats.pointbiserialr(df['RAIN'].values, df['TEMP'].values)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "N = 2 * 365\n",
    "corrs = []\n",
    "\n",
    "for i in range(len(df.index) - N):\n",
    "    x = np.roll(df['RAIN'].values, i)[:N]\n",
    "    y = np.roll(df['TEMP'].values, i)[:N]\n",
    "    corrs.append(stats.pointbiserialr(x, y)[0])\n",
    "\n",
    "corrs = pd.DataFrame(corrs,\n",
    "                     index=df.index[N:],\n",
    "                     columns=['Correlation']).resample('A')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "%matplotlib inline\n",
    "dl.options.mimic_seaborn()\n",
    "context = dl.nb.Context('correlating_pointbiserial')\n",
    "dl.nb.RcWidget(context)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "plt.plot(corrs.index.values, corrs.values)\n",
    "plt.hlines(stats_corr[0], corrs.index.values[0], corrs.index.values[-1],\n",
    "           label='Correlation using the whole data set')\n",
    "plt.title('Rolling Point-biserial Correlation of Rain and Temperature with a 2 Year Window')\n",
    "plt.xlabel('Year')\n",
    "plt.ylabel('Correlation')\n",
    "plt.legend(loc='best')\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
}
