Skip to content

Commit

Permalink
MAINT: Test fixes + miscellaneous cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
dmichalowicz committed Apr 27, 2020
1 parent 96a1c7e commit 32cfed8
Show file tree
Hide file tree
Showing 8 changed files with 358 additions and 364 deletions.
2 changes: 1 addition & 1 deletion alphalens/examples/tear_sheet_walk_through.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -1674,7 +1674,7 @@
"outputs": [],
"source": [
"quantile_factor = factor_data['factor_quantile']\n",
"turnover_period = '1D'"
"turnover_period = 1"
]
},
{
Expand Down
45 changes: 24 additions & 21 deletions alphalens/performance.py
Original file line number Diff line number Diff line change
Expand Up @@ -578,6 +578,7 @@ def quantile_turnover(quantile_factor, quantile, period=1):
Quantile on which to perform turnover analysis.
period: int, optional
Number of days over which to calculate the turnover.
Returns
-------
quant_turnover : pd.Series
Expand Down Expand Up @@ -616,14 +617,13 @@ def factor_rank_autocorrelation(factor_data, period=1):
- See full explanation in utils.get_clean_factor_and_forward_returns
period: int, optional
Number of days over which to calculate the turnover.
Returns
-------
autocorr : pd.Series
Rolling 1 period (defined by time_rule) autocorrelation of
factor values.
"""

grouper = [factor_data.index.get_level_values('date')]

ranks = factor_data.groupby(grouper)['factor'].rank()
Expand Down Expand Up @@ -658,17 +658,18 @@ def common_start_returns(factor,
factor : pd.DataFrame
DataFrame with at least date and equity as index, the columns are
irrelevant
prices : pd.DataFrame
A wide form Pandas DataFrame indexed by date with assets
in the columns. Pricing data should span the factor
analysis time period plus/minus an additional buffer window
corresponding to after/before period parameters.
returns : pd.DataFrame
A wide form Pandas DataFrame indexed by date with assets in the
columns. Returns data should span the factor analysis time period
plus/minus an additional buffer window corresponding to after/before
period parameters.
before:
How many returns to load before factor date
after:
How many returns to load after factor date
cumulative: bool, optional
Return cumulative returns
Whether or not the given returns are cumulative. If False the given
returns are assumed to be daily.
mean_by_date: bool, optional
If True, compute mean returns for each date and return that
instead of a return series for each asset
Expand All @@ -684,7 +685,6 @@ def common_start_returns(factor,
Dataframe containing returns series for each factor aligned to the same
index: -before to after
"""

if not cumulative:
returns = returns.apply(cumulative_returns, axis=0)

Expand Down Expand Up @@ -714,9 +714,6 @@ def common_start_returns(factor,
series.index = range(starting_index - day_zero_index,
ending_index - day_zero_index)

if cumulative:
series = (series / series.loc[0, :]) - 1

if demean_by is not None:
mean = series.loc[:, demean_equities].mean(axis=1)
series = series.loc[:, equities]
Expand Down Expand Up @@ -749,11 +746,11 @@ def average_cumulative_return_by_quantile(factor_data,
each period, the factor quantile/bin that factor value belongs to, and
(optionally) the group the asset belongs to.
- See full explanation in utils.get_clean_factor_and_forward_returns
prices : pd.DataFrame
A wide form Pandas DataFrame indexed by date with assets
in the columns. Pricing data should span the factor
analysis time period plus/minus an additional buffer window
corresponding to periods_after/periods_before parameters.
returns : pd.DataFrame
A wide form Pandas DataFrame indexed by date with assets in the
columns. Returns data should span the factor analysis time period
plus/minus an additional buffer window corresponding to periods_after/
periods_before parameters.
periods_before : int, optional
How many periods before factor to plot
periods_after : int, optional
Expand All @@ -765,6 +762,7 @@ def average_cumulative_return_by_quantile(factor_data,
neutral portfolio)
by_group : bool
If True, compute cumulative returns separately for each group
Returns
-------
cumulative returns and std deviation : pd.DataFrame
Expand All @@ -791,10 +789,15 @@ def average_cumulative_return_by_quantile(factor_data,
"""

def cumulative_return_around_event(q_fact, demean_by):
return common_start_returns(q_fact, returns,
periods_before,
periods_after,
True, True, demean_by)
return common_start_returns(
q_fact,
returns,
periods_before,
periods_after,
cumulative=True,
mean_by_date=True,
demean_by=demean_by,
)

def average_cumulative_return(q_fact, demean_by):
q_returns = cumulative_return_around_event(q_fact, demean_by)
Expand Down
2 changes: 1 addition & 1 deletion alphalens/plotting.py
Original file line number Diff line number Diff line change
Expand Up @@ -629,7 +629,7 @@ def plot_top_bottom_quantile_turnover(quantile_turnover, period=1, ax=None):
quantile_turnover: pd.Dataframe
Quantile turnover (each DataFrame column a quantile).
period: int, optional
Period over which to calculate the turnover
Period over which to calculate the turnover.
ax : matplotlib.Axes, optional
Axes upon which to plot.
Expand Down
52 changes: 28 additions & 24 deletions alphalens/tears.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ def create_summary_tear_sheet(
)

periods = utils.get_forward_returns_columns(factor_data.columns)
periods = list(map(lambda p: pd.Timedelta(p).days, periods))

fr_cols = len(periods)
vertical_sections = 2 + fr_cols * 3
Expand Down Expand Up @@ -430,9 +431,11 @@ def create_turnover_tear_sheet(factor_data, turnover_periods=None):
if turnover_periods is None:
input_periods = utils.get_forward_returns_columns(
factor_data.columns, require_exact_day_multiple=True,
)
turnover_periods = list(
map((lambda x: pd.Timedelta(x).days), input_periods.get_values())
).get_values()
turnover_periods = utils.timedelta_strings_to_integers(input_periods)
else:
turnover_periods = utils.timedelta_strings_to_integers(
turnover_periods,
)

quantile_factor = factor_data["factor_quantile"]
Expand Down Expand Up @@ -483,9 +486,10 @@ def create_turnover_tear_sheet(factor_data, turnover_periods=None):


@plotting.customize
def create_full_tear_sheet(
factor_data, long_short=True, group_neutral=False, by_group=False
):
def create_full_tear_sheet(factor_data,
long_short=True,
group_neutral=False,
by_group=False):
"""
Creates a full tear sheet for analysis and evaluating single
return predicting (alpha) factor.
Expand Down Expand Up @@ -523,15 +527,13 @@ def create_full_tear_sheet(


@plotting.customize
def create_event_returns_tear_sheet(
factor_data,
returns,
avgretplot=(5, 15),
long_short=True,
group_neutral=False,
std_bar=True,
by_group=False,
):
def create_event_returns_tear_sheet(factor_data,
returns,
avgretplot=(5, 15),
long_short=True,
group_neutral=False,
std_bar=True,
by_group=False):
"""
Creates a tear sheet to view the average cumulative returns for a
factor within a window (pre and post event).
Expand All @@ -544,9 +546,9 @@ def create_event_returns_tear_sheet(
quantile/bin that factor value belongs to and (optionally) the group
the asset belongs to.
- See full explanation in utils.get_clean_factor_and_forward_returns
prices : pd.DataFrame
A DataFrame indexed by date with assets in the columns containing the
pricing data.
returns : pd.DataFrame
A DataFrame indexed by date with assets in the columns containing daily
returns.
- See full explanation in utils.get_clean_factor_and_forward_returns
avgretplot: tuple (int, int) - (before, after)
If not None, plot quantile average cumulative returns
Expand Down Expand Up @@ -631,9 +633,11 @@ def create_event_returns_tear_sheet(


@plotting.customize
def create_event_study_tear_sheet(
factor_data, returns, avgretplot=(5, 15), rate_of_ret=True, n_bars=50
):
def create_event_study_tear_sheet(factor_data,
returns,
avgretplot=(5, 15),
rate_of_ret=True,
n_bars=50):
"""
Creates an event study tear sheet for analysis of a specific event.
Expand All @@ -644,9 +648,9 @@ def create_event_study_tear_sheet(
containing the values for a single event, forward returns for each
period, the factor quantile/bin that factor value belongs to, and
(optionally) the group the asset belongs to.
prices : pd.DataFrame, required only if 'avgretplot' is provided
A DataFrame indexed by date with assets in the columns containing the
pricing data.
returns : pd.DataFrame, required only if 'avgretplot' is provided
A DataFrame indexed by date with assets in the columns containing daily
returns.
- See full explanation in utils.get_clean_factor_and_forward_returns
avgretplot: tuple (int, int) - (before, after), optional
If not None, plot event style average cumulative returns within a
Expand Down
Loading

0 comments on commit 32cfed8

Please sign in to comment.