Skip to content

Commit

Permalink
added qe alphalens tut notebooks
Browse files Browse the repository at this point in the history
  • Loading branch information
quantopiancal committed Nov 20, 2018
1 parent 2aaf37c commit 6619305
Show file tree
Hide file tree
Showing 5 changed files with 995 additions and 74 deletions.
148 changes: 74 additions & 74 deletions notebooks/tutorials/3_alphalens_lesson_4/notebook.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,75 @@
")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Analyzing Alpha Factors By Group\n",
"\n",
"Alphalens allows you to group assets using a classifier. A common use case for this is creating a classifier that specifies which sector each equity belongs to, then comparing your alpha factor's returns among sectors.\n",
"\n",
"You can group assets by any classifier, but sector is most common. The Pipeline in the first cell of this lesson returns a column named `sector`, whose values represent the corresponding Morningstar sector code. All we have to do now is pass that column to the `groupby` argument of `get_clean_factor_and_forward_returns()`\n",
"\n",
"**Run the following cell, and notice the charts at the bottom of the tear sheet showing how our factor performs in different sectors.**"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from alphalens.tears import create_returns_tear_sheet\n",
"\n",
"sector_labels, sector_labels[-1] = dict(Sector.SECTOR_NAMES), \"Unknown\"\n",
"\n",
"factor_data = get_clean_factor_and_forward_returns(\n",
" factor=pipeline_output['factor_to_analyze'],\n",
" prices=pricing_data,\n",
" groupby=pipeline_output['sector'],\n",
" groupby_labels=sector_labels,\n",
")\n",
"\n",
"create_returns_tear_sheet(factor_data=factor_data, by_group=True)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Writing Group Neutral Strategies\n",
"\n",
"Not only does Alphalens allow us to simulate how our alpha factor would perform in a long/short trading strategy, it also allows us to simulate how it would do if we went long/short on every group! \n",
"\n",
"Grouping by sector, and going long/short on each sector allows you to limit exposure to the overall movement of sectors. For example, you may have noticed in step three of this tutorial, that certain sectors had all positive returns, or all negative returns. That information isn't useful to us, because that just means the sector group outperformed (or underperformed) the market; it doesn't give us any insight into how our factor performs within that sector.\n",
"\n",
"Since we grouped our assets by sector in the previous cell, going group neutral is easy; just make the two following changes:\n",
"- Pass `binning_by_group=True` as an argument to `get_clean_factor_and_forward_returns()`.\n",
"- Pass `group_neutral=True` as an argument to `create_full_tear_sheet()`.\n",
"\n",
"**The following cell has made the approriate changes. Try running it and notice how the results differ from the previous cell.**"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"scrolled": false
},
"outputs": [],
"source": [
"factor_data = get_clean_factor_and_forward_returns(\n",
" pipeline_output['factor_to_analyze'],\n",
" prices=pricing_data,\n",
" groupby=pipeline_output['sector'],\n",
" groupby_labels=sector_labels,\n",
" binning_by_group=True,\n",
")\n",
"\n",
"create_returns_tear_sheet(factor_data, by_group=True, group_neutral=True)"
]
},
{
"cell_type": "markdown",
"metadata": {},
Expand Down Expand Up @@ -169,75 +238,6 @@
"*Note: MaxLossExceededError has two possible causes; forward returns computation and binning. We showed you how to fix forward returns computation here because it is much more common. Try passing `quantiles=None` and `bins=5` if you get MaxLossExceededError because of binning.*"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Analyzing Alpha Factors By Group\n",
"\n",
"Alphalens allows you to group assets using a classifier. A common use case for this is creating a classifier that specifies which sector each equity belongs to, then comparing your alpha factor's returns among sectors.\n",
"\n",
"You can group assets by any classifier, but sector is most common. The Pipeline in the first cell of this lesson returns a column named `sector`, whose values represent the corresponding Morningstar sector code. All we have to do now is pass that column to the `groupby` argument of `get_clean_factor_and_forward_returns()`\n",
"\n",
"**Run the following cell, and notice the charts at the bottom of the tear sheet showing how our factor performs in different sectors.**"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from alphalens.tears import create_returns_tear_sheet\n",
"\n",
"sector_labels, sector_labels[-1] = dict(Sector.SECTOR_NAMES), \"Unknown\"\n",
"\n",
"factor_data = get_clean_factor_and_forward_returns(\n",
" factor=pipeline_output['factor_to_analyze'],\n",
" prices=pricing_data,\n",
" groupby=pipeline_output['sector'],\n",
" groupby_labels=sector_labels,\n",
")\n",
"\n",
"create_returns_tear_sheet(factor_data=factor_data, by_group=True)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Writing Group Neutral Strategies\n",
"\n",
"Not only does Alphalens allow us to simulate how our alpha factor would perform in a long/short trading strategy, it also allows us to simulate how it would do if we went long/short on every group! \n",
"\n",
"Grouping by sector, and going long/short on each sector allows you to limit exposure to the overall movement of sectors. For example, you may have noticed in step three of this tutorial, that certain sectors had all positive returns, or all negative returns. That information isn't useful to us, because that just means the sector group outperformed (or underperformed) the market; it doesn't give us any insight into how our factor performs within that sector.\n",
"\n",
"Since we grouped our assets by sector in the previous cell, going group neutral is easy; just make the two following changes:\n",
"- Pass `binning_by_group=True` as an argument to `get_clean_factor_and_forward_returns()`.\n",
"- Pass `group_neutral=True` as an argument to `create_full_tear_sheet()`.\n",
"\n",
"**The following cell has made the approriate changes. Try running it and notice how the results differ from the previous cell.**"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"scrolled": false
},
"outputs": [],
"source": [
"factor_data = get_clean_factor_and_forward_returns(\n",
" pipeline_output['factor_to_analyze'],\n",
" prices=pricing_data,\n",
" groupby=pipeline_output['sector'],\n",
" groupby_labels=sector_labels,\n",
" binning_by_group=True,\n",
")\n",
"\n",
"create_returns_tear_sheet(factor_data, by_group=True, group_neutral=True)"
]
},
{
"cell_type": "markdown",
"metadata": {},
Expand All @@ -255,21 +255,21 @@
],
"metadata": {
"kernelspec": {
"display_name": "Python 2",
"display_name": "Python 3",
"language": "python",
"name": "python2"
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 2
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython2",
"version": "2.7.12"
"pygments_lexer": "ipython3",
"version": "3.7.0"
}
},
"nbformat": 4,
Expand Down
189 changes: 189 additions & 0 deletions notebooks/tutorials/3_factset_alphalens_lesson_2/notebook.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,189 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Companion notebook for Alphalens tutorial lesson 2\n",
"\n",
"# Creating Tear Sheets With Alphalens\n",
"\n",
"In the previous lesson, you learned what Alphalens is. In this lesson, you will learn a four step process for how to use it:\n",
"\n",
"1. Express an alpha factor and define a trading universe by creating and running a Pipeline over a certain time period.\n",
"2. Query pricing data for the assets in our universe during that same time period with `get_pricing()`.\n",
"3. Align the alpha factor data with the pricing data with `get_clean_factor_and_forward_returns()`.\n",
"4. Visualize how well our alpha factor predicts future price movements with `create_full_tear_sheet()`."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Build And Run A Pipeline\n",
"The following code creates a trading universe and expresses an alpha factor within a pipeline, then runs it with `run_pipeline()`"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from quantopian.pipeline import Pipeline\n",
"from quantopian.research import run_pipeline\n",
"from quantopian.pipeline.factors import AverageDollarVolume\n",
"from quantopian.pipeline.data import factset, USEquityPricing\n",
"\n",
"def make_pipeline():\n",
" # Filter out equities with low market capitalization\n",
" market_cap_filter = factset.Fundamentals.mkt_val.latest > 500000000\n",
"\n",
" # Filter out equities with low volume\n",
" volume_filter = AverageDollarVolume(window_length=200) > 2500000\n",
"\n",
" # Filter out equities with a close price below $5\n",
" price_filter = USEquityPricing.close.latest > 5\n",
"\n",
" # Our final base universe\n",
" base_universe = market_cap_filter & volume_filter & price_filter\n",
"\n",
" # Measures a company's asset growth rate.\n",
" asset_growth = factset.Fundamentals.assets_gr_qf.latest\n",
"\n",
" return Pipeline(\n",
" columns={'asset_growth': asset_growth},\n",
" screen=base_universe & asset_growth.notnull()\n",
" )\n",
"\n",
"pipeline_output = run_pipeline(pipeline=make_pipeline(), start_date='2014-1-1', end_date='2016-1-1')\n",
"\n",
"# Show the first 5 rows of factor data\n",
"pipeline_output.head(5)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Query Pricing Data\n",
"\n",
"Now that we have factor data, let's get pricing data for the same time period. `get_pricing()` returns pricing data for a list of assets over a specified time period. It requires four arguments:\n",
"- A list of assets for which we want pricing.\n",
"- A start date\n",
"- An end date\n",
"- Whether to use open, high, low or close pricing.\n",
"\n",
"Execute the following cell to get pricing data."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"pricing_data = get_pricing(\n",
" symbols=pipeline_output.index.levels[1], # Finds all assets that appear at least once in \"pipeline_output\" \n",
" start_date='2014-1-1',\n",
" end_date='2016-2-1', # must be after run_pipeline()'s end date. Explained more in lesson 4\n",
" fields='open_price' # Generally, you should use open pricing. Explained more in lesson 4\n",
")\n",
"\n",
"# Show the first 5 rows of pricing_data\n",
"pricing_data.head(5)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Align Data\n",
"\n",
"`get_clean_factor_and_forward_returns()` aligns the factor data created by `run_pipeline()` with the pricing data created by `get_pricing()`, and returns an object suitable for analysis with Alphalens' charting functions. It requires two arguments:\n",
"- The factor data we created with `run_pipeline()`.\n",
"- The pricing data we created with `get_pricing()`.\n",
"\n",
"Execute the following cell to align the factor data with the pricing data."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"scrolled": false
},
"outputs": [],
"source": [
"from alphalens.utils import get_clean_factor_and_forward_returns\n",
"\n",
"factor_data = get_clean_factor_and_forward_returns(\n",
" factor=pipeline_output, \n",
" prices=pricing_data\n",
")\n",
"\n",
"# Show the first 5 rows of merged_data\n",
"factor_data.head(5)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Visualize Results\n",
"\n",
"Finally, execute the following cell to pass the output of `get_clean_factor_and_forward_returns()` to a function called `create_full_tear_sheet()`. This will create whats known as a tear sheet."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"scrolled": false
},
"outputs": [],
"source": [
"from alphalens.tears import create_full_tear_sheet\n",
"\n",
"create_full_tear_sheet(factor_data)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## That's It!\n",
"\n",
"In the next lesson, we will show you how to interpret the charts produced by `create_full_tear_sheet()`."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 2",
"language": "python",
"name": "python2"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 2
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython2",
"version": "2.7.12"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Loading

0 comments on commit 6619305

Please sign in to comment.