Skip to content

Commit 77084f1

Browse files
authored
Merge pull request quantopian#361 from quantopian/perf-improvements
Perf improvements
2 parents 0492c9d + 0d41461 commit 77084f1

File tree

8 files changed

+696
-776
lines changed

8 files changed

+696
-776
lines changed

alphalens/examples/tear_sheet_walk_through.ipynb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1674,7 +1674,7 @@
16741674
"outputs": [],
16751675
"source": [
16761676
"quantile_factor = factor_data['factor_quantile']\n",
1677-
"turnover_period = '1D'"
1677+
"turnover_period = 1"
16781678
]
16791679
},
16801680
{

alphalens/performance.py

Lines changed: 52 additions & 207 deletions
Large diffs are not rendered by default.

alphalens/plotting.py

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -152,11 +152,11 @@ def plot_turnover_table(autocorrelation_data, quantile_turnover):
152152
for period in sorted(quantile_turnover.keys()):
153153
for quantile, p_data in quantile_turnover[period].iteritems():
154154
turnover_table.loc["Quantile {} Mean Turnover ".format(quantile),
155-
"{}".format(period)] = p_data.mean()
155+
"{}D".format(period)] = p_data.mean()
156156
auto_corr = pd.DataFrame()
157157
for period, p_data in autocorrelation_data.iteritems():
158158
auto_corr.loc["Mean Factor Rank Autocorrelation",
159-
"{}".format(period)] = p_data.mean()
159+
"{}D".format(period)] = p_data.mean()
160160

161161
print("Turnover Analysis")
162162
utils.print_table(turnover_table.apply(lambda x: x.round(3)))
@@ -607,7 +607,7 @@ def plot_factor_rank_auto_correlation(factor_autocorrelation,
607607
if ax is None:
608608
f, ax = plt.subplots(1, 1, figsize=(18, 6))
609609

610-
factor_autocorrelation.plot(title='{} Period Factor Rank Autocorrelation'
610+
factor_autocorrelation.plot(title='{}D Period Factor Rank Autocorrelation'
611611
.format(period), ax=ax)
612612
ax.set(ylabel='Autocorrelation Coefficient', xlabel='')
613613
ax.axhline(0.0, linestyle='-', color='black', lw=1)
@@ -629,7 +629,7 @@ def plot_top_bottom_quantile_turnover(quantile_turnover, period=1, ax=None):
629629
quantile_turnover: pd.Dataframe
630630
Quantile turnover (each DataFrame column a quantile).
631631
period: int, optional
632-
Period over which to calculate the turnover
632+
Period over which to calculate the turnover.
633633
ax : matplotlib.Axes, optional
634634
Axes upon which to plot.
635635
@@ -646,7 +646,7 @@ def plot_top_bottom_quantile_turnover(quantile_turnover, period=1, ax=None):
646646
turnover = pd.DataFrame()
647647
turnover['top quantile turnover'] = quantile_turnover[max_quantile]
648648
turnover['bottom quantile turnover'] = quantile_turnover[min_quantile]
649-
turnover.plot(title='{} Period Top and Bottom Quantile Turnover'
649+
turnover.plot(title='{}D Period Top and Bottom Quantile Turnover'
650650
.format(period), ax=ax, alpha=0.6, lw=0.8)
651651
ax.set(ylabel='Proportion Of Names New To Quantile', xlabel="")
652652

@@ -711,7 +711,11 @@ def plot_monthly_ic_heatmap(mean_monthly_ic, ax=None):
711711
return ax
712712

713713

714-
def plot_cumulative_returns(factor_returns, period, freq, title=None, ax=None):
714+
def plot_cumulative_returns(factor_returns,
715+
period,
716+
freq=None,
717+
title=None,
718+
ax=None):
715719
"""
716720
Plots the cumulative returns of the returns series passed in.
717721
@@ -720,7 +724,7 @@ def plot_cumulative_returns(factor_returns, period, freq, title=None, ax=None):
720724
factor_returns : pd.Series
721725
Period wise returns of dollar neutral portfolio weighted by factor
722726
value.
723-
period: pandas.Timedelta or string
727+
period : pandas.Timedelta or string
724728
Length of period for which the returns are computed (e.g. 1 day)
725729
if 'period' is a string it must follow pandas.Timedelta constructor
726730
format (e.g. '1 days', '1D', '30m', '3h', '1D1h', etc)
@@ -742,7 +746,7 @@ def plot_cumulative_returns(factor_returns, period, freq, title=None, ax=None):
742746
if ax is None:
743747
f, ax = plt.subplots(1, 1, figsize=(18, 6))
744748

745-
factor_returns = perf.cumulative_returns(factor_returns, period, freq)
749+
factor_returns = perf.cumulative_returns(factor_returns)
746750

747751
factor_returns.plot(ax=ax, lw=3, color='forestgreen', alpha=0.6)
748752
ax.set(ylabel='Cumulative Returns',
@@ -756,7 +760,7 @@ def plot_cumulative_returns(factor_returns, period, freq, title=None, ax=None):
756760

757761
def plot_cumulative_returns_by_quantile(quantile_returns,
758762
period,
759-
freq,
763+
freq=None,
760764
ax=None):
761765
"""
762766
Plots the cumulative returns of various factor quantiles.
@@ -765,7 +769,7 @@ def plot_cumulative_returns_by_quantile(quantile_returns,
765769
----------
766770
quantile_returns : pd.DataFrame
767771
Returns by factor quantile
768-
period: pandas.Timedelta or string
772+
period : pandas.Timedelta or string
769773
Length of period for which the returns are computed (e.g. 1 day)
770774
if 'period' is a string it must follow pandas.Timedelta constructor
771775
format (e.g. '1 days', '1D', '30m', '3h', '1D1h', etc)
@@ -787,7 +791,8 @@ def plot_cumulative_returns_by_quantile(quantile_returns,
787791

788792
ret_wide = quantile_returns.unstack('factor_quantile')
789793

790-
cum_ret = ret_wide.apply(perf.cumulative_returns, period=period, freq=freq)
794+
cum_ret = ret_wide.apply(perf.cumulative_returns)
795+
791796
cum_ret = cum_ret.loc[:, ::-1] # we want negative quantiles as 'red'
792797

793798
cum_ret.plot(lw=2, ax=ax, cmap=cm.coolwarm)

0 commit comments

Comments
 (0)