Skip to content

Commit

Permalink
Merge pull request quantopian#313 from MichaelJMath/master
Browse files Browse the repository at this point in the history
BUG: Calculate mean returns by date before mean return by quantile
  • Loading branch information
twiecki authored Aug 24, 2018
2 parents 5614846 + 4c6d96a commit b3f9baf
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions alphalens/performance.py
Original file line number Diff line number Diff line change
Expand Up @@ -638,9 +638,7 @@ def mean_return_by_quantile(factor_data,
else:
factor_data = factor_data.copy()

grouper = ['factor_quantile']
if by_date:
grouper.append(factor_data.index.get_level_values('date'))
grouper = ['factor_quantile', factor_data.index.get_level_values('date')]

if by_group:
grouper.append('group')
Expand All @@ -651,6 +649,14 @@ def mean_return_by_quantile(factor_data,

mean_ret = group_stats.T.xs('mean', level=1).T

if not by_date:
grouper = [mean_ret.index.get_level_values('factor_quantile')]
if by_group:
grouper.append(mean_ret.index.get_level_values('group'))
group_stats = mean_ret.groupby(grouper)\
.agg(['mean', 'std', 'count'])
mean_ret = group_stats.T.xs('mean', level=1).T

std_error_ret = group_stats.T.xs('std', level=1).T \
/ np.sqrt(group_stats.T.xs('count', level=1).T)

Expand Down

0 comments on commit b3f9baf

Please sign in to comment.