Skip to content

Commit 32cfed8

Browse files
committed
MAINT: Test fixes + miscellaneous cleanup
1 parent 96a1c7e commit 32cfed8

File tree

8 files changed

+358
-364
lines changed

8 files changed

+358
-364
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: 24 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -578,6 +578,7 @@ def quantile_turnover(quantile_factor, quantile, period=1):
578578
Quantile on which to perform turnover analysis.
579579
period: int, optional
580580
Number of days over which to calculate the turnover.
581+
581582
Returns
582583
-------
583584
quant_turnover : pd.Series
@@ -616,14 +617,13 @@ def factor_rank_autocorrelation(factor_data, period=1):
616617
- See full explanation in utils.get_clean_factor_and_forward_returns
617618
period: int, optional
618619
Number of days over which to calculate the turnover.
620+
619621
Returns
620622
-------
621623
autocorr : pd.Series
622624
Rolling 1 period (defined by time_rule) autocorrelation of
623625
factor values.
624-
625626
"""
626-
627627
grouper = [factor_data.index.get_level_values('date')]
628628

629629
ranks = factor_data.groupby(grouper)['factor'].rank()
@@ -658,17 +658,18 @@ def common_start_returns(factor,
658658
factor : pd.DataFrame
659659
DataFrame with at least date and equity as index, the columns are
660660
irrelevant
661-
prices : pd.DataFrame
662-
A wide form Pandas DataFrame indexed by date with assets
663-
in the columns. Pricing data should span the factor
664-
analysis time period plus/minus an additional buffer window
665-
corresponding to after/before period parameters.
661+
returns : pd.DataFrame
662+
A wide form Pandas DataFrame indexed by date with assets in the
663+
columns. Returns data should span the factor analysis time period
664+
plus/minus an additional buffer window corresponding to after/before
665+
period parameters.
666666
before:
667667
How many returns to load before factor date
668668
after:
669669
How many returns to load after factor date
670670
cumulative: bool, optional
671-
Return cumulative returns
671+
Whether or not the given returns are cumulative. If False the given
672+
returns are assumed to be daily.
672673
mean_by_date: bool, optional
673674
If True, compute mean returns for each date and return that
674675
instead of a return series for each asset
@@ -684,7 +685,6 @@ def common_start_returns(factor,
684685
Dataframe containing returns series for each factor aligned to the same
685686
index: -before to after
686687
"""
687-
688688
if not cumulative:
689689
returns = returns.apply(cumulative_returns, axis=0)
690690

@@ -714,9 +714,6 @@ def common_start_returns(factor,
714714
series.index = range(starting_index - day_zero_index,
715715
ending_index - day_zero_index)
716716

717-
if cumulative:
718-
series = (series / series.loc[0, :]) - 1
719-
720717
if demean_by is not None:
721718
mean = series.loc[:, demean_equities].mean(axis=1)
722719
series = series.loc[:, equities]
@@ -749,11 +746,11 @@ def average_cumulative_return_by_quantile(factor_data,
749746
each period, the factor quantile/bin that factor value belongs to, and
750747
(optionally) the group the asset belongs to.
751748
- See full explanation in utils.get_clean_factor_and_forward_returns
752-
prices : pd.DataFrame
753-
A wide form Pandas DataFrame indexed by date with assets
754-
in the columns. Pricing data should span the factor
755-
analysis time period plus/minus an additional buffer window
756-
corresponding to periods_after/periods_before parameters.
749+
returns : pd.DataFrame
750+
A wide form Pandas DataFrame indexed by date with assets in the
751+
columns. Returns data should span the factor analysis time period
752+
plus/minus an additional buffer window corresponding to periods_after/
753+
periods_before parameters.
757754
periods_before : int, optional
758755
How many periods before factor to plot
759756
periods_after : int, optional
@@ -765,6 +762,7 @@ def average_cumulative_return_by_quantile(factor_data,
765762
neutral portfolio)
766763
by_group : bool
767764
If True, compute cumulative returns separately for each group
765+
768766
Returns
769767
-------
770768
cumulative returns and std deviation : pd.DataFrame
@@ -791,10 +789,15 @@ def average_cumulative_return_by_quantile(factor_data,
791789
"""
792790

793791
def cumulative_return_around_event(q_fact, demean_by):
794-
return common_start_returns(q_fact, returns,
795-
periods_before,
796-
periods_after,
797-
True, True, demean_by)
792+
return common_start_returns(
793+
q_fact,
794+
returns,
795+
periods_before,
796+
periods_after,
797+
cumulative=True,
798+
mean_by_date=True,
799+
demean_by=demean_by,
800+
)
798801

799802
def average_cumulative_return(q_fact, demean_by):
800803
q_returns = cumulative_return_around_event(q_fact, demean_by)

alphalens/plotting.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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

alphalens/tears.py

Lines changed: 28 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ def create_summary_tear_sheet(
124124
)
125125

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

128129
fr_cols = len(periods)
129130
vertical_sections = 2 + fr_cols * 3
@@ -430,9 +431,11 @@ def create_turnover_tear_sheet(factor_data, turnover_periods=None):
430431
if turnover_periods is None:
431432
input_periods = utils.get_forward_returns_columns(
432433
factor_data.columns, require_exact_day_multiple=True,
433-
)
434-
turnover_periods = list(
435-
map((lambda x: pd.Timedelta(x).days), input_periods.get_values())
434+
).get_values()
435+
turnover_periods = utils.timedelta_strings_to_integers(input_periods)
436+
else:
437+
turnover_periods = utils.timedelta_strings_to_integers(
438+
turnover_periods,
436439
)
437440

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

484487

485488
@plotting.customize
486-
def create_full_tear_sheet(
487-
factor_data, long_short=True, group_neutral=False, by_group=False
488-
):
489+
def create_full_tear_sheet(factor_data,
490+
long_short=True,
491+
group_neutral=False,
492+
by_group=False):
489493
"""
490494
Creates a full tear sheet for analysis and evaluating single
491495
return predicting (alpha) factor.
@@ -523,15 +527,13 @@ def create_full_tear_sheet(
523527

524528

525529
@plotting.customize
526-
def create_event_returns_tear_sheet(
527-
factor_data,
528-
returns,
529-
avgretplot=(5, 15),
530-
long_short=True,
531-
group_neutral=False,
532-
std_bar=True,
533-
by_group=False,
534-
):
530+
def create_event_returns_tear_sheet(factor_data,
531+
returns,
532+
avgretplot=(5, 15),
533+
long_short=True,
534+
group_neutral=False,
535+
std_bar=True,
536+
by_group=False):
535537
"""
536538
Creates a tear sheet to view the average cumulative returns for a
537539
factor within a window (pre and post event).
@@ -544,9 +546,9 @@ def create_event_returns_tear_sheet(
544546
quantile/bin that factor value belongs to and (optionally) the group
545547
the asset belongs to.
546548
- See full explanation in utils.get_clean_factor_and_forward_returns
547-
prices : pd.DataFrame
548-
A DataFrame indexed by date with assets in the columns containing the
549-
pricing data.
549+
returns : pd.DataFrame
550+
A DataFrame indexed by date with assets in the columns containing daily
551+
returns.
550552
- See full explanation in utils.get_clean_factor_and_forward_returns
551553
avgretplot: tuple (int, int) - (before, after)
552554
If not None, plot quantile average cumulative returns
@@ -631,9 +633,11 @@ def create_event_returns_tear_sheet(
631633

632634

633635
@plotting.customize
634-
def create_event_study_tear_sheet(
635-
factor_data, returns, avgretplot=(5, 15), rate_of_ret=True, n_bars=50
636-
):
636+
def create_event_study_tear_sheet(factor_data,
637+
returns,
638+
avgretplot=(5, 15),
639+
rate_of_ret=True,
640+
n_bars=50):
637641
"""
638642
Creates an event study tear sheet for analysis of a specific event.
639643
@@ -644,9 +648,9 @@ def create_event_study_tear_sheet(
644648
containing the values for a single event, forward returns for each
645649
period, the factor quantile/bin that factor value belongs to, and
646650
(optionally) the group the asset belongs to.
647-
prices : pd.DataFrame, required only if 'avgretplot' is provided
648-
A DataFrame indexed by date with assets in the columns containing the
649-
pricing data.
651+
returns : pd.DataFrame, required only if 'avgretplot' is provided
652+
A DataFrame indexed by date with assets in the columns containing daily
653+
returns.
650654
- See full explanation in utils.get_clean_factor_and_forward_returns
651655
avgretplot: tuple (int, int) - (before, after), optional
652656
If not None, plot event style average cumulative returns within a

0 commit comments

Comments
 (0)