{"id":6186,"date":"2024-01-23T14:08:35","date_gmt":"2024-01-23T21:08:35","guid":{"rendered":"https:\/\/jeremywhittaker.com\/?p=6186"},"modified":"2024-01-23T14:08:40","modified_gmt":"2024-01-23T21:08:40","slug":"modifying-pyfolio-to-output-to-html","status":"publish","type":"post","link":"https:\/\/new.jeremywhittaker.com\/index.php\/2024\/01\/23\/modifying-pyfolio-to-output-to-html\/","title":{"rendered":"Modifying PyFolio to output to HTML"},"content":{"rendered":"\n<p>Recently I was following a paper and in the example they used&nbsp;<a href=\"https:\/\/github.com\/quantopian\/pyfolio\">Pyfolio&nbsp;<\/a>which is an awesome performance and risk analysis library in Python developed by Quantopian Inc when they were still around. Given that Quantopian is no longer around nobody is maintaining this library. I ran into a few errors and figured I would outline the solutions below in case anyone has these issues. But before I dive too deep into modifying this library you may be better off just uninstalling Pyfolio and loading<a href=\"https:\/\/github.com\/stefan-jansen\/pyfolio-reloaded\">&nbsp;Pyfolio-reloaded<\/a>. But that is not the purpose of this article. <\/p>\n\n\n\n<p>Today I want to discuss the output of Pyfolio. It was written to output in a Jupyter Notebook, which no real programmer uses. Then if you output it to the console the format is horrible and all over the place. So I ended up rewriting some of the Pyfolio files so that when you run <em>create_full_tear_sheet() <\/em>it will generate an HTML file for later analysis. Here is a sample of the output. <\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Sample Output<\/h2>\n\n\n\n<style>\n    .responsive-iframe-container {\n        position: relative;\n        overflow: hidden;\n        padding-top: 56.25%; \/* Aspect Ratio 16:9 *\/\n    }\n    .responsive-iframe-container iframe {\n        position: absolute;\n        top: 0;\n        left: 0;\n        width: 100%;\n        height: 100%;\n        border: none;\n    }\n<\/style>\n\n<div class=\"responsive-iframe-container\">\n    <iframe data-src=\"https:\/\/new.jeremywhittaker.com\/plots\/misc\/full_tearsheet.html\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" class=\"lazyload\" data-load-mode=\"1\"><\/iframe>\n<\/div>\n\n\n\n<h2 class=\"wp-block-heading\">tears.py<\/h2>\n\n\n\n<p><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#\n# Copyright 2019 Quantopian, Inc.\n#\n# Licensed under the Apache License, Version 2.0 (the \"License\");\n# you may not use this file except in compliance with the License.\n# You may obtain a copy of the License at\n#\n#     http:\/\/www.apache.org\/licenses\/LICENSE-2.0\n#\n# Unless required by applicable law or agreed to in writing, software\n# distributed under the License is distributed on an \"AS IS\" BASIS,\n# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n# See the License for the specific language governing permissions and\n# limitations under the License.\nimport warnings\nfrom time import time\n\nimport empyrical as ep\nimport matplotlib.gridspec as gridspec\nimport matplotlib.pyplot as plt\nimport pandas as pd\nimport seaborn as sns\nfrom IPython.display import display, Markdown\nimport os\nimport glob\nimport datetime\nimport base64\n\nfrom . import capacity\nfrom . import perf_attrib\nfrom . import plotting\nfrom . import pos\nfrom . import round_trips\nfrom . import timeseries\nfrom . import txn\nfrom . import utils\nimport logging\n# Configure logging\nlogging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')\n\nFACTOR_PARTITIONS = {\n    \"style\": &#91;\n        \"momentum\",\n        \"size\",\n        \"value\",\n        \"reversal_short_term\",\n        \"volatility\",\n    ],\n    \"sector\": &#91;\n        \"basic_materials\",\n        \"consumer_cyclical\",\n        \"financial_services\",\n        \"real_estate\",\n        \"consumer_defensive\",\n        \"health_care\",\n        \"utilities\",\n        \"communication_services\",\n        \"energy\",\n        \"industrials\",\n        \"technology\",\n    ],\n}\n\ndef save_plot(fig_or_ax, plot_name, directory=\".\/plots\/temp\"):\n    \"\"\"Save a matplotlib figure or axes object.\"\"\"\n    logging.info(f'Saving {plot_name} to {directory}')\n    if not os.path.exists(directory):\n        os.makedirs(directory)\n    filepath = os.path.join(directory, f\"{plot_name}.png\")\n    if isinstance(fig_or_ax, plt.Figure):\n        fig_or_ax.savefig(filepath)\n    else:\n        fig_or_ax.get_figure().savefig(filepath)\n    print(f\"Plot saved: {filepath}\")\n\ndef timer(msg_body, previous_time):\n    current_time = time()\n    run_time = current_time - previous_time\n    message = \"\\nFinished \" + msg_body + \" (required {:.2f} seconds).\"\n    print(message.format(run_time))\n\n    return current_time\n\n\ndef create_full_tear_sheet(\n    returns,\n    positions=None,\n    transactions=None,\n    market_data=None,\n    benchmark_rets=None,\n    slippage=None,\n    live_start_date=None,\n    sector_mappings=None,\n    round_trips=False,\n    estimate_intraday=\"infer\",\n    hide_positions=False,\n    cone_std=(1.0, 1.5, 2.0),\n    bootstrap=False,\n    unadjusted_returns=None,\n    turnover_denom=\"AGB\",\n    set_context=True,\n    factor_returns=None,\n    factor_loadings=None,\n    pos_in_dollars=True,\n    header_rows=None,\n    factor_partitions=FACTOR_PARTITIONS,\n):\n    \"\"\"\n    Generate a number of tear sheets that are useful\n    for analyzing a strategy's performance.\n\n    - Fetches benchmarks if needed.\n    - Creates tear sheets for returns, and significant events.\n        If possible, also creates tear sheets for position analysis\n        and transaction analysis.\n\n    Parameters\n    ----------\n    returns : pd.Series\n        Daily returns of the strategy, noncumulative.\n         - Time series with decimal returns.\n         - Example:\n            2015-07-16    -0.012143\n            2015-07-17    0.045350\n            2015-07-20    0.030957\n            2015-07-21    0.004902\n    positions : pd.DataFrame, optional\n        Daily net position values.\n         - Time series of dollar amount invested in each position and cash.\n         - Days where stocks are not held can be represented by 0 or NaN.\n         - Non-working capital is labelled 'cash'\n         - Example:\n            index         'AAPL'         'MSFT'          cash\n            2004-01-09    13939.3800     -14012.9930     711.5585\n            2004-01-12    14492.6300     -14624.8700     27.1821\n            2004-01-13    -13853.2800    13653.6400      -43.6375\n    transactions : pd.DataFrame, optional\n        Executed trade volumes and fill prices.\n        - One row per trade.\n        - Trades on different names that occur at the\n          same time will have identical indicies.\n        - Example:\n            index                  amount   price    symbol\n            2004-01-09 12:18:01    483      324.12   'AAPL'\n            2004-01-09 12:18:01    122      83.10    'MSFT'\n            2004-01-13 14:12:23    -75      340.43   'AAPL'\n    market_data : pd.DataFrame, optional\n        Daily market_data\n        - DataFrame has a multi-index index, one level is dates and another is\n        market_data contains volume &amp; price, equities as columns\n    slippage : int\/float, optional\n        Basis points of slippage to apply to returns before generating\n        tearsheet stats and plots.\n        If a value is provided, slippage parameter sweep\n        plots will be generated from the unadjusted returns.\n        Transactions and positions must also be passed.\n        - See txn.adjust_returns_for_slippage for more details.\n    live_start_date : datetime, optional\n        The point in time when the strategy began live trading,\n        after its backtest period. This datetime should be normalized.\n    hide_positions : bool, optional\n        If True, will not output any symbol names.\n    round_trips: boolean, optional\n        If True, causes the generation of a round trip tear sheet.\n    sector_mappings : dict or pd.Series, optional\n        Security identifier to sector mapping.\n        Security ids as keys, sectors as values.\n    estimate_intraday: boolean or str, optional\n        Instead of using the end-of-day positions, use the point in the day\n        where we have the most $ invested. This will adjust positions to\n        better approximate and represent how an intraday strategy behaves.\n        By default, this is 'infer', and an attempt will be made to detect\n        an intraday strategy. Specifying this value will prevent detection.\n    cone_std : float, or tuple, optional\n        If float, The standard deviation to use for the cone plots.\n        If tuple, Tuple of standard deviation values to use for the cone plots\n         - The cone is a normal distribution with this standard deviation\n             centered around a linear regression.\n    bootstrap : boolean (optional)\n        Whether to perform bootstrap analysis for the performance\n        metrics. Takes a few minutes longer.\n    turnover_denom : str\n        Either AGB or portfolio_value, default AGB.\n        - See full explanation in txn.get_turnover.\n    factor_returns : pd.Dataframe, optional\n        Returns by factor, with date as index and factors as columns\n    factor_loadings : pd.Dataframe, optional\n        Factor loadings for all days in the date range, with date and\n        ticker as index, and factors as columns.\n    pos_in_dollars : boolean, optional\n        indicates whether positions is in dollars\n    header_rows : dict or OrderedDict, optional\n        Extra rows to display at the top of the perf stats table.\n    set_context : boolean, optional\n        If True, set default plotting style context.\n         - See plotting.context().\n    factor_partitions : dict, optional\n        dict specifying how factors should be separated in perf attrib\n        factor returns and risk exposures plots\n        - See create_perf_attrib_tear_sheet().\n    \"\"\"\n\n    if (\n        (unadjusted_returns is None)\n        and (slippage is not None)\n        and (transactions is not None)\n    ):\n        unadjusted_returns = returns.copy()\n        returns = txn.adjust_returns_for_slippage(\n            returns, positions, transactions, slippage\n        )\n\n    positions = utils.check_intraday(\n        estimate_intraday, returns, positions, transactions\n    )\n\n    create_returns_tear_sheet(\n        returns,\n        positions=positions,\n        transactions=transactions,\n        live_start_date=live_start_date,\n        cone_std=cone_std,\n        benchmark_rets=benchmark_rets,\n        bootstrap=bootstrap,\n        turnover_denom=turnover_denom,\n        header_rows=header_rows,\n        set_context=set_context,\n    )\n\n    create_interesting_times_tear_sheet(\n        returns, benchmark_rets=benchmark_rets, set_context=set_context\n    )\n\n    if positions is not None:\n        create_position_tear_sheet(\n            returns,\n            positions,\n            hide_positions=hide_positions,\n            set_context=set_context,\n            sector_mappings=sector_mappings,\n            estimate_intraday=False,\n        )\n\n        if transactions is not None:\n            create_txn_tear_sheet(\n                returns,\n                positions,\n                transactions,\n                unadjusted_returns=unadjusted_returns,\n                estimate_intraday=False,\n                set_context=set_context,\n            )\n            if round_trips:\n                create_round_trip_tear_sheet(\n                    returns=returns,\n                    positions=positions,\n                    transactions=transactions,\n                    sector_mappings=sector_mappings,\n                    estimate_intraday=False,\n                )\n\n            if market_data is not None:\n                create_capacity_tear_sheet(\n                    returns,\n                    positions,\n                    transactions,\n                    market_data,\n                    liquidation_daily_vol_limit=0.2,\n                    last_n_days=125,\n                    estimate_intraday=False,\n                )\n\n        if factor_returns is not None and factor_loadings is not None:\n            create_perf_attrib_tear_sheet(\n                returns,\n                positions,\n                factor_returns,\n                factor_loadings,\n                transactions,\n                pos_in_dollars=pos_in_dollars,\n                factor_partitions=factor_partitions,\n            )\n            \n    # Location where the individual HTML files are saved\n    html_files_dir = '.\/plots\/temp'\n    plots_dir = '.\/plots\/temp'  # Directory where PNG files are saved\n\n    # Aggregate HTML content from tables\n    aggregated_html_content = ''\n    for html_file in glob.glob(os.path.join(html_files_dir, \"*.html\")):\n        with open(html_file, 'r') as file:\n            aggregated_html_content += file.read() + '&lt;br&gt;&lt;hr&gt;&lt;br&gt;'\n\n    # Embed PNG files into HTML content\n    for png_file in glob.glob(os.path.join(plots_dir, \"*.png\")):\n        with open(png_file, \"rb\") as image_file:\n            encoded_string = base64.b64encode(image_file.read()).decode()\n            img_tag = f'&lt;img src=\"data:image\/png;base64,{encoded_string}\" style=\"width:100%\"&gt;&lt;br&gt;&lt;hr&gt;&lt;br&gt;'\n            aggregated_html_content += img_tag\n\n    # Save the aggregated content to a new HTML file\n    timestamp = datetime.datetime.now().strftime(\"%Y%m%d_%H%M%S%f\")\n    aggregated_filename = f\"full_tearsheet.html\"\n    aggregated_file_path = os.path.join('.\/plots', aggregated_filename)\n    with open(aggregated_file_path, 'w') as file:\n        file.write(aggregated_html_content)\n    logging.info(f\"Aggregated tearsheet saved to {aggregated_file_path}\")\n\n    # Delete individual HTML files to avoid duplication in the future\n    for html_file in glob.glob(os.path.join(html_files_dir, \"*.html\")):\n        os.remove(html_file)\n\n@plotting.customize\ndef create_simple_tear_sheet(\n    returns,\n    positions=None,\n    transactions=None,\n    benchmark_rets=None,\n    slippage=None,\n    estimate_intraday=\"infer\",\n    live_start_date=None,\n    turnover_denom=\"AGB\",\n    header_rows=None,\n):\n    \"\"\"\n    Simpler version of create_full_tear_sheet; generates summary performance\n    statistics and important plots as a single image.\n\n    - Plots: cumulative returns, rolling beta, rolling Sharpe, underwater,\n        exposure, top 10 holdings, total holdings, long\/short holdings,\n        daily turnover, transaction time distribution.\n    - Never accept market_data input (market_data = None)\n    - Never accept sector_mappings input (sector_mappings = None)\n    - Never perform bootstrap analysis (bootstrap = False)\n    - Never hide posistions on top 10 holdings plot (hide_positions = False)\n    - Always use default cone_std (cone_std = (1.0, 1.5, 2.0))\n\n    Parameters\n    ----------\n    returns : pd.Series\n        Daily returns of the strategy, noncumulative.\n         - Time series with decimal returns.\n         - Example:\n            2015-07-16    -0.012143\n            2015-07-17    0.045350\n            2015-07-20    0.030957\n            2015-07-21    0.004902\n    positions : pd.DataFrame, optional\n        Daily net position values.\n         - Time series of dollar amount invested in each position and cash.\n         - Days where stocks are not held can be represented by 0 or NaN.\n         - Non-working capital is labelled 'cash'\n         - Example:\n            index         'AAPL'         'MSFT'          cash\n            2004-01-09    13939.3800     -14012.9930     711.5585\n            2004-01-12    14492.6300     -14624.8700     27.1821\n            2004-01-13    -13853.2800    13653.6400      -43.6375\n    transactions : pd.DataFrame, optional\n        Executed trade volumes and fill prices.\n        - One row per trade.\n        - Trades on different names that occur at the\n          same time will have identical indicies.\n        - Example:\n            index                  amount   price    symbol\n            2004-01-09 12:18:01    483      324.12   'AAPL'\n            2004-01-09 12:18:01    122      83.10    'MSFT'\n            2004-01-13 14:12:23    -75      340.43   'AAPL'\n    benchmark_rets : pd.Series, optional\n        Daily returns of the benchmark, noncumulative.\n    slippage : int\/float, optional\n        Basis points of slippage to apply to returns before generating\n        tearsheet stats and plots.\n        If a value is provided, slippage parameter sweep\n        plots will be generated from the unadjusted returns.\n        Transactions and positions must also be passed.\n        - See txn.adjust_returns_for_slippage for more details.\n    live_start_date : datetime, optional\n        The point in time when the strategy began live trading,\n        after its backtest period. This datetime should be normalized.\n    turnover_denom : str, optional\n        Either AGB or portfolio_value, default AGB.\n        - See full explanation in txn.get_turnover.\n    header_rows : dict or OrderedDict, optional\n        Extra rows to display at the top of the perf stats table.\n    set_context : boolean, optional\n        If True, set default plotting style context.\n    \"\"\"\n\n    positions = utils.check_intraday(\n        estimate_intraday, returns, positions, transactions\n    )\n\n    if (slippage is not None) and (transactions is not None):\n        returns = txn.adjust_returns_for_slippage(\n            returns, positions, transactions, slippage\n        )\n\n    always_sections = 4\n    positions_sections = 4 if positions is not None else 0\n    transactions_sections = 2 if transactions is not None else 0\n    live_sections = 1 if live_start_date is not None else 0\n    benchmark_sections = 1 if benchmark_rets is not None else 0\n\n    vertical_sections = sum(\n        &#91;\n            always_sections,\n            positions_sections,\n            transactions_sections,\n            live_sections,\n            benchmark_sections,\n        ]\n    )\n\n    if live_start_date is not None:\n        live_start_date = ep.utils.get_utc_timestamp(live_start_date)\n\n    plotting.show_perf_stats(\n        returns,\n        benchmark_rets,\n        positions=positions,\n        transactions=transactions,\n        turnover_denom=turnover_denom,\n        live_start_date=live_start_date,\n        header_rows=header_rows,\n    )\n\n    fig = plt.figure(figsize=(14, vertical_sections * 6))\n    gs = gridspec.GridSpec(vertical_sections, 3, wspace=0.5, hspace=0.5)\n\n    ax_rolling_returns = plt.subplot(gs&#91;:2, :])\n    i = 2\n    if benchmark_rets is not None:\n        ax_rolling_beta = plt.subplot(gs&#91;i, :], sharex=ax_rolling_returns)\n        i += 1\n    ax_rolling_sharpe = plt.subplot(gs&#91;i, :], sharex=ax_rolling_returns)\n    i += 1\n    ax_underwater = plt.subplot(gs&#91;i, :], sharex=ax_rolling_returns)\n    i += 1\n\n    plotting.plot_rolling_returns(\n        returns,\n        factor_returns=benchmark_rets,\n        live_start_date=live_start_date,\n        cone_std=(1.0, 1.5, 2.0),\n        ax=ax_rolling_returns,\n    )\n    ax_rolling_returns.set_title(\"Cumulative returns\")\n\n    if benchmark_rets is not None:\n        plotting.plot_rolling_beta(returns, benchmark_rets, ax=ax_rolling_beta)\n\n    plotting.plot_rolling_sharpe(returns, ax=ax_rolling_sharpe)\n\n    plotting.plot_drawdown_underwater(returns, ax=ax_underwater)\n\n    if positions is not None:\n        # Plot simple positions tear sheet\n        ax_exposures = plt.subplot(gs&#91;i, :])\n        i += 1\n        ax_top_positions = plt.subplot(gs&#91;i, :], sharex=ax_exposures)\n        i += 1\n        ax_holdings = plt.subplot(gs&#91;i, :], sharex=ax_exposures)\n        i += 1\n        ax_long_short_holdings = plt.subplot(gs&#91;i, :])\n        i += 1\n\n        positions_alloc = pos.get_percent_alloc(positions)\n\n        plotting.plot_exposures(returns, positions, ax=ax_exposures)\n\n        plotting.show_and_plot_top_positions(\n            returns,\n            positions_alloc,\n            show_and_plot=0,\n            hide_positions=False,\n            ax=ax_top_positions,\n        )\n\n        plotting.plot_holdings(returns, positions_alloc, ax=ax_holdings)\n\n        plotting.plot_long_short_holdings(\n            returns, positions_alloc, ax=ax_long_short_holdings\n        )\n\n        if transactions is not None:\n            # Plot simple transactions tear sheet\n            ax_turnover = plt.subplot(gs&#91;i, :])\n            i += 1\n            ax_txn_timings = plt.subplot(gs&#91;i, :])\n            i += 1\n\n            plotting.plot_turnover(\n                returns,\n                transactions,\n                positions,\n                turnover_denom=turnover_denom,\n                ax=ax_turnover,\n            )\n\n            plotting.plot_txn_time_hist(transactions, ax=ax_txn_timings)\n\n    for ax in fig.axes:\n        ax.tick_params(\n            axis=\"x\",\n            which=\"major\",\n            bottom=True,\n            top=False,\n            labelbottom=True,\n        )\n\n\n@plotting.customize\ndef create_returns_tear_sheet(\n    returns,\n    positions=None,\n    transactions=None,\n    live_start_date=None,\n    cone_std=(1.0, 1.5, 2.0),\n    benchmark_rets=None,\n    bootstrap=False,\n    turnover_denom=\"AGB\",\n    header_rows=None,\n    return_fig=False,\n):\n    \"\"\"\n    Generate a number of plots for analyzing a strategy's returns.\n\n    - Fetches benchmarks, then creates the plots on a single figure.\n    - Plots: rolling returns (with cone), rolling beta, rolling sharpe,\n        rolling Fama-French risk factors, drawdowns, underwater plot, monthly\n        and annual return plots, daily similarity plots,\n        and return quantile box plot.\n    - Will also print the start and end dates of the strategy,\n        performance statistics, drawdown periods, and the return range.\n\n    Parameters\n    ----------\n    returns : pd.Series\n        Daily returns of the strategy, noncumulative.\n         - See full explanation in create_full_tear_sheet.\n    positions : pd.DataFrame, optional\n        Daily net position values.\n         - See full explanation in create_full_tear_sheet.\n    transactions : pd.DataFrame, optional\n        Executed trade volumes and fill prices.\n        - See full explanation in create_full_tear_sheet.\n    live_start_date : datetime, optional\n        The point in time when the strategy began live trading,\n        after its backtest period.\n    cone_std : float, or tuple, optional\n        If float, The standard deviation to use for the cone plots.\n        If tuple, Tuple of standard deviation values to use for the cone plots\n         - The cone is a normal distribution with this standard deviation\n             centered around a linear regression.\n    benchmark_rets : pd.Series, optional\n        Daily noncumulative returns of the benchmark.\n         - This is in the same style as returns.\n    bootstrap : boolean, optional\n        Whether to perform bootstrap analysis for the performance\n        metrics. Takes a few minutes longer.\n    turnover_denom : str, optional\n        Either AGB or portfolio_value, default AGB.\n        - See full explanation in txn.get_turnover.\n    header_rows : dict or OrderedDict, optional\n        Extra rows to display at the top of the perf stats table.\n    return_fig : boolean, optional\n        If True, returns the figure that was plotted on.\n    \"\"\"\n    if benchmark_rets is not None:\n        returns = utils.clip_returns_to_benchmark(returns, benchmark_rets)\n\n    plotting.show_perf_stats(\n        returns,\n        benchmark_rets,\n        positions=positions,\n        transactions=transactions,\n        turnover_denom=turnover_denom,\n        bootstrap=bootstrap,\n        live_start_date=live_start_date,\n        header_rows=header_rows,\n    )\n\n    plotting.show_worst_drawdown_periods(returns)\n\n    vertical_sections = 11\n\n    if live_start_date is not None:\n        vertical_sections += 1\n        live_start_date = ep.utils.get_utc_timestamp(live_start_date)\n\n    if benchmark_rets is not None:\n        vertical_sections += 1\n\n    if bootstrap:\n        vertical_sections += 1\n\n    fig = plt.figure(figsize=(14, vertical_sections * 6))\n    gs = gridspec.GridSpec(vertical_sections, 3, wspace=0.5, hspace=0.5)\n    ax_rolling_returns = plt.subplot(gs&#91;:2, :])\n\n    i = 2\n    ax_rolling_returns_vol_match = plt.subplot(gs&#91;i, :], sharex=ax_rolling_returns)\n    i += 1\n    ax_rolling_returns_log = plt.subplot(gs&#91;i, :], sharex=ax_rolling_returns)\n    i += 1\n    ax_returns = plt.subplot(gs&#91;i, :], sharex=ax_rolling_returns)\n    i += 1\n    if benchmark_rets is not None:\n        ax_rolling_beta = plt.subplot(gs&#91;i, :], sharex=ax_rolling_returns)\n        i += 1\n    ax_rolling_volatility = plt.subplot(gs&#91;i, :], sharex=ax_rolling_returns)\n    i += 1\n    ax_rolling_sharpe = plt.subplot(gs&#91;i, :], sharex=ax_rolling_returns)\n    i += 1\n    ax_drawdown = plt.subplot(gs&#91;i, :], sharex=ax_rolling_returns)\n    i += 1\n    ax_underwater = plt.subplot(gs&#91;i, :], sharex=ax_rolling_returns)\n    i += 1\n    ax_monthly_heatmap = plt.subplot(gs&#91;i, 0])\n    ax_annual_returns = plt.subplot(gs&#91;i, 1])\n    ax_monthly_dist = plt.subplot(gs&#91;i, 2])\n    i += 1\n    ax_return_quantiles = plt.subplot(gs&#91;i, :])\n    i += 1\n\n    plotting.plot_rolling_returns(\n        returns,\n        factor_returns=benchmark_rets,\n        live_start_date=live_start_date,\n        cone_std=cone_std,\n        ax=ax_rolling_returns,\n    )\n    ax_rolling_returns.set_title(\"Cumulative returns\")\n\n    plotting.plot_rolling_returns(\n        returns,\n        factor_returns=benchmark_rets,\n        live_start_date=live_start_date,\n        cone_std=None,\n        volatility_match=(benchmark_rets is not None),\n        legend_loc=None,\n        ax=ax_rolling_returns_vol_match,\n    )\n    ax_rolling_returns_vol_match.set_title(\n        \"Cumulative returns volatility matched to benchmark\"\n    )\n\n    plotting.plot_rolling_returns(\n        returns,\n        factor_returns=benchmark_rets,\n        logy=True,\n        live_start_date=live_start_date,\n        cone_std=cone_std,\n        ax=ax_rolling_returns_log,\n    )\n    ax_rolling_returns_log.set_title(\"Cumulative returns on logarithmic scale\")\n\n    plotting.plot_returns(\n        returns,\n        live_start_date=live_start_date,\n        ax=ax_returns,\n    )\n    ax_returns.set_title(\"Returns\")\n\n    if benchmark_rets is not None:\n        plotting.plot_rolling_beta(returns, benchmark_rets, ax=ax_rolling_beta)\n\n    plotting.plot_rolling_volatility(\n        returns, factor_returns=benchmark_rets, ax=ax_rolling_volatility\n    )\n\n    plotting.plot_rolling_sharpe(returns, ax=ax_rolling_sharpe)\n\n    # Drawdowns\n    plotting.plot_drawdown_periods(returns, top=5, ax=ax_drawdown)\n\n    plotting.plot_drawdown_underwater(returns=returns, ax=ax_underwater)\n\n    plotting.plot_monthly_returns_heatmap(returns, ax=ax_monthly_heatmap)\n    plotting.plot_annual_returns(returns, ax=ax_annual_returns)\n    plotting.plot_monthly_returns_dist(returns, ax=ax_monthly_dist)\n\n    plotting.plot_return_quantiles(\n        returns, live_start_date=live_start_date, ax=ax_return_quantiles\n    )\n\n    if bootstrap and (benchmark_rets is not None):\n        ax_bootstrap = plt.subplot(gs&#91;i, :])\n        plotting.plot_perf_stats(returns, benchmark_rets, ax=ax_bootstrap)\n    elif bootstrap:\n        raise ValueError(\"bootstrap requires passing of benchmark_rets.\")\n\n    for ax in fig.axes:\n        ax.tick_params(\n            axis=\"x\",\n            which=\"major\",\n            bottom=True,\n            top=False,\n            labelbottom=True,\n        )\n\n    save_plot(fig,'Full Tear Sheet')\n    \n    if return_fig:\n        return fig\n\n\n@plotting.customize\ndef create_position_tear_sheet(\n    returns,\n    positions,\n    show_and_plot_top_pos=2,\n    hide_positions=False,\n    sector_mappings=None,\n    transactions=None,\n    estimate_intraday=\"infer\",\n    return_fig=False,\n):\n    \"\"\"\n    Generate a number of plots for analyzing a\n    strategy's positions and holdings.\n\n    - Plots: gross leverage, exposures, top positions, and holdings.\n    - Will also print the top positions held.\n\n    Parameters\n    ----------\n    returns : pd.Series\n        Daily returns of the strategy, noncumulative.\n         - See full explanation in create_full_tear_sheet.\n    positions : pd.DataFrame\n        Daily net position values.\n         - See full explanation in create_full_tear_sheet.\n    show_and_plot_top_pos : int, optional\n        By default, this is 2, and both prints and plots the\n        top 10 positions.\n        If this is 0, it will only plot; if 1, it will only print.\n    hide_positions : bool, optional\n        If True, will not output any symbol names.\n        Overrides show_and_plot_top_pos to 0 to suppress text output.\n    sector_mappings : dict or pd.Series, optional\n        Security identifier to sector mapping.\n        Security ids as keys, sectors as values.\n    transactions : pd.DataFrame, optional\n        Prices and amounts of executed trades. One row per trade.\n         - See full explanation in create_full_tear_sheet.\n    estimate_intraday: boolean or str, optional\n        Approximate returns for intraday strategies.\n        See description in create_full_tear_sheet.\n    return_fig : boolean, optional\n        If True, returns the figure that was plotted on.\n    \"\"\"\n\n    positions = utils.check_intraday(\n        estimate_intraday, returns, positions, transactions\n    )\n\n    if hide_positions:\n        show_and_plot_top_pos = 0\n    vertical_sections = 7 if sector_mappings is not None else 6\n\n    fig = plt.figure(figsize=(14, vertical_sections * 6))\n    gs = gridspec.GridSpec(vertical_sections, 3, wspace=0.5, hspace=0.5)\n    ax_exposures = plt.subplot(gs&#91;0, :])\n    ax_top_positions = plt.subplot(gs&#91;1, :], sharex=ax_exposures)\n    ax_max_median_pos = plt.subplot(gs&#91;2, :], sharex=ax_exposures)\n    ax_holdings = plt.subplot(gs&#91;3, :], sharex=ax_exposures)\n    ax_long_short_holdings = plt.subplot(gs&#91;4, :])\n    ax_gross_leverage = plt.subplot(gs&#91;5, :], sharex=ax_exposures)\n\n    positions_alloc = pos.get_percent_alloc(positions)\n\n    plotting.plot_exposures(returns, positions, ax=ax_exposures)\n\n    plotting.show_and_plot_top_positions(\n        returns,\n        positions_alloc,\n        show_and_plot=show_and_plot_top_pos,\n        hide_positions=hide_positions,\n        ax=ax_top_positions,\n    )\n\n    plotting.plot_max_median_position_concentration(positions, ax=ax_max_median_pos)\n\n    plotting.plot_holdings(returns, positions_alloc, ax=ax_holdings)\n\n    plotting.plot_long_short_holdings(\n        returns, positions_alloc, ax=ax_long_short_holdings\n    )\n\n    plotting.plot_gross_leverage(returns, positions, ax=ax_gross_leverage)\n\n    if sector_mappings is not None:\n        sector_exposures = pos.get_sector_exposures(positions, sector_mappings)\n        if len(sector_exposures.columns) &gt; 1:\n            sector_alloc = pos.get_percent_alloc(sector_exposures)\n            sector_alloc = sector_alloc.drop(\"cash\", axis=\"columns\")\n            ax_sector_alloc = plt.subplot(gs&#91;6, :], sharex=ax_exposures)\n            plotting.plot_sector_allocations(returns, sector_alloc, ax=ax_sector_alloc)\n\n    for ax in fig.axes:\n        ax.tick_params(\n            axis=\"x\",\n            which=\"major\",\n            bottom=True,\n            top=False,\n            labelbottom=True,\n        )\n\n    save_plot(fig,'Position Tear Sheet')\n\n    if return_fig:\n        return fig\n\n\n@plotting.customize\ndef create_txn_tear_sheet(\n    returns,\n    positions,\n    transactions,\n    turnover_denom=\"AGB\",\n    unadjusted_returns=None,\n    estimate_intraday=\"infer\",\n    return_fig=False,\n):\n    \"\"\"\n    Generate a number of plots for analyzing a strategy's transactions.\n\n    Plots: turnover, daily volume, and a histogram of daily volume.\n\n    Parameters\n    ----------\n    returns : pd.Series\n        Daily returns of the strategy, noncumulative.\n         - See full explanation in create_full_tear_sheet.\n    positions : pd.DataFrame\n        Daily net position values.\n         - See full explanation in create_full_tear_sheet.\n    transactions : pd.DataFrame\n        Prices and amounts of executed trades. One row per trade.\n         - See full explanation in create_full_tear_sheet.\n    turnover_denom : str, optional\n        Either AGB or portfolio_value, default AGB.\n        - See full explanation in txn.get_turnover.\n    unadjusted_returns : pd.Series, optional\n        Daily unadjusted returns of the strategy, noncumulative.\n        Will plot additional swippage sweep analysis.\n         - See pyfolio.plotting.plot_swippage_sleep and\n           pyfolio.plotting.plot_slippage_sensitivity\n    estimate_intraday: boolean or str, optional\n        Approximate returns for intraday strategies.\n        See description in create_full_tear_sheet.\n    return_fig : boolean, optional\n        If True, returns the figure that was plotted on.\n    \"\"\"\n\n    positions = utils.check_intraday(\n        estimate_intraday, returns, positions, transactions\n    )\n\n    vertical_sections = 6 if unadjusted_returns is not None else 4\n\n    fig = plt.figure(figsize=(14, vertical_sections * 6))\n    gs = gridspec.GridSpec(vertical_sections, 3, wspace=0.5, hspace=0.5)\n    ax_turnover = plt.subplot(gs&#91;0, :])\n    ax_daily_volume = plt.subplot(gs&#91;1, :], sharex=ax_turnover)\n    ax_turnover_hist = plt.subplot(gs&#91;2, :])\n    ax_txn_timings = plt.subplot(gs&#91;3, :])\n\n    plotting.plot_turnover(\n        returns,\n        transactions,\n        positions,\n        turnover_denom=turnover_denom,\n        ax=ax_turnover,\n    )\n\n    plotting.plot_daily_volume(returns, transactions, ax=ax_daily_volume)\n\n    try:\n        plotting.plot_daily_turnover_hist(\n            transactions,\n            positions,\n            turnover_denom=turnover_denom,\n            ax=ax_turnover_hist,\n        )\n    except ValueError:\n        warnings.warn(\"Unable to generate turnover plot.\", UserWarning)\n\n    plotting.plot_txn_time_hist(transactions, ax=ax_txn_timings)\n\n    if unadjusted_returns is not None:\n        ax_slippage_sweep = plt.subplot(gs&#91;4, :])\n        plotting.plot_slippage_sweep(\n            unadjusted_returns, positions, transactions, ax=ax_slippage_sweep\n        )\n        ax_slippage_sensitivity = plt.subplot(gs&#91;5, :])\n        plotting.plot_slippage_sensitivity(\n            unadjusted_returns,\n            positions,\n            transactions,\n            ax=ax_slippage_sensitivity,\n        )\n    for ax in fig.axes:\n        ax.tick_params(\n            axis=\"x\",\n            which=\"major\",\n            bottom=True,\n            top=False,\n            labelbottom=True,\n        )\n\n    save_plot(fig,'TXN Tear Sheet')\n\n    if return_fig:\n        return fig\n\n\n@plotting.customize\ndef create_round_trip_tear_sheet(\n    returns,\n    positions,\n    transactions,\n    sector_mappings=None,\n    estimate_intraday=\"infer\",\n    return_fig=False,\n):\n    \"\"\"\n    Generate a number of figures and plots describing the duration,\n    frequency, and profitability of trade \"round trips.\"\n    A round trip is started when a new long or short position is\n    opened and is only completed when the number of shares in that\n    position returns to or crosses zero.\n\n    Parameters\n    ----------\n    returns : pd.Series\n        Daily returns of the strategy, noncumulative.\n         - See full explanation in create_full_tear_sheet.\n    positions : pd.DataFrame\n        Daily net position values.\n         - See full explanation in create_full_tear_sheet.\n    transactions : pd.DataFrame\n        Prices and amounts of executed trades. One row per trade.\n         - See full explanation in create_full_tear_sheet.\n    sector_mappings : dict or pd.Series, optional\n        Security identifier to sector mapping.\n        Security ids as keys, sectors as values.\n    estimate_intraday: boolean or str, optional\n        Approximate returns for intraday strategies.\n        See description in create_full_tear_sheet.\n    return_fig : boolean, optional\n        If True, returns the figure that was plotted on.\n    \"\"\"\n\n    positions = utils.check_intraday(\n        estimate_intraday, returns, positions, transactions\n    )\n\n    transactions_closed = round_trips.add_closing_transactions(positions, transactions)\n    # extract_round_trips requires BoD portfolio_value\n    trades = round_trips.extract_round_trips(\n        transactions_closed,\n        portfolio_value=positions.sum(axis=\"columns\") \/ (1 + returns),\n    )\n\n    if len(trades) &lt; 5:\n        warnings.warn(\n            \"\"\"Fewer than 5 round-trip trades made.\n               Skipping round trip tearsheet.\"\"\",\n            UserWarning,\n        )\n        return\n\n    round_trips.print_round_trip_stats(trades)\n\n    plotting.show_profit_attribution(trades)\n\n    if sector_mappings is not None:\n        sector_trades = round_trips.apply_sector_mappings_to_round_trips(\n            trades, sector_mappings\n        )\n        plotting.show_profit_attribution(sector_trades)\n\n    fig = plt.figure(figsize=(14, 3 * 6))\n\n    gs = gridspec.GridSpec(3, 2, wspace=0.5, hspace=0.5)\n\n    ax_trade_lifetimes = plt.subplot(gs&#91;0, :])\n    ax_prob_profit_trade = plt.subplot(gs&#91;1, 0])\n    ax_holding_time = plt.subplot(gs&#91;1, 1])\n    ax_pnl_per_round_trip_dollars = plt.subplot(gs&#91;2, 0])\n    ax_pnl_per_round_trip_pct = plt.subplot(gs&#91;2, 1])\n\n    plotting.plot_round_trip_lifetimes(trades, ax=ax_trade_lifetimes)\n\n    plotting.plot_prob_profit_trade(trades, ax=ax_prob_profit_trade)\n\n    trade_holding_times = &#91;x.days for x in trades&#91;\"duration\"]]\n    sns.histplot(trade_holding_times, ax=ax_holding_time)\n    ax_holding_time.set(xlabel=\"Holding time in days\")\n\n    sns.histplot(trades.pnl, ax=ax_pnl_per_round_trip_dollars)\n    ax_pnl_per_round_trip_dollars.set(xlabel=\"PnL per round-trip trade in $\")\n\n    sns.histplot(trades.returns.dropna() * 100, ax=ax_pnl_per_round_trip_pct)\n    ax_pnl_per_round_trip_pct.set(xlabel=\"Round-trip returns in %\")\n\n    gs.tight_layout(fig)\n\n    save_plot(fig,'Round Trip Tear Sheet')\n\n    if return_fig:\n        return fig\n\n\n@plotting.customize\ndef create_interesting_times_tear_sheet(\n    returns,\n    benchmark_rets=None,\n    periods=None,\n    legend_loc=\"best\",\n    return_fig=False,\n):\n    \"\"\"\n    Generate a number of returns plots around interesting points in time,\n    like the flash crash and 9\/11.\n\n    Plots: returns around the dotcom bubble burst, Lehmann Brothers' failure,\n    9\/11, US downgrade and EU debt crisis, Fukushima meltdown, US housing\n    bubble burst, EZB IR, Great Recession (August 2007, March and September\n    of 2008, Q1 &amp; Q2 2009), flash crash, April and October 2014.\n\n    benchmark_rets must be passed, as it is meaningless to analyze performance\n    during interesting times without some benchmark to refer to.\n\n    Parameters\n    ----------\n    returns : pd.Series\n        Daily returns of the strategy, noncumulative.\n         - See full explanation in create_full_tear_sheet.\n    benchmark_rets : pd.Series\n        Daily noncumulative returns of the benchmark.\n         - This is in the same style as returns.\n    periods: dict or OrderedDict, optional\n        historical event dates that may have had significant\n        impact on markets\n    legend_loc : plt.legend_loc, optional\n         The legend's location.\n    return_fig : boolean, optional\n        If True, returns the figure that was plotted on.\n    \"\"\"\n    logging.info('Running create_interesting_times_tear_sheet')\n\n    rets_interesting = timeseries.extract_interesting_date_ranges(returns, periods)\n\n    if not rets_interesting:\n        warnings.warn(\n            \"Passed returns do not overlap with any\" \"interesting times.\",\n            UserWarning,\n        )\n        return\n\n    utils.print_table(\n        pd.DataFrame(rets_interesting)\n        .describe()\n        .transpose()\n        .loc&#91;:, &#91;\"mean\", \"min\", \"max\"]]\n        * 100,\n        name=\"Stress Events\",\n        float_format=\"{0:.2f}%\".format,\n    )\n\n    if benchmark_rets is not None:\n        returns = utils.clip_returns_to_benchmark(returns, benchmark_rets)\n\n        bmark_interesting = timeseries.extract_interesting_date_ranges(\n            benchmark_rets, periods\n        )\n\n    num_plots = len(rets_interesting)\n    # 2 plots, 1 row; 3 plots, 2 rows; 4 plots, 2 rows; etc.\n    num_rows = int((num_plots + 1) \/ 2.0)\n    fig = plt.figure(figsize=(14, num_rows * 6.0))\n    gs = gridspec.GridSpec(num_rows, 2, wspace=0.5, hspace=0.5)\n\n    for i, (name, rets_period) in enumerate(rets_interesting.items()):\n        # i=0 -&gt; 0, i=1 -&gt; 0, i=2 -&gt; 1 ;; i=0 -&gt; 0, i=1 -&gt; 1, i=2 -&gt; 0\n        ax = plt.subplot(gs&#91;int(i \/ 2.0), i % 2])\n\n        ep.cum_returns(rets_period).plot(\n            ax=ax, color=\"forestgreen\", label=\"algo\", alpha=0.7, lw=2\n        )\n\n        if benchmark_rets is not None:\n            ep.cum_returns(bmark_interesting&#91;name]).plot(\n                ax=ax, color=\"gray\", label=\"benchmark\", alpha=0.6\n            )\n            ax.legend(\n                &#91;\"Algo\", \"benchmark\"],\n                loc=legend_loc,\n                frameon=True,\n                framealpha=0.5,\n            )\n        else:\n            ax.legend(&#91;\"Algo\"], loc=legend_loc, frameon=True, framealpha=0.5)\n\n        ax.set_title(name)\n        ax.set_ylabel(\"Returns\")\n        ax.set_xlabel(\"\")\n\n    save_plot(fig,'Interesting Times Tear Sheet')\n\n    if return_fig:\n        return fig\n\n\n@plotting.customize\ndef create_capacity_tear_sheet(\n    returns,\n    positions,\n    transactions,\n    market_data,\n    liquidation_daily_vol_limit=0.2,\n    trade_daily_vol_limit=0.05,\n    last_n_days=utils.APPROX_BDAYS_PER_MONTH * 6,\n    days_to_liquidate_limit=1,\n    estimate_intraday=\"infer\",\n    return_fig=False,\n):\n    \"\"\"\n    Generates a report detailing portfolio size constraints set by\n    least liquid tickers. Plots a \"capacity sweep,\" a curve describing\n    projected sharpe ratio given the slippage penalties that are\n    applied at various capital bases.\n\n    Parameters\n    ----------\n    returns : pd.Series\n        Daily returns of the strategy, noncumulative.\n         - See full explanation in create_full_tear_sheet.\n    positions : pd.DataFrame\n        Daily net position values.\n         - See full explanation in create_full_tear_sheet.\n    transactions : pd.DataFrame\n        Prices and amounts of executed trades. One row per trade.\n         - See full explanation in create_full_tear_sheet.\n    market_data : pd.DataFrame\n        Daily market_data\n        - DataFrame has a multi-index index, one level is dates and another is\n        market_data contains volume &amp; price, equities as columns\n    liquidation_daily_vol_limit : float\n        Max proportion of a daily bar that can be consumed in the\n        process of liquidating a position in the\n        \"days to liquidation\" analysis.\n    trade_daily_vol_limit : float\n        Flag daily transaction totals that exceed proportion of\n        daily bar.\n    last_n_days : integer\n        Compute max position allocation and dollar volume for only\n        the last N days of the backtest\n    days_to_liquidate_limit : integer\n        Display all tickers with greater max days to liquidation.\n    estimate_intraday: boolean or str, optional\n        Approximate returns for intraday strategies.\n        See description in create_full_tear_sheet.\n    return_fig : boolean, optional\n        If True, returns the figure that was plotted on.\n    \"\"\"\n\n    positions = utils.check_intraday(\n        estimate_intraday, returns, positions, transactions\n    )\n\n    print(\n        \"Max days to liquidation is computed for each traded name \"\n        \"assuming a 20% limit on daily bar consumption \\n\"\n        \"and trailing 5 day mean volume as the available bar volume.\\n\\n\"\n        \"Tickers with &gt;1 day liquidation time at a\"\n        \" constant $1m capital base:\"\n    )\n\n    max_days_by_ticker = capacity.get_max_days_to_liquidate_by_ticker(\n        positions,\n        market_data,\n        max_bar_consumption=liquidation_daily_vol_limit,\n        capital_base=1e6,\n        mean_volume_window=5,\n    )\n    max_days_by_ticker.index = max_days_by_ticker.index.map(utils.format_asset)\n\n    print(\"Whole backtest:\")\n    utils.print_table(\n        max_days_by_ticker&#91;\n            max_days_by_ticker.days_to_liquidate &gt; days_to_liquidate_limit\n        ]\n    )\n\n    max_days_by_ticker_lnd = capacity.get_max_days_to_liquidate_by_ticker(\n        positions,\n        market_data,\n        max_bar_consumption=liquidation_daily_vol_limit,\n        capital_base=1e6,\n        mean_volume_window=5,\n        last_n_days=last_n_days,\n    )\n    max_days_by_ticker_lnd.index = max_days_by_ticker_lnd.index.map(utils.format_asset)\n\n    print(\"Last {} trading days:\".format(last_n_days))\n    utils.print_table(\n        max_days_by_ticker_lnd&#91;max_days_by_ticker_lnd.days_to_liquidate &gt; 1]\n    )\n\n    llt = capacity.get_low_liquidity_transactions(transactions, market_data)\n    llt.index = llt.index.map(utils.format_asset)\n\n    print(\n        \"Tickers with daily transactions consuming &gt;{}% of daily bar \\n\"\n        \"all backtest:\".format(trade_daily_vol_limit * 100)\n    )\n    utils.print_table(llt&#91;llt&#91;\"max_pct_bar_consumed\"] &gt; trade_daily_vol_limit * 100])\n\n    llt = capacity.get_low_liquidity_transactions(\n        transactions, market_data, last_n_days=last_n_days\n    )\n\n    print(\"Last {} trading days:\".format(last_n_days))\n    utils.print_table(llt&#91;llt&#91;\"max_pct_bar_consumed\"] &gt; trade_daily_vol_limit * 100])\n\n    bt_starting_capital = positions.iloc&#91;0].sum() \/ (1 + returns.iloc&#91;0])\n    fig, ax_capacity_sweep = plt.subplots(figsize=(14, 6))\n    plotting.plot_capacity_sweep(\n        returns,\n        transactions,\n        market_data,\n        bt_starting_capital,\n        min_pv=100000,\n        max_pv=300000000,\n        step_size=1000000,\n        ax=ax_capacity_sweep,\n    )\n\n    save_plot(fig,'Capacity Tear Sheet')\n\n    if return_fig:\n        return fig\n\n\n@plotting.customize\ndef create_perf_attrib_tear_sheet(\n    returns,\n    positions,\n    factor_returns,\n    factor_loadings,\n    transactions=None,\n    pos_in_dollars=True,\n    factor_partitions=FACTOR_PARTITIONS,\n    return_fig=False,\n):\n    \"\"\"\n    Generate plots and tables for analyzing a strategy's performance.\n\n    Parameters\n    ----------\n    returns : pd.Series\n        Returns for each day in the date range.\n\n    positions: pd.DataFrame\n        Daily holdings (in dollars or percentages), indexed by date.\n        Will be converted to percentages if positions are in dollars.\n        Short positions show up as cash in the 'cash' column.\n\n    factor_returns : pd.DataFrame\n        Returns by factor, with date as index and factors as columns\n\n    factor_loadings : pd.DataFrame\n        Factor loadings for all days in the date range, with date\n        and ticker as index, and factors as columns.\n\n    transactions : pd.DataFrame, optional\n        Prices and amounts of executed trades. One row per trade.\n         - See full explanation in create_full_tear_sheet.\n         - Default is None.\n\n    pos_in_dollars : boolean, optional\n        Flag indicating whether `positions` are in dollars or percentages\n        If True, positions are in dollars.\n\n    factor_partitions : dict\n        dict specifying how factors should be separated in factor returns\n        and risk exposures plots\n        - Example:\n          {'style': &#91;'momentum', 'size', 'value', ...],\n           'sector': &#91;'technology', 'materials', ... ]}\n\n    return_fig : boolean, optional\n        If True, returns the figure that was plotted on.\n    \"\"\"\n    portfolio_exposures, perf_attrib_data = perf_attrib.perf_attrib(\n        returns,\n        positions,\n        factor_returns,\n        factor_loadings,\n        transactions,\n        pos_in_dollars=pos_in_dollars,\n    )\n\n    display(Markdown(\"## Performance Relative to Common Risk Factors\"))\n\n    # aggregate perf attrib stats and show summary table\n    perf_attrib.show_perf_attrib_stats(\n        returns,\n        positions,\n        factor_returns,\n        factor_loadings,\n        transactions,\n        pos_in_dollars,\n    )\n\n    # one section for the returns plot, and for each factor grouping\n    # one section for factor returns, and one for risk exposures\n    if factor_partitions is not None:\n        vertical_sections = 1 + 2 * max(len(factor_partitions), 1)\n    else:\n        vertical_sections = 1 + 2\n\n    current_section = 0\n\n    fig = plt.figure(figsize=&#91;14, vertical_sections * 6])\n\n    gs = gridspec.GridSpec(vertical_sections, 1, wspace=0.5, hspace=0.5)\n\n    perf_attrib.plot_returns(perf_attrib_data, ax=plt.subplot(gs&#91;current_section]))\n    current_section += 1\n\n    if factor_partitions is not None:\n\n        for factor_type, partitions in factor_partitions.items():\n            columns_to_select = perf_attrib_data.columns.intersection(partitions)\n\n            perf_attrib.plot_factor_contribution_to_perf(\n                perf_attrib_data&#91;columns_to_select],\n                ax=plt.subplot(gs&#91;current_section]),\n                title=(\"Cumulative common {} returns attribution\").format(factor_type),\n            )\n            current_section += 1\n\n        for factor_type, partitions in factor_partitions.items():\n            columns_to_select = portfolio_exposures.columns.intersection(partitions)\n\n            perf_attrib.plot_risk_exposures(\n                portfolio_exposures&#91;columns_to_select],\n                ax=plt.subplot(gs&#91;current_section]),\n                title=\"Daily {} factor exposures\".format(factor_type),\n            )\n            current_section += 1\n\n    else:\n\n        perf_attrib.plot_factor_contribution_to_perf(\n            perf_attrib_data, ax=plt.subplot(gs&#91;current_section])\n        )\n        current_section += 1\n\n        perf_attrib.plot_risk_exposures(\n            portfolio_exposures, ax=plt.subplot(gs&#91;current_section])\n        )\n\n    # gs.tight_layout(fig)\n    save_plot(fig,'Perf Attribution Tear Sheet')\n\n    if return_fig:\n        return fig\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">timeseries.py<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>#\n# Copyright 2018 Quantopian, Inc.\n#\n# Licensed under the Apache License, Version 2.0 (the \"License\");\n# you may not use this file except in compliance with the License.\n# You may obtain a copy of the License at\n#\n#     http:\/\/www.apache.org\/licenses\/LICENSE-2.0\n#\n# Unless required by applicable law or agreed to in writing, software\n# distributed under the License is distributed on an \"AS IS\" BASIS,\n# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n# See the License for the specific language governing permissions and\n# limitations under the License.\nfrom collections import OrderedDict\nfrom functools import partial\n\nimport empyrical as ep\nimport numpy as np\nimport pandas as pd\nimport scipy as sp\nimport scipy.stats as stats\nfrom sklearn import linear_model\n\nfrom .deprecate import deprecated\nfrom .interesting_periods import PERIODS\nfrom .txn import get_turnover\nfrom .utils import APPROX_BDAYS_PER_MONTH, APPROX_BDAYS_PER_YEAR\nfrom .utils import DAILY\n\nDEPRECATION_WARNING = (\n    \"Risk functions in pyfolio.timeseries are deprecated \"\n    \"and will be removed in a future release. Please \"\n    \"install the empyrical package instead.\"\n)\n\n\ndef var_cov_var_normal(P, c, mu=0, sigma=1):\n    \"\"\"\n    Variance-covariance calculation of daily Value-at-Risk in a\n    portfolio.\n\n    Parameters\n    ----------\n    P : float\n        Portfolio value.\n    c : float\n        Confidence level.\n    mu : float, optional\n        Mean.\n\n    Returns\n    -------\n    float\n        Variance-covariance.\n    \"\"\"\n\n    alpha = sp.stats.norm.ppf(1 - c, mu, sigma)\n    return P - P * (alpha + 1)\n\n\n@deprecated(msg=DEPRECATION_WARNING)\ndef max_drawdown(returns):\n    \"\"\"\n    Determines the maximum drawdown of a strategy.\n\n    Parameters\n    ----------\n    returns : pd.Series\n        Daily returns of the strategy, noncumulative.\n         - See full explanation in tears.create_full_tear_sheet.\n\n    Returns\n    -------\n    float\n        Maximum drawdown.\n\n    Note\n    -----\n    See https:\/\/en.wikipedia.org\/wiki\/Drawdown_(economics) for more details.\n    \"\"\"\n\n    return ep.max_drawdown(returns)\n\n\n@deprecated(msg=DEPRECATION_WARNING)\ndef annual_return(returns, period=DAILY):\n    \"\"\"\n    Determines the mean annual growth rate of returns.\n\n    Parameters\n    ----------\n    returns : pd.Series\n        Periodic returns of the strategy, noncumulative.\n        - See full explanation in :func:`~pyfolio.timeseries.cum_returns`.\n    period : str, optional\n        Defines the periodicity of the 'returns' data for purposes of\n        annualizing. Can be 'monthly', 'weekly', or 'daily'.\n        - Defaults to 'daily'.\n\n    Returns\n    -------\n    float\n        Annual Return as CAGR (Compounded Annual Growth Rate).\n    \"\"\"\n\n    return ep.annual_return(returns, period=period)\n\n\n@deprecated(msg=DEPRECATION_WARNING)\ndef annual_volatility(returns, period=DAILY):\n    \"\"\"\n    Determines the annual volatility of a strategy.\n\n    Parameters\n    ----------\n    returns : pd.Series\n        Periodic returns of the strategy, noncumulative.\n        - See full explanation in :func:`~pyfolio.timeseries.cum_returns`.\n    period : str, optional\n        Defines the periodicity of the 'returns' data for purposes of\n        annualizing volatility. Can be 'monthly' or 'weekly' or 'daily'.\n        - Defaults to 'daily'.\n\n    Returns\n    -------\n    float\n        Annual volatility.\n    \"\"\"\n\n    return ep.annual_volatility(returns, period=period)\n\n\n@deprecated(msg=DEPRECATION_WARNING)\ndef calmar_ratio(returns, period=DAILY):\n    \"\"\"\n    Determines the Calmar ratio, or drawdown ratio, of a strategy.\n\n    Parameters\n    ----------\n    returns : pd.Series\n        Daily returns of the strategy, noncumulative.\n        - See full explanation in :func:`~pyfolio.timeseries.cum_returns`.\n    period : str, optional\n        Defines the periodicity of the 'returns' data for purposes of\n        annualizing. Can be 'monthly', 'weekly', or 'daily'.\n        - Defaults to 'daily'.\n\n    Returns\n    -------\n    float\n        Calmar ratio (drawdown ratio) as float. Returns np.nan if there is no\n        calmar ratio.\n\n    Note\n    -----\n    See https:\/\/en.wikipedia.org\/wiki\/Calmar_ratio for more details.\n    \"\"\"\n\n    return ep.calmar_ratio(returns, period=period)\n\n\n@deprecated(msg=DEPRECATION_WARNING)\ndef omega_ratio(returns, annual_return_threshhold=0.0):\n    \"\"\"\n    Determines the Omega ratio of a strategy.\n\n    Parameters\n    ----------\n    returns : pd.Series\n        Daily returns of the strategy, noncumulative.\n        - See full explanation in :func:`~pyfolio.timeseries.cum_returns`.\n    annual_return_threshold : float, optional\n        Minimum acceptable return of the investor. Annual threshold over which\n        returns are considered positive or negative. It is converted to a\n        value appropriate for the period of the returns for this ratio.\n        E.g. An annual minimum acceptable return of 100 translates to a daily\n        minimum acceptable return of 0.01848.\n            (1 + 100) ** (1. \/ 252) - 1 = 0.01848\n        Daily returns must exceed this value to be considered positive. The\n        daily return yields the desired annual return when compounded over\n        the average number of business days in a year.\n            (1 + 0.01848) ** 252 - 1 = 99.93\n        - Defaults to 0.0\n\n\n    Returns\n    -------\n    float\n        Omega ratio.\n\n    Note\n    -----\n    See https:\/\/en.wikipedia.org\/wiki\/Omega_ratio for more details.\n    \"\"\"\n\n    return ep.omega_ratio(returns, required_return=annual_return_threshhold)\n\n\n@deprecated(msg=DEPRECATION_WARNING)\ndef sortino_ratio(returns, required_return=0, period=DAILY):\n    \"\"\"\n    Determines the Sortino ratio of a strategy.\n\n    Parameters\n    ----------\n    returns : pd.Series or pd.DataFrame\n        Daily returns of the strategy, noncumulative.\n        - See full explanation in :func:`~pyfolio.timeseries.cum_returns`.\n    required_return: float \/ series\n        minimum acceptable return\n    period : str, optional\n        Defines the periodicity of the 'returns' data for purposes of\n        annualizing. Can be 'monthly', 'weekly', or 'daily'.\n        - Defaults to 'daily'.\n\n    Returns\n    -------\n    depends on input type\n    series ==&gt; float\n    DataFrame ==&gt; np.array\n\n        Annualized Sortino ratio.\n    \"\"\"\n\n    return ep.sortino_ratio(returns, required_return=required_return)\n\n\n@deprecated(msg=DEPRECATION_WARNING)\ndef downside_risk(returns, required_return=0, period=DAILY):\n    \"\"\"\n    Determines the downside deviation below a threshold\n\n    Parameters\n    ----------\n    returns : pd.Series or pd.DataFrame\n        Daily returns of the strategy, noncumulative.\n        - See full explanation in :func:`~pyfolio.timeseries.cum_returns`.\n    required_return: float \/ series\n        minimum acceptable return\n    period : str, optional\n        Defines the periodicity of the 'returns' data for purposes of\n        annualizing. Can be 'monthly', 'weekly', or 'daily'.\n        - Defaults to 'daily'.\n\n    Returns\n    -------\n    depends on input type\n    series ==&gt; float\n    DataFrame ==&gt; np.array\n\n        Annualized downside deviation\n    \"\"\"\n\n    return ep.downside_risk(returns, required_return=required_return, period=period)\n\n\n@deprecated(msg=DEPRECATION_WARNING)\ndef sharpe_ratio(returns, risk_free=0, period=DAILY):\n    \"\"\"\n    Determines the Sharpe ratio of a strategy.\n\n    Parameters\n    ----------\n    returns : pd.Series\n        Daily returns of the strategy, noncumulative.\n        - See full explanation in :func:`~pyfolio.timeseries.cum_returns`.\n    risk_free : int, float\n        Constant risk-free return throughout the period.\n    period : str, optional\n        Defines the periodicity of the 'returns' data for purposes of\n        annualizing. Can be 'monthly', 'weekly', or 'daily'.\n        - Defaults to 'daily'.\n\n    Returns\n    -------\n    float\n        Sharpe ratio.\n    np.nan\n        If insufficient length of returns or if if adjusted returns are 0.\n\n    Note\n    -----\n    See https:\/\/en.wikipedia.org\/wiki\/Sharpe_ratio for more details.\n    \"\"\"\n\n    return ep.sharpe_ratio(returns, risk_free=risk_free, period=period)\n\n\n@deprecated(msg=DEPRECATION_WARNING)\ndef alpha_beta(returns, factor_returns):\n    \"\"\"\n    Calculates both alpha and beta.\n\n    Parameters\n    ----------\n    returns : pd.Series\n        Daily returns of the strategy, noncumulative.\n        - See full explanation in :func:`~pyfolio.timeseries.cum_returns`.\n    factor_returns : pd.Series\n        Daily noncumulative returns of the benchmark factor to which betas are\n        computed. Usually a benchmark such as market returns.\n         - This is in the same style as returns.\n\n    Returns\n    -------\n    float\n        Alpha.\n    float\n        Beta.\n    \"\"\"\n\n    return ep.alpha_beta(returns, factor_returns=factor_returns)\n\n\n@deprecated(msg=DEPRECATION_WARNING)\ndef alpha(returns, factor_returns):\n    \"\"\"\n    Calculates annualized alpha.\n\n    Parameters\n    ----------\n    returns : pd.Series\n        Daily returns of the strategy, noncumulative.\n        - See full explanation in :func:`~pyfolio.timeseries.cum_returns`.\n    factor_returns : pd.Series\n        Daily noncumulative returns of the benchmark factor to which betas are\n        computed. Usually a benchmark such as market returns.\n         - This is in the same style as returns.\n\n    Returns\n    -------\n    float\n        Alpha.\n    \"\"\"\n\n    return ep.alpha(returns, factor_returns=factor_returns)\n\n\n@deprecated(msg=DEPRECATION_WARNING)\ndef beta(returns, factor_returns):\n    \"\"\"\n    Calculates beta.\n\n    Parameters\n    ----------\n    returns : pd.Series\n        Daily returns of the strategy, noncumulative.\n        - See full explanation in :func:`~pyfolio.timeseries.cum_returns`.\n    factor_returns : pd.Series\n        Daily noncumulative returns of the benchmark factor to which betas are\n        computed. Usually a benchmark such as market returns.\n         - This is in the same style as returns.\n\n    Returns\n    -------\n    float\n        Beta.\n    \"\"\"\n\n    return ep.beta(returns, factor_returns)\n\n\n@deprecated(msg=DEPRECATION_WARNING)\ndef stability_of_timeseries(returns):\n    \"\"\"\n    Determines R-squared of a linear fit to the cumulative\n    log returns. Computes an ordinary least squares linear fit,\n    and returns R-squared.\n\n    Parameters\n    ----------\n    returns : pd.Series\n        Daily returns of the strategy, noncumulative.\n        - See full explanation in :func:`~pyfolio.timeseries.cum_returns`.\n\n    Returns\n    -------\n    float\n        R-squared.\n    \"\"\"\n\n    return ep.stability_of_timeseries(returns)\n\n\n@deprecated(msg=DEPRECATION_WARNING)\ndef tail_ratio(returns):\n    \"\"\"\n    Determines the ratio between the right (95%) and left tail (5%).\n\n    For example, a ratio of 0.25 means that losses are four times\n    as bad as profits.\n\n    Parameters\n    ----------\n    returns : pd.Series\n        Daily returns of the strategy, noncumulative.\n         - See full explanation in :func:`~pyfolio.timeseries.cum_returns`.\n\n    Returns\n    -------\n    float\n        tail ratio\n    \"\"\"\n\n    return ep.tail_ratio(returns)\n\n\ndef common_sense_ratio(returns):\n    \"\"\"\n    Common sense ratio is the multiplication of the tail ratio and the\n    Gain-to-Pain-Ratio -- sum(profits) \/ sum(losses).\n\n    See http:\/\/bit.ly\/1ORzGBk for more information on motivation of\n    this metric.\n\n\n    Parameters\n    ----------\n    returns : pd.Series\n        Daily returns of the strategy, noncumulative.\n         - See full explanation in tears.create_full_tear_sheet.\n\n    Returns\n    -------\n    float\n        common sense ratio\n    \"\"\"\n\n    return ep.tail_ratio(returns) * (1 + ep.annual_return(returns))\n\n\ndef normalize(returns, starting_value=1):\n    \"\"\"\n    Normalizes a returns timeseries based on the first value.\n\n    Parameters\n    ----------\n    returns : pd.Series\n        Daily returns of the strategy, noncumulative.\n         - See full explanation in tears.create_full_tear_sheet.\n    starting_value : float, optional\n       The starting returns (default 1).\n\n    Returns\n    -------\n    pd.Series\n        Normalized returns.\n    \"\"\"\n\n    return starting_value * (returns \/ returns.iloc&#91;0])\n\n\n@deprecated(msg=DEPRECATION_WARNING)\ndef cum_returns(returns, starting_value=0):\n    \"\"\"\n    Compute cumulative returns from simple returns.\n\n    Parameters\n    ----------\n    returns : pd.Series\n        Daily returns of the strategy, noncumulative.\n         - See full explanation in tears.create_full_tear_sheet.\n    starting_value : float, optional\n       The starting returns (default 1).\n\n    Returns\n    -------\n    pandas.Series\n        Series of cumulative returns.\n\n    Notes\n    -----\n    For increased numerical accuracy, convert input to log returns\n    where it is possible to sum instead of multiplying.\n    \"\"\"\n\n    return ep.cum_returns(returns, starting_value=starting_value)\n\n\n@deprecated(msg=DEPRECATION_WARNING)\ndef aggregate_returns(returns, convert_to):\n    \"\"\"\n    Aggregates returns by week, month, or year.\n\n    Parameters\n    ----------\n    returns : pd.Series\n       Daily returns of the strategy, noncumulative.\n        - See full explanation in :func:`~pyfolio.timeseries.cum_returns`.\n    convert_to : str\n        Can be 'weekly', 'monthly', or 'yearly'.\n\n    Returns\n    -------\n    pd.Series\n        Aggregated returns.\n    \"\"\"\n\n    return ep.aggregate_returns(returns, convert_to=convert_to)\n\n\ndef rolling_beta(returns, factor_returns, rolling_window=APPROX_BDAYS_PER_MONTH * 6):\n    \"\"\"\n    Determines the rolling beta of a strategy.\n\n    Parameters\n    ----------\n    returns : pd.Series\n        Daily returns of the strategy, noncumulative.\n         - See full explanation in tears.create_full_tear_sheet.\n    factor_returns : pd.Series or pd.DataFrame\n        Daily noncumulative returns of the benchmark factor to which betas are\n        computed. Usually a benchmark such as market returns.\n         - If DataFrame is passed, computes rolling beta for each column.\n         - This is in the same style as returns.\n    rolling_window : int, optional\n        The size of the rolling window, in days, over which to compute\n        beta (default 6 months).\n\n    Returns\n    -------\n    pd.Series\n        Rolling beta.\n\n    Note\n    -----\n    See https:\/\/en.wikipedia.org\/wiki\/Beta_(finance) for more details.\n    \"\"\"\n\n    if factor_returns.ndim &gt; 1:\n        # Apply column-wise\n        return factor_returns.apply(\n            partial(rolling_beta, returns), rolling_window=rolling_window\n        )\n    else:\n        out = pd.Series(index=returns.index, dtype=\"float64\")\n        for beg, end in zip(\n            returns.index&#91;0:-rolling_window], returns.index&#91;rolling_window:]\n        ):\n            out.loc&#91;end] = ep.beta(returns.loc&#91;beg:end], factor_returns.loc&#91;beg:end])\n\n        return out\n\n\ndef rolling_regression(\n    returns,\n    factor_returns,\n    rolling_window=APPROX_BDAYS_PER_MONTH * 6,\n    nan_threshold=0.1,\n):\n    \"\"\"\n    Computes rolling factor betas using a multivariate linear regression\n    (separate linear regressions is problematic because the factors may be\n    confounded).\n\n    Parameters\n    ----------\n    returns : pd.Series\n        Daily returns of the strategy, noncumulative.\n         - See full explanation in tears.create_full_tear_sheet.\n    factor_returns : pd.DataFrame\n        Daily noncumulative returns of the benchmark factor to which betas are\n        computed. Usually a benchmark such as market returns.\n         - Computes rolling beta for each column.\n         - This is in the same style as returns.\n    rolling_window : int, optional\n        The days window over which to compute the beta. Defaults to 6 months.\n    nan_threshold : float, optional\n        If there are more than this fraction of NaNs, the rolling regression\n        for the given date will be skipped.\n\n    Returns\n    -------\n    pandas.DataFrame\n        DataFrame containing rolling beta coefficients to SMB, HML and UMD\n    \"\"\"\n\n    # We need to drop NaNs to regress\n    ret_no_na = returns.dropna()\n\n    columns = &#91;\"alpha\"] + factor_returns.columns.tolist()\n    rolling_risk = pd.DataFrame(columns=columns, index=ret_no_na.index)\n\n    rolling_risk.index.name = \"dt\"\n\n    for beg, end in zip(\n        ret_no_na.index&#91;:-rolling_window], ret_no_na.index&#91;rolling_window:]\n    ):\n        returns_period = ret_no_na&#91;beg:end]\n        factor_returns_period = factor_returns.loc&#91;returns_period.index]\n\n        if np.all(factor_returns_period.isnull().mean()) &lt; nan_threshold:\n            factor_returns_period_dnan = factor_returns_period.dropna()\n            reg = linear_model.LinearRegression(fit_intercept=True).fit(\n                factor_returns_period_dnan,\n                returns_period.loc&#91;factor_returns_period_dnan.index],\n            )\n            rolling_risk.loc&#91;end, factor_returns.columns] = reg.coef_\n            rolling_risk.loc&#91;end, \"alpha\"] = reg.intercept_\n\n    return rolling_risk\n\n\ndef gross_lev(positions):\n    \"\"\"\n    Calculates the gross leverage of a strategy.\n\n    Parameters\n    ----------\n    positions : pd.DataFrame\n        Daily net position values.\n         - See full explanation in tears.create_full_tear_sheet.\n\n    Returns\n    -------\n    pd.Series\n        Gross leverage.\n    \"\"\"\n\n    exposure = positions.drop(\"cash\", axis=1).abs().sum(axis=1)\n    return exposure \/ positions.sum(axis=1)\n\n\ndef value_at_risk(returns, period=None, sigma=2.0):\n    \"\"\"\n    Get value at risk (VaR).\n\n    Parameters\n    ----------\n    returns : pd.Series\n        Daily returns of the strategy, noncumulative.\n         - See full explanation in tears.create_full_tear_sheet.\n    period : str, optional\n        Period over which to calculate VaR. Set to 'weekly',\n        'monthly', or 'yearly', otherwise defaults to period of\n        returns (typically daily).\n    sigma : float, optional\n        Standard deviations of VaR, default 2.\n    \"\"\"\n    if period is not None:\n        returns_agg = ep.aggregate_returns(returns, period)\n    else:\n        returns_agg = returns.copy()\n\n    value_at_risk = returns_agg.mean() - sigma * returns_agg.std()\n    return value_at_risk\n\n\nSIMPLE_STAT_FUNCS = &#91;\n    ep.annual_return,\n    ep.cum_returns_final,\n    ep.annual_volatility,\n    ep.sharpe_ratio,\n    ep.calmar_ratio,\n    ep.stability_of_timeseries,\n    ep.max_drawdown,\n    ep.omega_ratio,\n    ep.sortino_ratio,\n    stats.skew,\n    stats.kurtosis,\n    ep.tail_ratio,\n    value_at_risk,\n]\n\nFACTOR_STAT_FUNCS = &#91;\n    ep.alpha,\n    ep.beta,\n]\n\nSTAT_FUNC_NAMES = {\n    \"annual_return\": \"Annual return\",\n    \"cum_returns_final\": \"Cumulative returns\",\n    \"annual_volatility\": \"Annual volatility\",\n    \"sharpe_ratio\": \"Sharpe ratio\",\n    \"calmar_ratio\": \"Calmar ratio\",\n    \"stability_of_timeseries\": \"Stability\",\n    \"max_drawdown\": \"Max drawdown\",\n    \"omega_ratio\": \"Omega ratio\",\n    \"sortino_ratio\": \"Sortino ratio\",\n    \"skew\": \"Skew\",\n    \"kurtosis\": \"Kurtosis\",\n    \"tail_ratio\": \"Tail ratio\",\n    \"common_sense_ratio\": \"Common sense ratio\",\n    \"value_at_risk\": \"Daily value at risk\",\n    \"alpha\": \"Alpha\",\n    \"beta\": \"Beta\",\n}\n\n\ndef perf_stats(\n    returns,\n    factor_returns=None,\n    positions=None,\n    transactions=None,\n    turnover_denom=\"AGB\",\n):\n    \"\"\"\n    Calculates various performance metrics of a strategy, for use in\n    plotting.show_perf_stats.\n\n    Parameters\n    ----------\n    returns : pd.Series\n        Daily returns of the strategy, noncumulative.\n         - See full explanation in tears.create_full_tear_sheet.\n    factor_returns : pd.Series, optional\n        Daily noncumulative returns of the benchmark factor to which betas are\n        computed. Usually a benchmark such as market returns.\n         - This is in the same style as returns.\n         - If None, do not compute alpha, beta, and information ratio.\n    positions : pd.DataFrame\n        Daily net position values.\n         - See full explanation in tears.create_full_tear_sheet.\n    transactions : pd.DataFrame\n        Prices and amounts of executed trades. One row per trade.\n        - See full explanation in tears.create_full_tear_sheet.\n    turnover_denom : str\n        Either AGB or portfolio_value, default AGB.\n        - See full explanation in txn.get_turnover.\n\n    Returns\n    -------\n    pd.Series\n        Performance metrics.\n    \"\"\"\n\n    stats = pd.Series(dtype=\"float64\")\n    for stat_func in SIMPLE_STAT_FUNCS:\n        stats&#91;STAT_FUNC_NAMES&#91;stat_func.__name__]] = stat_func(returns)\n\n    if not (positions is None or positions.empty):\n        stats&#91;\"Gross leverage\"] = gross_lev(positions).mean()\n        if not (transactions is None or transactions.empty):\n            stats&#91;\"Daily turnover\"] = get_turnover(\n                positions, transactions, turnover_denom\n            ).mean()\n    if factor_returns is not None:\n        for stat_func in FACTOR_STAT_FUNCS:\n            res = stat_func(returns, factor_returns)\n            stats&#91;STAT_FUNC_NAMES&#91;stat_func.__name__]] = res\n\n    return stats\n\n\ndef perf_stats_bootstrap(returns, factor_returns=None, return_stats=True, **kwargs):\n    \"\"\"Calculates various bootstrapped performance metrics of a strategy.\n\n    Parameters\n    ----------\n    returns : pd.Series\n        Daily returns of the strategy, noncumulative.\n         - See full explanation in tears.create_full_tear_sheet.\n    factor_returns : pd.Series, optional\n        Daily noncumulative returns of the benchmark factor to which betas are\n        computed. Usually a benchmark such as market returns.\n         - This is in the same style as returns.\n         - If None, do not compute alpha, beta, and information ratio.\n    return_stats : boolean (optional)\n        If True, returns a DataFrame of mean, median, 5 and 95 percentiles\n        for each perf metric.\n        If False, returns a DataFrame with the bootstrap samples for\n        each perf metric.\n\n    Returns\n    -------\n    pd.DataFrame\n        if return_stats is True:\n        - Distributional statistics of bootstrapped sampling\n        distribution of performance metrics.\n        if return_stats is False:\n        - Bootstrap samples for each performance metric.\n    \"\"\"\n\n    bootstrap_values = OrderedDict()\n\n    for stat_func in SIMPLE_STAT_FUNCS:\n        stat_name = STAT_FUNC_NAMES&#91;stat_func.__name__]\n        bootstrap_values&#91;stat_name] = calc_bootstrap(stat_func, returns)\n\n    if factor_returns is not None:\n        for stat_func in FACTOR_STAT_FUNCS:\n            stat_name = STAT_FUNC_NAMES&#91;stat_func.__name__]\n            bootstrap_values&#91;stat_name] = calc_bootstrap(\n                stat_func, returns, factor_returns=factor_returns\n            )\n\n    bootstrap_values = pd.DataFrame(bootstrap_values)\n\n    if return_stats:\n        stats = bootstrap_values.apply(calc_distribution_stats)\n        return stats.T&#91;&#91;\"mean\", \"median\", \"5%\", \"95%\"]]\n    else:\n        return bootstrap_values\n\n\ndef calc_bootstrap(func, returns, *args, **kwargs):\n    \"\"\"Performs a bootstrap analysis on a user-defined function returning\n    a summary statistic.\n\n    Parameters\n    ----------\n    func : function\n        Function that either takes a single array (commonly returns)\n        or two arrays (commonly returns and factor returns) and\n        returns a single value (commonly a summary\n        statistic). Additional args and kwargs are passed as well.\n    returns : pd.Series\n        Daily returns of the strategy, noncumulative.\n         - See full explanation in tears.create_full_tear_sheet.\n    factor_returns : pd.Series, optional\n        Daily noncumulative returns of the benchmark factor to which betas are\n        computed. Usually a benchmark such as market returns.\n         - This is in the same style as returns.\n    n_samples : int, optional\n        Number of bootstrap samples to draw. Default is 1000.\n        Increasing this will lead to more stable \/ accurate estimates.\n\n    Returns\n    -------\n    numpy.ndarray\n        Bootstrapped sampling distribution of passed in func.\n    \"\"\"\n\n    n_samples = kwargs.pop(\"n_samples\", 1000)\n    out = np.empty(n_samples)\n\n    factor_returns = kwargs.pop(\"factor_returns\", None)\n\n    for i in range(n_samples):\n        idx = np.random.randint(len(returns), size=len(returns))\n        returns_i = returns.iloc&#91;idx].reset_index(drop=True)\n        if factor_returns is not None:\n            factor_returns_i = factor_returns.iloc&#91;idx].reset_index(drop=True)\n            out&#91;i] = func(returns_i, factor_returns_i, *args, **kwargs)\n        else:\n            out&#91;i] = func(returns_i, *args, **kwargs)\n\n    return out\n\n\ndef calc_distribution_stats(x):\n    \"\"\"Calculate various summary statistics of data.\n\n    Parameters\n    ----------\n    x : numpy.ndarray or pandas.Series\n        Array to compute summary statistics for.\n\n    Returns\n    -------\n    pandas.Series\n        Series containing mean, median, std, as well as 5, 25, 75 and\n        95 percentiles of passed in values.\n    \"\"\"\n\n    return pd.Series(\n        {\n            \"mean\": np.mean(x),\n            \"median\": np.median(x),\n            \"std\": np.std(x),\n            \"5%\": np.percentile(x, 5),\n            \"25%\": np.percentile(x, 25),\n            \"75%\": np.percentile(x, 75),\n            \"95%\": np.percentile(x, 95),\n            \"IQR\": np.subtract.reduce(np.percentile(x, &#91;75, 25])),\n        }\n    )\n\n\ndef get_max_drawdown_underwater(underwater):\n    \"\"\"\n    Determines peak, valley, and recovery dates given an 'underwater'\n    DataFrame.\n\n    An underwater DataFrame is a DataFrame that has precomputed\n    rolling drawdown.\n\n    Parameters\n    ----------\n    underwater : pd.Series\n       Underwater returns (rolling drawdown) of a strategy.\n\n    Returns\n    -------\n    peak : datetime\n        The maximum drawdown's peak.\n    valley : datetime\n        The maximum drawdown's valley.\n    recovery : datetime\n        The maximum drawdown's recovery.\n    \"\"\"\n\n    valley = underwater.idxmin()  # end of the period\n    # Find first 0\n    peak = underwater&#91;:valley]&#91;underwater&#91;:valley] == 0].index&#91;-1]\n    # Find last 0\n    try:\n        recovery = underwater&#91;valley:]&#91;underwater&#91;valley:] == 0].index&#91;0]\n    except IndexError:\n        recovery = np.nan  # drawdown not recovered\n    return peak, valley, recovery\n\n\ndef get_max_drawdown(returns):\n    \"\"\"\n    Determines the maximum drawdown of a strategy.\n\n    Parameters\n    ----------\n    returns : pd.Series\n        Daily returns of the strategy, noncumulative.\n        - See full explanation in :func:`~pyfolio.timeseries.cum_returns`.\n\n    Returns\n    -------\n    float\n        Maximum drawdown.\n\n    Note\n    -----\n    See https:\/\/en.wikipedia.org\/wiki\/Drawdown_(economics) for more details.\n    \"\"\"\n\n    returns = returns.copy()\n    df_cum = ep.cum_returns(returns, 1.0)\n    running_max = np.maximum.accumulate(df_cum)\n    underwater = df_cum \/ running_max - 1\n    return get_max_drawdown_underwater(underwater)\n\n\ndef get_top_drawdowns(returns, top=10):\n    \"\"\"\n    Finds top drawdowns, sorted by drawdown amount.\n\n    Parameters\n    ----------\n    returns : pd.Series\n        Daily returns of the strategy, noncumulative.\n         - See full explanation in tears.create_full_tear_sheet.\n    top : int, optional\n        The amount of top drawdowns to find (default 10).\n\n    Returns\n    -------\n    drawdowns : list\n        List of drawdown peaks, valleys, and recoveries. See get_max_drawdown.\n    \"\"\"\n\n    returns = returns.copy()\n    df_cum = ep.cum_returns(returns, 1.0)\n    running_max = np.maximum.accumulate(df_cum)\n    underwater = df_cum \/ running_max - 1\n\n    drawdowns = &#91;]\n    for _ in range(top):\n        peak, valley, recovery = get_max_drawdown_underwater(underwater)\n        # Slice out draw-down period\n        if not pd.isnull(recovery):\n            underwater.drop(underwater&#91;peak:recovery].index&#91;1:-1], inplace=True)\n        else:\n            # drawdown has not ended yet\n            underwater = underwater.loc&#91;:peak]\n\n        drawdowns.append((peak, valley, recovery))\n        if (len(returns) == 0) or (len(underwater) == 0) or (np.min(underwater) == 0):\n            break\n\n    return drawdowns\n\n\ndef gen_drawdown_table(returns, top=10):\n    \"\"\"\n    Places top drawdowns in a table.\n\n    Parameters\n    ----------\n    returns : pd.Series\n        Daily returns of the strategy, noncumulative.\n         - See full explanation in tears.create_full_tear_sheet.\n    top : int, optional\n        The amount of top drawdowns to find (default 10).\n\n    Returns\n    -------\n    df_drawdowns : pd.DataFrame\n        Information about top drawdowns.\n    \"\"\"\n\n    df_cum = ep.cum_returns(returns, 1.0)\n    drawdown_periods = get_top_drawdowns(returns, top=top)\n    df_drawdowns = pd.DataFrame(\n        index=list(range(top)),\n        columns=&#91;\n            \"Net drawdown in %\",\n            \"Peak date\",\n            \"Valley date\",\n            \"Recovery date\",\n            \"Duration\",\n        ],\n    )\n\n    for i, (peak, valley, recovery) in enumerate(drawdown_periods):\n        if pd.isnull(recovery):\n            df_drawdowns.loc&#91;i, \"Duration\"] = np.nan\n        else:\n            df_drawdowns.loc&#91;i, \"Duration\"] = len(\n                pd.date_range(peak, recovery, freq=\"B\")\n            )\n        df_drawdowns.loc&#91;i, \"Peak date\"] = peak.to_pydatetime().strftime(\"%Y-%m-%d\")\n        df_drawdowns.loc&#91;i, \"Valley date\"] = valley.to_pydatetime().strftime(\"%Y-%m-%d\")\n        if isinstance(recovery, float):\n            df_drawdowns.loc&#91;i, \"Recovery date\"] = recovery\n        else:\n            df_drawdowns.loc&#91;i, \"Recovery date\"] = recovery.to_pydatetime().strftime(\n                \"%Y-%m-%d\"\n            )\n        df_drawdowns.loc&#91;i, \"Net drawdown in %\"] = (\n            (df_cum.loc&#91;peak] - df_cum.loc&#91;valley]) \/ df_cum.loc&#91;peak]\n        ) * 100\n\n    df_drawdowns&#91;\"Peak date\"] = pd.to_datetime(df_drawdowns&#91;\"Peak date\"])\n    df_drawdowns&#91;\"Valley date\"] = pd.to_datetime(df_drawdowns&#91;\"Valley date\"])\n    df_drawdowns&#91;\"Recovery date\"] = pd.to_datetime(df_drawdowns&#91;\"Recovery date\"])\n\n    return df_drawdowns\n\n\ndef rolling_volatility(returns, rolling_vol_window):\n    \"\"\"\n    Determines the rolling volatility of a strategy.\n\n    Parameters\n    ----------\n    returns : pd.Series\n        Daily returns of the strategy, noncumulative.\n         - See full explanation in tears.create_full_tear_sheet.\n    rolling_vol_window : int\n        Length of rolling window, in days, over which to compute.\n\n    Returns\n    -------\n    pd.Series\n        Rolling volatility.\n    \"\"\"\n\n    return returns.rolling(rolling_vol_window).std() * np.sqrt(APPROX_BDAYS_PER_YEAR)\n\n\ndef rolling_sharpe(returns, rolling_sharpe_window):\n    \"\"\"\n    Determines the rolling Sharpe ratio of a strategy.\n\n    Parameters\n    ----------\n    returns : pd.Series\n        Daily returns of the strategy, noncumulative.\n         - See full explanation in tears.create_full_tear_sheet.\n    rolling_sharpe_window : int\n        Length of rolling window, in days, over which to compute.\n\n    Returns\n    -------\n    pd.Series\n        Rolling Sharpe ratio.\n\n    Note\n    -----\n    See https:\/\/en.wikipedia.org\/wiki\/Sharpe_ratio for more details.\n    \"\"\"\n\n    return (\n        returns.rolling(rolling_sharpe_window).mean()\n        \/ returns.rolling(rolling_sharpe_window).std()\n        * np.sqrt(APPROX_BDAYS_PER_YEAR)\n    )\n\n\ndef simulate_paths(\n    is_returns, num_days, starting_value=1, num_samples=1000, random_seed=None\n):\n    \"\"\"\n    Gnerate alternate paths using available values from in-sample returns.\n\n    Parameters\n    ----------\n    is_returns : pandas.core.frame.DataFrame\n        Non-cumulative in-sample returns.\n    num_days : int\n        Number of days to project the probability cone forward.\n    starting_value : int or float\n        Starting value of the out of sample period.\n    num_samples : int\n        Number of samples to draw from the in-sample daily returns.\n        Each sample will be an array with length num_days.\n        A higher number of samples will generate a more accurate\n        bootstrap cone.\n    random_seed : int\n        Seed for the pseudorandom number generator used by the pandas\n        sample method.\n\n    Returns\n    -------\n    samples : numpy.ndarray\n    \"\"\"\n\n    samples = np.empty((num_samples, num_days))\n    seed = np.random.RandomState(seed=random_seed)\n    for i in range(num_samples):\n        samples&#91;i, :] = is_returns.sample(num_days, replace=True, random_state=seed)\n\n    return samples\n\n\ndef summarize_paths(samples, cone_std=(1.0, 1.5, 2.0), starting_value=1.0):\n    \"\"\"\n    Gnerate the upper and lower bounds of an n standard deviation\n    cone of forecasted cumulative returns.\n\n    Parameters\n    ----------\n    samples : numpy.ndarray\n        Alternative paths, or series of possible outcomes.\n    cone_std : list of int\/float\n        Number of standard devations to use in the boundaries of\n        the cone. If multiple values are passed, cone bounds will\n        be generated for each value.\n\n    Returns\n    -------\n    samples : pandas.core.frame.DataFrame\n    \"\"\"\n\n    cum_samples = ep.cum_returns(samples.T, starting_value=starting_value).T\n\n    cum_mean = cum_samples.mean(axis=0)\n    cum_std = cum_samples.std(axis=0)\n\n    if isinstance(cone_std, (float, int)):\n        cone_std = &#91;cone_std]\n\n    cone_bounds = pd.DataFrame(columns=pd.Index(&#91;], dtype=\"float64\"))\n    for num_std in cone_std:\n        cone_bounds.loc&#91;:, float(num_std)] = cum_mean + cum_std * num_std\n        cone_bounds.loc&#91;:, float(-num_std)] = cum_mean - cum_std * num_std\n\n    return cone_bounds\n\n\ndef forecast_cone_bootstrap(\n    is_returns,\n    num_days,\n    cone_std=(1.0, 1.5, 2.0),\n    starting_value=1,\n    num_samples=1000,\n    random_seed=None,\n):\n    \"\"\"\n    Determines the upper and lower bounds of an n standard deviation\n    cone of forecasted cumulative returns. Future cumulative mean and\n    standard devation are computed by repeatedly sampling from the\n    in-sample daily returns (i.e. bootstrap). This cone is non-parametric,\n    meaning it does not assume that returns are normally distributed.\n\n    Parameters\n    ----------\n    is_returns : pd.Series\n        In-sample daily returns of the strategy, noncumulative.\n         - See full explanation in tears.create_full_tear_sheet.\n    num_days : int\n        Number of days to project the probability cone forward.\n    cone_std : int, float, or list of int\/float\n        Number of standard devations to use in the boundaries of\n        the cone. If multiple values are passed, cone bounds will\n        be generated for each value.\n    starting_value : int or float\n        Starting value of the out of sample period.\n    num_samples : int\n        Number of samples to draw from the in-sample daily returns.\n        Each sample will be an array with length num_days.\n        A higher number of samples will generate a more accurate\n        bootstrap cone.\n    random_seed : int\n        Seed for the pseudorandom number generator used by the pandas\n        sample method.\n\n    Returns\n    -------\n    pd.DataFrame\n        Contains upper and lower cone boundaries. Column names are\n        strings corresponding to the number of standard devations\n        above (positive) or below (negative) the projected mean\n        cumulative returns.\n    \"\"\"\n\n    samples = simulate_paths(\n        is_returns=is_returns,\n        num_days=num_days,\n        starting_value=starting_value,\n        num_samples=num_samples,\n        random_seed=random_seed,\n    )\n\n    cone_bounds = summarize_paths(\n        samples=samples, cone_std=cone_std, starting_value=starting_value\n    )\n\n    return cone_bounds\n\n\ndef extract_interesting_date_ranges(returns, periods=None):\n    \"\"\"\n    Extracts returns based on interesting events. See\n    gen_date_range_interesting.\n\n    Parameters\n    ----------\n    returns : pd.Series\n        Daily returns of the strategy, noncumulative.\n         - See full explanation in tears.create_full_tear_sheet.\n\n    Returns\n    -------\n    ranges : OrderedDict\n        Date ranges, with returns, of all valid events.\n    \"\"\"\n    if periods is None:\n        periods = PERIODS\n\n    returns_dupe = returns.copy()\n    returns_dupe.index = returns_dupe.index.map(pd.Timestamp)\n    ranges = OrderedDict()\n    for name, (start, end) in periods.items():\n        try:\n            period = returns_dupe.loc&#91;start:end]\n            if len(period) == 0:\n                continue\n            ranges&#91;name] = period\n        except BaseException:\n            continue\n\n    return ranges\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">plotting.py<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>#\r\n# Copyright 2018 Quantopian, Inc.\r\n#\r\n# Licensed under the Apache License, Version 2.0 (the \"License\");\r\n# you may not use this file except in compliance with the License.\r\n# You may obtain a copy of the License at\r\n#\r\n#     http:\/\/www.apache.org\/licenses\/LICENSE-2.0\r\n#\r\n# Unless required by applicable law or agreed to in writing, software\r\n# distributed under the License is distributed on an \"AS IS\" BASIS,\r\n# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r\n# See the License for the specific language governing permissions and\r\n# limitations under the License.\r\nimport datetime\r\nimport calendar\r\nfrom collections import OrderedDict\r\nfrom functools import wraps\r\n\r\nimport empyrical as ep\r\nimport matplotlib\r\nimport matplotlib.patches as patches\r\nimport matplotlib.pyplot as plt\r\nimport numpy as np\r\nimport pandas as pd\r\nimport pytz\r\nimport scipy as sp\r\nfrom matplotlib import figure\r\nfrom matplotlib.backends.backend_agg import FigureCanvasAgg\r\nfrom matplotlib.ticker import FuncFormatter\r\nimport os\r\nimport seaborn as sns\r\nfrom . import capacity\r\nfrom . import pos\r\nfrom . import timeseries\r\nfrom . import txn\r\nfrom . import utils\r\nfrom .utils import APPROX_BDAYS_PER_MONTH, MM_DISPLAY_UNIT\r\n\r\n\r\ndef customize(func):\r\n    \"\"\"\r\n    Decorator to set plotting context and axes style during function call.\r\n    \"\"\"\r\n\r\n    @wraps(func)\r\n    def call_w_context(*args, **kwargs):\r\n        set_context = kwargs.pop(\"set_context\", True)\r\n        if set_context:\r\n            with plotting_context(), axes_style():\r\n                return func(*args, **kwargs)\r\n        else:\r\n            return func(*args, **kwargs)\r\n\r\n    return call_w_context\r\n\r\n\r\ndef plotting_context(context=\"notebook\", font_scale=1.5, rc=None):\r\n    \"\"\"\r\n    Create pyfolio default plotting style context.\r\n\r\n    Under the hood, calls and returns seaborn.plotting_context() with\r\n    some custom settings. Usually you would use in a with-context.\r\n\r\n    Parameters\r\n    ----------\r\n    context : str, optional\r\n        Name of seaborn context.\r\n    font_scale : float, optional\r\n        Scale font by factor font_scale.\r\n    rc : dict, optional\r\n        Config flags.\r\n        By default, {'lines.linewidth': 1.5}\r\n        is being used and will be added to any\r\n        rc passed in, unless explicitly overriden.\r\n\r\n    Returns\r\n    -------\r\n    seaborn plotting context\r\n\r\n    Example\r\n    -------\r\n    >>> with pyfolio.plotting.plotting_context(font_scale=2):\r\n    >>>    pyfolio.create_full_tear_sheet(..., set_context=False)\r\n\r\n    See also\r\n    --------\r\n    For more information, see seaborn.plotting_context().\r\n\r\n    \"\"\"\r\n    if rc is None:\r\n        rc = {}\r\n\r\n    rc_default = {\"lines.linewidth\": 1.5}\r\n\r\n    # Add defaults if they do not exist\r\n    for name, val in rc_default.items():\r\n        rc.setdefault(name, val)\r\n\r\n    return sns.plotting_context(context=context, font_scale=font_scale, rc=rc)\r\n\r\n\r\ndef axes_style(style=\"darkgrid\", rc=None):\r\n    \"\"\"\r\n    Create pyfolio default axes style context.\r\n\r\n    Under the hood, calls and returns seaborn.axes_style() with\r\n    some custom settings. Usually you would use in a with-context.\r\n\r\n    Parameters\r\n    ----------\r\n    style : str, optional\r\n        Name of seaborn style.\r\n    rc : dict, optional\r\n        Config flags.\r\n\r\n    Returns\r\n    -------\r\n    seaborn plotting context\r\n\r\n    Example\r\n    -------\r\n    >>> with pyfolio.plotting.axes_style(style='whitegrid'):\r\n    >>>    pyfolio.create_full_tear_sheet(..., set_context=False)\r\n\r\n    See also\r\n    --------\r\n    For more information, see seaborn.plotting_context().\r\n\r\n    \"\"\"\r\n    if rc is None:\r\n        rc = {}\r\n\r\n    rc_default = {}\r\n\r\n    # Add defaults if they do not exist\r\n    for name, val in rc_default.items():\r\n        rc.setdefault(name, val)\r\n\r\n    return sns.axes_style(style=style, rc=rc)\r\n\r\n\r\ndef plot_monthly_returns_heatmap(returns, ax=None, **kwargs):\r\n    \"\"\"\r\n    Plots a heatmap of returns by month.\r\n\r\n    Parameters\r\n    ----------\r\n    returns : pd.Series\r\n        Daily returns of the strategy, noncumulative.\r\n         - See full explanation in tears.create_full_tear_sheet.\r\n    ax : matplotlib.Axes, optional\r\n        Axes upon which to plot.\r\n    **kwargs, optional\r\n        Passed to seaborn plotting function.\r\n\r\n    Returns\r\n    -------\r\n    ax : matplotlib.Axes\r\n        The axes that were plotted on.\r\n    \"\"\"\r\n\r\n    if ax is None:\r\n        ax = plt.gca()\r\n\r\n    monthly_ret_table = ep.aggregate_returns(returns, \"monthly\")\r\n    monthly_ret_table = monthly_ret_table.unstack().round(3)\r\n\r\n    monthly_ret_table.rename(\r\n        columns={i: m for i, m in enumerate(calendar.month_abbr)}, inplace=True\r\n    )\r\n\r\n    sns.heatmap(\r\n        monthly_ret_table.fillna(0) * 100.0,\r\n        annot=True,\r\n        annot_kws={\"size\": 9},\r\n        alpha=1.0,\r\n        center=0.0,\r\n        cbar=False,\r\n        cmap=matplotlib.cm.RdYlGn,\r\n        ax=ax,\r\n        **kwargs,\r\n    )\r\n    ax.set_ylabel(\"Year\")\r\n    ax.set_xlabel(\"Month\")\r\n    ax.set_title(\"Monthly returns (%)\")\r\n    return ax\r\n\r\n\r\ndef plot_annual_returns(returns, ax=None, **kwargs):\r\n    \"\"\"\r\n    Plots a bar graph of returns by year.\r\n\r\n    Parameters\r\n    ----------\r\n    returns : pd.Series\r\n        Daily returns of the strategy, noncumulative.\r\n         - See full explanation in tears.create_full_tear_sheet.\r\n    ax : matplotlib.Axes, optional\r\n        Axes upon which to plot.\r\n    **kwargs, optional\r\n        Passed to plotting function.\r\n\r\n    Returns\r\n    -------\r\n    ax : matplotlib.Axes\r\n        The axes that were plotted on.\r\n    \"\"\"\r\n\r\n    if ax is None:\r\n        ax = plt.gca()\r\n\r\n    x_axis_formatter = FuncFormatter(utils.percentage)\r\n    ax.xaxis.set_major_formatter(FuncFormatter(x_axis_formatter))\r\n    ax.tick_params(axis=\"x\", which=\"major\")\r\n\r\n    ann_ret_df = pd.DataFrame(ep.aggregate_returns(returns, \"yearly\"))\r\n\r\n    ax.axvline(\r\n        100 * ann_ret_df.values.mean(),\r\n        color=\"red\",\r\n        linestyle=\"--\",\r\n        lw=1,\r\n        alpha=0.7,\r\n    )\r\n    (100 * ann_ret_df.sort_index(ascending=False)).plot(\r\n        ax=ax, kind=\"barh\", alpha=0.70, **kwargs\r\n    )\r\n    ax.axvline(0.0, color=\"black\", linestyle=\"-\", lw=2)\r\n\r\n    ax.set_ylabel(\"Year\")\r\n    ax.set_xlabel(\"Returns\")\r\n    ax.set_title(\"Annual returns\")\r\n    ax.legend(&#91;\"Mean\"], frameon=True, framealpha=0.5)\r\n    return ax\r\n\r\n\r\ndef plot_monthly_returns_dist(returns, ax=None, **kwargs):\r\n    \"\"\"\r\n    Plots a distribution of monthly returns.\r\n\r\n    Parameters\r\n    ----------\r\n    returns : pd.Series\r\n        Daily returns of the strategy, noncumulative.\r\n         - See full explanation in tears.create_full_tear_sheet.\r\n    ax : matplotlib.Axes, optional\r\n        Axes upon which to plot.\r\n    **kwargs, optional\r\n        Passed to plotting function.\r\n\r\n    Returns\r\n    -------\r\n    ax : matplotlib.Axes\r\n        The axes that were plotted on.\r\n    \"\"\"\r\n\r\n    if ax is None:\r\n        ax = plt.gca()\r\n\r\n    x_axis_formatter = FuncFormatter(utils.percentage)\r\n    ax.xaxis.set_major_formatter(FuncFormatter(x_axis_formatter))\r\n    ax.tick_params(axis=\"x\", which=\"major\")\r\n\r\n    monthly_ret_table = ep.aggregate_returns(returns, \"monthly\")\r\n\r\n    ax.hist(\r\n        100 * monthly_ret_table,\r\n        color=\"steelblue\",\r\n        alpha=0.80,\r\n        bins=20,\r\n        **kwargs,\r\n    )\r\n\r\n    ax.axvline(\r\n        100 * monthly_ret_table.mean(),\r\n        color=\"red\",\r\n        linestyle=\"--\",\r\n        lw=1,\r\n        alpha=1.0,\r\n    )\r\n\r\n    ax.axvline(0.0, color=\"black\", linestyle=\"-\", lw=1, alpha=0.75)\r\n    ax.legend(&#91;\"Mean\"], frameon=True, framealpha=0.5)\r\n    ax.set_ylabel(\"Number of months\")\r\n    ax.set_xlabel(\"Returns\")\r\n    ax.set_title(\"Distribution of monthly returns\")\r\n    return ax\r\n\r\n\r\ndef plot_holdings(returns, positions, legend_loc=\"best\", ax=None, **kwargs):\r\n    \"\"\"\r\n    Plots total amount of stocks with an active position, either short\r\n    or long. Displays daily total, daily average per month, and\r\n    all-time daily average.\r\n\r\n    Parameters\r\n    ----------\r\n    returns : pd.Series\r\n        Daily returns of the strategy, noncumulative.\r\n         - See full explanation in tears.create_full_tear_sheet.\r\n    positions : pd.DataFrame, optional\r\n        Daily net position values.\r\n         - See full explanation in tears.create_full_tear_sheet.\r\n    legend_loc : matplotlib.loc, optional\r\n        The location of the legend on the plot.\r\n    ax : matplotlib.Axes, optional\r\n        Axes upon which to plot.\r\n    **kwargs, optional\r\n        Passed to plotting function.\r\n\r\n    Returns\r\n    -------\r\n    ax : matplotlib.Axes\r\n        The axes that were plotted on.\r\n    \"\"\"\r\n\r\n    if ax is None:\r\n        ax = plt.gca()\r\n\r\n    positions = positions.copy().drop(\"cash\", axis=\"columns\")\r\n    df_holdings = positions.replace(0, np.nan).count(axis=1)\r\n    df_holdings_by_month = df_holdings.resample(\"1M\").mean()\r\n    df_holdings.plot(color=\"steelblue\", alpha=0.6, lw=0.5, ax=ax, **kwargs)\r\n    df_holdings_by_month.plot(color=\"orangered\", lw=2, ax=ax, **kwargs)\r\n    ax.axhline(df_holdings.values.mean(), color=\"steelblue\", ls=\"--\", lw=3)\r\n\r\n    ax.set_xlim((returns.index&#91;0], returns.index&#91;-1]))\r\n\r\n    leg = ax.legend(\r\n        &#91;\r\n            \"Daily holdings\",\r\n            \"Average daily holdings, by month\",\r\n            \"Average daily holdings, overall\",\r\n        ],\r\n        loc=legend_loc,\r\n        frameon=True,\r\n        framealpha=0.5,\r\n    )\r\n    leg.get_frame().set_edgecolor(\"black\")\r\n\r\n    ax.set_title(\"Total holdings\")\r\n    ax.set_ylabel(\"Holdings\")\r\n    ax.set_xlabel(\"\")\r\n    return ax\r\n\r\n\r\ndef plot_long_short_holdings(\r\n    returns, positions, legend_loc=\"upper left\", ax=None, **kwargs\r\n):\r\n    \"\"\"\r\n    Plots total amount of stocks with an active position, breaking out\r\n    short and long into transparent filled regions.\r\n\r\n    Parameters\r\n    ----------\r\n    returns : pd.Series\r\n        Daily returns of the strategy, noncumulative.\r\n         - See full explanation in tears.create_full_tear_sheet.\r\n    positions : pd.DataFrame, optional\r\n        Daily net position values.\r\n         - See full explanation in tears.create_full_tear_sheet.\r\n    legend_loc : matplotlib.loc, optional\r\n        The location of the legend on the plot.\r\n    ax : matplotlib.Axes, optional\r\n        Axes upon which to plot.\r\n    **kwargs, optional\r\n        Passed to plotting function.\r\n\r\n    Returns\r\n    -------\r\n    ax : matplotlib.Axes\r\n        The axes that were plotted on.\r\n\r\n    \"\"\"\r\n\r\n    if ax is None:\r\n        ax = plt.gca()\r\n\r\n    positions = positions.drop(\"cash\", axis=\"columns\")\r\n    positions = positions.replace(0, np.nan)\r\n    df_longs = positions&#91;positions > 0].count(axis=1)\r\n    df_shorts = positions&#91;positions &lt; 0].count(axis=1)\r\n    lf = ax.fill_between(\r\n        df_longs.index, 0, df_longs.values, color=\"g\", alpha=0.5, lw=2.0\r\n    )\r\n    sf = ax.fill_between(\r\n        df_shorts.index, 0, df_shorts.values, color=\"r\", alpha=0.5, lw=2.0\r\n    )\r\n\r\n    bf = patches.Rectangle(&#91;0, 0], 1, 1, color=\"darkgoldenrod\")\r\n    leg = ax.legend(\r\n        &#91;lf, sf, bf],\r\n        &#91;\r\n            \"Long (max: %s, min: %s)\" % (df_longs.max(), df_longs.min()),\r\n            \"Short (max: %s, min: %s)\" % (df_shorts.max(), df_shorts.min()),\r\n            \"Overlap\",\r\n        ],\r\n        loc=legend_loc,\r\n        frameon=True,\r\n        framealpha=0.5,\r\n    )\r\n    leg.get_frame().set_edgecolor(\"black\")\r\n\r\n    ax.set_xlim((returns.index&#91;0], returns.index&#91;-1]))\r\n    ax.set_title(\"Long and short holdings\")\r\n    ax.set_ylabel(\"Holdings\")\r\n    ax.set_xlabel(\"\")\r\n    return ax\r\n\r\n\r\ndef plot_drawdown_periods(returns, top=10, ax=None, **kwargs):\r\n    \"\"\"\r\n    Plots cumulative returns highlighting top drawdown periods.\r\n\r\n    Parameters\r\n    ----------\r\n    returns : pd.Series\r\n        Daily returns of the strategy, noncumulative.\r\n         - See full explanation in tears.create_full_tear_sheet.\r\n    top : int, optional\r\n        Amount of top drawdowns periods to plot (default 10).\r\n    ax : matplotlib.Axes, optional\r\n        Axes upon which to plot.\r\n    **kwargs, optional\r\n        Passed to plotting function.\r\n\r\n    Returns\r\n    -------\r\n    ax : matplotlib.Axes\r\n        The axes that were plotted on.\r\n    \"\"\"\r\n\r\n    if ax is None:\r\n        ax = plt.gca()\r\n\r\n    y_axis_formatter = FuncFormatter(utils.two_dec_places)\r\n    ax.yaxis.set_major_formatter(FuncFormatter(y_axis_formatter))\r\n\r\n    df_cum_rets = ep.cum_returns(returns, starting_value=1.0)\r\n    df_drawdowns = timeseries.gen_drawdown_table(returns, top=top)\r\n\r\n    df_cum_rets.plot(ax=ax, **kwargs)\r\n\r\n    lim = ax.get_ylim()\r\n    colors = sns.cubehelix_palette(len(df_drawdowns))&#91;::-1]\r\n    for i, (peak, recovery) in df_drawdowns&#91;&#91;\"Peak date\", \"Recovery date\"]].iterrows():\r\n        if pd.isnull(recovery):\r\n            recovery = returns.index&#91;-1]\r\n        ax.fill_between((peak, recovery), lim&#91;0], lim&#91;1], alpha=0.4, color=colors&#91;i])\r\n    ax.set_ylim(lim)\r\n    ax.set_title(\"Top %i drawdown periods\" % top)\r\n    ax.set_ylabel(\"Cumulative returns\")\r\n    ax.legend(&#91;\"Portfolio\"], loc=\"upper left\", frameon=True, framealpha=0.5)\r\n    ax.set_xlabel(\"\")\r\n    return ax\r\n\r\n\r\ndef plot_drawdown_underwater(returns, ax=None, **kwargs):\r\n    \"\"\"\r\n    Plots how far underwaterr returns are over time, or plots current\r\n    drawdown vs. date.\r\n\r\n    Parameters\r\n    ----------\r\n    returns : pd.Series\r\n        Daily returns of the strategy, noncumulative.\r\n         - See full explanation in tears.create_full_tear_sheet.\r\n    ax : matplotlib.Axes, optional\r\n        Axes upon which to plot.\r\n    **kwargs, optional\r\n        Passed to plotting function.\r\n\r\n    Returns\r\n    -------\r\n    ax : matplotlib.Axes\r\n        The axes that were plotted on.\r\n    \"\"\"\r\n\r\n    if ax is None:\r\n        ax = plt.gca()\r\n\r\n    y_axis_formatter = FuncFormatter(utils.percentage)\r\n    ax.yaxis.set_major_formatter(FuncFormatter(y_axis_formatter))\r\n\r\n    df_cum_rets = ep.cum_returns(returns, starting_value=1.0)\r\n    running_max = np.maximum.accumulate(df_cum_rets)\r\n    underwater = -100 * ((running_max - df_cum_rets) \/ running_max)\r\n    underwater.plot(ax=ax, kind=\"area\", color=\"salmon\", alpha=0.7, **kwargs)\r\n    ax.set_ylabel(\"Drawdown\")\r\n    ax.set_title(\"Underwater plot\")\r\n    ax.set_xlabel(\"\")\r\n    return ax\r\n\r\n\r\ndef plot_perf_stats(returns, factor_returns, ax=None):\r\n    \"\"\"\r\n    Create box plot of some performance metrics of the strategy.\r\n    The width of the box whiskers is determined by a bootstrap.\r\n\r\n    Parameters\r\n    ----------\r\n    returns : pd.Series\r\n        Daily returns of the strategy, noncumulative.\r\n         - See full explanation in tears.create_full_tear_sheet.\r\n    factor_returns : pd.Series\r\n        Daily noncumulative returns of the benchmark factor to which betas are\r\n        computed. Usually a benchmark such as market returns.\r\n         - This is in the same style as returns.\r\n    ax : matplotlib.Axes, optional\r\n        Axes upon which to plot.\r\n\r\n    Returns\r\n    -------\r\n    ax : matplotlib.Axes\r\n        The axes that were plotted on.\r\n    \"\"\"\r\n\r\n    if ax is None:\r\n        ax = plt.gca()\r\n\r\n    bootstrap_values = timeseries.perf_stats_bootstrap(\r\n        returns, factor_returns, return_stats=False\r\n    )\r\n    bootstrap_values = bootstrap_values.drop(\"Kurtosis\", axis=\"columns\")\r\n\r\n    sns.boxplot(data=bootstrap_values, orient=\"h\", ax=ax)\r\n\r\n    return ax\r\n\r\n\r\nSTAT_FUNCS_PCT = &#91;\r\n    \"Annual return\",\r\n    \"Cumulative returns\",\r\n    \"Annual volatility\",\r\n    \"Max drawdown\",\r\n    \"Daily value at risk\",\r\n    \"Daily turnover\",\r\n]\r\n\r\n\r\ndef show_perf_stats(\r\n    returns,\r\n    factor_returns=None,\r\n    positions=None,\r\n    transactions=None,\r\n    turnover_denom=\"AGB\",\r\n    live_start_date=None,\r\n    bootstrap=False,\r\n    header_rows=None,\r\n):\r\n    \"\"\"\r\n    Prints some performance metrics of the strategy.\r\n\r\n    - Shows amount of time the strategy has been run in backtest and\r\n      out-of-sample (in live trading).\r\n\r\n    - Shows Omega ratio, max drawdown, Calmar ratio, annual return,\r\n      stability, Sharpe ratio, annual volatility, alpha, and beta.\r\n\r\n    Parameters\r\n    ----------\r\n    returns : pd.Series\r\n        Daily returns of the strategy, noncumulative.\r\n         - See full explanation in tears.create_full_tear_sheet.\r\n    factor_returns : pd.Series, optional\r\n        Daily noncumulative returns of the benchmark factor to which betas are\r\n        computed. Usually a benchmark such as market returns.\r\n         - This is in the same style as returns.\r\n    positions : pd.DataFrame, optional\r\n        Daily net position values.\r\n         - See full explanation in create_full_tear_sheet.\r\n    transactions : pd.DataFrame, optional\r\n        Prices and amounts of executed trades. One row per trade.\r\n        - See full explanation in tears.create_full_tear_sheet\r\n    turnover_denom : str, optional\r\n        Either AGB or portfolio_value, default AGB.\r\n        - See full explanation in txn.get_turnover.\r\n    live_start_date : datetime, optional\r\n        The point in time when the strategy began live trading, after\r\n        its backtest period.\r\n    bootstrap : boolean, optional\r\n        Whether to perform bootstrap analysis for the performance\r\n        metrics.\r\n         - For more information, see timeseries.perf_stats_bootstrap\r\n    header_rows : dict or OrderedDict, optional\r\n        Extra rows to display at the top of the displayed table.\r\n    \"\"\"\r\n\r\n    if bootstrap:\r\n        perf_func = timeseries.perf_stats_bootstrap\r\n    else:\r\n        perf_func = timeseries.perf_stats\r\n\r\n    perf_stats_all = perf_func(\r\n        returns,\r\n        factor_returns=factor_returns,\r\n        positions=positions,\r\n        transactions=transactions,\r\n        turnover_denom=turnover_denom,\r\n    )\r\n\r\n    date_rows = OrderedDict()\r\n    if len(returns.index) > 0:\r\n        date_rows&#91;\"Start date\"] = returns.index&#91;0].strftime(\"%Y-%m-%d\")\r\n        date_rows&#91;\"End date\"] = returns.index&#91;-1].strftime(\"%Y-%m-%d\")\r\n\r\n    if live_start_date is not None:\r\n        live_start_date = ep.utils.get_utc_timestamp(live_start_date)\r\n        returns_is = returns&#91;returns.index &lt; live_start_date]\r\n        returns_oos = returns&#91;returns.index >= live_start_date]\r\n\r\n        positions_is = None\r\n        positions_oos = None\r\n        transactions_is = None\r\n        transactions_oos = None\r\n\r\n        if positions is not None:\r\n            positions_is = positions&#91;positions.index &lt; live_start_date]\r\n            positions_oos = positions&#91;positions.index >= live_start_date]\r\n            if transactions is not None:\r\n                transactions_is = transactions&#91;(transactions.index &lt; live_start_date)]\r\n                transactions_oos = transactions&#91;(transactions.index > live_start_date)]\r\n\r\n        perf_stats_is = perf_func(\r\n            returns_is,\r\n            factor_returns=factor_returns,\r\n            positions=positions_is,\r\n            transactions=transactions_is,\r\n            turnover_denom=turnover_denom,\r\n        )\r\n\r\n        perf_stats_oos = perf_func(\r\n            returns_oos,\r\n            factor_returns=factor_returns,\r\n            positions=positions_oos,\r\n            transactions=transactions_oos,\r\n            turnover_denom=turnover_denom,\r\n        )\r\n        if len(returns.index) > 0:\r\n            date_rows&#91;\"In-sample months\"] = int(\r\n                len(returns_is) \/ APPROX_BDAYS_PER_MONTH\r\n            )\r\n            date_rows&#91;\"Out-of-sample months\"] = int(\r\n                len(returns_oos) \/ APPROX_BDAYS_PER_MONTH\r\n            )\r\n\r\n        perf_stats = pd.concat(\r\n            OrderedDict(\r\n                &#91;\r\n                    (\"In-sample\", perf_stats_is),\r\n                    (\"Out-of-sample\", perf_stats_oos),\r\n                    (\"All\", perf_stats_all),\r\n                ]\r\n            ),\r\n            axis=1,\r\n        )\r\n    else:\r\n        if len(returns.index) > 0:\r\n            date_rows&#91;\"Total months\"] = int(len(returns) \/ APPROX_BDAYS_PER_MONTH)\r\n        perf_stats = pd.DataFrame(perf_stats_all, columns=&#91;\"Backtest\"])\r\n\r\n    for column in perf_stats.columns:\r\n        for stat, value in perf_stats&#91;column].items():\r\n            if stat in STAT_FUNCS_PCT:\r\n                perf_stats.loc&#91;stat, column] = str(np.round(value * 100, 3)) + \"%\"\r\n    if header_rows is None:\r\n        header_rows = date_rows\r\n    else:\r\n        header_rows = OrderedDict(header_rows)\r\n        header_rows.update(date_rows)\r\n\r\n    utils.print_table(\r\n        perf_stats,\r\n        float_format=\"{0:.2f}\".format,\r\n        header_rows=header_rows,\r\n    )\r\n\r\n\r\ndef plot_returns(returns, live_start_date=None, ax=None):\r\n    \"\"\"\r\n    Plots raw returns over time.\r\n\r\n    Backtest returns are in green, and out-of-sample (live trading)\r\n    returns are in red.\r\n\r\n    Parameters\r\n    ----------\r\n    returns : pd.Series\r\n        Daily returns of the strategy, noncumulative.\r\n         - See full explanation in tears.create_full_tear_sheet.\r\n    live_start_date : datetime, optional\r\n        The date when the strategy began live trading, after\r\n        its backtest period. This date should be normalized.\r\n    ax : matplotlib.Axes, optional\r\n        Axes upon which to plot.\r\n    **kwargs, optional\r\n        Passed to plotting function.\r\n\r\n    Returns\r\n    -------\r\n    ax : matplotlib.Axes\r\n        The axes that were plotted on.\r\n    \"\"\"\r\n\r\n    if ax is None:\r\n        ax = plt.gca()\r\n\r\n    ax.set_label(\"\")\r\n    ax.set_ylabel(\"Returns\")\r\n\r\n    if live_start_date is not None:\r\n        live_start_date = ep.utils.get_utc_timestamp(live_start_date)\r\n        is_returns = returns.loc&#91;returns.index &lt; live_start_date]\r\n        oos_returns = returns.loc&#91;returns.index >= live_start_date]\r\n        is_returns.plot(ax=ax, color=\"g\")\r\n        oos_returns.plot(ax=ax, color=\"r\")\r\n\r\n    else:\r\n        returns.plot(ax=ax, color=\"g\")\r\n\r\n    return ax\r\n\r\n\r\ndef plot_rolling_returns(\r\n    returns,\r\n    factor_returns=None,\r\n    live_start_date=None,\r\n    logy=False,\r\n    cone_std=None,\r\n    legend_loc=\"best\",\r\n    volatility_match=False,\r\n    cone_function=timeseries.forecast_cone_bootstrap,\r\n    ax=None,\r\n    **kwargs,\r\n):\r\n    \"\"\"\r\n    Plots cumulative rolling returns versus some benchmarks'.\r\n\r\n    Backtest returns are in green, and out-of-sample (live trading)\r\n    returns are in red.\r\n\r\n    Additionally, a non-parametric cone plot may be added to the\r\n    out-of-sample returns region.\r\n\r\n    Parameters\r\n    ----------\r\n    returns : pd.Series\r\n        Daily returns of the strategy, noncumulative.\r\n         - See full explanation in tears.create_full_tear_sheet.\r\n    factor_returns : pd.Series, optional\r\n        Daily noncumulative returns of the benchmark factor to which betas are\r\n        computed. Usually a benchmark such as market returns.\r\n         - This is in the same style as returns.\r\n    live_start_date : datetime, optional\r\n        The date when the strategy began live trading, after\r\n        its backtest period. This date should be normalized.\r\n    logy : bool, optional\r\n        Whether to log-scale the y-axis.\r\n    cone_std : float, or tuple, optional\r\n        If float, The standard deviation to use for the cone plots.\r\n        If tuple, Tuple of standard deviation values to use for the cone plots\r\n         - See timeseries.forecast_cone_bounds for more details.\r\n    legend_loc : matplotlib.loc, optional\r\n        The location of the legend on the plot.\r\n    volatility_match : bool, optional\r\n        Whether to normalize the volatility of the returns to those of the\r\n        benchmark returns. This helps compare strategies with different\r\n        volatilities. Requires passing of benchmark_rets.\r\n    cone_function : function, optional\r\n        Function to use when generating forecast probability cone.\r\n        The function signiture must follow the form:\r\n        def cone(in_sample_returns (pd.Series),\r\n                 days_to_project_forward (int),\r\n                 cone_std= (float, or tuple),\r\n                 starting_value= (int, or float))\r\n        See timeseries.forecast_cone_bootstrap for an example.\r\n    ax : matplotlib.Axes, optional\r\n        Axes upon which to plot.\r\n    **kwargs, optional\r\n        Passed to plotting function.\r\n\r\n    Returns\r\n    -------\r\n    ax : matplotlib.Axes\r\n        The axes that were plotted on.\r\n    \"\"\"\r\n\r\n    if ax is None:\r\n        ax = plt.gca()\r\n\r\n    ax.set_xlabel(\"\")\r\n    ax.set_ylabel(\"Cumulative returns\")\r\n    ax.set_yscale(\"log\" if logy else \"linear\")\r\n\r\n    if volatility_match and factor_returns is None:\r\n        raise ValueError(\"volatility_match requires passing of factor_returns.\")\r\n    elif volatility_match and factor_returns is not None:\r\n        bmark_vol = factor_returns.loc&#91;returns.index].std()\r\n        returns = (returns \/ returns.std()) * bmark_vol\r\n\r\n    cum_rets = ep.cum_returns(returns, 1.0)\r\n\r\n    y_axis_formatter = FuncFormatter(utils.two_dec_places)\r\n    ax.yaxis.set_major_formatter(FuncFormatter(y_axis_formatter))\r\n\r\n    if factor_returns is not None:\r\n        cum_factor_returns = ep.cum_returns(factor_returns&#91;cum_rets.index], 1.0)\r\n        cum_factor_returns.plot(\r\n            lw=2,\r\n            color=\"gray\",\r\n            label=factor_returns.name,\r\n            alpha=0.60,\r\n            ax=ax,\r\n            **kwargs,\r\n        )\r\n\r\n    if live_start_date is not None:\r\n        live_start_date = ep.utils.get_utc_timestamp(live_start_date)\r\n        is_cum_returns = cum_rets.loc&#91;cum_rets.index &lt; live_start_date]\r\n        oos_cum_returns = cum_rets.loc&#91;cum_rets.index >= live_start_date]\r\n    else:\r\n        is_cum_returns = cum_rets\r\n        oos_cum_returns = pd.Series(&#91;], dtype=\"float64\")\r\n\r\n    is_cum_returns.plot(\r\n        lw=2, color=\"forestgreen\", alpha=0.6, label=\"Backtest\", ax=ax, **kwargs\r\n    )\r\n\r\n    if len(oos_cum_returns) > 0:\r\n        oos_cum_returns.plot(\r\n            lw=2, color=\"red\", alpha=0.6, label=\"Live\", ax=ax, **kwargs\r\n        )\r\n\r\n        if cone_std is not None:\r\n            if isinstance(cone_std, (float, int)):\r\n                cone_std = &#91;cone_std]\r\n\r\n            is_returns = returns.loc&#91;returns.index &lt; live_start_date]\r\n            cone_bounds = cone_function(\r\n                is_returns,\r\n                len(oos_cum_returns),\r\n                cone_std=cone_std,\r\n                starting_value=is_cum_returns&#91;-1],\r\n            )\r\n\r\n            cone_bounds = cone_bounds.set_index(oos_cum_returns.index)\r\n            for std in cone_std:\r\n                ax.fill_between(\r\n                    cone_bounds.index,\r\n                    cone_bounds&#91;float(std)],\r\n                    cone_bounds&#91;float(-std)],\r\n                    color=\"steelblue\",\r\n                    alpha=0.5,\r\n                )\r\n\r\n    if legend_loc is not None:\r\n        ax.legend(loc=legend_loc, frameon=True, framealpha=0.5)\r\n    ax.axhline(1.0, linestyle=\"--\", color=\"black\", lw=1)\r\n\r\n    return ax\r\n\r\n\r\ndef plot_rolling_beta(returns, factor_returns, legend_loc=\"best\", ax=None, **kwargs):\r\n    \"\"\"\r\n    Plots the rolling 6-month and 12-month beta versus date.\r\n\r\n    Parameters\r\n    ----------\r\n    returns : pd.Series\r\n        Daily returns of the strategy, noncumulative.\r\n         - See full explanation in tears.create_full_tear_sheet.\r\n    factor_returns : pd.Series\r\n        Daily noncumulative returns of the benchmark factor to which betas are\r\n        computed. Usually a benchmark such as market returns.\r\n         - This is in the same style as returns.\r\n    legend_loc : matplotlib.loc, optional\r\n        The location of the legend on the plot.\r\n    ax : matplotlib.Axes, optional\r\n        Axes upon which to plot.\r\n    **kwargs, optional\r\n        Passed to plotting function.\r\n\r\n    Returns\r\n    -------\r\n    ax : matplotlib.Axes\r\n        The axes that were plotted on.\r\n    \"\"\"\r\n\r\n    if ax is None:\r\n        ax = plt.gca()\r\n\r\n    y_axis_formatter = FuncFormatter(utils.two_dec_places)\r\n    ax.yaxis.set_major_formatter(FuncFormatter(y_axis_formatter))\r\n\r\n    ax.set_title(\"Rolling portfolio beta to \" + str(factor_returns.name))\r\n    ax.set_ylabel(\"Beta\")\r\n    rb_1 = timeseries.rolling_beta(\r\n        returns, factor_returns, rolling_window=APPROX_BDAYS_PER_MONTH * 6\r\n    )\r\n    rb_1.plot(color=\"steelblue\", lw=2, alpha=0.6, ax=ax, **kwargs)\r\n    rb_2 = timeseries.rolling_beta(\r\n        returns, factor_returns, rolling_window=APPROX_BDAYS_PER_MONTH * 12\r\n    )\r\n    rb_2.plot(color=\"grey\", lw=2, alpha=0.4, ax=ax, **kwargs)\r\n    ax.axhline(rb_1.mean(), color=\"steelblue\", linestyle=\"--\", lw=2)\r\n    ax.axhline(1.0, color=\"black\", linestyle=\"--\", lw=1)\r\n\r\n    ax.set_xlabel(\"\")\r\n    ax.legend(\r\n        &#91;\"6-mo\", \"12-mo\", \"6-mo Average\"],\r\n        loc=legend_loc,\r\n        frameon=True,\r\n        framealpha=0.5,\r\n    )\r\n    # ax.set_ylim((-0.5, 1.5))\r\n    return ax\r\n\r\n\r\ndef plot_rolling_volatility(\r\n    returns,\r\n    factor_returns=None,\r\n    rolling_window=APPROX_BDAYS_PER_MONTH * 6,\r\n    legend_loc=\"best\",\r\n    ax=None,\r\n    **kwargs,\r\n):\r\n    \"\"\"\r\n    Plots the rolling volatility versus date.\r\n\r\n    Parameters\r\n    ----------\r\n    returns : pd.Series\r\n        Daily returns of the strategy, noncumulative.\r\n         - See full explanation in tears.create_full_tear_sheet.\r\n    factor_returns : pd.Series, optional\r\n        Daily noncumulative returns of the benchmark factor for which the\r\n        benchmark rolling volatility is computed. Usually a benchmark such\r\n        as market returns.\r\n         - This is in the same style as returns.\r\n    rolling_window : int, optional\r\n        The days window over which to compute the volatility.\r\n    legend_loc : matplotlib.loc, optional\r\n        The location of the legend on the plot.\r\n    ax : matplotlib.Axes, optional\r\n        Axes upon which to plot.\r\n    **kwargs, optional\r\n        Passed to plotting function.\r\n\r\n    Returns\r\n    -------\r\n    ax : matplotlib.Axes\r\n        The axes that were plotted on.\r\n    \"\"\"\r\n\r\n    if ax is None:\r\n        ax = plt.gca()\r\n\r\n    y_axis_formatter = FuncFormatter(utils.two_dec_places)\r\n    ax.yaxis.set_major_formatter(FuncFormatter(y_axis_formatter))\r\n\r\n    rolling_vol_ts = timeseries.rolling_volatility(returns, rolling_window)\r\n    rolling_vol_ts.plot(alpha=0.7, lw=2, color=\"orangered\", ax=ax, **kwargs)\r\n    if factor_returns is not None:\r\n        rolling_vol_ts_factor = timeseries.rolling_volatility(\r\n            factor_returns, rolling_window\r\n        )\r\n        rolling_vol_ts_factor.plot(alpha=0.7, lw=2, color=\"grey\", ax=ax, **kwargs)\r\n\r\n    ax.set_title(\"Rolling volatility (6-month)\")\r\n    ax.axhline(rolling_vol_ts.mean(), color=\"steelblue\", linestyle=\"--\", lw=2)\r\n\r\n    ax.axhline(0.0, color=\"black\", linestyle=\"--\", lw=1, zorder=2)\r\n\r\n    ax.set_ylabel(\"Volatility\")\r\n    ax.set_xlabel(\"\")\r\n    if factor_returns is None:\r\n        ax.legend(\r\n            &#91;\"Volatility\", \"Average volatility\"],\r\n            loc=legend_loc,\r\n            frameon=True,\r\n            framealpha=0.5,\r\n        )\r\n    else:\r\n        ax.legend(\r\n            &#91;\"Volatility\", \"Benchmark volatility\", \"Average volatility\"],\r\n            loc=legend_loc,\r\n            frameon=True,\r\n            framealpha=0.5,\r\n        )\r\n    return ax\r\n\r\n\r\ndef plot_rolling_sharpe(\r\n    returns,\r\n    factor_returns=None,\r\n    rolling_window=APPROX_BDAYS_PER_MONTH * 6,\r\n    legend_loc=\"best\",\r\n    ax=None,\r\n    **kwargs,\r\n):\r\n    \"\"\"\r\n    Plots the rolling Sharpe ratio versus date.\r\n\r\n    Parameters\r\n    ----------\r\n    returns : pd.Series\r\n        Daily returns of the strategy, noncumulative.\r\n         - See full explanation in tears.create_full_tear_sheet.\r\n    factor_returns : pd.Series, optional\r\n        Daily noncumulative returns of the benchmark factor for\r\n        which the benchmark rolling Sharpe is computed. Usually\r\n        a benchmark such as market returns.\r\n         - This is in the same style as returns.\r\n    rolling_window : int, optional\r\n        The days window over which to compute the sharpe ratio.\r\n    legend_loc : matplotlib.loc, optional\r\n        The location of the legend on the plot.\r\n    ax : matplotlib.Axes, optional\r\n        Axes upon which to plot.\r\n    **kwargs, optional\r\n        Passed to plotting function.\r\n\r\n    Returns\r\n    -------\r\n    ax : matplotlib.Axes\r\n        The axes that were plotted on.\r\n    \"\"\"\r\n\r\n    if ax is None:\r\n        ax = plt.gca()\r\n\r\n    y_axis_formatter = FuncFormatter(utils.two_dec_places)\r\n    ax.yaxis.set_major_formatter(FuncFormatter(y_axis_formatter))\r\n\r\n    rolling_sharpe_ts = timeseries.rolling_sharpe(returns, rolling_window)\r\n    rolling_sharpe_ts.plot(alpha=0.7, lw=2, color=\"orangered\", ax=ax, **kwargs)\r\n\r\n    if factor_returns is not None:\r\n        rolling_sharpe_ts_factor = timeseries.rolling_sharpe(\r\n            factor_returns, rolling_window\r\n        )\r\n        rolling_sharpe_ts_factor.plot(alpha=0.7, lw=2, color=\"grey\", ax=ax, **kwargs)\r\n\r\n    ax.set_title(\"Rolling Sharpe ratio (6-month)\")\r\n    ax.axhline(rolling_sharpe_ts.mean(), color=\"steelblue\", linestyle=\"--\", lw=2)\r\n    ax.axhline(0.0, color=\"black\", linestyle=\"--\", lw=1, zorder=2)\r\n\r\n    ax.set_ylabel(\"Sharpe ratio\")\r\n    ax.set_xlabel(\"\")\r\n    if factor_returns is None:\r\n        ax.legend(&#91;\"Sharpe\", \"Average\"], loc=legend_loc, frameon=True, framealpha=0.5)\r\n    else:\r\n        ax.legend(\r\n            &#91;\"Sharpe\", \"Benchmark Sharpe\", \"Average\"],\r\n            loc=legend_loc,\r\n            frameon=True,\r\n            framealpha=0.5,\r\n        )\r\n\r\n    return ax\r\n\r\n\r\ndef plot_gross_leverage(returns, positions, ax=None, **kwargs):\r\n    \"\"\"\r\n    Plots gross leverage versus date.\r\n\r\n    Gross leverage is the sum of long and short exposure per share\r\n    divided by net asset value.\r\n\r\n    Parameters\r\n    ----------\r\n    returns : pd.Series\r\n        Daily returns of the strategy, noncumulative.\r\n         - See full explanation in tears.create_full_tear_sheet.\r\n    positions : pd.DataFrame\r\n        Daily net position values.\r\n         - See full explanation in create_full_tear_sheet.\r\n    ax : matplotlib.Axes, optional\r\n        Axes upon which to plot.\r\n    **kwargs, optional\r\n        Passed to plotting function.\r\n\r\n    Returns\r\n    -------\r\n    ax : matplotlib.Axes\r\n        The axes that were plotted on.\r\n    \"\"\"\r\n\r\n    if ax is None:\r\n        ax = plt.gca()\r\n    gl = timeseries.gross_lev(positions)\r\n    gl.plot(lw=0.5, color=\"limegreen\", legend=False, ax=ax, **kwargs)\r\n\r\n    ax.axhline(gl.mean(), color=\"g\", linestyle=\"--\", lw=3)\r\n\r\n    ax.set_title(\"Gross leverage\")\r\n    ax.set_ylabel(\"Gross leverage\")\r\n    ax.set_xlabel(\"\")\r\n    return ax\r\n\r\n\r\ndef plot_exposures(returns, positions, ax=None, **kwargs):\r\n    \"\"\"\r\n    Plots a cake chart of the long and short exposure.\r\n\r\n    Parameters\r\n    ----------\r\n    returns : pd.Series\r\n        Daily returns of the strategy, noncumulative.\r\n         - See full explanation in tears.create_full_tear_sheet.\r\n    positions_alloc : pd.DataFrame\r\n        Portfolio allocation of positions. See\r\n        pos.get_percent_alloc.\r\n    ax : matplotlib.Axes, optional\r\n        Axes upon which to plot.\r\n    **kwargs, optional\r\n        Passed to plotting function.\r\n\r\n    Returns\r\n    -------\r\n    ax : matplotlib.Axes\r\n        The axes that were plotted on.\r\n    \"\"\"\r\n\r\n    if ax is None:\r\n        ax = plt.gca()\r\n\r\n    pos_no_cash = positions.drop(\"cash\", axis=1)\r\n    l_exp = pos_no_cash&#91;pos_no_cash > 0].sum(axis=1) \/ positions.sum(axis=1)\r\n    s_exp = pos_no_cash&#91;pos_no_cash &lt; 0].sum(axis=1) \/ positions.sum(axis=1)\r\n    net_exp = pos_no_cash.sum(axis=1) \/ positions.sum(axis=1)\r\n\r\n    ax.fill_between(\r\n        l_exp.index, 0, l_exp.values, label=\"Long\", color=\"green\", alpha=0.5\r\n    )\r\n    ax.fill_between(s_exp.index, 0, s_exp.values, label=\"Short\", color=\"red\", alpha=0.5)\r\n    ax.plot(\r\n        net_exp.index,\r\n        net_exp.values,\r\n        label=\"Net\",\r\n        color=\"black\",\r\n        linestyle=\"dotted\",\r\n    )\r\n\r\n    ax.set_xlim((returns.index&#91;0], returns.index&#91;-1]))\r\n    ax.set_title(\"Exposure\")\r\n    ax.set_ylabel(\"Exposure\")\r\n    ax.legend(loc=\"lower left\", frameon=True, framealpha=0.5)\r\n    ax.set_xlabel(\"\")\r\n    return ax\r\n\r\n\r\ndef show_and_plot_top_positions(\r\n    returns,\r\n    positions_alloc,\r\n    show_and_plot=2,\r\n    hide_positions=False,\r\n    legend_loc=\"real_best\",\r\n    ax=None,\r\n    **kwargs,\r\n):\r\n    \"\"\"\r\n    Prints and\/or plots the exposures of the top 10 held positions of\r\n    all time.\r\n\r\n    Parameters\r\n    ----------\r\n    returns : pd.Series\r\n        Daily returns of the strategy, noncumulative.\r\n         - See full explanation in tears.create_full_tear_sheet.\r\n    positions_alloc : pd.DataFrame\r\n        Portfolio allocation of positions. See pos.get_percent_alloc.\r\n    show_and_plot : int, optional\r\n        By default, this is 2, and both prints and plots.\r\n        If this is 0, it will only plot; if 1, it will only print.\r\n    hide_positions : bool, optional\r\n        If True, will not output any symbol names.\r\n    legend_loc : matplotlib.loc, optional\r\n        The location of the legend on the plot.\r\n        By default, the legend will display below the plot.\r\n    ax : matplotlib.Axes, optional\r\n        Axes upon which to plot.\r\n    **kwargs, optional\r\n        Passed to plotting function.\r\n\r\n    Returns\r\n    -------\r\n    ax : matplotlib.Axes, conditional\r\n        The axes that were plotted on.\r\n\r\n    \"\"\"\r\n    positions_alloc = positions_alloc.copy()\r\n    positions_alloc.columns = positions_alloc.columns.map(utils.format_asset)\r\n\r\n    df_top_long, df_top_short, df_top_abs = pos.get_top_long_short_abs(positions_alloc)\r\n\r\n    if show_and_plot == 1 or show_and_plot == 2:\r\n        utils.print_table(\r\n            pd.DataFrame(df_top_long * 100, columns=&#91;\"max\"]),\r\n            float_format=\"{0:.2f}%\".format,\r\n            name=\"Top 10 long positions of all time\",\r\n        )\r\n\r\n        utils.print_table(\r\n            pd.DataFrame(df_top_short * 100, columns=&#91;\"max\"]),\r\n            float_format=\"{0:.2f}%\".format,\r\n            name=\"Top 10 short positions of all time\",\r\n        )\r\n\r\n        utils.print_table(\r\n            pd.DataFrame(df_top_abs * 100, columns=&#91;\"max\"]),\r\n            float_format=\"{0:.2f}%\".format,\r\n            name=\"Top 10 positions of all time\",\r\n        )\r\n\r\n    if show_and_plot == 0 or show_and_plot == 2:\r\n\r\n        if ax is None:\r\n            ax = plt.gca()\r\n\r\n        positions_alloc&#91;df_top_abs.index].plot(\r\n            title=\"Portfolio allocation over time, only top 10 holdings\",\r\n            alpha=0.5,\r\n            ax=ax,\r\n            **kwargs,\r\n        )\r\n\r\n        # Place legend below plot, shrink plot by 20%\r\n        if legend_loc == \"real_best\":\r\n            box = ax.get_position()\r\n            ax.set_position(\r\n                &#91;\r\n                    box.x0,\r\n                    box.y0 + box.height * 0.1,\r\n                    box.width,\r\n                    box.height * 0.9,\r\n                ]\r\n            )\r\n\r\n            # Put a legend below current axis\r\n            ax.legend(\r\n                loc=\"upper center\",\r\n                frameon=True,\r\n                framealpha=0.5,\r\n                bbox_to_anchor=(0.5, -0.14),\r\n                ncol=5,\r\n            )\r\n        else:\r\n            ax.legend(loc=legend_loc)\r\n\r\n        ax.set_xlim((returns.index&#91;0], returns.index&#91;-1]))\r\n        ax.set_ylabel(\"Exposure by holding\")\r\n\r\n        if hide_positions:\r\n            ax.legend_.remove()\r\n\r\n        return ax\r\n\r\n\r\ndef plot_max_median_position_concentration(positions, ax=None, **kwargs):\r\n    \"\"\"\r\n    Plots the max and median of long and short position concentrations\r\n    over the time.\r\n\r\n    Parameters\r\n    ----------\r\n    positions : pd.DataFrame\r\n        The positions that the strategy takes over time.\r\n    ax : matplotlib.Axes, optional\r\n        Axes upon which to plot.\r\n\r\n    Returns\r\n    -------\r\n    ax : matplotlib.Axes\r\n        The axes that were plotted on.\r\n    \"\"\"\r\n\r\n    if ax is None:\r\n        ax = plt.gca()\r\n\r\n    alloc_summary = pos.get_max_median_position_concentration(positions)\r\n    colors = &#91;\"mediumblue\", \"steelblue\", \"tomato\", \"firebrick\"]\r\n    alloc_summary.plot(linewidth=1, color=colors, alpha=0.6, ax=ax)\r\n\r\n    ax.legend(loc=\"center left\", frameon=True, framealpha=0.5)\r\n    ax.set_ylabel(\"Exposure\")\r\n    ax.set_title(\"Long\/short max and median position concentration\")\r\n\r\n    return ax\r\n\r\n\r\ndef plot_sector_allocations(returns, sector_alloc, ax=None, **kwargs):\r\n    \"\"\"\r\n    Plots the sector exposures of the portfolio over time.\r\n\r\n    Parameters\r\n    ----------\r\n    returns : pd.Series\r\n        Daily returns of the strategy, noncumulative.\r\n         - See full explanation in tears.create_full_tear_sheet.\r\n    sector_alloc : pd.DataFrame\r\n        Portfolio allocation of positions. See pos.get_sector_alloc.\r\n    ax : matplotlib.Axes, optional\r\n        Axes upon which to plot.\r\n    **kwargs, optional\r\n        Passed to plotting function.\r\n\r\n    Returns\r\n    -------\r\n    ax : matplotlib.Axes\r\n        The axes that were plotted on.\r\n    \"\"\"\r\n\r\n    if ax is None:\r\n        ax = plt.gca()\r\n\r\n    sector_alloc.plot(title=\"Sector allocation over time\", alpha=0.5, ax=ax, **kwargs)\r\n\r\n    box = ax.get_position()\r\n    ax.set_position(&#91;box.x0, box.y0 + box.height * 0.1, box.width, box.height * 0.9])\r\n\r\n    # Put a legend below current axis\r\n    ax.legend(\r\n        loc=\"upper center\",\r\n        frameon=True,\r\n        framealpha=0.5,\r\n        bbox_to_anchor=(0.5, -0.14),\r\n        ncol=5,\r\n    )\r\n\r\n    ax.set_xlim((sector_alloc.index&#91;0], sector_alloc.index&#91;-1]))\r\n    ax.set_ylabel(\"Exposure by sector\")\r\n    ax.set_xlabel(\"\")\r\n\r\n    return ax\r\n\r\n\r\ndef plot_return_quantiles(returns, live_start_date=None, ax=None, **kwargs):\r\n    \"\"\"\r\n    Creates a box plot of daily, weekly, and monthly return\r\n    distributions.\r\n\r\n    Parameters\r\n    ----------\r\n    returns : pd.Series\r\n        Daily returns of the strategy, noncumulative.\r\n         - See full explanation in tears.create_full_tear_sheet.\r\n    live_start_date : datetime, optional\r\n        The point in time when the strategy began live trading, after\r\n        its backtest period.\r\n    ax : matplotlib.Axes, optional\r\n        Axes upon which to plot.\r\n    **kwargs, optional\r\n        Passed to seaborn plotting function.\r\n\r\n    Returns\r\n    -------\r\n    ax : matplotlib.Axes\r\n        The axes that were plotted on.\r\n    \"\"\"\r\n\r\n    if ax is None:\r\n        ax = plt.gca()\r\n\r\n    is_returns = (\r\n        returns\r\n        if live_start_date is None\r\n        else returns.loc&#91;returns.index &lt; live_start_date]\r\n    )\r\n    is_weekly = ep.aggregate_returns(is_returns, \"weekly\")\r\n    is_monthly = ep.aggregate_returns(is_returns, \"monthly\")\r\n    sns.boxplot(\r\n        data=&#91;is_returns, is_weekly, is_monthly],\r\n        palette=&#91;\"#4c72B0\", \"#55A868\", \"#CCB974\"],\r\n        ax=ax,\r\n        **kwargs,\r\n    )\r\n\r\n    if live_start_date is not None:\r\n        oos_returns = returns.loc&#91;returns.index >= live_start_date]\r\n        oos_weekly = ep.aggregate_returns(oos_returns, \"weekly\")\r\n        oos_monthly = ep.aggregate_returns(oos_returns, \"monthly\")\r\n\r\n        sns.swarmplot(\r\n            data=&#91;oos_returns, oos_weekly, oos_monthly],\r\n            ax=ax,\r\n            palette=\"dark:red\",\r\n            marker=\"d\",\r\n            **kwargs,\r\n        )\r\n        red_dots = matplotlib.lines.Line2D(\r\n            &#91;],\r\n            &#91;],\r\n            color=\"red\",\r\n            marker=\"d\",\r\n            label=\"Out-of-sample data\",\r\n            linestyle=\"\",\r\n        )\r\n        ax.legend(handles=&#91;red_dots], frameon=True, framealpha=0.5)\r\n    ax.set_xticklabels(&#91;\"Daily\", \"Weekly\", \"Monthly\"])\r\n    ax.set_title(\"Return quantiles\")\r\n\r\n    return ax\r\n\r\n\r\ndef plot_turnover(\r\n    returns,\r\n    transactions,\r\n    positions,\r\n    turnover_denom=\"AGB\",\r\n    legend_loc=\"best\",\r\n    ax=None,\r\n    **kwargs,\r\n):\r\n    \"\"\"\r\n    Plots turnover vs. date.\r\n\r\n    Turnover is the number of shares traded for a period as a fraction\r\n    of total shares.\r\n\r\n    Displays daily total, daily average per month, and all-time daily\r\n    average.\r\n\r\n    Parameters\r\n    ----------\r\n    returns : pd.Series\r\n        Daily returns of the strategy, noncumulative.\r\n         - See full explanation in tears.create_full_tear_sheet.\r\n    transactions : pd.DataFrame\r\n        Prices and amounts of executed trades. One row per trade.\r\n         - See full explanation in tears.create_full_tear_sheet.\r\n    positions : pd.DataFrame\r\n        Daily net position values.\r\n         - See full explanation in tears.create_full_tear_sheet.\r\n    turnover_denom : str, optional\r\n        Either AGB or portfolio_value, default AGB.\r\n        - See full explanation in txn.get_turnover.\r\n    legend_loc : matplotlib.loc, optional\r\n        The location of the legend on the plot.\r\n    ax : matplotlib.Axes, optional\r\n        Axes upon which to plot.\r\n    **kwargs, optional\r\n        Passed to plotting function.\r\n\r\n    Returns\r\n    -------\r\n    ax : matplotlib.Axes\r\n        The axes that were plotted on.\r\n    \"\"\"\r\n\r\n    if ax is None:\r\n        ax = plt.gca()\r\n\r\n    y_axis_formatter = FuncFormatter(utils.two_dec_places)\r\n    ax.yaxis.set_major_formatter(FuncFormatter(y_axis_formatter))\r\n\r\n    df_turnover = txn.get_turnover(positions, transactions, turnover_denom)\r\n    df_turnover_by_month = df_turnover.resample(\"M\").mean()\r\n    df_turnover.plot(color=\"steelblue\", alpha=1.0, lw=0.5, ax=ax, **kwargs)\r\n    df_turnover_by_month.plot(color=\"orangered\", alpha=0.5, lw=2, ax=ax, **kwargs)\r\n    ax.axhline(df_turnover.mean(), color=\"steelblue\", linestyle=\"--\", lw=3, alpha=1.0)\r\n    ax.legend(\r\n        &#91;\r\n            \"Daily turnover\",\r\n            \"Average daily turnover, by month\",\r\n            \"Average daily turnover, net\",\r\n        ],\r\n        loc=legend_loc,\r\n        frameon=True,\r\n        framealpha=0.5,\r\n    )\r\n    ax.set_title(\"Daily turnover\")\r\n    ax.set_xlim((returns.index&#91;0], returns.index&#91;-1]))\r\n    ax.set_ylim((0, 2))\r\n    ax.set_ylabel(\"Turnover\")\r\n    ax.set_xlabel(\"\")\r\n    return ax\r\n\r\n\r\ndef plot_slippage_sweep(\r\n    returns,\r\n    positions,\r\n    transactions,\r\n    slippage_params=(3, 8, 10, 12, 15, 20, 50),\r\n    ax=None,\r\n    **kwargs,\r\n):\r\n    \"\"\"\r\n    Plots equity curves at different per-dollar slippage assumptions.\r\n\r\n    Parameters\r\n    ----------\r\n    returns : pd.Series\r\n        Timeseries of portfolio returns to be adjusted for various\r\n        degrees of slippage.\r\n    positions : pd.DataFrame\r\n        Daily net position values.\r\n         - See full explanation in tears.create_full_tear_sheet.\r\n    transactions : pd.DataFrame\r\n        Prices and amounts of executed trades. One row per trade.\r\n         - See full explanation in tears.create_full_tear_sheet.\r\n    slippage_params: tuple\r\n        Slippage pameters to apply to the return time series (in\r\n        basis points).\r\n    ax : matplotlib.Axes, optional\r\n        Axes upon which to plot.\r\n    **kwargs, optional\r\n        Passed to seaborn plotting function.\r\n\r\n    Returns\r\n    -------\r\n    ax : matplotlib.Axes\r\n        The axes that were plotted on.\r\n    \"\"\"\r\n\r\n    if ax is None:\r\n        ax = plt.gca()\r\n\r\n    slippage_sweep = pd.DataFrame()\r\n    for bps in slippage_params:\r\n        adj_returns = txn.adjust_returns_for_slippage(\r\n            returns, positions, transactions, bps\r\n        )\r\n        label = str(bps) + \" bps\"\r\n        slippage_sweep&#91;label] = ep.cum_returns(adj_returns, 1)\r\n\r\n    slippage_sweep.plot(alpha=1.0, lw=0.5, ax=ax)\r\n\r\n    ax.set_title(\"Cumulative returns given additional per-dollar slippage\")\r\n    ax.set_ylabel(\"\")\r\n\r\n    ax.legend(loc=\"center left\", frameon=True, framealpha=0.5)\r\n\r\n    return ax\r\n\r\n\r\ndef plot_slippage_sensitivity(returns, positions, transactions, ax=None, **kwargs):\r\n    \"\"\"\r\n    Plots curve relating per-dollar slippage to average annual returns.\r\n\r\n    Parameters\r\n    ----------\r\n    returns : pd.Series\r\n        Timeseries of portfolio returns to be adjusted for various\r\n        degrees of slippage.\r\n    positions : pd.DataFrame\r\n        Daily net position values.\r\n         - See full explanation in tears.create_full_tear_sheet.\r\n    transactions : pd.DataFrame\r\n        Prices and amounts of executed trades. One row per trade.\r\n         - See full explanation in tears.create_full_tear_sheet.\r\n    ax : matplotlib.Axes, optional\r\n        Axes upon which to plot.\r\n    **kwargs, optional\r\n        Passed to seaborn plotting function.\r\n\r\n    Returns\r\n    -------\r\n    ax : matplotlib.Axes\r\n        The axes that were plotted on.\r\n    \"\"\"\r\n\r\n    if ax is None:\r\n        ax = plt.gca()\r\n\r\n    avg_returns_given_slippage = pd.Series(dtype=\"float64\")\r\n    for bps in range(1, 100):\r\n        adj_returns = txn.adjust_returns_for_slippage(\r\n            returns, positions, transactions, bps\r\n        )\r\n        avg_returns = ep.annual_return(adj_returns)\r\n        avg_returns_given_slippage.loc&#91;bps] = avg_returns\r\n\r\n    avg_returns_given_slippage.plot(alpha=1.0, lw=2, ax=ax)\r\n\r\n    ax.set_title(\"Average annual returns given additional per-dollar slippage\")\r\n    ax.set_xticks(np.arange(0, 100, 10))\r\n    ax.set_ylabel(\"Average annual return\")\r\n    ax.set_xlabel(\"Per-dollar slippage (bps)\")\r\n\r\n    return ax\r\n\r\n\r\ndef plot_capacity_sweep(\r\n    returns,\r\n    transactions,\r\n    market_data,\r\n    bt_starting_capital,\r\n    min_pv=100000,\r\n    max_pv=300000000,\r\n    step_size=1000000,\r\n    ax=None,\r\n):\r\n    txn_daily_w_bar = capacity.daily_txns_with_bar_data(transactions, market_data)\r\n\r\n    captial_base_sweep = pd.Series()\r\n    for start_pv in range(min_pv, max_pv, step_size):\r\n        adj_ret = capacity.apply_slippage_penalty(\r\n            returns, txn_daily_w_bar, start_pv, bt_starting_capital\r\n        )\r\n        sharpe = ep.sharpe_ratio(adj_ret)\r\n        if sharpe &lt; -1:\r\n            break\r\n        captial_base_sweep.loc&#91;start_pv] = sharpe\r\n    captial_base_sweep.index = captial_base_sweep.index \/ MM_DISPLAY_UNIT\r\n\r\n    if ax is None:\r\n        ax = plt.gca()\r\n\r\n    captial_base_sweep.plot(ax=ax)\r\n    ax.set_xlabel(\"Capital base ($mm)\")\r\n    ax.set_ylabel(\"Sharpe ratio\")\r\n    ax.set_title(\"Capital base performance sweep\")\r\n\r\n    return ax\r\n\r\n\r\ndef plot_daily_turnover_hist(\r\n    transactions, positions, turnover_denom=\"AGB\", ax=None, **kwargs\r\n):\r\n    \"\"\"\r\n    Plots a histogram of daily turnover rates.\r\n\r\n    Parameters\r\n    ----------\r\n    transactions : pd.DataFrame\r\n        Prices and amounts of executed trades. One row per trade.\r\n         - See full explanation in tears.create_full_tear_sheet.\r\n    positions : pd.DataFrame\r\n        Daily net position values.\r\n         - See full explanation in tears.create_full_tear_sheet.\r\n    turnover_denom : str, optional\r\n        Either AGB or portfolio_value, default AGB.\r\n        - See full explanation in txn.get_turnover.\r\n    ax : matplotlib.Axes, optional\r\n        Axes upon which to plot.\r\n    **kwargs, optional\r\n        Passed to seaborn plotting function.\r\n\r\n    Returns\r\n    -------\r\n    ax : matplotlib.Axes\r\n        The axes that were plotted on.\r\n    \"\"\"\r\n\r\n    if ax is None:\r\n        ax = plt.gca()\r\n    turnover = txn.get_turnover(positions, transactions, turnover_denom)\r\n    sns.histplot(turnover, ax=ax, **kwargs)\r\n    ax.set_title(\"Distribution of daily turnover rates\")\r\n    ax.set_xlabel(\"Turnover rate\")\r\n    return ax\r\n\r\n\r\ndef plot_daily_volume(returns, transactions, ax=None, **kwargs):\r\n    \"\"\"\r\n    Plots trading volume per day vs. date.\r\n\r\n    Also displays all-time daily average.\r\n\r\n    Parameters\r\n    ----------\r\n    returns : pd.Series\r\n        Daily returns of the strategy, noncumulative.\r\n         - See full explanation in tears.create_full_tear_sheet.\r\n    transactions : pd.DataFrame\r\n        Prices and amounts of executed trades. One row per trade.\r\n         - See full explanation in tears.create_full_tear_sheet.\r\n    ax : matplotlib.Axes, optional\r\n        Axes upon which to plot.\r\n    **kwargs, optional\r\n        Passed to plotting function.\r\n\r\n    Returns\r\n    -------\r\n    ax : matplotlib.Axes\r\n        The axes that were plotted on.\r\n    \"\"\"\r\n\r\n    if ax is None:\r\n        ax = plt.gca()\r\n    daily_txn = txn.get_txn_vol(transactions)\r\n    daily_txn.txn_shares.plot(alpha=1.0, lw=0.5, ax=ax, **kwargs)\r\n    ax.axhline(\r\n        daily_txn.txn_shares.mean(),\r\n        color=\"steelblue\",\r\n        linestyle=\"--\",\r\n        lw=3,\r\n        alpha=1.0,\r\n    )\r\n    ax.set_title(\"Daily trading volume\")\r\n    ax.set_xlim((returns.index&#91;0], returns.index&#91;-1]))\r\n    ax.set_ylabel(\"Amount of shares traded\")\r\n    ax.set_xlabel(\"\")\r\n    return ax\r\n\r\n\r\ndef plot_txn_time_hist(\r\n    transactions, bin_minutes=5, tz=\"America\/New_York\", ax=None, **kwargs\r\n):\r\n    \"\"\"\r\n    Plots a histogram of transaction times, binning the times into\r\n    buckets of a given duration.\r\n\r\n    Parameters\r\n    ----------\r\n    transactions : pd.DataFrame\r\n        Prices and amounts of executed trades. One row per trade.\r\n         - See full explanation in tears.create_full_tear_sheet.\r\n    bin_minutes : float, optional\r\n        Sizes of the bins in minutes, defaults to 5 minutes.\r\n    tz : str, optional\r\n        Time zone to plot against. Note that if the specified\r\n        zone does not apply daylight savings, the distribution\r\n        may be partially offset.\r\n    ax : matplotlib.Axes, optional\r\n        Axes upon which to plot.\r\n    **kwargs, optional\r\n        Passed to plotting function.\r\n\r\n    Returns\r\n    -------\r\n    ax : matplotlib.Axes\r\n        The axes that were plotted on.\r\n    \"\"\"\r\n\r\n    if ax is None:\r\n        ax = plt.gca()\r\n\r\n    txn_time = transactions.copy()\r\n\r\n    txn_time.index = txn_time.index.tz_convert(pytz.timezone(tz))\r\n    txn_time.index = txn_time.index.map(lambda x: x.hour * 60 + x.minute)\r\n    txn_time&#91;\"trade_value\"] = (txn_time.amount * txn_time.price).abs()\r\n    txn_time = (\r\n        txn_time.groupby(level=0).sum(numeric_only=True).reindex(index=range(570, 961))\r\n    )\r\n    txn_time.index = (txn_time.index \/ bin_minutes).astype(int) * bin_minutes\r\n    txn_time = txn_time.groupby(level=0).sum(numeric_only=True)\r\n\r\n    txn_time&#91;\"time_str\"] = txn_time.index.map(\r\n        lambda x: str(datetime.time(int(x \/ 60), x % 60))&#91;:-3]\r\n    )\r\n\r\n    trade_value_sum = txn_time.trade_value.sum()\r\n    txn_time.trade_value = txn_time.trade_value.fillna(0) \/ trade_value_sum\r\n\r\n    ax.bar(txn_time.index, txn_time.trade_value, width=bin_minutes, **kwargs)\r\n\r\n    ax.set_xlim(570, 960)\r\n    ax.set_xticks(txn_time.index&#91;:: int(30 \/ bin_minutes)])\r\n    ax.set_xticklabels(txn_time.time_str&#91;:: int(30 \/ bin_minutes)])\r\n    ax.set_title(\"Transaction time distribution\")\r\n    ax.set_ylabel(\"Proportion\")\r\n    ax.set_xlabel(\"\")\r\n    return ax\r\n\r\n\r\ndef show_worst_drawdown_periods(returns, top=5):\r\n    \"\"\"\r\n    Prints information about the worst drawdown periods.\r\n\r\n    Prints peak dates, valley dates, recovery dates, and net\r\n    drawdowns.\r\n\r\n    Parameters\r\n    ----------\r\n    returns : pd.Series\r\n        Daily returns of the strategy, noncumulative.\r\n         - See full explanation in tears.create_full_tear_sheet.\r\n    top : int, optional\r\n        Amount of top drawdowns periods to plot (default 5).\r\n    \"\"\"\r\n\r\n    drawdown_df = timeseries.gen_drawdown_table(returns, top=top)\r\n    utils.print_table(\r\n        drawdown_df.sort_values(\"Net drawdown in %\", ascending=False),\r\n        name=\"Worst drawdown periods\",\r\n        float_format=\"{0:.2f}\".format,\r\n    )\r\n\r\n\r\ndef plot_monthly_returns_timeseries(returns, ax=None, **kwargs):\r\n    \"\"\"\r\n    Plots monthly returns as a timeseries.\r\n\r\n    Parameters\r\n    ----------\r\n    returns : pd.Series\r\n        Daily returns of the strategy, noncumulative.\r\n         - See full explanation in tears.create_full_tear_sheet.\r\n    ax : matplotlib.Axes, optional\r\n        Axes upon which to plot.\r\n    **kwargs, optional\r\n        Passed to seaborn plotting function.\r\n\r\n    Returns\r\n    -------\r\n    ax : matplotlib.Axes\r\n        The axes that were plotted on.\r\n    \"\"\"\r\n\r\n    def cumulate_returns(x):\r\n        return ep.cum_returns(x)&#91;-1]\r\n\r\n    if ax is None:\r\n        ax = plt.gca()\r\n\r\n    monthly_rets = returns.resample(\"M\").apply(lambda x: cumulate_returns(x))\r\n    monthly_rets = monthly_rets.to_period()\r\n\r\n    sns.barplot(x=monthly_rets.index, y=monthly_rets.values, color=\"steelblue\")\r\n\r\n    _, labels = plt.xticks()\r\n    plt.setp(labels, rotation=90)\r\n\r\n    # only show x-labels on year boundary\r\n    xticks_coord = &#91;]\r\n    xticks_label = &#91;]\r\n    count = 0\r\n    for i in monthly_rets.index:\r\n        if i.month == 1:\r\n            xticks_label.append(i)\r\n            xticks_coord.append(count)\r\n            # plot yearly boundary line\r\n            ax.axvline(count, color=\"gray\", ls=\"--\", alpha=0.3)\r\n\r\n        count += 1\r\n\r\n    ax.axhline(0.0, color=\"darkgray\", ls=\"-\")\r\n    ax.set_xticks(xticks_coord)\r\n    ax.set_xticklabels(xticks_label)\r\n\r\n    return ax\r\n\r\n\r\ndef plot_round_trip_lifetimes(round_trips, disp_amount=16, lsize=18, ax=None):\r\n    \"\"\"\r\n    Plots timespans and directions of a sample of round trip trades.\r\n\r\n    Parameters\r\n    ----------\r\n    round_trips : pd.DataFrame\r\n        DataFrame with one row per round trip trade.\r\n        - See full explanation in round_trips.extract_round_trips\r\n    ax : matplotlib.Axes, optional\r\n        Axes upon which to plot.\r\n\r\n    Returns\r\n    -------\r\n    ax : matplotlib.Axes\r\n        The axes that were plotted on.\r\n    \"\"\"\r\n\r\n    if ax is None:\r\n        ax = plt.subplot()\r\n\r\n    symbols_sample = round_trips.symbol.unique()\r\n    np.random.seed(1)\r\n    sample = np.random.choice(\r\n        round_trips.symbol.unique(),\r\n        replace=False,\r\n        size=min(disp_amount, len(symbols_sample)),\r\n    )\r\n    sample_round_trips = round_trips&#91;round_trips.symbol.isin(sample)]\r\n\r\n    symbol_idx = pd.Series(np.arange(len(sample)), index=sample)\r\n\r\n    for symbol, sym_round_trips in sample_round_trips.groupby(\"symbol\"):\r\n        for _, row in sym_round_trips.iterrows():\r\n            c = \"b\" if row.long else \"r\"\r\n            y_ix = symbol_idx&#91;symbol] + 0.05\r\n            ax.plot(\r\n                &#91;row&#91;\"open_dt\"], row&#91;\"close_dt\"]],\r\n                &#91;y_ix, y_ix],\r\n                color=c,\r\n                linewidth=lsize,\r\n                solid_capstyle=\"butt\",\r\n            )\r\n\r\n    ax.set_yticks(range(len(sample)))\r\n    ax.set_yticklabels(&#91;utils.format_asset(s) for s in sample])\r\n\r\n    ax.set_ylim((-0.5, min(len(sample), disp_amount) - 0.5))\r\n    blue = patches.Rectangle(&#91;0, 0], 1, 1, color=\"b\", label=\"Long\")\r\n    red = patches.Rectangle(&#91;0, 0], 1, 1, color=\"r\", label=\"Short\")\r\n    leg = ax.legend(handles=&#91;blue, red], loc=\"lower left\", frameon=True, framealpha=0.5)\r\n    leg.get_frame().set_edgecolor(\"black\")\r\n    ax.grid(False)\r\n\r\n    return ax\r\n\r\n\r\ndef show_profit_attribution(round_trips):\r\n    \"\"\"\r\n    Prints the share of total PnL contributed by each\r\n    traded name.\r\n\r\n    Parameters\r\n    ----------\r\n    round_trips : pd.DataFrame\r\n        DataFrame with one row per round trip trade.\r\n        - See full explanation in round_trips.extract_round_trips\r\n    ax : matplotlib.Axes, optional\r\n        Axes upon which to plot.\r\n\r\n    Returns\r\n    -------\r\n    ax : matplotlib.Axes\r\n        The axes that were plotted on.\r\n    \"\"\"\r\n\r\n    total_pnl = round_trips&#91;\"pnl\"].sum()\r\n    pnl_attribution = round_trips.groupby(\"symbol\")&#91;\"pnl\"].sum() \/ total_pnl\r\n    pnl_attribution.name = \"\"\r\n\r\n    pnl_attribution.index = pnl_attribution.index.map(utils.format_asset)\r\n    utils.print_table(\r\n        pnl_attribution.sort_values(\r\n            inplace=False,\r\n            ascending=False,\r\n        ),\r\n        name=\"Profitability (PnL \/ PnL total) per name\",\r\n        float_format=\"{:.2%}\".format,\r\n    )\r\n\r\n\r\ndef plot_prob_profit_trade(round_trips, ax=None):\r\n    \"\"\"\r\n    Plots a probability distribution for the event of making\r\n    a profitable trade.\r\n\r\n    Parameters\r\n    ----------\r\n    round_trips : pd.DataFrame\r\n        DataFrame with one row per round trip trade.\r\n        - See full explanation in round_trips.extract_round_trips\r\n    ax : matplotlib.Axes, optional\r\n        Axes upon which to plot.\r\n\r\n    Returns\r\n    -------\r\n    ax : matplotlib.Axes\r\n        The axes that were plotted on.\r\n    \"\"\"\r\n\r\n    x = np.linspace(0, 1.0, 500)\r\n\r\n    round_trips&#91;\"profitable\"] = round_trips.pnl > 0\r\n\r\n    dist = sp.stats.beta(round_trips.profitable.sum(), (~round_trips.profitable).sum())\r\n    y = dist.pdf(x)\r\n    lower_perc = dist.ppf(0.025)\r\n    upper_perc = dist.ppf(0.975)\r\n\r\n    lower_plot = dist.ppf(0.001)\r\n    upper_plot = dist.ppf(0.999)\r\n\r\n    if ax is None:\r\n        ax = plt.subplot()\r\n\r\n    ax.plot(x, y)\r\n    ax.axvline(lower_perc, color=\"0.5\")\r\n    ax.axvline(upper_perc, color=\"0.5\")\r\n\r\n    ax.set_xlabel(\"Probability of making a profitable decision\")\r\n    ax.set_ylabel(\"Belief\")\r\n    ax.set_xlim(lower_plot, upper_plot)\r\n    ax.set_ylim((0, y.max() + 1.0))\r\n\r\n    return ax\r\n\r\n\r\ndef plot_cones(\r\n    name,\r\n    bounds,\r\n    oos_returns,\r\n    num_samples=1000,\r\n    ax=None,\r\n    cone_std=(1.0, 1.5, 2.0),\r\n    random_seed=None,\r\n    num_strikes=3,\r\n):\r\n    \"\"\"\r\n    Plots the upper and lower bounds of an n standard deviation\r\n    cone of forecasted cumulative returns. Redraws a new cone when\r\n    cumulative returns fall outside of last cone drawn.\r\n\r\n    Parameters\r\n    ----------\r\n    name : str\r\n        Account name to be used as figure title.\r\n    bounds : pandas.core.frame.DataFrame\r\n        Contains upper and lower cone boundaries. Column names are\r\n        strings corresponding to the number of standard devations\r\n        above (positive) or below (negative) the projected mean\r\n        cumulative returns.\r\n    oos_returns : pandas.core.frame.DataFrame\r\n        Non-cumulative out-of-sample returns.\r\n    num_samples : int\r\n        Number of samples to draw from the in-sample daily returns.\r\n        Each sample will be an array with length num_days.\r\n        A higher number of samples will generate a more accurate\r\n        bootstrap cone.\r\n    ax : matplotlib.Axes, optional\r\n        Axes upon which to plot.\r\n    cone_std : list of int\/float\r\n        Number of standard devations to use in the boundaries of\r\n        the cone. If multiple values are passed, cone bounds will\r\n        be generated for each value.\r\n    random_seed : int\r\n        Seed for the pseudorandom number generator used by the pandas\r\n        sample method.\r\n    num_strikes : int\r\n        Upper limit for number of cones drawn. Can be anything from 0 to 3.\r\n\r\n    Returns\r\n    -------\r\n    Returns are either an ax or fig option, but not both. If a\r\n    matplotlib.Axes instance is passed in as ax, then it will be modified\r\n    and returned. This allows for users to plot interactively in jupyter\r\n    notebook. When no ax object is passed in, a matplotlib.figure instance\r\n    is generated and returned. This figure can then be used to save\r\n    the plot as an image without viewing it.\r\n\r\n    ax : matplotlib.Axes\r\n        The axes that were plotted on.\r\n    fig : matplotlib.figure\r\n        The figure instance which contains all the plot elements.\r\n    \"\"\"\r\n\r\n    if ax is None:\r\n        fig = figure.Figure(figsize=(10, 8))\r\n        FigureCanvasAgg(fig)\r\n        axes = fig.add_subplot(111)\r\n    else:\r\n        axes = ax\r\n\r\n    returns = ep.cum_returns(oos_returns, starting_value=1.0)\r\n    bounds_tmp = bounds.copy()\r\n    returns_tmp = returns.copy()\r\n    cone_start = returns.index&#91;0]\r\n    colors = &#91;\"green\", \"orange\", \"orangered\", \"darkred\"]\r\n\r\n    for c in range(num_strikes + 1):\r\n        if c > 0:\r\n            tmp = returns.loc&#91;cone_start:]\r\n            bounds_tmp = bounds_tmp.iloc&#91;0 : len(tmp)]\r\n            bounds_tmp = bounds_tmp.set_index(tmp.index)\r\n            crossing = tmp &lt; bounds_tmp&#91;float(-2.0)].iloc&#91;: len(tmp)]\r\n            if crossing.sum() &lt;= 0:\r\n                break\r\n            cone_start = crossing.loc&#91;crossing].index&#91;0]\r\n            returns_tmp = returns.loc&#91;cone_start:]\r\n            bounds_tmp = bounds - (1 - returns.loc&#91;cone_start])\r\n        for std in cone_std:\r\n            x = returns_tmp.index\r\n            y1 = bounds_tmp&#91;float(std)].iloc&#91;: len(returns_tmp)]\r\n            y2 = bounds_tmp&#91;float(-std)].iloc&#91;: len(returns_tmp)]\r\n            axes.fill_between(x, y1, y2, color=colors&#91;c], alpha=0.5)\r\n\r\n    # Plot returns line graph\r\n    label = \"Cumulative returns = {:.2f}%\".format((returns.iloc&#91;-1] - 1) * 100)\r\n    axes.plot(returns.index, returns.values, color=\"black\", lw=2.0, label=label)\r\n\r\n    if name is not None:\r\n        axes.set_title(name)\r\n    axes.axhline(1, color=\"black\", alpha=0.2)\r\n    axes.legend(frameon=True, framealpha=0.5)\r\n\r\n    if ax is None:\r\n        return fig\r\n    else:\r\n        return axes\r\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">utils.py<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>#\r\n# Copyright 2018 Quantopian, Inc.\r\n#\r\n# Licensed under the Apache License, Version 2.0 (the \"License\");\r\n# you may not use this file except in compliance with the License.\r\n# You may obtain a copy of the License at\r\n#\r\n#     http:\/\/www.apache.org\/licenses\/LICENSE-2.0\r\n#\r\n# Unless required by applicable law or agreed to in writing, software\r\n# distributed under the License is distributed on an \"AS IS\" BASIS,\r\n# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r\n# See the License for the specific language governing permissions and\r\n# limitations under the License.\r\n\r\nimport warnings\r\nfrom itertools import cycle\r\n\r\nimport empyrical.utils\r\nimport numpy as np\r\nimport pandas as pd\r\nfrom IPython.display import display, HTML\r\nfrom matplotlib.pyplot import cm\r\nfrom packaging.version import Version\r\nimport os\r\nimport datetime\r\n\r\nfrom . import pos\r\nfrom . import txn\r\n\r\nAPPROX_BDAYS_PER_MONTH = 21\r\nAPPROX_BDAYS_PER_YEAR = 252\r\n\r\nMONTHS_PER_YEAR = 12\r\nWEEKS_PER_YEAR = 52\r\n\r\nMM_DISPLAY_UNIT = 1000000.0\r\n\r\nDAILY = \"daily\"\r\nWEEKLY = \"weekly\"\r\nMONTHLY = \"monthly\"\r\nYEARLY = \"yearly\"\r\n\r\nANNUALIZATION_FACTORS = {\r\n    DAILY: APPROX_BDAYS_PER_YEAR,\r\n    WEEKLY: WEEKS_PER_YEAR,\r\n    MONTHLY: MONTHS_PER_YEAR,\r\n}\r\n\r\nCOLORMAP = \"Paired\"\r\nCOLORS = &#91;\r\n    \"#e6194b\",\r\n    \"#3cb44b\",\r\n    \"#ffe119\",\r\n    \"#0082c8\",\r\n    \"#f58231\",\r\n    \"#911eb4\",\r\n    \"#46f0f0\",\r\n    \"#f032e6\",\r\n    \"#d2f53c\",\r\n    \"#fabebe\",\r\n    \"#008080\",\r\n    \"#e6beff\",\r\n    \"#aa6e28\",\r\n    \"#800000\",\r\n    \"#aaffc3\",\r\n    \"#808000\",\r\n    \"#ffd8b1\",\r\n    \"#000080\",\r\n    \"#808080\",\r\n]\r\n\r\npandas_version = Version(pd.__version__)\r\n\r\npandas_one_point_one_or_less = pandas_version &lt; Version(\"1.2\")\r\n\r\n\r\ndef one_dec_places(x, pos):\r\n    \"\"\"\r\n    Adds 1\/10th decimal to plot ticks.\r\n    \"\"\"\r\n\r\n    return \"%.1f\" % x\r\n\r\n\r\ndef two_dec_places(x, pos):\r\n    \"\"\"\r\n    Adds 1\/100th decimal to plot ticks.\r\n    \"\"\"\r\n\r\n    return \"%.2f\" % x\r\n\r\n\r\ndef percentage(x, pos):\r\n    \"\"\"\r\n    Adds percentage sign to plot ticks.\r\n    \"\"\"\r\n\r\n    return \"%.0f%%\" % x\r\n\r\n\r\ndef format_asset(asset):\r\n    \"\"\"\r\n    If zipline asset objects are used, we want to print them out prettily\r\n    within the tear sheet. This function should only be applied directly\r\n    before displaying.\r\n    \"\"\"\r\n\r\n    try:\r\n        import zipline.assets\r\n    except ImportError:\r\n        return asset\r\n\r\n    if isinstance(asset, zipline.assets.Asset):\r\n        return asset.symbol\r\n    else:\r\n        return asset\r\n\r\n\r\ndef vectorize(func):\r\n    \"\"\"\r\n    Decorator so that functions can be written to work on Series but\r\n    may still be called with DataFrames.\r\n    \"\"\"\r\n\r\n    def wrapper(df, *args, **kwargs):\r\n        if df.ndim == 1:\r\n            return func(df, *args, **kwargs)\r\n        elif df.ndim == 2:\r\n            return df.apply(func, *args, **kwargs)\r\n\r\n    return wrapper\r\n\r\n\r\ndef extract_rets_pos_txn_from_zipline(backtest):\r\n    \"\"\"\r\n    Extract returns, positions, transactions and leverage from the\r\n    backtest data structure returned by zipline.TradingAlgorithm.run().\r\n\r\n    The returned data structures are in a format compatible with the\r\n    rest of pyfolio and can be directly passed to\r\n    e.g. tears.create_full_tear_sheet().\r\n\r\n    Parameters\r\n    ----------\r\n    backtest : pd.DataFrame\r\n        DataFrame returned by zipline.TradingAlgorithm.run()\r\n\r\n    Returns\r\n    -------\r\n    returns : pd.Series\r\n        Daily returns of strategy.\r\n         - See full explanation in tears.create_full_tear_sheet.\r\n    positions : pd.DataFrame\r\n        Daily net position values.\r\n         - See full explanation in tears.create_full_tear_sheet.\r\n    transactions : pd.DataFrame\r\n        Prices and amounts of executed trades. One row per trade.\r\n         - See full explanation in tears.create_full_tear_sheet.\r\n\r\n\r\n    Example (on the Quantopian research platform)\r\n    ---------------------------------------------\r\n    >>> backtest = my_algo.run()\r\n    >>> returns, positions, transactions =\r\n    >>>     pyfolio.utils.extract_rets_pos_txn_from_zipline(backtest)\r\n    >>> pyfolio.tears.create_full_tear_sheet(returns,\r\n    >>>     positions, transactions)\r\n    \"\"\"\r\n\r\n    backtest.index = backtest.index.normalize()\r\n    if backtest.index.tzinfo is None:\r\n        backtest.index = backtest.index.tz_localize(\"UTC\")\r\n    returns = backtest.returns\r\n    raw_positions = &#91;]\r\n    for dt, pos_row in backtest.positions.items():\r\n        df = pd.DataFrame(pos_row)\r\n        df.index = &#91;dt] * len(df)\r\n        raw_positions.append(df)\r\n    if not raw_positions:\r\n        raise ValueError(\"The backtest does not have any positions.\")\r\n    positions = pd.concat(raw_positions)\r\n    positions = pos.extract_pos(positions, backtest.ending_cash)\r\n    transactions = txn.make_transaction_frame(backtest.transactions)\r\n    if transactions.index.tzinfo is None:\r\n        transactions.index = transactions.index.tz_localize(\"utc\")\r\n\r\n    return returns, positions, transactions\r\n\r\n\r\n\r\n\r\ndef print_table(table, name=None, float_format=None, formatters=None, \r\n                header_rows=None, save_to_file=True, \r\n                output_dir='\/home\/shared\/algos\/ml4t\/plots\/temp'):\r\n    \"\"\"\r\n    Pretty print a pandas DataFrame and optionally save it as an HTML file with a unique timestamped filename.\r\n\r\n    Parameters\r\n    ----------\r\n    table : pandas.Series or pandas.DataFrame\r\n        Table to pretty-print.\r\n    name : str, optional\r\n        Table name to display in upper left corner.\r\n    float_format : function, optional\r\n        Formatter to use for displaying table elements, passed as the\r\n        `float_format` arg to pd.Dataframe.to_html.\r\n    formatters : list or dict, optional\r\n        Formatters to use by column, passed as the `formatters` arg to\r\n        pd.Dataframe.to_html.\r\n    header_rows : dict, optional\r\n        Extra rows to display at the top of the table.\r\n    save_to_file : bool, optional\r\n        If True, save the table to an HTML file.\r\n    output_dir : str, optional\r\n        Directory where the HTML file will be saved.\r\n    \"\"\"\r\n\r\n    if isinstance(table, pd.Series):\r\n        table = pd.DataFrame(table)\r\n\r\n    if name is not None:\r\n        table.columns.name = name\r\n\r\n    html = table.to_html(float_format=float_format, formatters=formatters)\r\n\r\n    if header_rows is not None:\r\n        n_cols = html.split(\"&lt;thead>\")&#91;1].split(\"&lt;\/thead>\")&#91;0].count(\"&lt;th>\")\r\n        rows = \"\"\r\n        for name, value in header_rows.items():\r\n            rows += '\\n    &lt;tr style=\"text-align: right;\">&lt;th>%s&lt;\/th>' % name\r\n            rows += \"&lt;td colspan=%d>%s&lt;\/td>&lt;\/tr>\" % (n_cols, value)\r\n        html = html.replace(\"&lt;thead>\", \"&lt;thead>\" + rows)\r\n\r\n    if save_to_file:\r\n        # Generate a timestamped filename\r\n        timestamp = datetime.datetime.now().strftime(\"%Y%m%d_%H%M%S%f\")\r\n        filename = f\"table_{timestamp}.html\"\r\n        file_path = os.path.join(output_dir, filename)\r\n\r\n        # Create output directory if it doesn't exist\r\n        if not os.path.exists(output_dir):\r\n            os.makedirs(output_dir)\r\n\r\n        with open(file_path, 'w') as f:\r\n            f.write(html)\r\n        print(f\"Table saved to {file_path}\")\r\n    else:\r\n        display(HTML(html))\r\n\r\n\r\ndef standardize_data(x):\r\n    \"\"\"\r\n    Standardize an array with mean and standard deviation.\r\n\r\n    Parameters\r\n    ----------\r\n    x : np.array\r\n        Array to standardize.\r\n\r\n    Returns\r\n    -------\r\n    np.array\r\n        Standardized array.\r\n    \"\"\"\r\n\r\n    return (x - np.mean(x)) \/ np.std(x)\r\n\r\n\r\ndef detect_intraday(positions, transactions, threshold=0.25):\r\n    \"\"\"\r\n    Attempt to detect an intraday strategy. Get the number of\r\n    positions held at the end of the day, and divide that by the\r\n    number of unique stocks transacted every day. If the average quotient\r\n    is below a threshold, then an intraday strategy is detected.\r\n\r\n    Parameters\r\n    ----------\r\n    positions : pd.DataFrame\r\n        Daily net position values.\r\n         - See full explanation in create_full_tear_sheet.\r\n    transactions : pd.DataFrame\r\n        Prices and amounts of executed trades. One row per trade.\r\n         - See full explanation in create_full_tear_sheet.\r\n\r\n    Returns\r\n    -------\r\n    boolean\r\n        True if an intraday strategy is detected.\r\n    \"\"\"\r\n\r\n    daily_txn = transactions.copy()\r\n    daily_txn.index = daily_txn.index.date\r\n    txn_count = daily_txn.groupby(level=0).symbol.nunique().sum()\r\n    daily_pos = positions.drop(\"cash\", axis=1).replace(0, np.nan)\r\n    return daily_pos.count(axis=1).sum() \/ txn_count &lt; threshold\r\n\r\n\r\ndef check_intraday(estimate, returns, positions, transactions):\r\n    \"\"\"\r\n    Logic for checking if a strategy is intraday and processing it.\r\n\r\n    Parameters\r\n    ----------\r\n    estimate: boolean or str, optional\r\n        Approximate returns for intraday strategies.\r\n        See description in tears.create_full_tear_sheet.\r\n    returns : pd.Series\r\n        Daily returns of the strategy, noncumulative.\r\n         - See full explanation in create_full_tear_sheet.\r\n    positions : pd.DataFrame\r\n        Daily net position values.\r\n         - See full explanation in create_full_tear_sheet.\r\n    transactions : pd.DataFrame\r\n        Prices and amounts of executed trades. One row per trade.\r\n         - See full explanation in create_full_tear_sheet.\r\n\r\n    Returns\r\n    -------\r\n    pd.DataFrame\r\n        Daily net position values, adjusted for intraday movement.\r\n    \"\"\"\r\n\r\n    if estimate == \"infer\":\r\n        if positions is not None and transactions is not None:\r\n            if detect_intraday(positions, transactions):\r\n                warnings.warn(\r\n                    \"Detected intraday strategy; inferring positi\"\r\n                    + \"ons from transactions. Set estimate_intraday\"\r\n                    + \"=False to disable.\"\r\n                )\r\n                return estimate_intraday(returns, positions, transactions)\r\n            else:\r\n                return positions\r\n        else:\r\n            return positions\r\n\r\n    elif estimate:\r\n        if positions is not None and transactions is not None:\r\n            return estimate_intraday(returns, positions, transactions)\r\n        else:\r\n            raise ValueError(\"Positions and txns needed to estimate intraday\")\r\n    else:\r\n        return positions\r\n\r\n\r\ndef estimate_intraday(returns, positions, transactions, EOD_hour=23):\r\n    \"\"\"\r\n    Intraday strategies will often not hold positions at the day end.\r\n    This attempts to find the point in the day that best represents\r\n    the activity of the strategy on that day, and effectively resamples\r\n    the end-of-day positions with the positions at this point of day.\r\n    The point of day is found by detecting when our exposure in the\r\n    market is at its maximum point. Note that this is an estimate.\r\n\r\n    Parameters\r\n    ----------\r\n    returns : pd.Series\r\n        Daily returns of the strategy, noncumulative.\r\n         - See full explanation in create_full_tear_sheet.\r\n    positions : pd.DataFrame\r\n        Daily net position values.\r\n         - See full explanation in create_full_tear_sheet.\r\n    transactions : pd.DataFrame\r\n        Prices and amounts of executed trades. One row per trade.\r\n         - See full explanation in create_full_tear_sheet.\r\n\r\n    Returns\r\n    -------\r\n    pd.DataFrame\r\n        Daily net position values, resampled for intraday behavior.\r\n    \"\"\"\r\n\r\n    # Construct DataFrame of transaction amounts\r\n    txn_val = transactions.copy()\r\n    txn_val.index.names = &#91;\"date\"]\r\n    txn_val&#91;\"value\"] = txn_val.amount * txn_val.price\r\n    txn_val = (\r\n        txn_val.reset_index()\r\n        .pivot_table(index=\"date\", values=\"value\", columns=\"symbol\")\r\n        .replace(np.nan, 0)\r\n    )\r\n\r\n    # Cumulate transaction amounts each day\r\n    txn_val = txn_val.groupby(txn_val.index.date).cumsum()\r\n\r\n    # Calculate exposure, then take peak of exposure every day\r\n    txn_val&#91;\"exposure\"] = txn_val.abs().sum(axis=1)\r\n    condition = txn_val&#91;\"exposure\"] == txn_val.groupby(pd.Grouper(freq=\"24H\"))&#91;\r\n        \"exposure\"\r\n    ].transform(max)\r\n    txn_val = txn_val&#91;condition].drop(\"exposure\", axis=1)\r\n\r\n    # Compute cash delta\r\n    txn_val&#91;\"cash\"] = -txn_val.sum(axis=1)\r\n\r\n    # Shift EOD positions to positions at start of next trading day\r\n    positions_shifted = positions.copy().shift(1).fillna(0)\r\n    starting_capital = positions.iloc&#91;0].sum() \/ (1 + returns&#91;0])\r\n    positions_shifted.cash&#91;0] = starting_capital\r\n\r\n    # Format and add start positions to intraday position changes\r\n    txn_val.index = txn_val.index.normalize()\r\n    corrected_positions = positions_shifted.add(txn_val, fill_value=0)\r\n    corrected_positions.index.name = \"period_close\"\r\n    corrected_positions.columns.name = \"sid\"\r\n\r\n    return corrected_positions\r\n\r\n\r\ndef clip_returns_to_benchmark(rets, benchmark_rets):\r\n    \"\"\"\r\n    Drop entries from rets so that the start and end dates of rets match those\r\n    of benchmark_rets.\r\n\r\n    Parameters\r\n    ----------\r\n    rets : pd.Series\r\n        Daily returns of the strategy, noncumulative.\r\n         - See pf.tears.create_full_tear_sheet for more details\r\n\r\n    benchmark_rets : pd.Series\r\n        Daily returns of the benchmark, noncumulative.\r\n\r\n    Returns\r\n    -------\r\n    clipped_rets : pd.Series\r\n        Daily noncumulative returns with index clipped to match that of\r\n        benchmark returns.\r\n    \"\"\"\r\n\r\n    if (rets.index&#91;0] &lt; benchmark_rets.index&#91;0]) or (\r\n        rets.index&#91;-1] > benchmark_rets.index&#91;-1]\r\n    ):\r\n        clipped_rets = rets&#91;benchmark_rets.index]\r\n    else:\r\n        clipped_rets = rets\r\n\r\n    return clipped_rets\r\n\r\n\r\ndef to_utc(df):\r\n    \"\"\"\r\n    For use in tests; applied UTC timestamp to DataFrame.\r\n    \"\"\"\r\n\r\n    try:\r\n        df.index = df.index.tz_localize(\"UTC\")\r\n    except TypeError:\r\n        df.index = df.index.tz_convert(\"UTC\")\r\n\r\n    return df\r\n\r\n\r\ndef to_series(df):\r\n    \"\"\"\r\n    For use in tests; converts DataFrame's first column to Series.\r\n    \"\"\"\r\n\r\n    return df&#91;df.columns&#91;0]]\r\n\r\n\r\n# This functions is simply a passthrough to empyrical, but is\r\n# required by the register_returns_func and get_symbol_rets.\r\ndefault_returns_func = empyrical.utils.default_returns_func\r\n\r\n# Settings dict to store functions\/values that may\r\n# need to be overridden depending on the users environment\r\nSETTINGS = {\"returns_func\": default_returns_func}\r\n\r\n\r\ndef register_return_func(func):\r\n    \"\"\"\r\n    Registers the 'returns_func' that will be called for\r\n    retrieving returns data.\r\n\r\n    Parameters\r\n    ----------\r\n    func : function\r\n        A function that returns a pandas Series of asset returns.\r\n        The signature of the function must be as follows\r\n\r\n        >>> func(symbol)\r\n\r\n        Where symbol is an asset identifier\r\n\r\n    Returns\r\n    -------\r\n    None\r\n    \"\"\"\r\n\r\n    SETTINGS&#91;\"returns_func\"] = func\r\n\r\n\r\ndef get_symbol_rets(symbol, start=None, end=None):\r\n    \"\"\"\r\n    Calls the currently registered 'returns_func'\r\n\r\n    Parameters\r\n    ----------\r\n    symbol : object\r\n        An identifier for the asset whose return\r\n        series is desired.\r\n        e.g. ticker symbol or database ID\r\n    start : date, optional\r\n        Earliest date to fetch data for.\r\n        Defaults to earliest date available.\r\n    end : date, optional\r\n        Latest date to fetch data for.\r\n        Defaults to latest date available.\r\n\r\n    Returns\r\n    -------\r\n    pandas.Series\r\n        Returned by the current 'returns_func'\r\n    \"\"\"\r\n\r\n    return SETTINGS&#91;\"returns_func\"](symbol, start=start, end=end)\r\n\r\n\r\ndef configure_legend(\r\n    ax, autofmt_xdate=True, change_colors=False, rotation=30, ha=\"right\"\r\n):\r\n    \"\"\"\r\n    Format legend for perf attribution plots:\r\n    - put legend to the right of plot instead of overlapping with it\r\n    - make legend order match up with graph lines\r\n    - set colors according to colormap\r\n    \"\"\"\r\n    chartBox = ax.get_position()\r\n    ax.set_position(&#91;chartBox.x0, chartBox.y0, chartBox.width * 0.75, chartBox.height])\r\n\r\n    # make legend order match graph lines\r\n    handles, labels = ax.get_legend_handles_labels()\r\n    handles_and_labels_sorted = sorted(\r\n        zip(handles, labels), key=lambda x: x&#91;0].get_ydata()&#91;-1], reverse=True\r\n    )\r\n\r\n    handles_sorted = &#91;h&#91;0] for h in handles_and_labels_sorted]\r\n    labels_sorted = &#91;h&#91;1] for h in handles_and_labels_sorted]\r\n\r\n    if change_colors:\r\n        for handle, color in zip(handles_sorted, cycle(COLORS)):\r\n            handle.set_color(color)\r\n\r\n    ax.legend(\r\n        handles=handles_sorted,\r\n        labels=labels_sorted,\r\n        frameon=True,\r\n        framealpha=0.5,\r\n        loc=\"upper left\",\r\n        bbox_to_anchor=(1.05, 1),\r\n        fontsize=\"small\",\r\n    )\r\n\r\n    # manually rotate xticklabels instead of using matplotlib's autofmt_xdate\r\n    # because it disables xticklabels for all but the last plot\r\n    if autofmt_xdate:\r\n        for label in ax.get_xticklabels():\r\n            label.set_ha(ha)\r\n            label.set_rotation(rotation)\r\n\r\n\r\ndef sample_colormap(cmap_name, n_samples):\r\n    \"\"\"\r\n    Sample a colormap from matplotlib\r\n    \"\"\"\r\n    colors = &#91;]\r\n    colormap = cm.cmap_d&#91;cmap_name]\r\n    for i in np.linspace(0, 1, n_samples):\r\n        colors.append(colormap(i))\r\n\r\n    return colors\r\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Recently I was following a paper and in the example they used&nbsp;Pyfolio&nbsp;which is an awesome performance and risk analysis library in Python developed by Quantopian Inc when they&#8230;<\/p>\n","protected":false},"author":1,"featured_media":6215,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-6186","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-uncategorized"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.2 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Modifying PyFolio to output to HTML - Jeremy Whittaker<\/title>\n<meta name=\"robots\" content=\"noindex, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Modifying PyFolio to output to HTML - Jeremy Whittaker\" \/>\n<meta property=\"og:description\" content=\"Recently I was following a paper and in the example they used&nbsp;Pyfolio&nbsp;which is an awesome performance and risk analysis library in Python developed by Quantopian Inc when they...\" \/>\n<meta property=\"og:url\" content=\"https:\/\/new.jeremywhittaker.com\/index.php\/2024\/01\/23\/modifying-pyfolio-to-output-to-html\/\" \/>\n<meta property=\"og:site_name\" content=\"Jeremy Whittaker\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/WhittakerJeremy\" \/>\n<meta property=\"article:author\" content=\"https:\/\/www.facebook.com\/WhittakerJeremy\" \/>\n<meta property=\"article:published_time\" content=\"2024-01-23T21:08:35+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-01-23T21:08:40+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/new.jeremywhittaker.com\/wp-content\/uploads\/2024\/01\/DALL\u00b7E-2024-01-23-14.07.05-An-image-depicting-the-concept-of-Pyfolio-a-Python-library-used-for-financial-portfolio-analysis.-The-image-shows-a-stylized-representation-of-a-comp.png\" \/>\n\t<meta property=\"og:image:width\" content=\"1024\" \/>\n\t<meta property=\"og:image:height\" content=\"1024\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"JeremyWhittaker\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"JeremyWhittaker\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"1 minute\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/new.jeremywhittaker.com\/index.php\/2024\/01\/23\/modifying-pyfolio-to-output-to-html\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/new.jeremywhittaker.com\/index.php\/2024\/01\/23\/modifying-pyfolio-to-output-to-html\/\"},\"author\":{\"name\":\"JeremyWhittaker\",\"@id\":\"https:\/\/new.jeremywhittaker.com\/#\/schema\/person\/ed0edfdefb3e180693efef453372980c\"},\"headline\":\"Modifying PyFolio to output to HTML\",\"datePublished\":\"2024-01-23T21:08:35+00:00\",\"dateModified\":\"2024-01-23T21:08:40+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/new.jeremywhittaker.com\/index.php\/2024\/01\/23\/modifying-pyfolio-to-output-to-html\/\"},\"wordCount\":189,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/new.jeremywhittaker.com\/#\/schema\/person\/ed0edfdefb3e180693efef453372980c\"},\"image\":{\"@id\":\"https:\/\/new.jeremywhittaker.com\/index.php\/2024\/01\/23\/modifying-pyfolio-to-output-to-html\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/new.jeremywhittaker.com\/wp-content\/uploads\/2024\/01\/DALL\u00b7E-2024-01-23-14.07.05-An-image-depicting-the-concept-of-Pyfolio-a-Python-library-used-for-financial-portfolio-analysis.-The-image-shows-a-stylized-representation-of-a-comp.png\",\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/new.jeremywhittaker.com\/index.php\/2024\/01\/23\/modifying-pyfolio-to-output-to-html\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/new.jeremywhittaker.com\/index.php\/2024\/01\/23\/modifying-pyfolio-to-output-to-html\/\",\"url\":\"https:\/\/new.jeremywhittaker.com\/index.php\/2024\/01\/23\/modifying-pyfolio-to-output-to-html\/\",\"name\":\"Modifying PyFolio to output to HTML - Jeremy Whittaker\",\"isPartOf\":{\"@id\":\"https:\/\/new.jeremywhittaker.com\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/new.jeremywhittaker.com\/index.php\/2024\/01\/23\/modifying-pyfolio-to-output-to-html\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/new.jeremywhittaker.com\/index.php\/2024\/01\/23\/modifying-pyfolio-to-output-to-html\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/new.jeremywhittaker.com\/wp-content\/uploads\/2024\/01\/DALL\u00b7E-2024-01-23-14.07.05-An-image-depicting-the-concept-of-Pyfolio-a-Python-library-used-for-financial-portfolio-analysis.-The-image-shows-a-stylized-representation-of-a-comp.png\",\"datePublished\":\"2024-01-23T21:08:35+00:00\",\"dateModified\":\"2024-01-23T21:08:40+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/new.jeremywhittaker.com\/index.php\/2024\/01\/23\/modifying-pyfolio-to-output-to-html\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/new.jeremywhittaker.com\/index.php\/2024\/01\/23\/modifying-pyfolio-to-output-to-html\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/new.jeremywhittaker.com\/index.php\/2024\/01\/23\/modifying-pyfolio-to-output-to-html\/#primaryimage\",\"url\":\"https:\/\/new.jeremywhittaker.com\/wp-content\/uploads\/2024\/01\/DALL\u00b7E-2024-01-23-14.07.05-An-image-depicting-the-concept-of-Pyfolio-a-Python-library-used-for-financial-portfolio-analysis.-The-image-shows-a-stylized-representation-of-a-comp.png\",\"contentUrl\":\"https:\/\/new.jeremywhittaker.com\/wp-content\/uploads\/2024\/01\/DALL\u00b7E-2024-01-23-14.07.05-An-image-depicting-the-concept-of-Pyfolio-a-Python-library-used-for-financial-portfolio-analysis.-The-image-shows-a-stylized-representation-of-a-comp.png\",\"width\":1024,\"height\":1024},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/new.jeremywhittaker.com\/index.php\/2024\/01\/23\/modifying-pyfolio-to-output-to-html\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/new.jeremywhittaker.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Modifying PyFolio to output to HTML\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/new.jeremywhittaker.com\/#website\",\"url\":\"https:\/\/new.jeremywhittaker.com\/\",\"name\":\"Jeremy Whittaker\",\"description\":\"Research, software, markets, housing, and energy\",\"publisher\":{\"@id\":\"https:\/\/new.jeremywhittaker.com\/#\/schema\/person\/ed0edfdefb3e180693efef453372980c\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/new.jeremywhittaker.com\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"https:\/\/new.jeremywhittaker.com\/#\/schema\/person\/ed0edfdefb3e180693efef453372980c\",\"name\":\"JeremyWhittaker\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/secure.gravatar.com\/avatar\/c8ac20e6dfa86b5f27ce9bffee4851099770cbea5ae7338a274865bfbc8c0218?s=96&d=retro&r=g\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/c8ac20e6dfa86b5f27ce9bffee4851099770cbea5ae7338a274865bfbc8c0218?s=96&d=retro&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/c8ac20e6dfa86b5f27ce9bffee4851099770cbea5ae7338a274865bfbc8c0218?s=96&d=retro&r=g\",\"caption\":\"JeremyWhittaker\"},\"logo\":{\"@id\":\"https:\/\/secure.gravatar.com\/avatar\/c8ac20e6dfa86b5f27ce9bffee4851099770cbea5ae7338a274865bfbc8c0218?s=96&d=retro&r=g\"},\"sameAs\":[\"http:\/\/www.jeremywhittaker.com\",\"https:\/\/www.facebook.com\/WhittakerJeremy\",\"https:\/\/www.linkedin.com\/in\/jeremywhittaker\/\"],\"url\":\"https:\/\/new.jeremywhittaker.com\/index.php\/author\/jeremywhittaker\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Modifying PyFolio to output to HTML - Jeremy Whittaker","robots":{"index":"noindex","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"og_locale":"en_US","og_type":"article","og_title":"Modifying PyFolio to output to HTML - Jeremy Whittaker","og_description":"Recently I was following a paper and in the example they used&nbsp;Pyfolio&nbsp;which is an awesome performance and risk analysis library in Python developed by Quantopian Inc when they...","og_url":"https:\/\/new.jeremywhittaker.com\/index.php\/2024\/01\/23\/modifying-pyfolio-to-output-to-html\/","og_site_name":"Jeremy Whittaker","article_publisher":"https:\/\/www.facebook.com\/WhittakerJeremy","article_author":"https:\/\/www.facebook.com\/WhittakerJeremy","article_published_time":"2024-01-23T21:08:35+00:00","article_modified_time":"2024-01-23T21:08:40+00:00","og_image":[{"width":1024,"height":1024,"url":"https:\/\/new.jeremywhittaker.com\/wp-content\/uploads\/2024\/01\/DALL\u00b7E-2024-01-23-14.07.05-An-image-depicting-the-concept-of-Pyfolio-a-Python-library-used-for-financial-portfolio-analysis.-The-image-shows-a-stylized-representation-of-a-comp.png","type":"image\/png"}],"author":"JeremyWhittaker","twitter_card":"summary_large_image","twitter_misc":{"Written by":"JeremyWhittaker","Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/new.jeremywhittaker.com\/index.php\/2024\/01\/23\/modifying-pyfolio-to-output-to-html\/#article","isPartOf":{"@id":"https:\/\/new.jeremywhittaker.com\/index.php\/2024\/01\/23\/modifying-pyfolio-to-output-to-html\/"},"author":{"name":"JeremyWhittaker","@id":"https:\/\/new.jeremywhittaker.com\/#\/schema\/person\/ed0edfdefb3e180693efef453372980c"},"headline":"Modifying PyFolio to output to HTML","datePublished":"2024-01-23T21:08:35+00:00","dateModified":"2024-01-23T21:08:40+00:00","mainEntityOfPage":{"@id":"https:\/\/new.jeremywhittaker.com\/index.php\/2024\/01\/23\/modifying-pyfolio-to-output-to-html\/"},"wordCount":189,"commentCount":0,"publisher":{"@id":"https:\/\/new.jeremywhittaker.com\/#\/schema\/person\/ed0edfdefb3e180693efef453372980c"},"image":{"@id":"https:\/\/new.jeremywhittaker.com\/index.php\/2024\/01\/23\/modifying-pyfolio-to-output-to-html\/#primaryimage"},"thumbnailUrl":"https:\/\/new.jeremywhittaker.com\/wp-content\/uploads\/2024\/01\/DALL\u00b7E-2024-01-23-14.07.05-An-image-depicting-the-concept-of-Pyfolio-a-Python-library-used-for-financial-portfolio-analysis.-The-image-shows-a-stylized-representation-of-a-comp.png","inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/new.jeremywhittaker.com\/index.php\/2024\/01\/23\/modifying-pyfolio-to-output-to-html\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/new.jeremywhittaker.com\/index.php\/2024\/01\/23\/modifying-pyfolio-to-output-to-html\/","url":"https:\/\/new.jeremywhittaker.com\/index.php\/2024\/01\/23\/modifying-pyfolio-to-output-to-html\/","name":"Modifying PyFolio to output to HTML - Jeremy Whittaker","isPartOf":{"@id":"https:\/\/new.jeremywhittaker.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/new.jeremywhittaker.com\/index.php\/2024\/01\/23\/modifying-pyfolio-to-output-to-html\/#primaryimage"},"image":{"@id":"https:\/\/new.jeremywhittaker.com\/index.php\/2024\/01\/23\/modifying-pyfolio-to-output-to-html\/#primaryimage"},"thumbnailUrl":"https:\/\/new.jeremywhittaker.com\/wp-content\/uploads\/2024\/01\/DALL\u00b7E-2024-01-23-14.07.05-An-image-depicting-the-concept-of-Pyfolio-a-Python-library-used-for-financial-portfolio-analysis.-The-image-shows-a-stylized-representation-of-a-comp.png","datePublished":"2024-01-23T21:08:35+00:00","dateModified":"2024-01-23T21:08:40+00:00","breadcrumb":{"@id":"https:\/\/new.jeremywhittaker.com\/index.php\/2024\/01\/23\/modifying-pyfolio-to-output-to-html\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/new.jeremywhittaker.com\/index.php\/2024\/01\/23\/modifying-pyfolio-to-output-to-html\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/new.jeremywhittaker.com\/index.php\/2024\/01\/23\/modifying-pyfolio-to-output-to-html\/#primaryimage","url":"https:\/\/new.jeremywhittaker.com\/wp-content\/uploads\/2024\/01\/DALL\u00b7E-2024-01-23-14.07.05-An-image-depicting-the-concept-of-Pyfolio-a-Python-library-used-for-financial-portfolio-analysis.-The-image-shows-a-stylized-representation-of-a-comp.png","contentUrl":"https:\/\/new.jeremywhittaker.com\/wp-content\/uploads\/2024\/01\/DALL\u00b7E-2024-01-23-14.07.05-An-image-depicting-the-concept-of-Pyfolio-a-Python-library-used-for-financial-portfolio-analysis.-The-image-shows-a-stylized-representation-of-a-comp.png","width":1024,"height":1024},{"@type":"BreadcrumbList","@id":"https:\/\/new.jeremywhittaker.com\/index.php\/2024\/01\/23\/modifying-pyfolio-to-output-to-html\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/new.jeremywhittaker.com\/"},{"@type":"ListItem","position":2,"name":"Modifying PyFolio to output to HTML"}]},{"@type":"WebSite","@id":"https:\/\/new.jeremywhittaker.com\/#website","url":"https:\/\/new.jeremywhittaker.com\/","name":"Jeremy Whittaker","description":"Research, software, markets, housing, and energy","publisher":{"@id":"https:\/\/new.jeremywhittaker.com\/#\/schema\/person\/ed0edfdefb3e180693efef453372980c"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/new.jeremywhittaker.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":["Person","Organization"],"@id":"https:\/\/new.jeremywhittaker.com\/#\/schema\/person\/ed0edfdefb3e180693efef453372980c","name":"JeremyWhittaker","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/c8ac20e6dfa86b5f27ce9bffee4851099770cbea5ae7338a274865bfbc8c0218?s=96&d=retro&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/c8ac20e6dfa86b5f27ce9bffee4851099770cbea5ae7338a274865bfbc8c0218?s=96&d=retro&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/c8ac20e6dfa86b5f27ce9bffee4851099770cbea5ae7338a274865bfbc8c0218?s=96&d=retro&r=g","caption":"JeremyWhittaker"},"logo":{"@id":"https:\/\/secure.gravatar.com\/avatar\/c8ac20e6dfa86b5f27ce9bffee4851099770cbea5ae7338a274865bfbc8c0218?s=96&d=retro&r=g"},"sameAs":["http:\/\/www.jeremywhittaker.com","https:\/\/www.facebook.com\/WhittakerJeremy","https:\/\/www.linkedin.com\/in\/jeremywhittaker\/"],"url":"https:\/\/new.jeremywhittaker.com\/index.php\/author\/jeremywhittaker\/"}]}},"_links":{"self":[{"href":"https:\/\/new.jeremywhittaker.com\/index.php\/wp-json\/wp\/v2\/posts\/6186","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/new.jeremywhittaker.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/new.jeremywhittaker.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/new.jeremywhittaker.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/new.jeremywhittaker.com\/index.php\/wp-json\/wp\/v2\/comments?post=6186"}],"version-history":[{"count":8,"href":"https:\/\/new.jeremywhittaker.com\/index.php\/wp-json\/wp\/v2\/posts\/6186\/revisions"}],"predecessor-version":[{"id":6216,"href":"https:\/\/new.jeremywhittaker.com\/index.php\/wp-json\/wp\/v2\/posts\/6186\/revisions\/6216"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/new.jeremywhittaker.com\/index.php\/wp-json\/wp\/v2\/media\/6215"}],"wp:attachment":[{"href":"https:\/\/new.jeremywhittaker.com\/index.php\/wp-json\/wp\/v2\/media?parent=6186"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/new.jeremywhittaker.com\/index.php\/wp-json\/wp\/v2\/categories?post=6186"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/new.jeremywhittaker.com\/index.php\/wp-json\/wp\/v2\/tags?post=6186"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}