Gini Mean Difference (GMD)
Calculate the Gini Mean Difference (GMD): how far apart two returns of an investment portfolio or asset lie on average, a measure of dispersion that does not square the deviations.
The Gini Mean Difference is the average absolute difference between two returns drawn from the sample, a measure of dispersion that, unlike the standard deviation, does not square the deviations and needs no mean to measure them from. Yitzhaki (1982) showed that, unlike the variance, it is consistent with second-degree stochastic dominance, which makes it a sound risk measure for returns that are not normally distributed. For a normal distribution it equals \(2 \sigma / \sqrt{\pi}\), about 1.13 times the standard deviation.
The formula is as follows:
\[\text{Gini Mean Difference} = \frac{\Sigma_{i} \Sigma_{j} \left|\text{Return}_{i} - \text{Return}_{j}\right|}{n \cdot (n - 1)}\]Where the double sum runs over every pair of the n returns. It is calculated as an ordered weighted average (OWA) of the returns sorted from low to high, with weight 2 * (2i - 1 - n) / (n * (n - 1)) on the i-th lowest (Cajas 2021), and equals twice the second L-moment (get_l_moment).
Also known as: GMD, Gini’s mean difference, mean absolute difference.
Example · MCD, SBUX, CMG · 2021 to 2025
Install the Finance Toolkit with pip install financetoolkit -U, then call get_gini_mean_difference:
from financetoolkit import Toolkit
toolkit = Toolkit(
["MCD", "SBUX", "CMG"],
api_key="FINANCIAL_MODELING_PREP_KEY",
start_date="2021-01-01",
end_date="2025-12-31",
)
toolkit.risk.get_gini_mean_difference(period="yearly", within_period=True)
Or, with the Finance Toolkit MCP set up, ask an AI assistant such as Claude or ChatGPT:
How far apart did two daily returns of McDonald's, Starbucks, Chipotle and the S&P 500 lie on average in each year from 2021 to 2025, measured by the Gini mean difference?
Which returns:
| Item | MCD | SBUX | CMG | Benchmark |
|---|---|---|---|---|
| 2021 | 0.0104 | 0.0149 | 0.0174 | 0.0090 |
| 2022 | 0.0136 | 0.0244 | 0.0274 | 0.0171 |
| 2023 | 0.0098 | 0.0142 | 0.0158 | 0.0093 |
| 2024 | 0.0120 | 0.0189 | 0.0194 | 0.0086 |
| 2025 | 0.0126 | 0.0220 | 0.0237 | 0.0112 |
5 rows
Parameters
get_gini_mean_difference 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" |
within_period | bool | Whether to calculate the Gini Mean Difference within the specified period or for the entire period. Thus whether to look at the Gini Mean Difference within a specific year (if period = ‘yearly’) or look at the entirety of all years. Defaults to False. Note that this requires intraday data when period = ‘daily’, since a single day only nests observations when intraday data was fetched. | True |
rolling | int | The rolling window size to use for the calculation. If set, the Gini Mean Difference is calculated over a rolling window of this many periods across the full return history instead of per period. At least 2. Defaults to None. | 4 |
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 |
growth | bool | Whether to calculate the growth of the Gini Mean Difference values over time. Defaults to False. | True |
lag | int | list[int] | The lag to use for the growth calculation. Defaults to 1. | 4 |
standardize | bool | Whether to standardize (Z-Score) the result. When combined with growth=True, standardizes the growth values instead of the raw values. Defaults to False. | True |
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 Yitzhaki, S. (1982). “Stochastic Dominance, Mean Variance, and Gini’s Mean Difference.” The American Economic Review, 72(1), 178-185.
- Paper Cajas, D. (2021). “OWA Portfolio Optimization: A Disciplined Convex Programming Framework.” SSRN Electronic Journal.
- Library Referenced against Riskfolio-Lib’s GMD
- Library Referenced against skfolio’s gini_mean_difference
- Library Shares similar logic as Riskfolio-Lib’s owa_gmd