Calculate the Conditional Value at Risk (CVaR) of an investment portfolio or asset’s returns.

Conditional Value at Risk (CVaR) is a risk management metric that quantifies the loss in the worst % of cases of an investment portfolio or asset may experience over a specified time horizon and confidence level. It provides insights into the downside risk associated with an investment and helps investors make informed decisions about risk tolerance.

The CVaR is calculated as the expected loss given that the loss threshold (VaR) with a given confidence level (e.g., 5% for alpha=0.05) is exceeded.

Also known as: CVaR, expected shortfall, ES, tail risk.

No programming experience? With the Finance Toolkit MCP server, AI assistants such as Claude and ChatGPT can calculate the Conditional Value at Risk (cVaR) for you. Just ask in plain English.

Calculate the Conditional Value at Risk (cVaR) in Python

The Conditional Value at Risk (cVaR) is available in the Risk module of the open-source Finance Toolkit. Install it with:

pip install financetoolkit -U

Then call get_conditional_value_at_risk as shown below.

from financetoolkit import Toolkit

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

toolkit.risk.get_conditional_value_at_risk()

Which returns:

  AMZN TSLA
2012 -0.0302 -0.0622
2013 -0.0323 -0.0807
2014 -0.0552 -0.0607
2015 -0.0318 -0.053
2016 -0.0456 -0.0604
2017 -0.0236 -0.0483
2018 -0.0540 -0.0746
2019 -0.0327 -0.0758
2020 -0.0510 -0.1262
2021 -0.0327 -0.0683
2022 -0.0685 -0.0914
2023 -0.0397 -0.0747

Parameters

get_conditional_value_at_risk accepts the following parameters:

  • period (str, optional): The data frequency for returns (daily, weekly, monthly, quarterly, or yearly). Defaults to “quarterly” if the Toolkit is initialised with quarterly=True, otherwise “yearly”.
  • alpha (float, optional): The confidence level for CVaR calculation (e.g., 0.05 for 95% confidence). Defaults to 0.05.
  • within_period (bool, optional): Whether to calculate CVaR within the specified period or for the entire period. Thus whether to look at the CVaR within a specific year (if period = ‘yearly’) or look at the entirety of all years. Defaults to True.
  • rolling (int, optional): The rolling window size to use for the calculation. If set, CVaR is calculated over a rolling window of this many periods across the full return history instead of per period (e.g. a rolling 60-day CVaR). Only available for distribution="historic"; see get_acerbi_szekely_test for a rolling, out-of-sample CVaR path under the parametric distributions. Defaults to None.
  • rounding (int | None, optional): The number of decimals to round the results to. Defaults to 4.
  • growth (bool, optional): Whether to calculate the growth of the CVaR values over time. Defaults to False.
  • lag (int | list[int], optional): The lag to use for the growth calculation. Defaults to 1.
  • standardize (bool, optional): Whether to standardize (Z-Score) the result. When combined with growth=True, standardizes the growth values instead of the raw values. Defaults to False.
  • distribution (str): The distribution to use for the CVaR calculations (historic, gaussian, studentt, laplace, logistic, cornish-fisher or evt). Defaults to “historic”.
  • threshold_percentile (float, optional): Only used when distribution is “evt”. The percentile of losses above which the Generalized Pareto Distribution is fitted. Defaults to 0.95.

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

Share