Mean Absolute Deviation

Calculate the Mean Absolute Deviation of a price series: the average distance of the prices from their mean, over the full history or a rolling window.

It measures the dispersion of the prices like the standard deviation, but without squaring the deviations, so a single large deviation weighs less. Lambert’s Commodity Channel Index divides by it.

The formula is as follows:

\[\text{Mean Absolute Deviation} = \operatorname{Mean}(\left|\operatorname{Close}(t - i) - \operatorname{Mean}(\text{Close})\right|,\; i = 0,\; \ldots,\; n - 1)\]

with n the number of observations of the full history or of the rolling window, and the average of those same observations.

Also known as: AVGDEV, average deviation, mean deviation, MAD, MD.

Example · AAPL, MSFT · 2021 to 2025

Install the Finance Toolkit with pip install financetoolkit -U, then call get_mean_absolute_deviation:

from financetoolkit import Toolkit

toolkit = Toolkit(
    tickers=["AAPL", "MSFT"],
    api_key="FINANCIAL_MODELING_PREP_KEY",
    start_date="2021-01-01",
    end_date="2025-12-31",
)

toolkit.econometrics.get_mean_absolute_deviation(rolling=14)

Or, with the Finance Toolkit MCP set up, ask an AI assistant such as Claude or ChatGPT:

How far did Apple's and Microsoft's share prices stray from their 14-day average over the last five trading days of 2025, measured by the mean absolute deviation?

Which returns:

Using Your Own Data

get_mean_absolute_deviation runs on any data, not only prices: ratios, economic indicators, yield curves or your own dataset. Pass a DataFrame, a Series or a dictionary of them to Econometrics as data, with the dates in the rows or the columns, and set changes to how its changes are calculated: "percentage", "log", "difference" for rates and ratios, or None when the data are changes already. For example, for the monthly unemployment rates of the United States and Germany:

from financetoolkit import Economics, Econometrics

economics = Economics(start_date="2000-01-01")

unemployment = economics.get_unemployment_rate(
    countries=["United States", "Germany"], period="monthly"
)

econometrics = Econometrics(data=unemployment, changes="difference")

econometrics.get_mean_absolute_deviation()

Parameters

get_mean_absolute_deviation accepts the following parameters.

ParameterTypeDescriptionExample
periodstr | NoneThe data frequency (daily, weekly, monthly, quarterly, or yearly). Defaults to None, which uses “daily”."quarterly"
columnstrThe historical data column the statistic is calculated on: “Adj Close” for the prices (the series as given with Econometrics(data=...)), “Close” for the traded prices or “Return” for the returns (the changes of the series). Defaults to “Adj Close”."Return"
rollingint | NoneThe number of periods of a rolling window, at least 1. Defaults to None, which calculates one value over the full history of every asset.4
include_benchmarkboolWhether to include “Benchmark” among the assets. Defaults to False.True
roundingint | NoneThe number of decimals to round the results to. Defaults to None, which uses the rounding of the Toolkit (4 unless set otherwise).2
show_columnsstr | list[str] | NoneThe 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 Lambert, D.R. (1980). “Commodity Channel Index: Tool for Trading Cyclic Trends.” Commodities, October 1980.
  • Library Referenced against TA-Lib’s AVGDEV
  • Library Referenced against pandas-ta-classic’s mad
  • Library Referenced against pandas-ta-classic’s md
  • Library Referenced against Tulip Indicators’ md
Share