|
70 | 70 | ")"
|
71 | 71 | ]
|
72 | 72 | },
|
| 73 | + { |
| 74 | + "cell_type": "markdown", |
| 75 | + "metadata": {}, |
| 76 | + "source": [ |
| 77 | + "## Analyzing Alpha Factors By Group\n", |
| 78 | + "\n", |
| 79 | + "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", |
| 80 | + "\n", |
| 81 | + "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", |
| 82 | + "\n", |
| 83 | + "**Run the following cell, and notice the charts at the bottom of the tear sheet showing how our factor performs in different sectors.**" |
| 84 | + ] |
| 85 | + }, |
| 86 | + { |
| 87 | + "cell_type": "code", |
| 88 | + "execution_count": null, |
| 89 | + "metadata": {}, |
| 90 | + "outputs": [], |
| 91 | + "source": [ |
| 92 | + "from alphalens.tears import create_returns_tear_sheet\n", |
| 93 | + "\n", |
| 94 | + "sector_labels, sector_labels[-1] = dict(Sector.SECTOR_NAMES), \"Unknown\"\n", |
| 95 | + "\n", |
| 96 | + "factor_data = get_clean_factor_and_forward_returns(\n", |
| 97 | + " factor=pipeline_output['factor_to_analyze'],\n", |
| 98 | + " prices=pricing_data,\n", |
| 99 | + " groupby=pipeline_output['sector'],\n", |
| 100 | + " groupby_labels=sector_labels,\n", |
| 101 | + ")\n", |
| 102 | + "\n", |
| 103 | + "create_returns_tear_sheet(factor_data=factor_data, by_group=True)" |
| 104 | + ] |
| 105 | + }, |
| 106 | + { |
| 107 | + "cell_type": "markdown", |
| 108 | + "metadata": {}, |
| 109 | + "source": [ |
| 110 | + "## Writing Group Neutral Strategies\n", |
| 111 | + "\n", |
| 112 | + "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", |
| 113 | + "\n", |
| 114 | + "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", |
| 115 | + "\n", |
| 116 | + "Since we grouped our assets by sector in the previous cell, going group neutral is easy; just make the two following changes:\n", |
| 117 | + "- Pass `binning_by_group=True` as an argument to `get_clean_factor_and_forward_returns()`.\n", |
| 118 | + "- Pass `group_neutral=True` as an argument to `create_full_tear_sheet()`.\n", |
| 119 | + "\n", |
| 120 | + "**The following cell has made the approriate changes. Try running it and notice how the results differ from the previous cell.**" |
| 121 | + ] |
| 122 | + }, |
| 123 | + { |
| 124 | + "cell_type": "code", |
| 125 | + "execution_count": null, |
| 126 | + "metadata": { |
| 127 | + "scrolled": false |
| 128 | + }, |
| 129 | + "outputs": [], |
| 130 | + "source": [ |
| 131 | + "factor_data = get_clean_factor_and_forward_returns(\n", |
| 132 | + " pipeline_output['factor_to_analyze'],\n", |
| 133 | + " prices=pricing_data,\n", |
| 134 | + " groupby=pipeline_output['sector'],\n", |
| 135 | + " groupby_labels=sector_labels,\n", |
| 136 | + " binning_by_group=True,\n", |
| 137 | + ")\n", |
| 138 | + "\n", |
| 139 | + "create_returns_tear_sheet(factor_data, by_group=True, group_neutral=True)" |
| 140 | + ] |
| 141 | + }, |
73 | 142 | {
|
74 | 143 | "cell_type": "markdown",
|
75 | 144 | "metadata": {},
|
|
169 | 238 | "*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.*"
|
170 | 239 | ]
|
171 | 240 | },
|
172 |
| - { |
173 |
| - "cell_type": "markdown", |
174 |
| - "metadata": {}, |
175 |
| - "source": [ |
176 |
| - "## Analyzing Alpha Factors By Group\n", |
177 |
| - "\n", |
178 |
| - "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", |
179 |
| - "\n", |
180 |
| - "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", |
181 |
| - "\n", |
182 |
| - "**Run the following cell, and notice the charts at the bottom of the tear sheet showing how our factor performs in different sectors.**" |
183 |
| - ] |
184 |
| - }, |
185 |
| - { |
186 |
| - "cell_type": "code", |
187 |
| - "execution_count": null, |
188 |
| - "metadata": {}, |
189 |
| - "outputs": [], |
190 |
| - "source": [ |
191 |
| - "from alphalens.tears import create_returns_tear_sheet\n", |
192 |
| - "\n", |
193 |
| - "sector_labels, sector_labels[-1] = dict(Sector.SECTOR_NAMES), \"Unknown\"\n", |
194 |
| - "\n", |
195 |
| - "factor_data = get_clean_factor_and_forward_returns(\n", |
196 |
| - " factor=pipeline_output['factor_to_analyze'],\n", |
197 |
| - " prices=pricing_data,\n", |
198 |
| - " groupby=pipeline_output['sector'],\n", |
199 |
| - " groupby_labels=sector_labels,\n", |
200 |
| - ")\n", |
201 |
| - "\n", |
202 |
| - "create_returns_tear_sheet(factor_data=factor_data, by_group=True)" |
203 |
| - ] |
204 |
| - }, |
205 |
| - { |
206 |
| - "cell_type": "markdown", |
207 |
| - "metadata": {}, |
208 |
| - "source": [ |
209 |
| - "## Writing Group Neutral Strategies\n", |
210 |
| - "\n", |
211 |
| - "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", |
212 |
| - "\n", |
213 |
| - "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", |
214 |
| - "\n", |
215 |
| - "Since we grouped our assets by sector in the previous cell, going group neutral is easy; just make the two following changes:\n", |
216 |
| - "- Pass `binning_by_group=True` as an argument to `get_clean_factor_and_forward_returns()`.\n", |
217 |
| - "- Pass `group_neutral=True` as an argument to `create_full_tear_sheet()`.\n", |
218 |
| - "\n", |
219 |
| - "**The following cell has made the approriate changes. Try running it and notice how the results differ from the previous cell.**" |
220 |
| - ] |
221 |
| - }, |
222 |
| - { |
223 |
| - "cell_type": "code", |
224 |
| - "execution_count": null, |
225 |
| - "metadata": { |
226 |
| - "scrolled": false |
227 |
| - }, |
228 |
| - "outputs": [], |
229 |
| - "source": [ |
230 |
| - "factor_data = get_clean_factor_and_forward_returns(\n", |
231 |
| - " pipeline_output['factor_to_analyze'],\n", |
232 |
| - " prices=pricing_data,\n", |
233 |
| - " groupby=pipeline_output['sector'],\n", |
234 |
| - " groupby_labels=sector_labels,\n", |
235 |
| - " binning_by_group=True,\n", |
236 |
| - ")\n", |
237 |
| - "\n", |
238 |
| - "create_returns_tear_sheet(factor_data, by_group=True, group_neutral=True)" |
239 |
| - ] |
240 |
| - }, |
241 | 241 | {
|
242 | 242 | "cell_type": "markdown",
|
243 | 243 | "metadata": {},
|
|
255 | 255 | ],
|
256 | 256 | "metadata": {
|
257 | 257 | "kernelspec": {
|
258 |
| - "display_name": "Python 2", |
| 258 | + "display_name": "Python 3", |
259 | 259 | "language": "python",
|
260 |
| - "name": "python2" |
| 260 | + "name": "python3" |
261 | 261 | },
|
262 | 262 | "language_info": {
|
263 | 263 | "codemirror_mode": {
|
264 | 264 | "name": "ipython",
|
265 |
| - "version": 2 |
| 265 | + "version": 3 |
266 | 266 | },
|
267 | 267 | "file_extension": ".py",
|
268 | 268 | "mimetype": "text/x-python",
|
269 | 269 | "name": "python",
|
270 | 270 | "nbconvert_exporter": "python",
|
271 |
| - "pygments_lexer": "ipython2", |
272 |
| - "version": "2.7.12" |
| 271 | + "pygments_lexer": "ipython3", |
| 272 | + "version": "3.7.0" |
273 | 273 | }
|
274 | 274 | },
|
275 | 275 | "nbformat": 4,
|
|
0 commit comments