{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "# Alphalens: predictive vs. non-predictive factor"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "This is a very simply notebook in which we use Alphalens to analyse a predictive and a non-predictive factor. The goal is to have exemplifying output for both scenarios to make clear what to expect from Alphalens in the case of a predictive and a non-preditive factor. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Populating the interactive namespace from numpy and matplotlib\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/home/lucasca/.local/lib/python3.5/site-packages/statsmodels/compat/pandas.py:56: FutureWarning: The pandas.core.datetools module is deprecated and will be removed in a future version. Please use the pandas.tseries module instead.\n",
      "  from pandas.core import datetools\n"
     ]
    }
   ],
   "source": [
    "%pylab inline --no-import-all\n",
    "import alphalens\n",
    "import pandas as pd\n",
    "import numpy as np\n",
    "import datetime"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": true,
    "deletable": true,
    "editable": true
   },
   "outputs": [],
   "source": [
    "import warnings\n",
    "warnings.filterwarnings('ignore')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "Below is a simple mapping of tickers to sectors for a universe of 500 large cap stocks."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": true,
    "deletable": true,
    "editable": true
   },
   "outputs": [],
   "source": [
    "sector_names = {\n",
    "    0 : \"information_technology\",\n",
    "    1 : \"financials\",\n",
    "    2 : \"health_care\",\n",
    "    3 : \"industrials\",\n",
    "    4 : \"utilities\", \n",
    "    5 : \"real_estate\", \n",
    "    6 : \"materials\", \n",
    "    7 : \"telecommunication_services\", \n",
    "    8 : \"consumer_staples\", \n",
    "    9 : \"consumer_discretionary\", \n",
    "    10 : \"energy\" \n",
    "}\n",
    "\n",
    "ticker_sector = {\n",
    "    \"ACN\" : 0, \"ATVI\" : 0, \"ADBE\" : 0, \"AMD\" : 0, \"AKAM\" : 0, \"ADS\" : 0, \"GOOGL\" : 0, \"GOOG\" : 0, \n",
    "    \"APH\" : 0, \"ADI\" : 0, \"ANSS\" : 0, \"AAPL\" : 0, \"AMAT\" : 0, \"ADSK\" : 0, \"ADP\" : 0, \"AVGO\" : 0,\n",
    "    \"AMG\" : 1, \"AFL\" : 1, \"ALL\" : 1, \"AXP\" : 1, \"AIG\" : 1, \"AMP\" : 1, \"AON\" : 1, \"AJG\" : 1, \"AIZ\" : 1, \"BAC\" : 1,\n",
    "    \"BK\" : 1, \"BBT\" : 1, \"BRK.B\" : 1, \"BLK\" : 1, \"HRB\" : 1, \"BHF\" : 1, \"COF\" : 1, \"CBOE\" : 1, \"SCHW\" : 1, \"CB\" : 1,\n",
    "    \"ABT\" : 2, \"ABBV\" : 2, \"AET\" : 2, \"A\" : 2, \"ALXN\" : 2, \"ALGN\" : 2, \"AGN\" : 2, \"ABC\" : 2, \"AMGN\" : 2, \"ANTM\" : 2,\n",
    "    \"BCR\" : 2, \"BAX\" : 2, \"BDX\" : 2, \"BIIB\" : 2, \"BSX\" : 2, \"BMY\" : 2, \"CAH\" : 2, \"CELG\" : 2, \"CNC\" : 2, \"CERN\" : 2,\n",
    "    \"MMM\" : 3, \"AYI\" : 3, \"ALK\" : 3, \"ALLE\" : 3, \"AAL\" : 3, \"AME\" : 3, \"AOS\" : 3, \"ARNC\" : 3, \"BA\" : 3, \"CHRW\" : 3,\n",
    "    \"CAT\" : 3, \"CTAS\" : 3, \"CSX\" : 3, \"CMI\" : 3, \"DE\" : 3, \"DAL\" : 3, \"DOV\" : 3, \"ETN\" : 3, \"EMR\" : 3, \"EFX\" : 3,\n",
    "    \"AES\" : 4, \"LNT\" : 4, \"AEE\" : 4, \"AEP\" : 4, \"AWK\" : 4, \"CNP\" : 4, \"CMS\" : 4, \"ED\" : 4, \"D\" : 4, \"DTE\" : 4,\n",
    "    \"DUK\" : 4, \"EIX\" : 4, \"ETR\" : 4, \"ES\" : 4, \"EXC\" : 4, \"FE\" : 4, \"NEE\" : 4, \"NI\" : 4, \"NRG\" : 4, \"PCG\" : 4,\n",
    "    \"ARE\" : 5, \"AMT\" : 5, \"AIV\" : 5, \"AVB\" : 5, \"BXP\" : 5, \"CBG\" : 5, \"CCI\" : 5, \"DLR\" : 5, \"DRE\" : 5,\n",
    "    \"EQIX\" : 5, \"EQR\" : 5, \"ESS\" : 5, \"EXR\" : 5, \"FRT\" : 5, \"GGP\" : 5, \"HCP\" : 5, \"HST\" : 5, \"IRM\" : 5, \"KIM\" : 5,\n",
    "    \"APD\" : 6, \"ALB\" : 6, \"AVY\" : 6, \"BLL\" : 6, \"CF\" : 6, \"DWDP\" : 6, \"EMN\" : 6, \"ECL\" : 6, \"FMC\" : 6, \"FCX\" : 6,\n",
    "    \"IP\" : 6, \"IFF\" : 6, \"LYB\" : 6, \"MLM\" : 6, \"MON\" : 6, \"MOS\" : 6, \"NEM\" : 6, \"NUE\" : 6, \"PKG\" : 6, \"PPG\" : 6,\n",
    "    \"T\" : 7, \"CTL\" : 7, \"VZ\" : 7, \n",
    "    \"MO\" : 8, \"ADM\" : 8, \"BF.B\" : 8, \"CPB\" : 8, \"CHD\" : 8, \"CLX\" : 8, \"KO\" : 8, \"CL\" : 8, \"CAG\" : 8,\n",
    "    \"STZ\" : 8, \"COST\" : 8, \"COTY\" : 8, \"CVS\" : 8, \"DPS\" : 8, \"EL\" : 8, \"GIS\" : 8, \"HSY\" : 8, \"HRL\" : 8,\n",
    "    \"AAP\" : 9, \"AMZN\" : 9, \"APTV\" : 9, \"AZO\" : 9, \"BBY\" : 9, \"BWA\" : 9, \"KMX\" : 9, \"CCL\" : 9, \n",
    "    \"APC\" : 10, \"ANDV\" : 10, \"APA\" : 10, \"BHGE\" : 10, \"COG\" : 10, \"CHK\" : 10, \"CVX\" : 10, \"XEC\" : 10, \"CXO\" : 10,\n",
    "    \"COP\" : 10, \"DVN\" : 10, \"EOG\" : 10, \"EQT\" : 10, \"XOM\" : 10, \"HAL\" : 10, \"HP\" : 10, \"HES\" : 10, \"KMI\" : 10\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [],
   "source": [
    "import pandas_datareader.data as web\n",
    "\n",
    "tickers = list(ticker_sector.keys())\n",
    "pan = web.DataReader(tickers, \"google\", datetime.datetime(2017, 1, 1),  datetime.datetime(2017, 6, 1))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "The pricing data passed to alphalens should contain the entry price for the assets so it must reflect the next available price after a factor value was observed at a given timestamp. Those prices must not be used in the calculation of the factor values for that time. Always double check to ensure you are not introducing lookahead bias to your study.\n",
    "\n",
    "The pricing data must also contain the exit price for the assets, for period 1 the price at the next timestamp will be used, for period 2 the price after 2 timestamps will be used and so on.\n",
    "\n",
    "There are no restrinctions/assumptions on the time frequencies a factor should be computed at and neither on the specific time a factor should be traded (trading at the open vs trading at the close vs intraday trading), it is only required that factor and price DataFrames are properly aligned given the rules above.\n",
    "\n",
    "In our example, before the trading starts every day, we observe yesterday factor values. The price we pass to alphalens is the next available price after that factor observation: the daily open price that will be used as assets entry price. Also, we are not adding additional prices so the assets exit price will be the following days open prices (how many days depends on 'periods' argument). The retuns computed by Alphalens will therefore based on  assets open prices."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [],
   "source": [
    "pricing = pan['Open']"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "We'll use a naive factor as non-predictive factor, we'll rank stocks based on recent performance: stocks that performed well in the last 10 days will have high rank and vice versa."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [],
   "source": [
    "non_predictive_factor = pricing.pct_change(10)\n",
    "\n",
    "non_predictive_factor = non_predictive_factor.stack()\n",
    "non_predictive_factor.index = non_predictive_factor.index.set_names(['date', 'asset'])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "To create a predictive factor we'll cheat: we will look at future prices to make sure we'll rank high stoks that will perform well and vice versa."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [],
   "source": [
    "lookahead_bias_days = 5\n",
    "\n",
    "predictive_factor = pricing.pct_change(lookahead_bias_days)\n",
    "# introduce look-ahead bias and make the factor predictive\n",
    "predictive_factor = predictive_factor.shift(-lookahead_bias_days)\n",
    "\n",
    "predictive_factor = predictive_factor.stack()\n",
    "predictive_factor.index = predictive_factor.index.set_names(['date', 'asset'])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "# Run Alphalens on the non-predictive factor"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Dropped 14.3% entries from factor data: 14.3% in forward returns computation and 0.0% in binning phase (set max_loss=0 to see potentially suppressed Exceptions).\n",
      "max_loss is 35.0%, not exceeded: OK!\n"
     ]
    }
   ],
   "source": [
    "non_predictive_factor_data = alphalens.utils.get_clean_factor_and_forward_returns(non_predictive_factor, \n",
    "                                                                                  pricing, \n",
    "                                                                                  quantiles=5,\n",
    "                                                                                  bins=None,\n",
    "                                                                                  groupby=ticker_sector,\n",
    "                                                                                  groupby_labels=sector_names)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true,
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Quantiles Statistics\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>min</th>\n",
       "      <th>max</th>\n",
       "      <th>mean</th>\n",
       "      <th>std</th>\n",
       "      <th>count</th>\n",
       "      <th>count %</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>factor_quantile</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>-0.241692</td>\n",
       "      <td>0.012966</td>\n",
       "      <td>-0.047524</td>\n",
       "      <td>0.030175</td>\n",
       "      <td>2624</td>\n",
       "      <td>20.125786</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>-0.041287</td>\n",
       "      <td>0.023685</td>\n",
       "      <td>-0.009477</td>\n",
       "      <td>0.010986</td>\n",
       "      <td>2624</td>\n",
       "      <td>20.125786</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>-0.016844</td>\n",
       "      <td>0.038118</td>\n",
       "      <td>0.005255</td>\n",
       "      <td>0.009888</td>\n",
       "      <td>2542</td>\n",
       "      <td>19.496855</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>-0.001787</td>\n",
       "      <td>0.056039</td>\n",
       "      <td>0.019915</td>\n",
       "      <td>0.011570</td>\n",
       "      <td>2624</td>\n",
       "      <td>20.125786</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>0.008996</td>\n",
       "      <td>0.412060</td>\n",
       "      <td>0.055218</td>\n",
       "      <td>0.037834</td>\n",
       "      <td>2624</td>\n",
       "      <td>20.125786</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                      min       max      mean       std  count    count %\n",
       "factor_quantile                                                          \n",
       "1               -0.241692  0.012966 -0.047524  0.030175   2624  20.125786\n",
       "2               -0.041287  0.023685 -0.009477  0.010986   2624  20.125786\n",
       "3               -0.016844  0.038118  0.005255  0.009888   2542  19.496855\n",
       "4               -0.001787  0.056039  0.019915  0.011570   2624  20.125786\n",
       "5                0.008996  0.412060  0.055218  0.037834   2624  20.125786"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Returns Analysis\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>1D</th>\n",
       "      <th>5D</th>\n",
       "      <th>10D</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>Ann. alpha</th>\n",
       "      <td>0.004</td>\n",
       "      <td>0.088</td>\n",
       "      <td>0.066</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>beta</th>\n",
       "      <td>-0.150</td>\n",
       "      <td>-0.164</td>\n",
       "      <td>-0.030</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Mean Period Wise Return Top Quantile (bps)</th>\n",
       "      <td>-4.055</td>\n",
       "      <td>0.259</td>\n",
       "      <td>-0.537</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Mean Period Wise Return Bottom Quantile (bps)</th>\n",
       "      <td>-4.470</td>\n",
       "      <td>-5.693</td>\n",
       "      <td>-6.322</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Mean Period Wise Spread (bps)</th>\n",
       "      <td>0.414</td>\n",
       "      <td>5.973</td>\n",
       "      <td>5.791</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                                                  1D     5D    10D\n",
       "Ann. alpha                                     0.004  0.088  0.066\n",
       "beta                                          -0.150 -0.164 -0.030\n",
       "Mean Period Wise Return Top Quantile (bps)    -4.055  0.259 -0.537\n",
       "Mean Period Wise Return Bottom Quantile (bps) -4.470 -5.693 -6.322\n",
       "Mean Period Wise Spread (bps)                  0.414  5.973  5.791"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "<matplotlib.figure.Figure at 0x7f1d3149eb70>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "[encoded data, 837180 characters]\n",
      "text/plain": [
       "<matplotlib.figure.Figure at 0x7f1d304bcb00>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "[encoded data, 135200 characters]\n",
      "text/plain": [
       "<matplotlib.figure.Figure at 0x7f1d2e0a6160>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Information Analysis\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>1D</th>\n",
       "      <th>5D</th>\n",
       "      <th>10D</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>IC Mean</th>\n",
       "      <td>-0.013</td>\n",
       "      <td>0.022</td>\n",
       "      <td>0.043</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>IC Std.</th>\n",
       "      <td>0.169</td>\n",
       "      <td>0.198</td>\n",
       "      <td>0.173</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Risk-Adjusted IC</th>\n",
       "      <td>-0.075</td>\n",
       "      <td>0.114</td>\n",
       "      <td>0.248</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>t-stat(IC)</th>\n",
       "      <td>-0.676</td>\n",
       "      <td>1.029</td>\n",
       "      <td>2.245</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>p-value(IC)</th>\n",
       "      <td>0.501</td>\n",
       "      <td>0.307</td>\n",
       "      <td>0.027</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>IC Skew</th>\n",
       "      <td>-0.308</td>\n",
       "      <td>-0.406</td>\n",
       "      <td>-0.615</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>IC Kurtosis</th>\n",
       "      <td>0.430</td>\n",
       "      <td>-0.330</td>\n",
       "      <td>-0.614</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                     1D     5D    10D\n",
       "IC Mean          -0.013  0.022  0.043\n",
       "IC Std.           0.169  0.198  0.173\n",
       "Risk-Adjusted IC -0.075  0.114  0.248\n",
       "t-stat(IC)       -0.676  1.029  2.245\n",
       "p-value(IC)       0.501  0.307  0.027\n",
       "IC Skew          -0.308 -0.406 -0.615\n",
       "IC Kurtosis       0.430 -0.330 -0.614"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "[encoded data, 444564 characters]\n",
      "text/plain": [
       "<matplotlib.figure.Figure at 0x7f1d304bc278>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Turnover Analysis\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>10D</th>\n",
       "      <th>1D</th>\n",
       "      <th>5D</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>Quantile 1 Mean Turnover</th>\n",
       "      <td>0.717</td>\n",
       "      <td>0.236</td>\n",
       "      <td>0.504</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Quantile 2 Mean Turnover</th>\n",
       "      <td>0.804</td>\n",
       "      <td>0.504</td>\n",
       "      <td>0.741</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Quantile 3 Mean Turnover</th>\n",
       "      <td>0.793</td>\n",
       "      <td>0.567</td>\n",
       "      <td>0.757</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Quantile 4 Mean Turnover</th>\n",
       "      <td>0.774</td>\n",
       "      <td>0.505</td>\n",
       "      <td>0.731</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Quantile 5 Mean Turnover</th>\n",
       "      <td>0.800</td>\n",
       "      <td>0.255</td>\n",
       "      <td>0.582</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                             10D     1D     5D\n",
       "Quantile 1 Mean Turnover   0.717  0.236  0.504\n",
       "Quantile 2 Mean Turnover   0.804  0.504  0.741\n",
       "Quantile 3 Mean Turnover   0.793  0.567  0.757\n",
       "Quantile 4 Mean Turnover   0.774  0.505  0.731\n",
       "Quantile 5 Mean Turnover   0.800  0.255  0.582"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>1D</th>\n",
       "      <th>5D</th>\n",
       "      <th>10D</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>Mean Factor Rank Autocorrelation</th>\n",
       "      <td>0.855</td>\n",
       "      <td>0.441</td>\n",
       "      <td>0.045</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                                     1D     5D    10D\n",
       "Mean Factor Rank Autocorrelation  0.855  0.441  0.045"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "[encoded data, 452084 characters]\n",
      "text/plain": [
       "<matplotlib.figure.Figure at 0x7f1d2cca74a8>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "alphalens.tears.create_full_tear_sheet(non_predictive_factor_data, by_group=True)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "# Run Alphalens on the predictive factor"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Dropped 8.7% entries from factor data: 8.7% in forward returns computation and 0.0% in binning phase (set max_loss=0 to see potentially suppressed Exceptions).\n",
      "max_loss is 35.0%, not exceeded: OK!\n"
     ]
    }
   ],
   "source": [
    "predictive_factor_data = alphalens.utils.get_clean_factor_and_forward_returns(predictive_factor, \n",
    "                                                                              pricing, \n",
    "                                                                              quantiles=5,\n",
    "                                                                              bins=None,\n",
    "                                                                              groupby=ticker_sector,\n",
    "                                                                              groupby_labels=sector_names)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true,
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Quantiles Statistics\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>min</th>\n",
       "      <th>max</th>\n",
       "      <th>mean</th>\n",
       "      <th>std</th>\n",
       "      <th>count</th>\n",
       "      <th>count %</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>factor_quantile</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>-0.262200</td>\n",
       "      <td>0.009253</td>\n",
       "      <td>-0.033353</td>\n",
       "      <td>0.024418</td>\n",
       "      <td>2912</td>\n",
       "      <td>20.125786</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>-0.037797</td>\n",
       "      <td>0.019766</td>\n",
       "      <td>-0.007552</td>\n",
       "      <td>0.009132</td>\n",
       "      <td>2912</td>\n",
       "      <td>20.125786</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>-0.023201</td>\n",
       "      <td>0.026628</td>\n",
       "      <td>0.002891</td>\n",
       "      <td>0.008283</td>\n",
       "      <td>2821</td>\n",
       "      <td>19.496855</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>-0.011861</td>\n",
       "      <td>0.038359</td>\n",
       "      <td>0.013056</td>\n",
       "      <td>0.009104</td>\n",
       "      <td>2912</td>\n",
       "      <td>20.125786</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>-0.002438</td>\n",
       "      <td>0.325472</td>\n",
       "      <td>0.037946</td>\n",
       "      <td>0.026974</td>\n",
       "      <td>2912</td>\n",
       "      <td>20.125786</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                      min       max      mean       std  count    count %\n",
       "factor_quantile                                                          \n",
       "1               -0.262200  0.009253 -0.033353  0.024418   2912  20.125786\n",
       "2               -0.037797  0.019766 -0.007552  0.009132   2912  20.125786\n",
       "3               -0.023201  0.026628  0.002891  0.008283   2821  19.496855\n",
       "4               -0.011861  0.038359  0.013056  0.009104   2912  20.125786\n",
       "5               -0.002438  0.325472  0.037946  0.026974   2912  20.125786"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Returns Analysis\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>1D</th>\n",
       "      <th>5D</th>\n",
       "      <th>10D</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>Ann. alpha</th>\n",
       "      <td>7.037</td>\n",
       "      <td>6.159</td>\n",
       "      <td>1.665</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>beta</th>\n",
       "      <td>-0.048</td>\n",
       "      <td>0.148</td>\n",
       "      <td>0.253</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Mean Period Wise Return Top Quantile (bps)</th>\n",
       "      <td>73.013</td>\n",
       "      <td>69.721</td>\n",
       "      <td>34.687</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Mean Period Wise Return Bottom Quantile (bps)</th>\n",
       "      <td>-75.716</td>\n",
       "      <td>-72.953</td>\n",
       "      <td>-38.435</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Mean Period Wise Spread (bps)</th>\n",
       "      <td>148.728</td>\n",
       "      <td>142.680</td>\n",
       "      <td>73.129</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                                                    1D       5D     10D\n",
       "Ann. alpha                                       7.037    6.159   1.665\n",
       "beta                                            -0.048    0.148   0.253\n",
       "Mean Period Wise Return Top Quantile (bps)      73.013   69.721  34.687\n",
       "Mean Period Wise Return Bottom Quantile (bps)  -75.716  -72.953 -38.435\n",
       "Mean Period Wise Spread (bps)                  148.728  142.680  73.129"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "<matplotlib.figure.Figure at 0x7f1d682fca90>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "[encoded data, 598608 characters]\n",
      "text/plain": [
       "<matplotlib.figure.Figure at 0x7f1d30190278>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "[encoded data, 138344 characters]\n",
      "text/plain": [
       "<matplotlib.figure.Figure at 0x7f1d2cc75518>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Information Analysis\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>1D</th>\n",
       "      <th>5D</th>\n",
       "      <th>10D</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>IC Mean</th>\n",
       "      <td>0.416</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>0.672</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>IC Std.</th>\n",
       "      <td>0.147</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.104</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Risk-Adjusted IC</th>\n",
       "      <td>2.824</td>\n",
       "      <td>inf</td>\n",
       "      <td>6.446</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>t-stat(IC)</th>\n",
       "      <td>26.938</td>\n",
       "      <td>inf</td>\n",
       "      <td>61.487</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>p-value(IC)</th>\n",
       "      <td>0.000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>IC Skew</th>\n",
       "      <td>-1.190</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>-1.326</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>IC Kurtosis</th>\n",
       "      <td>3.134</td>\n",
       "      <td>-3.000000</td>\n",
       "      <td>1.711</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                      1D        5D     10D\n",
       "IC Mean            0.416  1.000000   0.672\n",
       "IC Std.            0.147  0.000000   0.104\n",
       "Risk-Adjusted IC   2.824       inf   6.446\n",
       "t-stat(IC)        26.938       inf  61.487\n",
       "p-value(IC)        0.000  0.000000   0.000\n",
       "IC Skew           -1.190  0.000000  -1.326\n",
       "IC Kurtosis        3.134 -3.000000   1.711"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "[encoded data, 387180 characters]\n",
      "text/plain": [
       "<matplotlib.figure.Figure at 0x7f1d2dfb65f8>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Turnover Analysis\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>10D</th>\n",
       "      <th>1D</th>\n",
       "      <th>5D</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>Quantile 1 Mean Turnover</th>\n",
       "      <td>0.736</td>\n",
       "      <td>0.342</td>\n",
       "      <td>0.737</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Quantile 2 Mean Turnover</th>\n",
       "      <td>0.799</td>\n",
       "      <td>0.610</td>\n",
       "      <td>0.803</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Quantile 3 Mean Turnover</th>\n",
       "      <td>0.785</td>\n",
       "      <td>0.671</td>\n",
       "      <td>0.774</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Quantile 4 Mean Turnover</th>\n",
       "      <td>0.797</td>\n",
       "      <td>0.615</td>\n",
       "      <td>0.792</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Quantile 5 Mean Turnover</th>\n",
       "      <td>0.800</td>\n",
       "      <td>0.361</td>\n",
       "      <td>0.785</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                             10D     1D     5D\n",
       "Quantile 1 Mean Turnover   0.736  0.342  0.737\n",
       "Quantile 2 Mean Turnover   0.799  0.610  0.803\n",
       "Quantile 3 Mean Turnover   0.785  0.671  0.774\n",
       "Quantile 4 Mean Turnover   0.797  0.615  0.792\n",
       "Quantile 5 Mean Turnover   0.800  0.361  0.785"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>1D</th>\n",
       "      <th>5D</th>\n",
       "      <th>10D</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>Mean Factor Rank Autocorrelation</th>\n",
       "      <td>0.736</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.032</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                                     1D   5D    10D\n",
       "Mean Factor Rank Autocorrelation  0.736  0.0  0.032"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "[encoded data, 486208 characters]\n",
      "text/plain": [
       "<matplotlib.figure.Figure at 0x7f1d2e07e390>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "alphalens.tears.create_full_tear_sheet(predictive_factor_data, by_group=True)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "# Run Event Style Returns Analysis"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "## Non-predictive factor"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true,
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<matplotlib.figure.Figure at 0x7f1d2df15e80>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "[encoded data, 166436 characters]\n",
      "text/plain": [
       "<matplotlib.figure.Figure at 0x7f1d2dfe4198>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "alphalens.tears.create_event_returns_tear_sheet(non_predictive_factor_data, pricing)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "## Predictive factor"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true,
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<matplotlib.figure.Figure at 0x7f1d2cd9dc88>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "[encoded data, 171048 characters]\n",
      "text/plain": [
       "<matplotlib.figure.Figure at 0x7f1d2cd807f0>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "alphalens.tears.create_event_returns_tear_sheet(predictive_factor_data, pricing)"
   ]
  }
 ],
 "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.5.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
