{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import dautil as dl\n",
    "import numpy as np\n",
    "import pandas as pd\n",
    "import ch7util\n",
    "import matplotlib.pyplot as plt\n",
    "from scipy.stats import norm\n",
    "from IPython.display import HTML"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "context = dl.nb.Context('non_parametric')\n",
    "lr = dl.nb.LatexRenderer(chapter=7, start=6, context=context)\n",
    "lr.render(r'=\\mu=\\frac{2\\ N_+\\ N_-}{N} + 1')\n",
    "lr.render(r'\\sigma^2=\\frac{2\\ N_+\\ N_-\\ (2\\ N_+\\ N_--N)}{N^2\\ (N-1)}=\\frac{(\\mu-1)(\\mu-2)}{N-1}')\n",
    "lr.render(r'Z = \\frac{R - \\mu}{\\sigma}')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def count_runs(signs):\n",
    "    nruns = 0\n",
    "    prev = None\n",
    "\n",
    "    for s in signs:\n",
    "        if s != 0 and s != prev:\n",
    "            nruns += 1\n",
    "\n",
    "        prev = s\n",
    "\n",
    "    return nruns"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def proc_runs(symbol):\n",
    "    ohlc = dl.data.OHLC()\n",
    "    close = ohlc.get(symbol)['Adj Close'].values\n",
    "    diffs = np.diff(close)\n",
    "    nplus = (diffs > 0).sum()\n",
    "    nmin = (diffs < 0).sum()\n",
    "    n = nplus + nmin\n",
    "    mean = (2 * (nplus * nmin) / n) + 1\n",
    "    var = (mean - 1) * (mean - 2) / (n - 1)\n",
    "    std = np.sqrt(var)\n",
    "    signs = np.sign(diffs)\n",
    "    nruns = count_runs(np.diff(signs))\n",
    "\n",
    "    return mean, std, (nruns - mean) / std"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "means = []\n",
    "stds = []\n",
    "zscores = []\n",
    "\n",
    "for symbol in ch7util.STOCKS:\n",
    "    mean, std, zscore = proc_runs(symbol)\n",
    "    means.append(mean)\n",
    "    stds.append(std)\n",
    "    zscores.append(zscore)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "%matplotlib inline\n",
    "dl.options.mimic_seaborn()\n",
    "dl.nb.RcWidget(context)\n",
    "dl.nb.LabelWidget(2, 1, context)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "sp = dl.plotting.Subplotter(2, 1, context)\n",
    "dl.plotting.plot_text(sp.ax, means, stds, ch7util.STOCKS, add_scatter=True)\n",
    "sp.label()\n",
    "\n",
    "dl.plotting.bar(sp.next_ax(), ch7util.STOCKS, zscores)\n",
    "sp.ax.axhline(norm.ppf(0.95), label='95 % confidence level')\n",
    "sp.label()\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
}
