Mean Excess Function
Calculate the Mean Excess Function of the losses: the average amount by which the losses exceed a range of thresholds, the plot used to choose the threshold of a Generalized Pareto fit.
Above a threshold where the losses follow a Generalized Pareto Distribution, the mean excess is a straight line in the threshold with slope xi / (1 - xi): rising for a heavy (power-law) tail, flat for an exponential tail and falling for a tail with an end point. A good threshold for get_generalized_pareto_fit and the Extreme Value Theory VaR is the lowest one above which the line is close to straight; the last points rest on a handful of losses and are noisy.
The formula is as follows:
\[\text{Losses} = - \text{Returns}\] \[e(u) = \frac{\sum(\text{Loss}_{i} - u) \cdot 1 \{\text{Loss}_{i} > u\}}{\sum 1 \{\text{Loss}_{i} > u\}}\]Where u is the quantile of each asset’s losses at each of quantiles (linear interpolation).
Also known as: mean excess plot, mean residual life plot, ME plot.
Example · BTCUSD, ^GSPC · 2021 to 2025
Install the Finance Toolkit with pip install financetoolkit -U, then call get_mean_excess_function:
from financetoolkit import Toolkit
toolkit = Toolkit(
["BTCUSD", "^GSPC"],
api_key="FINANCIAL_MODELING_PREP_KEY",
start_date="2021-01-01",
end_date="2025-12-31",
)
toolkit.risk.get_mean_excess_function(quantiles=[0.9, 0.95, 0.975, 0.99], show_columns="Mean Excess")
Or, with the Finance Toolkit MCP set up, ask an AI assistant such as Claude or ChatGPT:
By how much did the daily losses of Bitcoin and the S&P 500 from 2021 to 2025 exceed their 90%, 95%, 97.5% and 99% quantiles on average?
Which returns:
| Quantile | BTCUSD | ^GSPC | Benchmark |
|---|---|---|---|
| 0.9 | 0.0227 | 0.0078 | 0.0076 |
| 0.95 | 0.0227 | 0.0083 | 0.0082 |
| 0.975 | 0.0243 | 0.0091 | 0.0093 |
| 0.99 | 0.0252 | 0.0086 | 0.0095 |
4 rows
Parameters
get_mean_excess_function accepts the following parameters.
| Parameter | Type | Description | Example |
|---|---|---|---|
period | str | The data frequency for returns. Options are: “daily”, “weekly”, “monthly”, “quarterly”, “yearly”. Defaults to “daily”. | "weekly" |
quantiles | list[float] | None | The quantiles of the losses to use as thresholds, each between 0 and 1. Defaults to None, which uses 0.80, 0.81, …, 0.99. | [0.05, 0.5, 0.95] |
rounding | int | None | The number of decimals to round the results to. Defaults to None, which uses the rounding of the Toolkit (4 unless set otherwise). | 2 |
show_columns | str | list[str] | None | The columns to keep, a name or a list of names, matched on every column level, or the rows when no column has those names (such as the line items of a financial statement). Names that do not exist are logged, and when none exist the result is returned in full. Defaults to None, which keeps every column. | None, which keeps every column |
References
- Paper Davison, A.C. and Smith, R.L. (1990). “Models for Exceedances over High Thresholds.” Journal of the Royal Statistical Society, Series B, 52(3), 393-442.
- Paper McNeil, A.J., Frey, R. and Embrechts, P. (2015). “Quantitative Risk Management: Concepts, Techniques and Tools.” Revised Edition, Princeton University Press, Section 5.2.