Robust scale, skewness and tail weight¶
Robust alternatives to the standard deviation, and robust measures of skewness
and tail weight, in robustmodels.scale.
For a visual summary that uses the medcouple to adjust for skewness, see
plots.adjusted_boxplot.
Choosing an estimator¶
Function |
Breakdown point |
Gaussian efficiency |
Notes |
|---|---|---|---|
|
50% |
37% |
Simplest; needs a center |
|
50% |
82% |
No center needed; a good default |
|
50% |
58% |
No center needed |
|
13.4% |
86% |
Most efficient, least robust |
|
The scale used by |
||
|
|
The scale minimized by S-estimators |
The consistency calibration targets the normal standard deviation
asymptotically. Efficiency and breakdown percentages in this table are
asymptotic values, not finite-sample guarantees. m_scale is always calibrated;
the other scale estimators expose consistency=True.
Use¶
>>> from robustmodels import scale
>>> x = [1, 2, 2, 3, 4, 100]
>>> round(scale.mad(x), 4)
1.4826
>>> round(scale.qn(x), 4)
1.3586
>>> round(scale.sn(x), 4)
1.1843
>>> round(scale.tau_scale(x), 4)
1.5998
>>> round(scale.m_scale(x), 4)
1.5019
One outlier in six is 16.7% of the sample, above the breakdown point of Pn:
>>> round(scale.pn(x), 4)
54.6572
Corrections¶
consistency=False disables the normal-consistency multiplier. For Qn, Sn and
Pn the finite correction is independent: disable finite_correction as well
to obtain the raw order statistic. For example, the default uncorrected MAD is:
>>> scale.mad(x, consistency=False)
1.0
Qn, Sn and Pn also apply a finite-sample correction by default, which reduces
their bias in small normal samples; mad offers it with
finite_correction=True. Disable it to get the asymptotic version:
>>> round(scale.qn(x, finite_correction=False), 4)
2.2191
Skewness and tail weight¶
The medcouple lies in [-1, 1] and is positive for right-skewed data. The left and right medcouples measure the weight of each tail.
>>> y = [0.2, 0.5, 0.9, 1.1, 1.6, 2.4, 3.8, 7.5]
>>> round(scale.medcouple(y), 4)
0.4424
>>> round(scale.lmc(y), 4), round(scale.rmc(y), 4)
(0.0556, 0.2458)
Numerical conventions and sources¶
The small-sample factors are fitted or simulated approximations; they do not make every finite sample unbiased. They target normal data and the documented center/quantile conventions. MAD’s finite factors assume the sample median as center; they are not calibrated for an arbitrary supplied center. MAD selects the middle values with NumPy and averages them without overflow. It rescales by powers of two to retain small deviations when the measurements span very different magnitudes.
Qn uses the exact normal-quantile constant instead of robustbase’s rounded
2.21914. robustbase’s Qn C routine also casts some selection pivots to single
precision, so even unscaled results can differ around 1e-8. Sn uses the
low median of high medians, not averages of middle distances.
Pn uses inverse empirical quantiles (type=1 in R), including the author’s
finite correction factors. Its normal constant is 1/(sqrt(2)*Phi^-1(3/4));
the author’s reference R code uses the rounded reciprocal 1/0.9539.
For the medcouple, the tie kernel follows Brys et al. (2004), equations
(2.1)–(2.3). An even number of kernels uses the average of the two middle
values. For [-3, 0, 0], this definition gives -0.5, while robustbase 0.99-7
returns -1; the same issue can propagate to lmc and rmc.
The current generic sorted-row selector used by Qn, Pn and medcouple has an
O(n log² n) time bound and O(n) auxiliary storage. Sn uses O(n log n) time.
The existence of an O(n log n) algorithm in a paper does not establish that
bound for a different implementation. “Exact” order-statistic calculations
still use floating-point arithmetic.
Sources: Rousseeuw–Croux, Tarr–Müller–Weber, Brys–Hubert–Struyf, tail weights, and Maronna–Zamar. The source map covers every public method.
Reference¶
- mad(x, *, center=None, consistency=True, finite_correction=False)[source]¶
Median absolute deviation,
1.4826 * med|x_i - center|.Breakdown point 50%, Gaussian efficiency 37%.
- Parameters:
x (array_like) – One-dimensional sample of finite values.
center (float, optional) – Center of the deviations. Defaults to the sample median.
consistency (bool, default True) – Multiply by
1 / Phi^-1(3/4) = 1.4826, so that the MAD estimates the standard deviation at the normal model.finite_correction (bool, default False) – Multiply by the small-sample factors of Croux & Rousseeuw (1992). Without them the MAD underestimates the normal standard deviation by 17% at n = 5.
- Returns:
nanfor an empty sample, 0 for a single observation,infif the scaled MAD overflows float64.- Return type:
float
References
Hampel, F. R. (1974). The influence curve and its role in robust estimation. Journal of the American Statistical Association, 69(346), 383-393.
Croux, C. and Rousseeuw, P. J. (1992). Time-efficient algorithms for two highly robust estimators of scale. Computational Statistics, 1, 411-428.
Examples
>>> from robustmodels.scale import mad >>> round(mad([1, 2, 2, 3, 4, 100]), 4) 1.4826 >>> mad([1, 2, 2, 3, 4, 100], consistency=False) 1.0
- qn(x, *, consistency=True, finite_correction=True)[source]¶
Qn scale estimator of Rousseeuw & Croux (1993).
The
k-th smallest of the distances|x_i - x_j|,i < j, withk = h(h - 1) / 2andh = n // 2 + 1. Breakdown point 50%, Gaussian asymptotic efficiency 82%. The current sorted-row selection uses O(n log^2 n) time and O(n) auxiliary storage, without forming all pairs.- Parameters:
x (array_like) – One-dimensional sample of finite values.
consistency (bool, default True) – Multiply by
1 / (sqrt(2) * Phi^-1(5/8)) = 2.2191, so that Qn estimates the standard deviation at the normal model.finite_correction (bool, default True) – Multiply by the small-sample factors used by R’s robustbase, which make Qn approximately unbiased at the normal model for small
n.
- Returns:
nanfor an empty sample, 0 for a single observation.- Return type:
float
References
Rousseeuw, P. J. and Croux, C. (1993). Alternatives to the median absolute deviation. Journal of the American Statistical Association, 88(424), 1273-1283.
Examples
>>> from robustmodels.scale import qn >>> round(qn([1, 2, 2, 3, 4, 100]), 4) 1.3586
- sn(x, *, consistency=True, finite_correction=True)[source]¶
Sn scale estimator of Rousseeuw & Croux (1993).
lomed_i himed_j |x_i - x_j|: for each observation, the high median of its distances to all observations; then the low median of these values. Breakdown point 50%, Gaussian efficiency 58%. Computed exactly in O(n log n) time.- Parameters:
x (array_like) – One-dimensional sample of finite values.
consistency (bool, default True) – Multiply by 1.1926, so that Sn estimates the standard deviation at the normal model.
finite_correction (bool, default True) – Multiply by the small-sample factors of Croux & Rousseeuw (1992).
- Returns:
nanfor an empty sample, 0 for a single observation.- Return type:
float
References
Rousseeuw, P. J. and Croux, C. (1993). Alternatives to the median absolute deviation. Journal of the American Statistical Association, 88(424), 1273-1283.
Croux, C. and Rousseeuw, P. J. (1992). Time-efficient algorithms for two highly robust estimators of scale. Computational Statistics, 1, 411-428.
Examples
>>> from robustmodels.scale import sn >>> round(sn([1, 2, 2, 3, 4, 100]), 4) 1.1843
- pn(x, *, consistency=True, finite_correction=True)[source]¶
Pn scale estimator of Tarr, Müller & Weber (2012).
Interquartile range of the pairwise means:
H^-1(3/4) - H^-1(1/4)withHthe empirical distribution of(x_i + x_j) / 2,i < j. Asymptotic breakdown point 13.4%, Gaussian efficiency 86%. The current sorted-row selection uses O(n log^2 n) time and O(n) auxiliary storage.- Parameters:
x (array_like) – One-dimensional sample of finite values.
consistency (bool, default True) – Multiply by
1 / (sqrt(2) * Phi^-1(3/4)) = 1.0484, so that Pn estimates the standard deviation at the normal model.finite_correction (bool, default True) – Multiply by the factors in Tarr’s reference R code for
n <= 40and byn / (n - 0.7)for larger samples.
- Returns:
nanfor an empty sample, 0 for one or two observations.- Return type:
float
References
Tarr, G., Müller, S. and Weber, N. C. (2012). A robust scale estimator based on pairwise means. Journal of Nonparametric Statistics, 24(1), 187-199.
Tarr, G. (2014). Quantile Based Estimation of Scale and Dependence. PhD thesis, University of Sydney. https://hdl.handle.net/2123/10590
Tarr’s reference R implementation and finite-sample factors: https://www.maths.usyd.edu.au/u/gartht/R/Pn.R
Examples
One outlier in six exceeds the breakdown point of Pn:
>>> from robustmodels.scale import pn >>> round(pn([1, 2, 2, 3, 4, 100]), 4) 54.6572
- tau_scale(x, *, c1=4.5, c2=3.0, consistency=True)[source]¶
Tau scale of Yohai & Zamar (1988), in the form of Maronna & Zamar (2002).
s0 * sqrt(mean(min(((x - mu) / s0)^2, c2^2)) / E), withs0the raw (unscaled) MAD andmuthe tau location (robustmodels.location.tau_location()).- Parameters:
x (array_like) – One-dimensional sample of finite values.
c1 (float, default 4.5) – Bisquare tuning constant of the location weights.
c2 (float, default 3.0) – Truncation point of the standardized residuals.
consistency (bool, default True) – Divide by
E = E[min(Z^2, (c2 * Phi^-1(3/4))^2)], so that the result estimates the standard deviation at the normal model.
- Returns:
nanfor an empty sample, 0 for a single observation or a zero MAD.- Return type:
float
References
Yohai, V. J. and Zamar, R. H. (1988). High breakdown-point estimates of regression by means of the minimization of an efficient scale. Journal of the American Statistical Association, 83(402), 406-413.
Maronna, R. A. and Zamar, R. H. (2002). Robust estimates of location and dispersion for high-dimensional datasets. Technometrics, 44(4), 307-317.
Examples
>>> from robustmodels.scale import tau_scale >>> round(tau_scale([1, 2, 2, 3, 4, 100]), 4) 1.5998
- m_scale(x, *, center=None, breakdown=0.5)[source]¶
Bisquare M-estimator of scale.
Solves
mean(rho((x_i - center) / s)) = breakdownfors, with Tukey’s bisquarerhoscaled to a maximum of 1. Its tuning constant makessestimate the standard deviation at the normal model. This is the scale that S-estimators minimize.- Parameters:
x (array_like) – One-dimensional sample of finite values.
center (float, optional) – Center of the residuals. Defaults to the sample median; pass 0 for residuals that are already centered.
breakdown (float in (0, 0.5], default 0.5) – Breakdown point, the right-hand side of the equation.
- Returns:
nanfor an empty sample. 0 for a single observation, or when at most a fractionbreakdownof the residuals is nonzero.- Return type:
float
References
Maronna, R. A., Martin, R. D., Yohai, V. J. and Salibián-Barrera, M. (2019). Robust Statistics: Theory and Methods (with R), 2nd ed. Wiley.
Examples
>>> from robustmodels.scale import m_scale >>> round(m_scale([1, 2, 2, 3, 4, 100]), 4) 1.5019
- medcouple(x)[source]¶
Medcouple, a robust measure of skewness (Brys, Hubert & Struyf 2004).
Median of
((x_i - m) - (m - x_j)) / (x_i - x_j)overx_i >= m >= x_j, withmthe sample median and the paper’s kernel for ties atm. Lies in [-1, 1]: positive for right skew, 0 for symmetric or constant samples, andmedcouple(-x) == -medcouple(x). Breakdown point 25%. The current sorted-row selection uses O(n log^2 n) time and O(n) auxiliary storage. For an even number of kernels the two middle values are averaged; this can differ from robustbase with ties at the median.- Parameters:
x (array_like) – One-dimensional sample of finite values.
- Returns:
nanfor an empty sample.- Return type:
float
References
Brys, G., Hubert, M. and Struyf, A. (2004). A robust measure of skewness. Journal of Computational and Graphical Statistics, 13(4), 996-1017.
Examples
>>> from robustmodels.scale import medcouple >>> medcouple([1, 2, 2, 3, 4, 100]) 0.5
- lmc(x)[source]¶
Left medcouple, a robust measure of left tail weight (Brys, Hubert & Struyf 2006).
LMC = -medcouple(x[x <= median(x)]); larger values mean a heavier left tail.- Parameters:
x (array_like) – One-dimensional sample of finite values.
- Returns:
nanfor an empty sample.- Return type:
float
References
Brys, G., Hubert, M. and Struyf, A. (2006). Robust measures of tail weight. Computational Statistics & Data Analysis, 50(3), 733-759.
Examples
>>> from robustmodels.scale import lmc >>> round(lmc([0.2, 0.5, 0.9, 1.1, 1.6, 2.4, 3.8, 7.5]), 4) 0.0556
- rmc(x)[source]¶
Right medcouple, a robust measure of right tail weight (Brys, Hubert & Struyf 2006).
RMC = medcouple(x[x >= median(x)]); larger values mean a heavier right tail.- Parameters:
x (array_like) – One-dimensional sample of finite values.
- Returns:
nanfor an empty sample.- Return type:
float
References
Brys, G., Hubert, M. and Struyf, A. (2006). Robust measures of tail weight. Computational Statistics & Data Analysis, 50(3), 733-759.
Examples
>>> from robustmodels.scale import rmc >>> round(rmc([0.2, 0.5, 0.9, 1.1, 1.6, 2.4, 3.8, 7.5]), 4) 0.2458