bundles / scipy latest / scipy / stats / _stats_py / median_abs_deviation
function
scipy.stats._stats_py:median_abs_deviation
source: /scipy/stats/_stats_py.py :3234
Signature
def median_abs_deviation ( x , axis = 0 , center = None , scale = 1.0 , nan_policy = propagate , * , keepdims = False ) Summary
Compute the median absolute deviation of the data along the given axis.
Extended Summary
The median absolute deviation (MAD, [1]) computes the median over the absolute deviations from the median. It is a measure of dispersion similar to the standard deviation but more robust to outliers [2].
The MAD of an empty array is np.nan.
Parameters
x: array_likeInput array or object that can be converted to an array.
axis: int or None, default: 0If an int, the axis of the input along which to compute the statistic. The statistic of each axis-slice (e.g. row) of the input will appear in a corresponding element of the output. If
None, the input will be raveled before computing the statistic.center: callable, optionalA function that will return the central value. The default is to use np.median. Any user defined function used will need to have the function signature
func(arr, axis).scale: scalar or str, optionalThe numerical value of scale will be divided out of the final result. The default is 1.0. The string "normal" is also accepted, and results in
scalebeing the inverse of the standard normal quantile function at 0.75, which is approximately 0.67449. Array-like scale is also allowed, as long as it broadcasts correctly to the output such thatout / scaleis a valid operation. The output dimensions depend on the input array,x, and theaxisargument.nan_policy: {'propagate', 'omit', 'raise'}Defines how to handle input NaNs.
propagate: if a NaN is present in the axis slice (e.g. row) along which the statistic is computed, the corresponding entry of the output will be NaN.omit: NaNs will be omitted when performing the calculation. If insufficient data remains in the axis slice along which the statistic is computed, the corresponding entry of the output will be NaN.raise: if a NaN is present, aValueErrorwill be raised.
keepdims: bool, default: FalseIf this is set to True, the axes which are reduced are left in the result as dimensions with size one. With this option, the result will broadcast correctly against the input array.
Returns
mad: scalar or ndarrayIf
axis=None, a scalar is returned. If the input contains integers or floats of smaller precision thannp.float64, then the output data-type isnp.float64. Otherwise, the output data-type is the same as that of the input.
Notes
The center argument only affects the calculation of the central value around which the MAD is calculated. That is, passing in center=np.mean will calculate the MAD around the mean - it will not calculate the mean absolute deviation.
The input array may contain inf, but if center returns inf, the corresponding MAD for that data will be nan.
Beginning in SciPy 1.9, np.matrix inputs (not recommended for new code) are converted to np.ndarray before the calculation is performed. In this case, the output will be a scalar or np.ndarray of appropriate shape rather than a 2D np.matrix. Similarly, while masked elements of masked arrays are ignored, the output will be a scalar or np.ndarray rather than a masked array with mask=False.
Array API Standard Support
median_abs_deviation has experimental support for Python Array API Standard compatible backends in addition to NumPy. Please consider testing these features by setting an environment variable SCIPY_ARRAY_API=1 and providing CuPy, PyTorch, JAX, or Dask arrays as array arguments. The following combinations of backend and device (or other capability) are supported.
==================== ==================== ==================== Library CPU GPU ==================== ==================== ==================== NumPy ✅ n/a CuPy n/a ✅ PyTorch ✅ ✅ JAX ⛔ ⛔ Dask ⛔ n/a ==================== ==================== ====================
See
dev-arrayapifor more information.
Examples
When comparing the behavior of `median_abs_deviation` with ``np.std``, the latter is affected when we change a single value of an array to have an outlier value while the MAD hardly changes:import numpy as np from scipy import stats x = stats.norm.rvs(size=100, scale=1, random_state=123456)✓
x.std() stats.median_abs_deviation(x)✗
x[0] = 345.6
✓x.std() stats.median_abs_deviation(x)✗
x = np.array([[10, 7, 4], [3, 2, 1]]) x stats.median_abs_deviation(x)✓
stats.median_abs_deviation(x, axis=None)
✗x = stats.norm.rvs(size=1000000, scale=2, random_state=123456)
✓stats.median_abs_deviation(x) stats.median_abs_deviation(x, scale='normal')✗
See also
- numpy.median
- numpy.std
- numpy.var
- scipy.stats.iqr
- scipy.stats.tmean
- scipy.stats.tstd
- scipy.stats.tvar
Aliases
-
scipy.stats.median_abs_deviation