def create_bayesian_tear_sheet(returns, benchmark_rets=None,
                               live_start_date=None, samples=2000,
                               return_fig=False, stoch_vol=False,
                               progressbar=True):
    """
    Generate a number of Bayesian distributions and a Bayesian
    cone plot of returns.

    Plots: Sharpe distribution, annual volatility distribution,
    annual alpha distribution, beta distribution, predicted 1 and 5
    day returns distributions, and a cumulative returns cone plot.

    Parameters
    ----------
    returns : pd.Series
        Daily returns of the strategy, noncumulative.
         - See full explanation in create_full_tear_sheet.
    benchmark_rets : pd.Series, optional
        Daily noncumulative returns of the benchmark.
         - This is in the same style as returns.
    live_start_date : datetime, optional
        The point in time when the strategy began live
        trading, after its backtest period.
    samples : int, optional
        Number of posterior samples to draw.
    return_fig : boolean, optional
        If True, returns the figure that was plotted on.
    stoch_vol : boolean, optional
        If True, run and plot the stochastic volatility model
    progressbar : boolean, optional
        If True, show a progress bar
    """

    if not have_bayesian:
        raise NotImplementedError(
            "Bayesian tear sheet requirements not found.\n"
            "Run 'pip install pyfolio[bayesian]' to install "
            "bayesian requirements."
        )

    if live_start_date is None:
        raise NotImplementedError(
            'Bayesian tear sheet requires setting of live_start_date'
        )

    live_start_date = ep.utils.get_utc_timestamp(live_start_date)
    df_train = returns.loc[returns.index < live_start_date]
    df_test = returns.loc[returns.index >= live_start_date]

    # Run T model with missing data
    print("Running T model")
    previous_time = time()
    # track the total run time of the Bayesian tear sheet
    start_time = previous_time

    trace_t, ppc_t = bayesian.run_model('t', df_train,
                                        returns_test=df_test,
                                        samples=samples, ppc=True,
                                        progressbar=progressbar)
    previous_time = timer("T model", previous_time)

    # Compute BEST model
    print("\nRunning BEST model")
    trace_best = bayesian.run_model('best', df_train,
                                    returns_test=df_test,
                                    samples=samples,
                                    progressbar=progressbar)
    previous_time = timer("BEST model", previous_time)

    # Plot results

    fig = plt.figure(figsize=(14, 10 * 2))
    gs = gridspec.GridSpec(9, 2, wspace=0.3, hspace=0.3)

    axs = []
    row = 0

    # Plot Bayesian cone
    ax_cone = plt.subplot(gs[row, :])
    bayesian.plot_bayes_cone(df_train, df_test, ppc_t, ax=ax_cone)
    previous_time = timer("plotting Bayesian cone", previous_time)

    # Plot BEST results
    row += 1
    axs.append(plt.subplot(gs[row, 0]))
    axs.append(plt.subplot(gs[row, 1]))
    row += 1
    axs.append(plt.subplot(gs[row, 0]))
    axs.append(plt.subplot(gs[row, 1]))
    row += 1
    axs.append(plt.subplot(gs[row, 0]))
    axs.append(plt.subplot(gs[row, 1]))
    row += 1
    # Effect size across two
    axs.append(plt.subplot(gs[row, :]))

    bayesian.plot_best(trace=trace_best, axs=axs)
    previous_time = timer("plotting BEST results", previous_time)

    # Compute Bayesian predictions
    row += 1
    ax_ret_pred_day = plt.subplot(gs[row, 0])
    ax_ret_pred_week = plt.subplot(gs[row, 1])
    day_pred = ppc_t[:, 0]
    p5 = scipy.stats.scoreatpercentile(day_pred, 5)
    sns.distplot(day_pred,
                 ax=ax_ret_pred_day
                 )
    ax_ret_pred_day.axvline(p5, linestyle='--', linewidth=3.)
    ax_ret_pred_day.set_xlabel('Predicted returns 1 day')
    ax_ret_pred_day.set_ylabel('Frequency')
    ax_ret_pred_day.text(0.4, 0.9, 'Bayesian VaR = %.2f' % p5,
                         verticalalignment='bottom',
                         horizontalalignment='right',
                         transform=ax_ret_pred_day.transAxes)
    previous_time = timer("computing Bayesian predictions", previous_time)

    # Plot Bayesian VaRs
    week_pred = (
        np.cumprod(ppc_t[:, :5] + 1, 1) - 1)[:, -1]
    p5 = scipy.stats.scoreatpercentile(week_pred, 5)
    sns.distplot(week_pred,
                 ax=ax_ret_pred_week
                 )
    ax_ret_pred_week.axvline(p5, linestyle='--', linewidth=3.)
    ax_ret_pred_week.set_xlabel('Predicted cum returns 5 days')
    ax_ret_pred_week.set_ylabel('Frequency')
    ax_ret_pred_week.text(0.4, 0.9, 'Bayesian VaR = %.2f' % p5,
                          verticalalignment='bottom',
                          horizontalalignment='right',
                          transform=ax_ret_pred_week.transAxes)
    previous_time = timer("plotting Bayesian VaRs estimate", previous_time)

    # Run alpha beta model
    if benchmark_rets is not None:
        print("\nRunning alpha beta model")
        benchmark_rets = benchmark_rets.loc[df_train.index]
        trace_alpha_beta = bayesian.run_model('alpha_beta', df_train,
                                              bmark=benchmark_rets,
                                              samples=samples,
                                              progressbar=progressbar)
        previous_time = timer("running alpha beta model", previous_time)

        # Plot alpha and beta
        row += 1
        ax_alpha = plt.subplot(gs[row, 0])
        ax_beta = plt.subplot(gs[row, 1])
        sns.distplot((1 + trace_alpha_beta['alpha'][100:])**252 - 1,
                     ax=ax_alpha)
        sns.distplot(trace_alpha_beta['beta'][100:], ax=ax_beta)
        ax_alpha.set_xlabel('Annual Alpha')
        ax_alpha.set_ylabel('Belief')
        ax_beta.set_xlabel('Beta')
        ax_beta.set_ylabel('Belief')
        previous_time = timer("plotting alpha beta model", previous_time)

    if stoch_vol:
        # run stochastic volatility model
        returns_cutoff = 400
        print(
            "\nRunning stochastic volatility model on "
            "most recent {} days of returns.".format(returns_cutoff)
        )
        if df_train.size > returns_cutoff:
            df_train_truncated = df_train[-returns_cutoff:]
        _, trace_stoch_vol = bayesian.model_stoch_vol(df_train_truncated)
        previous_time = timer(
            "running stochastic volatility model", previous_time)

        # plot latent volatility
        row += 1
        ax_volatility = plt.subplot(gs[row, :])
        bayesian.plot_stoch_vol(
            df_train_truncated, trace=trace_stoch_vol, ax=ax_volatility)
        previous_time = timer(
            "plotting stochastic volatility model", previous_time)

    total_time = time() - start_time
    print("\nTotal runtime was {:.2f} seconds.".format(total_time))

    gs.tight_layout(fig)

    if return_fig:
        return fig
def create_bayesian_tear_sheet(returns, benchmark_rets=None,
                               live_start_date=None, samples=2000,
                               return_fig=False, stoch_vol=False,
                               progressbar=True):
    """
    Generate a number of Bayesian distributions and a Bayesian
    cone plot of returns.

    Plots: Sharpe distribution, annual volatility distribution,
    annual alpha distribution, beta distribution, predicted 1 and 5
    day returns distributions, and a cumulative returns cone plot.

    Parameters
    ----------
    returns : pd.Series
        Daily returns of the strategy, noncumulative.
         - See full explanation in create_full_tear_sheet.
    benchmark_rets : pd.Series, optional
        Daily noncumulative returns of the benchmark.
         - This is in the same style as returns.
    live_start_date : datetime, optional
        The point in time when the strategy began live
        trading, after its backtest period.
    samples : int, optional
        Number of posterior samples to draw.
    return_fig : boolean, optional
        If True, returns the figure that was plotted on.
    stoch_vol : boolean, optional
        If True, run and plot the stochastic volatility model
    progressbar : boolean, optional
        If True, show a progress bar
    """

    if not have_bayesian:
        raise NotImplementedError(
            "Bayesian tear sheet requirements not found.\n"
            "Run 'pip install pyfolio[bayesian]' to install "
            "bayesian requirements."
        )

    if live_start_date is None:
        raise NotImplementedError(
            'Bayesian tear sheet requires setting of live_start_date'
        )

    live_start_date = ep.utils.get_utc_timestamp(live_start_date)
    df_train = returns.loc[returns.index < live_start_date]
    df_test = returns.loc[returns.index >= live_start_date]

    # Run T model with missing data
    print("Running T model")
    previous_time = time()
    # track the total run time of the Bayesian tear sheet
    start_time = previous_time

    trace_t, ppc_t = bayesian.run_model('t', df_train,
                                        returns_test=df_test,
                                        samples=samples, ppc=True,
                                        progressbar=progressbar)
    previous_time = timer("T model", previous_time)

    # Compute BEST model
    print("\nRunning BEST model")
    trace_best = bayesian.run_model('best', df_train,
                                    returns_test=df_test,
                                    samples=samples,
                                    progressbar=progressbar)
    previous_time = timer("BEST model", previous_time)

    # Plot results

    fig = plt.figure(figsize=(14, 10 * 2))
    gs = gridspec.GridSpec(9, 2, wspace=0.3, hspace=0.3)

    axs = []
    row = 0

    # Plot Bayesian cone
    ax_cone = plt.subplot(gs[row, :])
    bayesian.plot_bayes_cone(df_train, df_test, ppc_t, ax=ax_cone)
    previous_time = timer("plotting Bayesian cone", previous_time)

    # Plot BEST results
    row += 1
    axs.append(plt.subplot(gs[row, 0]))
    axs.append(plt.subplot(gs[row, 1]))
    row += 1
    axs.append(plt.subplot(gs[row, 0]))
    axs.append(plt.subplot(gs[row, 1]))
    row += 1
    axs.append(plt.subplot(gs[row, 0]))
    axs.append(plt.subplot(gs[row, 1]))
    row += 1
    # Effect size across two
    axs.append(plt.subplot(gs[row, :]))

    bayesian.plot_best(trace=trace_best, axs=axs)
    previous_time = timer("plotting BEST results", previous_time)

    # Compute Bayesian predictions
    row += 1
    ax_ret_pred_day = plt.subplot(gs[row, 0])
    ax_ret_pred_week = plt.subplot(gs[row, 1])
    day_pred = ppc_t[:, 0]
    p5 = scipy.stats.scoreatpercentile(day_pred, 5)
    sns.distplot(day_pred,
                 ax=ax_ret_pred_day
                 )
    ax_ret_pred_day.axvline(p5, linestyle='--', linewidth=3.)
    ax_ret_pred_day.set_xlabel('Predicted returns 1 day')
    ax_ret_pred_day.set_ylabel('Frequency')
    ax_ret_pred_day.text(0.4, 0.9, 'Bayesian VaR = %.2f' % p5,
                         verticalalignment='bottom',
                         horizontalalignment='right',
                         transform=ax_ret_pred_day.transAxes)
    previous_time = timer("computing Bayesian predictions", previous_time)

    # Plot Bayesian VaRs
    week_pred = (
        np.cumprod(ppc_t[:, :5] + 1, 1) - 1)[:, -1]
    p5 = scipy.stats.scoreatpercentile(week_pred, 5)
    sns.distplot(week_pred,
                 ax=ax_ret_pred_week
                 )
    ax_ret_pred_week.axvline(p5, linestyle='--', linewidth=3.)
    ax_ret_pred_week.set_xlabel('Predicted cum returns 5 days')
    ax_ret_pred_week.set_ylabel('Frequency')
    ax_ret_pred_week.text(0.4, 0.9, 'Bayesian VaR = %.2f' % p5,
                          verticalalignment='bottom',
                          horizontalalignment='right',
                          transform=ax_ret_pred_week.transAxes)
    previous_time = timer("plotting Bayesian VaRs estimate", previous_time)

    # Run alpha beta model
    if benchmark_rets is not None:
        print("\nRunning alpha beta model")
        benchmark_rets = benchmark_rets.loc[df_train.index]
        trace_alpha_beta = bayesian.run_model('alpha_beta', df_train,
                                              bmark=benchmark_rets,
                                              samples=samples,
                                              progressbar=progressbar)
        previous_time = timer("running alpha beta model", previous_time)

        # Plot alpha and beta
        row += 1
        ax_alpha = plt.subplot(gs[row, 0])
        ax_beta = plt.subplot(gs[row, 1])
        sns.distplot((1 + trace_alpha_beta['alpha'][100:])**252 - 1,
                     ax=ax_alpha)
        sns.distplot(trace_alpha_beta['beta'][100:], ax=ax_beta)
        ax_alpha.set_xlabel('Annual Alpha')
        ax_alpha.set_ylabel('Belief')
        ax_beta.set_xlabel('Beta')
        ax_beta.set_ylabel('Belief')
        previous_time = timer("plotting alpha beta model", previous_time)

    if stoch_vol:
        # run stochastic volatility model
        returns_cutoff = 400
        print(
            "\nRunning stochastic volatility model on "
            "most recent {} days of returns.".format(returns_cutoff)
        )
        if df_train.size > returns_cutoff:
            df_train_truncated = df_train[-returns_cutoff:]
        _, trace_stoch_vol = bayesian.model_stoch_vol(df_train_truncated)
        previous_time = timer(
            "running stochastic volatility model", previous_time)

        # plot latent volatility
        row += 1
        ax_volatility = plt.subplot(gs[row, :])
        bayesian.plot_stoch_vol(
            df_train_truncated, trace=trace_stoch_vol, ax=ax_volatility)
        previous_time = timer(
            "plotting stochastic volatility model", previous_time)

    total_time = time() - start_time
    print("\nTotal runtime was {:.2f} seconds.".format(total_time))

    gs.tight_layout(fig)

    if return_fig:
        return fig
def create_bayesian_tear_sheet ( returns , benchmark_rets = None , live_start_date = None , samples = 2000 , return_fig = False , stoch_vol = False , progressbar = True ) : if not have_bayesian : raise NotImplementedError ( "Bayesian tear sheet requirements not found.\n" "Run 'pip install pyfolio[bayesian]' to install " "bayesian requirements." ) if live_start_date is None : raise NotImplementedError ( 'Bayesian tear sheet requires setting of live_start_date' ) live_start_date = ep . utils . get_utc_timestamp ( live_start_date ) df_train = returns . loc [ returns . index < live_start_date ] df_test = returns . loc [ returns . index >= live_start_date ] # Run T model with missing data print ( "Running T model" ) previous_time = time ( ) # track the total run time of the Bayesian tear sheet start_time = previous_time trace_t , ppc_t = bayesian . run_model ( 't' , df_train , returns_test = df_test , samples = samples , ppc = True , progressbar = progressbar ) previous_time = timer ( "T model" , previous_time ) # Compute BEST model print ( "\nRunning BEST model" ) trace_best = bayesian . run_model ( 'best' , df_train , returns_test = df_test , samples = samples , progressbar = progressbar ) previous_time = timer ( "BEST model" , previous_time ) # Plot results fig = plt . figure ( figsize = ( 14 , 10 * 2 ) ) gs = gridspec . GridSpec ( 9 , 2 , wspace = 0.3 , hspace = 0.3 ) axs = [ ] row = 0 # Plot Bayesian cone ax_cone = plt . subplot ( gs [ row , : ] ) bayesian . plot_bayes_cone ( df_train , df_test , ppc_t , ax = ax_cone ) previous_time = timer ( "plotting Bayesian cone" , previous_time ) # Plot BEST results row += 1 axs . append ( plt . subplot ( gs [ row , 0 ] ) ) axs . append ( plt . subplot ( gs [ row , 1 ] ) ) row += 1 axs . append ( plt . subplot ( gs [ row , 0 ] ) ) axs . append ( plt . subplot ( gs [ row , 1 ] ) ) row += 1 axs . append ( plt . subplot ( gs [ row , 0 ] ) ) axs . append ( plt . subplot ( gs [ row , 1 ] ) ) row += 1 # Effect size across two axs . append ( plt . subplot ( gs [ row , : ] ) ) bayesian . plot_best ( trace = trace_best , axs = axs ) previous_time = timer ( "plotting BEST results" , previous_time ) # Compute Bayesian predictions row += 1 ax_ret_pred_day = plt . subplot ( gs [ row , 0 ] ) ax_ret_pred_week = plt . subplot ( gs [ row , 1 ] ) day_pred = ppc_t [ : , 0 ] p5 = scipy . stats . scoreatpercentile ( day_pred , 5 ) sns . distplot ( day_pred , ax = ax_ret_pred_day ) ax_ret_pred_day . axvline ( p5 , linestyle = '--' , linewidth = 3. ) ax_ret_pred_day . set_xlabel ( 'Predicted returns 1 day' ) ax_ret_pred_day . set_ylabel ( 'Frequency' ) ax_ret_pred_day . text ( 0.4 , 0.9 , 'Bayesian VaR = %.2f' % p5 , verticalalignment = 'bottom' , horizontalalignment = 'right' , transform = ax_ret_pred_day . transAxes ) previous_time = timer ( "computing Bayesian predictions" , previous_time ) # Plot Bayesian VaRs week_pred = ( np . cumprod ( ppc_t [ : , : 5 ] + 1 , 1 ) - 1 ) [ : , - 1 ] p5 = scipy . stats . scoreatpercentile ( week_pred , 5 ) sns . distplot ( week_pred , ax = ax_ret_pred_week ) ax_ret_pred_week . axvline ( p5 , linestyle = '--' , linewidth = 3. ) ax_ret_pred_week . set_xlabel ( 'Predicted cum returns 5 days' ) ax_ret_pred_week . set_ylabel ( 'Frequency' ) ax_ret_pred_week . text ( 0.4 , 0.9 , 'Bayesian VaR = %.2f' % p5 , verticalalignment = 'bottom' , horizontalalignment = 'right' , transform = ax_ret_pred_week . transAxes ) previous_time = timer ( "plotting Bayesian VaRs estimate" , previous_time ) # Run alpha beta model if benchmark_rets is not None : print ( "\nRunning alpha beta model" ) benchmark_rets = benchmark_rets . loc [ df_train . index ] trace_alpha_beta = bayesian . run_model ( 'alpha_beta' , df_train , bmark = benchmark_rets , samples = samples , progressbar = progressbar ) previous_time = timer ( "running alpha beta model" , previous_time ) # Plot alpha and beta row += 1 ax_alpha = plt . subplot ( gs [ row , 0 ] ) ax_beta = plt . subplot ( gs [ row , 1 ] ) sns . distplot ( ( 1 + trace_alpha_beta [ 'alpha' ] [ 100 : ] ) ** 252 - 1 , ax = ax_alpha ) sns . distplot ( trace_alpha_beta [ 'beta' ] [ 100 : ] , ax = ax_beta ) ax_alpha . set_xlabel ( 'Annual Alpha' ) ax_alpha . set_ylabel ( 'Belief' ) ax_beta . set_xlabel ( 'Beta' ) ax_beta . set_ylabel ( 'Belief' ) previous_time = timer ( "plotting alpha beta model" , previous_time ) if stoch_vol : # run stochastic volatility model returns_cutoff = 400 print ( "\nRunning stochastic volatility model on " "most recent {} days of returns." . format ( returns_cutoff ) ) if df_train . size > returns_cutoff : df_train_truncated = df_train [ - returns_cutoff : ] _ , trace_stoch_vol = bayesian . model_stoch_vol ( df_train_truncated ) previous_time = timer ( "running stochastic volatility model" , previous_time ) # plot latent volatility row += 1 ax_volatility = plt . subplot ( gs [ row , : ] ) bayesian . plot_stoch_vol ( df_train_truncated , trace = trace_stoch_vol , ax = ax_volatility ) previous_time = timer ( "plotting stochastic volatility model" , previous_time ) total_time = time ( ) - start_time print ( "\nTotal runtime was {:.2f} seconds." . format ( total_time ) ) gs . tight_layout ( fig ) if return_fig : return fig
Generate a number of Bayesian distributions and a Bayesian
    cone plot of returns.

    Plots: Sharpe distribution, annual volatility distribution,
    annual alpha distribution, beta distribution, predicted 1 and 5
    day returns distributions, and a cumulative returns cone plot.

    Parameters
    ----------
    returns : pd.Series
        Daily returns of the strategy, noncumulative.
         - See full explanation in create_full_tear_sheet.
    benchmark_rets : pd.Series, optional
        Daily noncumulative returns of the benchmark.
         - This is in the same style as returns.
    live_start_date : datetime, optional
        The point in time when the strategy began live
        trading, after its backtest period.
    samples : int, optional
        Number of posterior samples to draw.
    return_fig : boolean, optional
        If True, returns the figure that was plotted on.
    stoch_vol : boolean, optional
        If True, run and plot the stochastic volatility model
    progressbar : boolean, optional
        If True, show a progress bar
Generate a number of Bayesian distributions and a Bayesian cone plot of returns .