Calculate the Mean Absolute Deviation (MAD) of an investment portfolio or asset’s returns for a given period based on the daily historical returns.

MAD measures the average absolute distance of each return from the mean return. Unlike Variance and Volatility, it does not square the deviations, making it less sensitive to outliers.

Mean Absolute Deviation (MAD) in Python

get_mean_absolute_deviation is part of the Risk module of the open-source Finance Toolkit. Install it with:

pip install financetoolkit -U

Then call get_mean_absolute_deviation as shown below.

from financetoolkit import Toolkit

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

toolkit.risk.get_mean_absolute_deviation(period="yearly")

Which returns:

Date AMZN TSLA Benchmark
2021 0.0114 0.0246 0.0062
2022 0.0235 0.032 0.0119
2023 0.0156 0.0255 0.0065
2024 0.0132 0.0286 0.0058
2025 0.015 0.0292 0.0074
2026 0.0157 0.0216 0.0067

Parameters

get_mean_absolute_deviation accepts the following parameters:

  • period (str, optional): The data frequency for returns (weekly, monthly, quarterly, or yearly). Defaults to “quarterly” if the Toolkit is initialized with quarterly=True, otherwise “yearly”.
  • 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 MAD 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.
Share