Backtest a Conditional Value at Risk (Expected Shortfall) model against realized returns, via the Acerbi-Szekely (2014) Z2 statistic.

get_var_backtest above only backtests the VaR estimate itself – it checks how often (and how independently) the VaR threshold is breached, but says nothing about the severity of the losses on those breach days, which is exactly the extra information a CVaR (Expected Shortfall) estimate is supposed to add over VaR. This method builds a rolling, out-of-sample CVaR (and VaR) path and compares the actual loss on each breach day to the CVaR that was supposed to describe the average loss on such days.

For more information about the method, see the following paper:

  • Acerbi, C., & Szekely, B. (2014). “Back-Testing Expected Shortfall.” RISK Magazine, 27(11), 76-81.

Also known as: Acerbi-Szekely test, ES backtest, Z2 test.

No programming experience? With the Finance Toolkit MCP server, AI assistants such as Claude and ChatGPT can calculate the Acerbi-Szekely Test for you. Just ask in plain English.

Calculate the Acerbi-Szekely Test in Python

The Acerbi-Szekely Test is available in the Risk module of the open-source Finance Toolkit. Install it with:

pip install financetoolkit -U

Then call get_acerbi_szekely_test as shown below.

from financetoolkit import Toolkit

toolkit = Toolkit(["AMZN", "TSLA"], api_key="FINANCIAL_MODELING_PREP_KEY")

toolkit.risk.get_acerbi_szekely_test(window_size=100)

Which returns:

  AAPL MSFT Benchmark
Acerbi-Szekely Statistic 0.0905 0.0095 -0.003
Standard Error 0.1817 0.1723 0.1663
P-Value 0.6185 0.9558 0.9858
Breaches 37 33 33

Parameters

get_acerbi_szekely_test accepts the following parameters:

  • period (str, optional): The data frequency for returns (daily, weekly, quarterly, or yearly). Defaults to “daily”, since window_size is expressed in return observations of this frequency.
  • distribution (str, optional): The distribution to use for the rolling VaR/CVaR estimates, one of “historic”, “gaussian”, “studentt” or “evt”. Defaults to “historic”.
  • alpha (float, optional): The confidence level for the VaR/CVaR estimates (e.g., 0.05 for 95% confidence). Defaults to 0.05.
  • window_size (int, optional): The rolling window size (in number of return observations) used to estimate each VaR/CVaR value. Defaults to 252 (approximately one trading year of daily returns).
  • n_bootstrap (int, optional): The number of bootstrap resamples used to estimate the Standard Error of Z2. Defaults to 1000.
  • random_state (int, optional): The seed for the bootstrap random number generator. Defaults to 42.
  • rounding (int | None, optional): The number of decimals to round the results to. Defaults to None.

The Risk module page introduces the module, and the sidebar lists all of its functions.

Share