Documentation

Mean or median absolute deviation

Description

example

y = mad(X) returns the mean absolute deviation of the values in X.

• If X is a vector, then mad returns the mean or median absolute deviation of the values in X.

• If X is a matrix, then mad returns a row vector containing the mean or median absolute deviation of each column of X.

• If X is a multidimensional array, then mad operates along the first nonsingleton dimension of X.

example

y = mad(X,flag) specifies whether to compute the mean absolute deviation (flag = 0, the default) or the median absolute deviation (flag = 1).

example

y = mad(X,flag,'all') returns the mean or median absolute deviation of all elements of X.

example

y = mad(X,flag,dim) returns the mean or median absolute deviation along the operating dimension dim of X.

example

y = mad(X,flag,vecdim) returns the mean or median absolute deviation over the dimensions specified in the vector vecdim. For example, if X is a 2-by-3-by-4 array, then mad(X,0,[1 2]) returns a 1-by-1-by-4 array. Each element of the output array is the mean absolute deviation of the elements on the corresponding page of X.

Examples

collapse all

Compare the robustness of the standard deviation, mean absolute deviation, and median absolute deviation in the presence of outliers.

Create a data set x of normally distributed data. Create another data set xo that contains the elements of x and an additional outlier.

rng('default') % For reproducibility
x = normrnd(0,1,1,50);
xo = [x 10];

Compute the ratio of the standard deviations of the two data sets.

r1 = std(xo)/std(x)
r1 = 1.4633

Compute the ratio of the mean absolute deviations of the two data sets.

r2 = 1.1833

Compute the ratio of the median absolute deviations of the two data sets.

r3 = 1.0336

In this case, the median absolute deviation is less influenced by the outlier compared to the other two scale estimates.

Find the mean and median absolute deviations of all the values in an array.

Create a 3-by-5-by-2 array X and add an outlier.

X = reshape(1:30,[3 5 2]);
X(6) = 100
X =
X(:,:,1) =

1     4     7    10    13
2     5     8    11    14
3   100     9    12    15

X(:,:,2) =

16    19    22    25    28
17    20    23    26    29
18    21    24    27    30

Find the mean and median absolute deviations of the elements in X.

meandev = 10.1178
mediandev = 7.5000

meandev is the mean absolute deviation of all the elements in X, and mediandev is the median absolute deviation of all the elements in X.

Find the median absolute deviation along different dimensions for a multidimensional array.

Set the random seed for reproducibility of the results.

rng('default')

Create a 1-by-3-by-2 array of random numbers.

X = randn([1,3,2])
X =
X(:,:,1) =

0.5377    1.8339   -2.2588

X(:,:,2) =

0.8622    0.3188   -1.3077

Find the median absolute deviation of X along the default dimension.

Y2 = mad(X,1) % Flag is set to 1 for the median absolute deviation
Y2 =
Y2(:,:,1) =

1.2962

Y2(:,:,2) =

0.5434

By default, mad operates along the first dimension of X whose size does not equal 1. In this case, this dimension is the second dimension of X. Therefore, Y2 is a 1-by-1-by-2 array.

Find the median absolute deviation of X along the third dimension.

Y3 = 1×3

0.1623    0.7576    0.4756

Y3 is a 1-by-3 matrix.

Find the mean absolute deviation over multiple dimensions by using the vecdim input argument.

Set the random seed for reproducibility of the results.

rng('default')

Create a 4-by-3-by-2 array of random numbers.

X = randn([4 3 2])
X =
X(:,:,1) =

0.5377    0.3188    3.5784
1.8339   -1.3077    2.7694
-2.2588   -0.4336   -1.3499
0.8622    0.3426    3.0349

X(:,:,2) =

0.7254   -0.1241    0.6715
-0.0631    1.4897   -1.2075
0.7147    1.4090    0.7172
-0.2050    1.4172    1.6302

Find the mean absolute deviation of each page of X by specifying the first and second dimensions.

ypage = mad(X,0,[1 2])
ypage =
ypage(:,:,1) =

1.4626

ypage(:,:,2) =

0.6652

For example, ypage(:,:,2) is the mean absolute deviation of all the elements in X(:,:,2), and is equivalent to specifying mad(X(:,:,2),0,'all').

Find the mean absolute deviation of the elements in each X(:,i,:) slice by specifying the first and third dimensions.

ycol = mad(X,0,[1 3])
ycol = 1×3

0.8330    0.7872    1.5227

For example, ycol(3) is the mean absolute deviation of all the elements in X(:,3,:), and is equivalent to specifying mad(X(:,3,:),0,'all').

Input Arguments

collapse all

Input data that represents a sample from a population, specified as a vector, matrix, or multidimensional array.

• If X is a vector, then mad returns the mean or median absolute deviation of the values in X.

• If X is a matrix, then mad returns a row vector containing the mean or median absolute deviation of each column of X.

• If X is a multidimensional array, then mad operates along the first nonsingleton dimension of X.

To specify the operating dimension when X is a matrix or an array, use the dim input argument.

mad treats NaNs as missing values and removes them.

Data Types: single | double

Indicator for the type of deviation, specified as 0 or 1.

• If flag is 0 (default), then mad computes the mean absolute deviation, mean(abs(X – mean(X))).

• If flag is 1, then mad computes the median absolute deviation, median(abs(X – median(X))).

Data Types: single | double | logical

Dimension along which to operate, specified as a positive integer. If you do not specify a value for dim, then the default is the first dimension of X whose size does not equal 1.

Consider the mean absolute deviation of a matrix X:

• If dim is equal to 1, then mad(X) returns a row vector that contains the mean absolute deviation of each column in X.

• If dim is equal to 2, then mad(X) returns a column vector that contains the mean absolute deviation of each row in X.

Data Types: single | double

Vector of dimensions, specified as a positive integer vector. Each element of vecdim represents a dimension of the input array X. The output y has length 1 in the specified operating dimensions. The other dimension lengths are the same for X and y.

For example, if X is a 2-by-3-by-3 array, then mad(X,0,[1 2]) returns a 1-by-1-by-3 array. Each element of the output array is the mean absolute deviation of the elements on the corresponding page of X. Data Types: single | double

Output Arguments

collapse all

Mean or median absolute deviation, returned as a scalar, vector, matrix, or multidimensional array. If flag is 0 (default), then y is the mean absolute deviation of the values in X, mean(abs(X – mean(X))). If flag is 1, then y is the median absolute deviation of the values in X, median(abs(X – median(X))).

Tips

• For normally distributed data, multiply mad by one of the following factors to obtain an estimate of the normal scale parameter σ:

• sigma = 1.253 * mad(X,0) — For mean absolute deviation

• sigma = 1.4826 * mad(X,1) — For median absolute deviation

 Mosteller, F., and J. Tukey. Data Analysis and Regression. Upper Saddle River, NJ: Addison-Wesley, 1977.

 Sachs, L. Applied Statistics: A Handbook of Techniques. New York: Springer-Verlag, 1984, p. 253.