-
Notifications
You must be signed in to change notification settings - Fork 1.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Summary table #442
Merged
vikram-narayan
merged 4 commits into
quantopian:master
from
vikram-narayan:summary_table
Sep 27, 2017
Merged
Summary table #442
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,10 +14,11 @@ | |
# limitations under the License. | ||
from __future__ import division | ||
|
||
from collections import OrderedDict | ||
import empyrical as ep | ||
import pandas as pd | ||
|
||
import matplotlib.pyplot as plt | ||
|
||
from pyfolio.pos import get_percent_alloc | ||
from pyfolio.utils import print_table, set_legend_location | ||
|
||
|
@@ -124,12 +125,12 @@ def perf_attrib(returns, positions, factor_returns, factor_loadings, | |
pd.concat([perf_attrib_by_factor, returns_df], axis='columns')) | ||
|
||
|
||
def create_perf_attrib_stats(perf_attrib): | ||
def create_perf_attrib_stats(perf_attrib, risk_exposures): | ||
""" | ||
Takes perf attribution data over a period of time and computes annualized | ||
multifactor alpha, multifactor sharpe, risk exposures. | ||
""" | ||
summary = {} | ||
summary = OrderedDict() | ||
specific_returns = perf_attrib['specific_returns'] | ||
common_returns = perf_attrib['common_returns'] | ||
|
||
|
@@ -139,6 +140,8 @@ def create_perf_attrib_stats(perf_attrib): | |
summary['Multi-factor sharpe'] =\ | ||
ep.sharpe_ratio(specific_returns) | ||
|
||
# empty line between common/specific/total returns | ||
summary[' '] = ' ' | ||
summary['Cumulative specific returns'] =\ | ||
ep.cum_returns_final(specific_returns) | ||
summary['Cumulative common returns'] =\ | ||
|
@@ -147,7 +150,9 @@ def create_perf_attrib_stats(perf_attrib): | |
ep.cum_returns_final(perf_attrib['total_returns']) | ||
|
||
summary = pd.Series(summary) | ||
return summary | ||
|
||
risk_exposure_summary = risk_exposures.sum(axis='rows') | ||
return summary, risk_exposure_summary | ||
|
||
|
||
def show_perf_attrib_stats(returns, positions, factor_returns, | ||
|
@@ -164,46 +169,63 @@ def show_perf_attrib_stats(returns, positions, factor_returns, | |
pos_in_dollars=pos_in_dollars, | ||
) | ||
|
||
perf_attrib_stats = create_perf_attrib_stats(perf_attrib_data) | ||
perf_attrib_stats, risk_exposure_stats =\ | ||
create_perf_attrib_stats(perf_attrib_data, risk_exposures) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thoughts on having |
||
|
||
print_table(perf_attrib_stats) | ||
print_table(risk_exposures) | ||
print_table(risk_exposure_stats) | ||
|
||
|
||
def plot_returns(perf_attrib_data, ax=None): | ||
def plot_returns(perf_attrib_data, cost=None, ax=None): | ||
""" | ||
Plot total, specific, and common returns. | ||
|
||
Parameters | ||
---------- | ||
perf_attrib_data : pd.DataFrame | ||
df with factors, common returns, and specific returns as columns, | ||
and datetimes as index | ||
and datetimes as index. Assumes the `total_returns` column is NOT | ||
cost adjusted. | ||
- Example: | ||
momentum reversal common_returns specific_returns | ||
dt | ||
2017-01-01 0.249087 0.935925 1.185012 1.185012 | ||
2017-01-02 -0.003194 -0.400786 -0.403980 -0.403980 | ||
|
||
cost : pd.Series, optional | ||
if present, gets subtracted from `perf_attrib_data['total_returns']`, | ||
and gets plotted separately | ||
|
||
ax : matplotlib.axes.Axes | ||
axes on which plots are made. if None, current axes will be used | ||
|
||
Returns | ||
------- | ||
ax : matplotlib.axes.Axes | ||
""" | ||
|
||
if ax is None: | ||
ax = plt.gca() | ||
|
||
returns = perf_attrib_data['total_returns'] | ||
total_returns_label = 'Total returns' | ||
|
||
if cost is not None: | ||
returns = returns - cost | ||
total_returns_label += ' (adjusted)' | ||
|
||
specific_returns = perf_attrib_data['specific_returns'] | ||
common_returns = perf_attrib_data['common_returns'] | ||
|
||
ax.plot(ep.cum_returns(returns), color='g', label='Total returns') | ||
ax.plot(ep.cum_returns(returns), color='g', label=total_returns_label) | ||
ax.plot(ep.cum_returns(specific_returns), color='b', | ||
label='Cumulative specific returns') | ||
ax.plot(ep.cum_returns(common_returns), color='r', | ||
label='Cumulative common returns') | ||
|
||
if cost is not None: | ||
ax.plot(cost, color='p', label='Cost') | ||
|
||
ax.set_title('Time series of cumulative returns') | ||
ax.set_ylabel('Returns') | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should be grouped with the standard lib imports