quantopian / alphalens

Performance analysis of predictive (alpha) stock factors
http://quantopian.github.io/alphalens
Apache License 2.0
3.26k stars 1.13k forks source link

Could there be a logical error in the function plotting. plot_top_bottom_quantile_turnover ? #263

Closed Gabrielvon closed 6 years ago

Gabrielvon commented 6 years ago

I just came across the Lecture 38: Factor Analysis and felt confused by first line in Turnover section.

screen shot 2018-02-04 at 19 13 06

Since factor_data is a dataframe with columns [1, 5, 10, 'factor', 'group', 'factor_quantile'],

    max_quantile = quantile_turnover.columns.max()
    min_quantile = quantile_turnover.columns.min()

what is the logic of extracting the max and min value from this list with the code in the original script like above?

Question: Is the code in the jupyter notebook in the lecture incorrect? If not, could you illustrate a bit how to use this function? Big thanks!

Below is the code for entire function.

def plot_top_bottom_quantile_turnover(quantile_turnover, period=1, ax=None):
    """
    Plots period wise top and bottom quantile factor turnover.
    Parameters
    ----------
    quantile_turnover: pd.Dataframe
        Quantile turnover (each DataFrame column a quantile).
    period: int, optional
        Period over which to calculate the turnover
    ax : matplotlib.Axes, optional
        Axes upon which to plot.
    Returns
    -------
    ax : matplotlib.Axes
        The axes that were plotted on.
    """
    if ax is None:
        f, ax = plt.subplots(1, 1, figsize=(18, 6))

    max_quantile = quantile_turnover.columns.max()
    min_quantile = quantile_turnover.columns.min()
    turnover = pd.DataFrame()
    turnover['top quantile turnover'] = quantile_turnover[max_quantile]
    turnover['bottom quantile turnover'] = quantile_turnover[min_quantile]
    turnover.plot(title='{} Period Top and Bottom Quantile Turnover'
                  .format(period), ax=ax, alpha=0.6, lw=0.8)
    ax.set(ylabel='Proportion Of Names New To Quantile', xlabel="")

    return ax
luca-s commented 6 years ago

@Gabrielvon I now understand what you meant, disregard my previous comment, sorry. I believe you are right, the lecture 38 has a mistake.

Please report the error to Quantopian support as the Quantopian lectures are not developed as part of Alphalens project, so we (the community, which is not Quantopian employees only) cannot make change to the Lectures material, only Quantopian can.

Gabrielvon commented 6 years ago

@luca-s Understood. Thanks!

luca-s commented 6 years ago

Thanks to you @Gabrielvon

mmargenot commented 6 years ago

I have added the correction here: https://github.com/quantopian/research_public/pull/223. Thanks, @Gabrielvon.