Example #1
0
def plot_risk_exposures(exposures, ax=None):
    """
    Parameters
    ----------
    exposures : pd.DataFrame
        df indexed by datetime, with factors as columns
        - Example:
                        momentum  reversal
            dt
            2017-01-01 -0.238655  0.077123
            2017-01-02  0.821872  1.520515

    ax :  matplotlib.axes.Axes
        axes on which plots are made. if None, current axes will be used

    Returns
    -------
    ax :  matplotlib.axes.Axes
    """
    if ax is None:
        ax = plt.gca()

    for col in exposures:
        ax.plot(exposures[col])

    set_legend_location(ax)
    ax.set_ylabel('Factor exposures')
    ax.set_title('Risk factor exposures')

    return ax
Example #2
0
def plot_alpha_returns(alpha_returns, ax=None):
    """
    Plot histogram of daily multi-factor alpha returns (specific returns).

    Parameters
    ----------
    alpha_returns : pd.Series
        series of daily alpha returns indexed by datetime

    ax :  matplotlib.axes.Axes
        axes on which plots are made. if None, current axes will be used

    Returns
    -------
    ax :  matplotlib.axes.Axes
    """
    if ax is None:
        ax = plt.gca()

    ax.hist(alpha_returns, color='g', label='Multi-factor alpha')
    ax.set_title('Histogram of alphas')
    ax.axvline(0, color='k', linestyle='--', label='Zero')

    avg = alpha_returns.mean()
    ax.axvline(avg, color='b', label='Mean = {: 0.5f}'.format(avg))
    set_legend_location(ax)

    return ax
Example #3
0
def plot_returns(perf_attrib_data, cost=None, ax=None):
    """
    Plot total, specific, and common returns.

    Parameters
    ----------
    perf_attrib_data : pd.DataFrame
        df with factors, common returns, and specific returns as columns,
        and datetimes as index. Assumes the `total_returns` column is NOT
        cost adjusted.
        - Example:
                        momentum  reversal  common_returns  specific_returns
            dt
            2017-01-01  0.249087  0.935925        1.185012          1.185012
            2017-01-02 -0.003194 -0.400786       -0.403980         -0.403980

    cost : pd.Series, optional
        if present, gets subtracted from `perf_attrib_data['total_returns']`,
        and gets plotted separately

    ax :  matplotlib.axes.Axes
        axes on which plots are made. if None, current axes will be used

    Returns
    -------
    ax :  matplotlib.axes.Axes
    """

    if ax is None:
        ax = plt.gca()

    returns = perf_attrib_data['total_returns']
    total_returns_label = 'Total returns'

    if cost is not None:
        returns = returns - cost
        total_returns_label += ' (adjusted)'

    specific_returns = perf_attrib_data['specific_returns']
    common_returns = perf_attrib_data['common_returns']

    ax.plot(ep.cum_returns(returns), color='g', label=total_returns_label)
    ax.plot(ep.cum_returns(specific_returns),
            color='b',
            label='Cumulative specific returns')
    ax.plot(ep.cum_returns(common_returns),
            color='r',
            label='Cumulative common returns')

    if cost is not None:
        ax.plot(cost, color='p', label='Cost')

    ax.set_title('Time series of cumulative returns')
    ax.set_ylabel('Returns')

    set_legend_location(ax)

    return ax
Example #4
0
def plot_factor_contribution_to_perf(exposures, perf_attrib_data, ax=None):
    """
    Plot each factor's contribution to performance.

    Parameters
    ----------
    exposures : pd.DataFrame
        df indexed by datetime, with factors as columns
        - Example:
                        momentum  reversal
            dt
            2017-01-01 -0.238655  0.077123
            2017-01-02  0.821872  1.520515

    perf_attrib_data : pd.DataFrame
        df with factors, common returns, and specific returns as columns,
        and datetimes as index
        - Example:
                        momentum  reversal  common_returns  specific_returns
            dt
            2017-01-01  0.249087  0.935925        1.185012          1.185012
            2017-01-02 -0.003194 -0.400786       -0.403980         -0.403980

    ax :  matplotlib.axes.Axes
        axes on which plots are made. if None, current axes will be used

    Returns
    -------
    ax :  matplotlib.axes.Axes
    """
    if ax is None:
        ax = plt.gca()

    factors_and_specific = perf_attrib_data.drop(
        ['total_returns', 'common_returns'], axis='columns')

    ax.stackplot(factors_and_specific.index,
                 [factors_and_specific[s] for s in factors_and_specific],
                 labels=factors_and_specific.columns,
                 colors=COLORS)

    ax.axhline(0, color='k')
    set_legend_location(ax)

    ax.set_ylabel('Contribution to returns by factor')
    ax.set_title('Returns attribution')

    return ax
Example #5
0
def plot_returns(perf_attrib_data, ax=None):
    """
    Plot total, specific, and common returns.

    Parameters
    ----------
    perf_attrib_data : pd.DataFrame
        df with factors, common returns, and specific returns as columns,
        and datetimes as index
        - Example:
                        momentum  reversal  common_returns  specific_returns
            dt
            2017-01-01  0.249087  0.935925        1.185012          1.185012
            2017-01-02 -0.003194 -0.400786       -0.403980         -0.403980

    ax :  matplotlib.axes.Axes
        axes on which plots are made. if None, current axes will be used

    Returns
    -------
    ax :  matplotlib.axes.Axes
    """
    if ax is None:
        ax = plt.gca()

    returns = perf_attrib_data['total_returns']
    specific_returns = perf_attrib_data['specific_returns']
    common_returns = perf_attrib_data['common_returns']

    ax.plot(ep.cum_returns(returns), color='g', label='Total returns')
    ax.plot(ep.cum_returns(specific_returns), color='b',
            label='Cumulative specific returns')
    ax.plot(ep.cum_returns(common_returns), color='r',
            label='Cumulative common returns')

    ax.set_title('Time series of cumulative returns')
    ax.set_ylabel('Returns')

    set_legend_location(ax)

    return ax
Example #6
0
def plot_returns(returns, specific_returns, common_returns, ax=None):
    """
    Plot total, specific, and common returns.

    Parameters
    ----------
    returns : pd.Series
        total returns, indexed by datetime

    specific_returns : pd.Series
        specific returns, indexed by datetime

    commons_returns : pd.Series
        common returns, indexed by datetime

    ax :  matplotlib.axes.Axes
        axes on which plots are made. if None, current axes will be used

    Returns
    -------
    ax :  matplotlib.axes.Axes
    """
    if ax is None:
        ax = plt.gca()

    ax.plot(ep.cum_returns(returns), color='g', label='Total returns')
    ax.plot(ep.cum_returns(specific_returns),
            color='b',
            label='Cumulative specific returns')
    ax.plot(ep.cum_returns(common_returns),
            color='r',
            label='Cumulative common returns')

    ax.set_title('Time Series of cumulative returns')
    ax.set_ylabel('Returns')

    set_legend_location(ax)

    return ax