{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import ch7util\n",
    "import dautil as dl\n",
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "from IPython.display import HTML"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def hl2(df, suffix):\n",
    "    high = df['High_' + suffix]\n",
    "    low = df['Low_' + suffix]\n",
    "\n",
    "    return (high - low) ** 2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def correlate(stock, sp500):\n",
    "    merged = ch7util.merge_sp500(stock, sp500)\n",
    "    rets = ch7util.log_rets(merged['Adj Close_stock'])\n",
    "    sp500_rets = ch7util.log_rets(merged['Adj Close_sp500'])\n",
    "    result = {}\n",
    "\n",
    "    result['corrcoef'] = np.corrcoef(rets, sp500_rets)[0][1]\n",
    "    slope, _ = np.polyfit(sp500_rets, rets, 1)\n",
    "    result['slope'] = slope\n",
    "\n",
    "    srd = (sp500_rets - rets) ** 2\n",
    "    result['msrd'] = srd.mean()\n",
    "    result['std_srd'] = srd.std()\n",
    "\n",
    "    result['vols'] = np.corrcoef(merged['Volume_stock'],\n",
    "                                 merged['Volume_sp500'])[0][1]\n",
    "\n",
    "    result['hl2'] = np.corrcoef(hl2(merged, 'stock'),\n",
    "                                hl2(merged, 'sp500'))[0][1]\n",
    "\n",
    "    return result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "ohlc = dl.data.OHLC()\n",
    "dfs = [ohlc.get(stock) for stock in ch7util.STOCKS]\n",
    "sp500 = ohlc.get('^GSPC')\n",
    "corrs = [correlate(df, sp500) for df in dfs]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "%matplotlib inline\n",
    "dl.options.mimic_seaborn()\n",
    "context = dl.nb.Context('correlating_market')\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",
    "dl.plotting.bar(sp.ax, ch7util.STOCKS,\n",
    "                [corr['corrcoef'] for corr in corrs])\n",
    "sp.label()\n",
    "\n",
    "dl.plotting.bar(sp.next_ax(), ch7util.STOCKS,\n",
    "                [corr['slope'] for corr in corrs])\n",
    "sp.label()\n",
    "\n",
    "sp.next_ax().set_xlim([0, 0.001])\n",
    "dl.plotting.plot_text(sp.ax, [corr['msrd'] for corr in corrs],\n",
    "                      [corr['std_srd'] for corr in corrs],\n",
    "                      ch7util.STOCKS, add_scatter=True,\n",
    "                      fontsize=9, alpha=0.6)\n",
    "sp.label()\n",
    "\n",
    "dl.plotting.plot_text(sp.next_ax(), [corr['vols'] for corr in corrs],\n",
    "                      [corr['hl2'] for corr in corrs],\n",
    "                      ch7util.STOCKS, add_scatter=True,\n",
    "                      fontsize=9, alpha=0.6)\n",
    "sp.label()\n",
    "\n",
    "\n",
    "HTML(sp.exit())"
   ]
  }
 ],
 "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
}
