Generalized Extreme Studentized Deviate test according to Rosner
[outlierfree, outlier] = ST_esd(v, p, maxoutliers) [outlierfree, outlier] = ST_esd(v, p, maxoutliers, side) outlierfree = ST_esd(v, p, maxoutliers) outlierfree = ST_esd(v, p, maxoutliers, side)
real vector of numerical sample values; at least three values are required
statistical confidence level as a string or significance level alpha as a decimal value "95%", "99%", "99.9%", 0.05, 0.01 or 0.001
positive integer specifying the maximum number of suspected outliers; it must not exceed n-2
test direction, "both" (default), "left" or "right"
input vector with all detected outliers removed; unchanged if the test does not identify an outlier
detected outliers in their original input order; [] if no outlier is detected
ST_esd performs Rosner's generalized Extreme Studentized Deviate (ESD) test for one or more outliers. Unlike repeatedly applying the classical Grubbs test, the generalized ESD procedure evaluates a sequence of test statistics and matching critical values while controlling the overall significance level for up to maxoutliers suspected observations.
The procedure first removes the most extreme remaining observation and calculates Ri and λi for i = 1,...,maxoutliers. The number of outliers is then the largest index k for which Rk > λk. The first k observations removed during the sequence are classified as outliers. Observations removed only for calculating later test statistics are restored when their index is greater than k.
The test assumes that the non-outlying observations are independent and approximately normally distributed. A minimum sample size of n >= 3 is required. Rosner's critical-value approximation is most reliable for moderate and large samples; test power and calibration are restricted for very small samples. maxoutliers should be chosen before inspecting the results and should normally be small relative to the sample size.
Test directions: "both" removes the observation with the largest absolute deviation from the current sample mean. The critical probability uses α/(2*ni). This is Rosner's standard two-sided procedure. "left" removes only the current minimum. The critical probability uses α/ni. "right" removes only the current maximum. The critical probability uses α/ni.
The sample standard deviation is calculated with denominator ni-1. If all remaining values are identical at any step, no additional test statistic can be calculated and the sequence stops.
Test statistic at step i

Critical value at step i

Decision rule
Let k be the largest index i for which Ri > λi. The first k observations removed during the calculation sequence are classified as outliers. Equality does not lead to rejection. If no such index exists, no outlier is returned.
Rosner, B. (1983). Percentage Points for a Generalized ESD Many-Outlier Procedure. Technometrics, 25(2), 165-172.
NIST/SEMATECH e-Handbook of Statistical Methods, Generalized ESD Test for Outliers.