Diagnostic plots for robust models

The functions in robustmodels.plots turn fitted estimates into Matplotlib diagnostics. Each returns an Axes, accepts an existing ax, and styles that axes without changing global rcParams.

Choosing a plot

Function

Input

What to inspect

outlier_map(model, X, y)

A fitted regressor with predict and scale_

Vertical outliers, good leverage and bad leverage

distance_distance(model, X)

A fitted covariance estimator

Observations masked by the classical covariance

tolerance_ellipse(model, X)

A fitted covariance estimator for exactly two variables

Robust and classical centers, spread and flagged rows

adjusted_boxplot(x)

A finite, nonempty vector or matrix

Marginal outliers after allowing for skewness

bagplot(X)

A nonsingular two-column sample

Bivariate center, spread, skewness and outliers

outlyingness_plot(detector, X=None)

A fitted projection detector

Scores and flags for training or new observations

residual_plot(model, X, y)

A fitted regressor with predict and scale_

Outlying residuals, trends and changing spread along the fitted values

residual_qq(model, X, y)

A fitted regressor with predict and scale_

Whether the regular residuals look normal, and which do not

distance_qq(model, X)

A fitted covariance estimator

Whether the regular robust distances follow the chi-square reference

cluster_plot(model, X)

A fitted TClust

Clusters, their 97.5% ellipses and the trimmed observations

hdr_plot(model)

A fitted KernelDensity of one or two variables

Highest density regions and modes

log_concave_plot(model)

A fitted LogConcaveDensity

The estimate and its knots against the histogram, or the log density

score_plot(model, X, y)

A fitted ScoreMatchingRegressor

The estimated score and how it bounds large residuals

coefficient_plot(models, X=, y=)

Fitted regressors or GLMs with std_errors_

Whether robust and least squares fits lead to different conclusions

dd_plot(X1, X2)

Two samples with the same variables

Whether two samples share a distribution, and how they differ

ctl_curves(X)

A sample to cluster

How many clusters and how much trimming

pca_outlier_map(model, X=None)

A fitted ROBPCA or ProjectionPursuitPCA

Orthogonal outliers, good and bad leverage points of a PCA

scree_plot(model)

A fitted robust PCA

How many components carry the variance

biplot(model, X)

A fitted robust PCA with at least two components

Scores of the observations and loadings of the variables together

discriminant_plot(model, X, y)

A fitted RobustLDA or RobustQDA for exactly two variables

Class regions, boundaries and misclassified observations

Rows are observations and columns are variables. Fit the estimator first; outlier_map additionally fits an MCD to the supplied predictors, while the other model-based functions use the supplied fit. Pass the same predictor columns, in the same order, as when fitting the model.

Figures below are generated from their displayed code when the documentation is built. The examples use fixed random seeds and require no downloaded data.

Regression outlier map

The vertical coordinate is the residual divided by the fitted residual scale, (y - model.predict(X)) / model.scale_. The horizontal coordinate is the unsquared MCD distance of the predictors. The thresholds are ±2.5 and sqrt(chi2.ppf(0.975, p)), where p counts predictors, excluding the intercept.

Class

Robust predictor distance

Absolute standardized residual

Regular

At or below the distance cutoff

At or below 2.5

Vertical outlier

At or below the distance cutoff

Above 2.5

Good leverage

Above the distance cutoff

At or below 2.5

Bad leverage

Above the distance cutoff

Above 2.5

import numpy as np
from robustmodels import plots
from robustmodels.regression import MMRegressor

rng = np.random.default_rng(0)
X = rng.normal(size=(100, 2))
X[:10] += 6
y = 1 + X @ [1.0, 2.0] + rng.normal(scale=0.5, size=100)
y[:5] += 12          # bad leverage: extreme predictors and response off the line
y[10:15] += 12      # vertical outliers; rows 5:10 remain good leverage
model = MMRegressor(random_state=0).fit(X, y)
ax = plots.outlier_map(model, X, y, random_state=0)

(png, svg)

Regression diagnostic separating vertical outliers and good and bad leverage.

random_state controls the additional MCD search, independently of the regression fit. A zero residual scale is an exact fit, for which standardized residuals are undefined: the function raises ValueError. The residual cutoff is a diagnostic convention; it differs from the approximately 2.24 threshold used internally to reweight LTS regression.

By default, up to three eligible observations are labeled with their zero-based row positions, not DataFrame index labels. In this plot their severity is the larger of abs(residual) / 2.5 and distance / distance_cutoff. A good leverage point can therefore be labeled ahead of a bad leverage point. Set the positive integer id_n to change the maximum number of labels.

Distance-distance plot

import numpy as np
from robustmodels import plots
from robustmodels.covariance import MCD

X = np.random.default_rng(0).normal(size=(100, 2))
X[:10] += 10
model = MCD(random_state=0).fit(X)
ax = plots.distance_distance(model, X)

(png, svg)

Robust versus classical Mahalanobis distances for a sample with ten shifted rows.

Both axes show unsquared distances; the estimator’s mahalanobis method returns squared distances and the plot takes their square root. The classical fit is recomputed from X using its mean and sample covariance (divisor n−1). The robust fit is the supplied model. A row is flagged when its robust distance exceeds the 97.5% chi-square reference cutoff.

A point with a large robust distance but a small classical distance is a candidate for masking: the classical fit has absorbed the contamination. Being slightly above the diagonal alone is not an outlier rule. The labels identify the largest flagged robust distances.

Tolerance ellipses

import numpy as np
from robustmodels import plots
from robustmodels.covariance import MCD

X = np.random.default_rng(4).multivariate_normal(
    [0, 0], [[1, 0.7], [0.7, 1]], size=100
)
X[:8] += [5, -3]
model = MCD(random_state=0).fit(X)
ax = plots.tolerance_ellipse(model, X, feature_names=["Measurement 1", "Measurement 2"])

(png, svg)

Robust and classical 97.5 percent reference ellipses with flagged observations.

Each boundary satisfies (x - center)' covariance^-1 (x - center) = chi2.ppf(0.975, 2). The robust boundary uses model.location_ and model.covariance_; the classical boundary uses the sample mean and covariance with divisor n−1. Crosses mark the centers. Flags and labels use robust distances, so a flagged observation is outside the robust ellipse.

Pass data with exactly two columns and, if supplied, exactly two feature_names. Fit a two-dimensional model to those columns first; selecting two columns from a higher-dimensional fit does not define this diagnostic.

The 97.5% level is a multivariate-normal reference contour. It is not a confidence region for the estimated center, an exact finite-sample tolerance region or a guarantee that 2.5% of a fitted sample will be flagged. Distances and chi-square calibration require a nonsingular covariance; a pseudoinverse on a degenerate sample does not restore that calibration.

Skewness-adjusted boxplot

import numpy as np
from robustmodels import plots

rng = np.random.default_rng(0)
X = np.column_stack([
    rng.lognormal(size=200),
    rng.normal(size=200),
    -rng.exponential(size=200),
])
ax = plots.adjusted_boxplot(X, labels=["lognormal", "normal", "-exponential"])

(png, svg)

Adjusted boxplots for right-skewed, symmetric and left-skewed samples.

One box is drawn per column. The box uses Tukey’s lower and upper hinges, as R’s fivenum, and the middle line is the median. Let IQR = Q3 - Q1 and MC be the medcouple. The lower and upper fences are:

Medcouple

Lower fence

Upper fence

MC >= 0

Q1 - 1.5 exp(-4 MC) IQR

Q3 + 1.5 exp(3 MC) IQR

MC < 0

Q1 - 1.5 exp(-3 MC) IQR

Q3 + 1.5 exp(4 MC) IQR

Whiskers end at the most extreme observed values inside the fences, not at the fences themselves. Values strictly beyond a fence are drawn as orange diamonds; values on a fence stay inside. For MC = 0 the usual 1.5-IQR fences are recovered. Positive skewness lengthens the upper fence and shortens the lower one; reflecting the sample exchanges the two sides.

The medcouple above each box summarizes its skewness. With several columns, the subtitle counts flagged values across columns, so a row flagged in two variables contributes twice. This is a marginal diagnostic and does not detect every multivariate outlier. Supply one label per column; empty, nonfinite or higher-dimensional input raises ValueError.

These hinges follow robustbase::adjboxStats; the projection detector’s adjusted outlyingness instead uses type-5 quartiles, following mrfDepth. Both use this package’s medcouple definition, including its documented handling of ties, which can differ from R; see the method references.

Outlyingness by observation

import numpy as np
from robustmodels import plots
from robustmodels.outliers import AdjustedOutlyingness

X = np.random.default_rng(0).lognormal(size=(100, 2))
X[:5] = [-3, -3]
detector = AdjustedOutlyingness(random_state=0).fit(X)
ax = plots.outlyingness_plot(detector)

(png, svg)

Adjusted outlyingness scores with the fitted cutoff and five flagged observations.

Without X, the plot uses detector.outlyingness_. With X, it evaluates detector.outlyingness(X) against the existing fit, and keeps its training cutoff_; it does not refit or recalibrate on the new observations.

The horizontal coordinate is the zero-based position in the plotted data. Finite scores above the cutoff are orange diamonds. Positive infinite outlyingness, possible when a projection has zero scale, is displayed as an orange triangle at a finite height above the finite scores and cutoff. That height is only a plotting position, not a replacement for the infinite score.

The legend gives separate counts for regular observations, finite flagged scores and infinite scores; empty classes are omitted. The subtitle counts all scores above the cutoff, including positive infinities. Up to id_n flagged observations with the largest scores are labeled, keeping input order when scores tie. Nearby labels are separated with small offsets and, when needed, leader lines.

StahelDonohoOutlyingness can be used in the same way. The plot displays the detector’s rule; it does not introduce a second outlier criterion. See outlier detection for the log-scale cutoff and approximation by a finite set of projection directions.

PCA outlier map

import numpy as np
from robustmodels import plots
from robustmodels.multivariate import ROBPCA

rng = np.random.default_rng(0)
X = rng.normal(size=(100, 2)) @ rng.normal(size=(2, 6))
X += 0.3 * rng.normal(size=X.shape)
X[:8] += 4
ax = plots.pca_outlier_map(ROBPCA(2, random_state=0).fit(X))

(png, svg)

PCA outlier map with orthogonal outliers, a good leverage point and bad leverage points.

The horizontal axis is the score distance, the robust distance of an observation’s scores from the center inside the principal subspace. The vertical axis is the orthogonal distance from the observation to that subspace. The cutoffs are those of the fitted model: the square root of the 0.975 chi-square quantile with as many degrees of freedom as components, and orthogonal_cutoff_.

Region

Class

Reading

Below, left

Regular

Close to the subspace and to the center

Above, left

Orthogonal outliers

Far from the subspace; their scores look ordinary

Below, right

Good leverage

In the subspace but far from the center

Above, right

Bad leverage

Far from the subspace and from the center

Without X the map shows the training data; with X it places new observations against the existing fit and cutoffs. A model that keeps as many components as the rank of the data has no orthogonal distances, and the function raises ValueError. See the PCA guide.

Scree plot and biplot

import matplotlib.pyplot as plt
import numpy as np
from robustmodels import plots
from robustmodels.multivariate import ROBPCA

rng = np.random.default_rng(0)
X = rng.normal(size=(100, 2)) @ rng.normal(size=(2, 6))
X += 0.3 * rng.normal(size=X.shape)
X[:8] += 4
names = ["Ca", "Mg", "Na", "K", "Fe", "Zn"]

fig, axes = plt.subplots(1, 2, figsize=(10.4, 3.8), layout="constrained")
plots.scree_plot(ROBPCA(5, random_state=0).fit(X), ax=axes[0])
plots.biplot(ROBPCA(2, random_state=0).fit(X), X, feature_names=names, ax=axes[1])

(png, svg)

Scree plot of five robust components beside a biplot of the first two.

The scree plot shows the robust variance of each fitted component; the label above each point is the cumulative share of the variance of the fitted components, not of the total variance of the data, which a robust fit with fewer components than variables does not estimate. Fit as many components as you want to compare, then refit with the number you keep.

In the biplot, points are scores on two components (components=(0, 1) by default) and arrows are the loadings of the variables on the same components. All arrows are multiplied by one factor, so only their directions and relative lengths are meaningful: variables with arrows in the same direction vary together in this plane. Observations beyond the score or the orthogonal cutoff of the model are flagged, so an orthogonal outlier can appear in the middle of the cloud. n_arrows limits the arrows to the longest ones.

Classification regions

import numpy as np
from robustmodels import plots
from robustmodels.multivariate import RobustQDA

rng = np.random.default_rng(0)
X = np.vstack([
    rng.normal(size=(50, 2)),
    2 * rng.normal(size=(50, 2)) + 4,
    rng.normal(size=(50, 2)) + [5, -2],
])
y = np.repeat(["a", "b", "c"], 50)
X[:5] = [9, 9]  # five outliers labeled "a"
ax = plots.discriminant_plot(RobustQDA(random_state=0).fit(X, y), X, y)

(png, svg)

Quadratic classification regions of three classes with misclassified points circled.

Shaded regions show the class predicted at each point of the plane and gray lines the boundaries. Crosses and colored ellipses are the robust center and 97.5% tolerance ellipse of each class, the display used by Hubert and Van Driessen; RobustLDA draws the same shape for every class. Observations keep the color and marker of their true class; those assigned to another class are circled. The subtitle reports the share of plotted observations that are misclassified. For the training data this apparent error is optimistic: report the error on held-out data or by cross-validation. The function needs exactly two variables.

Residual and quantile plots

import matplotlib.pyplot as plt
import numpy as np
from robustmodels import plots
from robustmodels.covariance import MCD
from robustmodels.regression import MMRegressor

rng = np.random.default_rng(0)
X = rng.normal(size=(100, 3))
y = 1 + X @ [2.0, -1.0, 0.5] + rng.normal(scale=0.5, size=100)
X[:10] += 4
y[10:20] += 6
model = MMRegressor(random_state=0).fit(X, y)

fig, axes = plt.subplots(1, 3, figsize=(15.6, 3.8), layout="constrained")
plots.residual_plot(model, X, y, ax=axes[0])
plots.residual_qq(model, X, y, ax=axes[1])
plots.distance_qq(MCD(random_state=0).fit(X), X, ax=axes[2])

(png, svg)

Residuals against fitted values, their normal quantile plot and a chi-square quantile plot of robust distances.

residual_plot shows the standardized residuals against the fitted values, with +-2.5 as cutoffs. A trend or a funnel shape among the regular observations points to a missing term or to a variance that changes with the mean.

distance_qq plots the sorted robust distances against the square roots of chi-square quantiles, and residual_qq the sorted standardized residuals against normal quantiles, both at probabilities (i - 0.5) / n. Regular observations from the normal model follow the diagonal. Outliers leave it at the ends and lie beyond the dashed cutoffs: the square root of the 0.975 chi-square quantile, and +-2.5. A curved pattern in the regular observations suggests that the normal reference does not describe the bulk of the data, in which case the cutoffs are only rough guides. The distances and residuals come from the robust fit, so the outliers do not bend the reference themselves.

Bagplot

import numpy as np
from robustmodels import plots

X = np.random.default_rng(0).normal(size=(100, 2))
X[:3] = [[6, 6], [7, -6], [-8, 5]]
ax = plots.bagplot(X, feature_names=["First measurement", "Second measurement"])

(png, svg)

Bagplot with a Tukey median, central bag, fence and three outliers.

The bagplot is the bivariate boxplot. The cross is the Tukey median, the dark bag holds the half of the observations with the largest halfspace depth, and the dashed fence is the bag inflated by factor (3 by default) from the median. The light loop is the convex hull of the observations inside the fence; observations outside it are flagged. A bag elongated along a diagonal shows correlation, and a bag off center inside the loop shows skewness. For more than 200 observations the median and the bag are computed on a random subset of max_samples rows, controlled by random_state. See the depth guide.

Clusters and trimmed observations

import numpy as np
from robustmodels import plots
from robustmodels.cluster import TClust

rng = np.random.default_rng(1)
X = np.vstack([
    rng.multivariate_normal([0, 0], np.eye(2), 100),
    rng.multivariate_normal([5, 5], [[4, 0], [0, 0.3]], 80),
    rng.multivariate_normal([-4, 6], [[1, 0.8], [0.8, 1]], 60),
    rng.uniform(-10, 12, (12, 2)),
])
ax = plots.cluster_plot(TClust(3, random_state=0).fit(X), X)

(png, svg)

Three clusters of different shapes with 97.5% ellipses and trimmed noise points.

Observations are colored by cluster and the trimmed ones are orange diamonds. Crosses and ellipses are the center and 97.5% tolerance ellipse of each cluster’s normal model. With more than two variables, features selects the two columns on the axes and the ellipses are the marginal ones of those columns. Passing other data than the training sample assigns it with model.predict. See the clustering guide.

Choosing the number of clusters and the trimming

import numpy as np
from robustmodels import plots

rng = np.random.default_rng(4)
X = np.vstack([
    rng.multivariate_normal([0, 0], np.eye(2), 100),
    rng.multivariate_normal([5, 5], [[4, 0], [0, 0.3]], 80),
    rng.multivariate_normal([-4, 6], [[1, 0.8], [0.8, 1]], 60),
    rng.uniform(-10, 12, (15, 2)),
])
ax = plots.ctl_curves(X, n_init=100, random_state=0)

(png, svg)

Trimmed likelihood against the trimming level for one to four clusters.

ctl_curves fits TClust for every number of clusters in n_clusters and every trimming level in alphas, and draws the trimmed classification log-likelihood against alpha, one curve per number of clusters. The likelihood always grows with the number of clusters. Choose the smallest k whose curve is close to that of k + 1, and the smallest alpha from which they stay close. Here the curves for three and four clusters merge from alpha = 0.04. Each point is a full fit, so the plot takes len(n_clusters) * len(alphas) times as long as one fit; lower n_init for a first look.

Highest density regions

import numpy as np
from robustmodels import plots
from robustmodels.density import KernelDensity

rng = np.random.default_rng(1)
X = np.vstack([
    rng.multivariate_normal([0, 0], [[1, 0.6], [0.6, 1]], 250),
    rng.multivariate_normal([3, 3], [[0.4, -0.2], [-0.2, 0.4]], 150),
])
ax = plots.hdr_plot(KernelDensity().fit(X))

(png, svg)

Filled 25%, 50% and 75% highest density regions of a two-mode sample.

The region of probability prob is the smallest region with that probability, {x : f(x) >= level}, with the level from model.hdr_level(prob). For two variables the regions are filled, darker for the smaller ones, over the observations; for one variable they are shaded under the density curve over a rug of the observations. A region that splits into separate pieces reveals several modes; here every region has one piece around each of the two modes. See the density guide.

Log-concave density

import matplotlib.pyplot as plt
import numpy as np
from robustmodels import plots
from robustmodels.shape import LogConcaveDensity

x = np.random.default_rng(3).gamma(2, size=250)
model = LogConcaveDensity().fit(x)

fig, axes = plt.subplots(1, 2, figsize=(10.4, 3.8), layout="constrained")
plots.log_concave_plot(model, ax=axes[0])
plots.log_concave_plot(model, log=True, ax=axes[1])

(png, svg)

Log-concave density estimate and its log density for a gamma sample.

The left panel draws the estimate over the histogram of the sample, and the right panel, with log=True, its logarithm. The log density is piecewise linear and concave; the knots are the observations where its slope changes. The estimate is zero outside the range of the sample. A histogram with two modes or heavier tails than the estimate contradicts the log-concave assumption. See the shape-constrained guide.

Antitonic score

import numpy as np
from robustmodels import plots
from robustmodels.shape import ScoreMatchingRegressor

rng = np.random.default_rng(4)
X = rng.normal(size=(400, 2))
y = 1 + X @ [2.0, -1.0] + rng.standard_t(2, 400)
ax = plots.score_plot(ScoreMatchingRegressor().fit(X, y), X, y)

(png, svg)

Estimated antitonic score, bounded in the tails, against the normal score.

The curve is the estimated score psi, minus the derivative of the convex loss, against the residual, over a rug of the residuals. The straight line is the score of a normal distribution with the MAD of the residuals, which least squares uses. Where the estimated score flattens, large residuals stop gaining influence: here the errors have Student t tails with two degrees of freedom. The horizontal axis covers the central 98% of the residuals, widened by a quarter of that range on each side. See the shape-constrained guide.

Comparing coefficients across fits

import numpy as np
from robustmodels import plots
from robustmodels.rank import RankRegressor
from robustmodels.regression import MMRegressor
from robustmodels.shape import ScoreMatchingRegressor

rng = np.random.default_rng(4)
X = rng.normal(size=(150, 3))
y = 1 + X @ [2.0, -1.0, 0.0] + rng.standard_t(2, 150)
y[:8] += 25 * X[:8, 2]  # eight observations tie y to the third variable
fits = [MMRegressor().fit(X, y), RankRegressor().fit(X, y), ScoreMatchingRegressor().fit(X, y)]
ax = plots.coefficient_plot(fits, labels=["MM", "Rank", "Score matching"], X=X, y=y,
                            feature_names=["dose", "age", "site"])

(png, svg)

Coefficient estimates with 95% intervals from MM, rank and score matching fits and least squares.

Each row is a coefficient and each fit a marker with its interval estimate ± z * standard error, with z the normal quantile of level (0.95 by default). Passing both X and y adds least squares with its classical standard errors. Here eight observations make the third variable look important to least squares, while the robust fits agree that it is not. Every model needs std_errors_ and the same predictors; set intercept=False to hide the intercept row. See the regression guide.

Depth-depth plot

import numpy as np
from robustmodels import plots

rng = np.random.default_rng(4)
control = rng.normal(size=(150, 2))
treated = rng.normal(size=(120, 2)) * [1.6, 1]
ax = plots.dd_plot(control, treated, labels=["Control", "Treated"])

(png, svg)

Depth-depth plot of two samples whose spreads differ, with points off the diagonal.

Every observation of both samples is placed at its halfspace depth in the first sample and in the second. Samples from one distribution keep the points near the diagonal. A difference in location bends them into a lens, and a difference in spread, as here, moves them to one side. For three or more variables the depth is approximated with random directions controlled by random_state. The plot is descriptive; density.kde_permutation_test and density.kde_test test the difference. See the depth guide.

Customizing and saving

Use ax= to compose several diagnostics. Functions return the supplied axes, so Matplotlib methods remain available after plotting:

>>> import matplotlib.pyplot as plt
>>> from robustmodels import plots
>>> fig, axes = plt.subplots(1, 2, figsize=(10, 4), layout="constrained")
>>> left = plots.adjusted_boxplot([1, 2, 3, 4, 9], ax=axes[0])
>>> right = plots.adjusted_boxplot([-9, -4, -3, -2, -1], ax=axes[1])
>>> left is axes[0] and right is axes[1]
True
>>> plt.close(fig)

Save the returned figure with ax.figure.savefig("diagnostic.png", dpi=300) or ax.figure.savefig("diagnostic.svg") for vector output. In a script, use plt.show() to display it, and plt.close(ax.figure) after saving many figures. On a machine without a display, select Matplotlib’s Agg backend before importing matplotlib.pyplot.

Colors and marker shapes identify groups consistently. Empty scatter groups are omitted from legends. The style is applied locally; the functions do not change Matplotlib’s global style. Very large id_n values can still crowd a figure; inspect its final size before publication.

Sources and R conventions

  • The regression map follows Rousseeuw and van Zomeren. The chi-square degrees of freedom count predictors; plot.lmrob in robustbase 0.99-7 uses the regression rank, including the intercept.

  • Classical distances use the sample covariance as robustbase::covPlot. The robust distances come directly from the fitted estimator.

  • Ellipses use the supplied covariance without an additional multiplier. robustbase::tolEllipsePlot in 0.99-7 multiplies both covariance matrices by n / (n - 1) when drawing its contours. We omit that extra inflation so the robust boundary agrees with the distance used to flag observations.

  • The adjusted fences follow Hubert and Vandervieren. Hinges and whiskers follow adjboxStats; medcouple approximation and ties can produce numerical differences from R without changing the definition.

  • The PCA outlier map follows Hubert, Rousseeuw and Vanden Branden, with their univariate-MCD cutoff for the orthogonal distances; rrcov’s plot method for PcaHubert uses a median and MAD cutoff.

  • distance_distance, tolerance_ellipse and outlier_map raise ValueError when the robust covariance is singular. A pseudo-inverse gives distance zero along the directions without spread, so points far away in those directions would sit inside a contour labeled 97.5%.

Figures for publication

Every plot is sized for one journal column (5.2 × 3.8 inches), uses one color per role across the plots, and pairs each color with its own marker so that figures remain readable in grayscale and for color-blind readers. Save vector figures with ax.figure.savefig("figure.pdf"). Many submission systems (IEEE, Elsevier) reject the Type 3 fonts that Matplotlib embeds in PDF files by default; select TrueType fonts before plotting:

import matplotlib

matplotlib.rcParams["pdf.fonttype"] = 42
matplotlib.rcParams["ps.fonttype"] = 42

Combine several plots in one figure by passing each its own axes, for example from plt.subplots(1, 2, figsize=(10.4, 3.8), layout="constrained").

The validation notes record the pre-release comparison and its limits. These plots are tools for investigating observations, not rules for automatically deleting them. The examples with classic data apply them to data sets whose outliers are documented.

Reference

outlier_map(model, X, y, *, id_n=3, random_state=None, ax=None)[source]

Regression outlier map (Rousseeuw & van Zomeren 1990).

Standardized residuals (y - model.predict(X)) / model.scale_ against robust distances of X from MCD. Residuals beyond +-2.5 and distances beyond the square root of the 0.975 chi-square quantile split the observations into regular observations, vertical outliers, good leverage points and bad leverage points.

Parameters:
  • model (fitted regressor with predict and scale_) – For example MMRegressor.

  • X (array-like of shape (n_samples, n_features)) – Predictors of full rank; ValueError if their MCD covariance is singular, because the chi-square cutoff does not apply.

  • y (array-like of shape (n_samples,)) – One response per row of X.

  • id_n (int, default 3) – Positive maximum number of eligible observations labeled with their zero-based row positions. Severity combines residual and predictor distance.

  • random_state (int, numpy.random.Generator, numpy.random.RandomState or None) – Seed of the MCD fit for the distances.

  • ax (matplotlib Axes, optional) – Axes to draw on; a new figure is created by default.

Return type:

matplotlib.axes.Axes

References

Rousseeuw, P. J. and van Zomeren, B. C. (1990). Unmasking multivariate outliers and leverage points. Journal of the American Statistical Association, 85(411), 633-639.

distance_distance(model, X, *, id_n=3, ax=None)[source]

Distance-distance plot (Rousseeuw & Van Driessen 1999).

Robust distances from a fitted covariance estimator against classical Mahalanobis distances. Large robust distances paired with small classical distances suggest masking. Both axes show unsquared distances; the classical covariance uses divisor n - 1. Lines mark the square root of the 0.975 chi-square quantile.

Parameters:
  • model (fitted covariance estimator with mahalanobis) – For example MCD. ValueError if its covariance is singular, because the chi-square cutoff does not apply.

  • X (array-like of shape (n_samples, n_features))

  • id_n (int, default 3) – Positive maximum number of flagged robust distances labeled with their zero-based row positions.

  • ax (matplotlib Axes, optional)

Return type:

matplotlib.axes.Axes

References

Rousseeuw, P. J. and Van Driessen, K. (1999). A fast algorithm for the minimum covariance determinant estimator. Technometrics, 41(3), 212-223.

tolerance_ellipse(model, X, *, feature_names=None, id_n=3, ax=None)[source]

Robust and classical 97.5% tolerance ellipses for two variables.

The robust ellipse uses the location and covariance of a fitted covariance estimator; the classical one, the sample mean and covariance. Observations with a robust distance beyond the ellipse are flagged. The classical covariance has divisor n - 1. These are normal-reference contours, not confidence regions for the center or exact finite-sample tolerance regions.

Parameters:
  • model (fitted covariance estimator with location_ and covariance_) – ValueError if its covariance is singular: the ellipse would degenerate and the 97.5% label would not hold.

  • X (array-like of shape (n_samples, 2))

  • feature_names (sequence of two str, optional) – Exactly two axis labels; x_1 and x_2 by default.

  • id_n (int, default 3) – Positive maximum number of flagged robust distances labeled with their zero-based row positions.

  • ax (matplotlib Axes, optional)

Return type:

matplotlib.axes.Axes

adjusted_boxplot(x, *, labels=None, ax=None)[source]

Skewness-adjusted boxplot (Hubert & Vandervieren 2008).

The whiskers reach the most extreme observations inside the fences Q1 - 1.5 exp(-4 MC) IQR and Q3 + 1.5 exp(3 MC) IQR for medcouple MC >= 0 (exp(-3 MC) and exp(4 MC) otherwise); observations beyond them are drawn individually. The box spans Tukey’s hinges, as R’s robustbase::adjbox. Each box is labeled with its medcouple (MC).

Parameters:
  • x (array-like of shape (n_samples,) or (n_samples, n_variables)) – Finite, nonempty data; one box per column.

  • labels (sequence of str, optional) – Box labels.

  • ax (matplotlib Axes, optional)

Return type:

matplotlib.axes.Axes

References

Hubert, M. and Vandervieren, E. (2008). An adjusted boxplot for skewed distributions. Computational Statistics & Data Analysis, 52(12), 5186-5201.

outlyingness_plot(detector, X=None, *, id_n=3, ax=None)[source]

Outlyingness of each observation with the detector’s cutoff.

Parameters:
  • detector (fitted StahelDonohoOutlyingness,) – AdjustedOutlyingness or DirectionalOutlyingness

  • X (array-like of shape (n_samples, n_features), optional) – Observations to score against the fitted detector; training data by default. The cutoff is retained from training, without refitting.

  • id_n (int, default 3) – Positive maximum number of flagged scores labeled with their zero-based row positions. Tied scores keep input order.

  • ax (matplotlib Axes, optional)

Return type:

matplotlib.axes.Axes

Notes

Infinite scores are drawn as triangles above the finite scores and cutoff; their display height is not a finite estimate. Legend counts separate finite flagged scores from infinite scores; the subtitle includes both. Empty classes are omitted.

residual_plot(model, X, y, *, id_n=3, ax=None)[source]

Standardized residuals of a robust regression against its fitted values.

Residuals (y - model.predict(X)) / model.scale_ beyond +-2.5 are flagged. Because the fit is robust, outliers keep large residuals instead of pulling the fit towards them. A trend or a funnel shape among the regular observations suggests a missing term or a variance that changes with the mean.

Parameters:
  • model (fitted regressor with predict and scale_) – For example MMRegressor.

  • X (array-like of shape (n_samples, n_features))

  • y (array-like of shape (n_samples,)) – One response per row of X.

  • id_n (int, default 3) – Positive maximum number of flagged residuals labeled with their zero-based row positions.

  • ax (matplotlib Axes, optional)

Return type:

matplotlib.axes.Axes

References

Rousseeuw, P. J. and Leroy, A. M. (1987). Robust Regression and Outlier Detection. Wiley.

residual_qq(model, X, y, *, id_n=3, ax=None)[source]

Normal quantile plot of the standardized residuals of a robust regression.

Sorted residuals (y - model.predict(X)) / model.scale_ against the normal quantiles at (i - 0.5) / n. Regular observations with normal errors follow the diagonal; the dashed lines mark +-2.5.

Parameters:
  • model (fitted regressor with predict and scale_) – For example MMRegressor.

  • X (array-like of shape (n_samples, n_features))

  • y (array-like of shape (n_samples,)) – One response per row of X.

  • id_n (int, default 3) – Positive maximum number of flagged residuals labeled with their zero-based row positions.

  • ax (matplotlib Axes, optional)

Return type:

matplotlib.axes.Axes

distance_qq(model, X, *, id_n=3, ax=None)[source]

Chi-square quantile plot of robust distances.

Sorted robust distances against the square roots of the chi-square quantiles at (i - 0.5) / n. Regular multivariate normal observations follow the diagonal; outliers leave it at the upper end. The dashed line is the square root of the 0.975 chi-square quantile.

Parameters:
  • model (fitted covariance estimator with mahalanobis) – For example MCD. ValueError if its covariance is singular, because the chi-square reference does not apply.

  • X (array-like of shape (n_samples, n_features))

  • id_n (int, default 3) – Positive maximum number of flagged distances labeled with their zero-based row positions.

  • ax (matplotlib Axes, optional)

Return type:

matplotlib.axes.Axes

References

Rousseeuw, P. J. and van Zomeren, B. C. (1990). Unmasking multivariate outliers and leverage points. Journal of the American Statistical Association, 85(411), 633-639.

pca_outlier_map(model, X=None, *, id_n=3, ax=None)[source]

PCA outlier map (Hubert, Rousseeuw & Vanden Branden 2005).

Orthogonal distances to the principal subspace against score distances inside it. The two cutoffs of the fitted model split the observations into regular observations, good leverage points (far inside the subspace), orthogonal outliers (far from the subspace) and bad leverage points (both).

Parameters:
  • model (fitted ROBPCA or) – ProjectionPursuitPCA It must keep fewer components than the rank of the data; otherwise there are no orthogonal distances and ValueError is raised.

  • X (array-like of shape (n_samples, n_features), optional) – Observations to place on the map; training data by default. The cutoffs are retained from training.

  • id_n (int, default 3) – Positive maximum number of flagged observations labeled with their zero-based row positions.

  • ax (matplotlib Axes, optional)

Return type:

matplotlib.axes.Axes

References

Hubert, M., Rousseeuw, P. J. and Vanden Branden, K. (2005). ROBPCA: a new approach to robust principal component analysis. Technometrics, 47(1), 64-79.

scree_plot(model, *, ax=None)[source]

Scree plot: the robust variance of each principal component.

Each point is labeled with the cumulative share of the variance of the fitted components, so fit as many components as you want to compare.

Parameters:
Return type:

matplotlib.axes.Axes

bagplot(X, *, factor=3.0, feature_names=None, id_n=3, max_samples=200, random_state=None, ax=None)[source]

Bagplot, the bivariate boxplot of Rousseeuw, Ruts & Tukey (1999).

The cross is the Tukey median, the dark bag holds the half of the observations with the largest halfspace depth and the light loop is the convex hull of the observations inside the fence, the bag inflated by factor from the median. Observations outside the fence are flagged. The shape of the bag and the loop shows location, spread, correlation and skewness, as the box and whiskers do for one variable.

Parameters:
  • X (array-like of shape (n_samples, 2))

  • factor (float, default 3.0) – Inflation of the bag that gives the fence.

  • feature_names (sequence of two str, optional) – Axis labels; x_1 and x_2 by default.

  • id_n (int, default 3) – Maximum number of outliers labeled with their zero-based row positions, the farthest from the median after scaling by the sample ranges first.

  • max_samples (int, default 200) – Subset size for the median and the bag of larger samples; see bagplot_stats().

  • random_state (int, numpy.random.Generator, numpy.random.RandomState or None) – Seed for the subset.

  • ax (matplotlib Axes, optional)

Return type:

matplotlib.axes.Axes

biplot(model, X, *, components=(0, 1), feature_names=None, n_arrows=10, id_n=3, ax=None)[source]

Biplot of a robust PCA: scores of the observations and loadings of the variables.

Points are the scores on two components. Arrows are the loadings of the variables on the same components, all multiplied by one factor so that the longest arrow reaches 80% of the largest score. Observations beyond the score or orthogonal cutoff of the model are flagged.

Parameters:
  • model (fitted ROBPCA or) – ProjectionPursuitPCA With at least two components.

  • X (array-like of shape (n_samples, n_features)) – Observations to draw, usually the training data.

  • components (pair of int, default (0, 1)) – Zero-based components on the horizontal and vertical axes.

  • feature_names (sequence of str, optional) – One name per variable; x_1, x_2, … by default.

  • n_arrows (int, default 10) – Positive maximum number of variables drawn, those with the longest arrows.

  • id_n (int, default 3) – Positive maximum number of flagged observations labeled with their zero-based row positions.

  • ax (matplotlib Axes, optional)

Return type:

matplotlib.axes.Axes

References

Gabriel, K. R. (1971). The biplot graphic display of matrices with application to principal component analysis. Biometrika, 58(3), 453-467.

discriminant_plot(model, X, y, *, feature_names=None, ax=None)[source]

Classification regions of a discriminant rule for two variables.

Shaded regions show the class the rule predicts at each point and gray lines the boundaries between classes. Crosses and colored ellipses are the robust center and 97.5% tolerance ellipse of each class, from the fitted covariance (one for all classes in linear discriminant analysis). Observations are drawn by their true class; those the rule assigns to another class are circled. The error in the subtitle is the share of the plotted observations that are misclassified: for the training data it is the apparent error, which is optimistic.

Parameters:
  • model (fitted RobustLDA or) – RobustQDA

  • X (array-like of shape (n_samples, 2))

  • y (array-like of shape (n_samples,)) – True class of each observation.

  • feature_names (sequence of two str, optional) – Axis labels; x_1 and x_2 by default.

  • ax (matplotlib Axes, optional)

Return type:

matplotlib.axes.Axes

cluster_plot(model, X, *, features=(0, 1), feature_names=None, ax=None)[source]

Clusters and trimmed observations of a robust clustering, as R’s plot.tclust.

Observations are drawn by cluster, the trimmed ones as orange diamonds. Crosses and ellipses are the center and 97.5% tolerance ellipse of each cluster’s normal model in the two plotted variables. With more variables, the ellipses are the marginal ones of those two.

Parameters:
  • model (fitted TClust)

  • X (array-like of shape (n_samples, n_features)) – The training data, or other data to assign with model.predict.

  • features (pair of int, default (0, 1)) – Columns of X on the axes.

  • feature_names (sequence of two str, optional) – Axis labels; x_1 and x_2 (the column numbers plus one) by default.

  • ax (matplotlib Axes, optional)

Return type:

matplotlib.axes.Axes

References

Fritz, H., García-Escudero, L. A. and Mayo-Iscar, A. (2012). tclust: An R package for a trimming approach to cluster analysis. Journal of Statistical Software, 47(12), 1-26.

hdr_plot(model, *, probs=(0.25, 0.5, 0.75), feature_names=None, ax=None)[source]

Highest density regions of a kernel density estimate (Hyndman 1996), as R’s plot.kde.

The region of probability prob is {x : f(x) >= level}, with the level from hdr_level(). For two variables the regions are filled, darker for the smaller ones, inside their boundary contours, over the training observations; for one variable they are shaded under the density curve, over a rug of the observations. Separate regions of the same probability reveal modes.

Parameters:
  • model (fitted KernelDensity with one or two variables)

  • probs (sequence of float in (0, 1), default (0.25, 0.5, 0.75))

  • feature_names (sequence of str, optional) – Axis labels; x_1 and x_2 by default.

  • ax (matplotlib Axes, optional)

Return type:

matplotlib.axes.Axes

References

Hyndman, R. J. (1996). Computing and graphing highest density regions. The American Statistician, 50(2), 120-126.

log_concave_plot(model, *, log=False, feature_name=None, ax=None)[source]

Log-concave maximum likelihood density over the histogram of the sample, as R’s plot.dlc.

The estimate is drawn with its knots, the observations where the slope of the log density changes. With log=True the log density, which is piecewise linear and concave, is drawn instead.

Parameters:
  • model (fitted LogConcaveDensity)

  • log (bool, default False)

  • feature_name (str, optional) – Label of the horizontal axis; x by default.

  • ax (matplotlib Axes, optional)

Return type:

matplotlib.axes.Axes

References

Dümbgen, L. and Rufibach, K. (2011). logcondens: Computations related to univariate log-concave density estimation. Journal of Statistical Software, 39(6), 1-28.

score_plot(model, X, y, *, ax=None)[source]

Antitonic score of a regression by score matching, as R’s plot.asm.

The estimated score psi (minus the derivative of the convex loss) against the residual, over a rug of the residuals, with the score of a normal distribution with the MAD of the residuals for comparison: the least squares fit uses that straight line, while a bounded score limits the influence of large residuals. The horizontal axis covers the central 98% of the residuals, widened by a quarter of that range on each side.

Parameters:
  • model (fitted ScoreMatchingRegressor)

  • X (array-like of shape (n_samples, n_features))

  • y (array-like of shape (n_samples,))

  • ax (matplotlib Axes, optional)

Return type:

matplotlib.axes.Axes

References

Feng, O. Y., Kao, Y.-C., Xu, M. and Samworth, R. J. (2024). Optimal convex M-estimation via score matching. arXiv:2403.16688.

coefficient_plot(models, *, labels=None, X=None, y=None, feature_names=None, intercept=False, level=0.95, ax=None)[source]

Coefficient estimates of several fitted models with their confidence intervals.

Each row is a coefficient and each model a marker with its interval estimate +- z * standard error, z the normal quantile of level. Passing X and y adds least squares, with its classical standard errors, as the reference. Differences between intervals are a diagnostic for sensitivity to the choice of estimator.

Parameters:
  • models (fitted model or sequence of fitted models) – Each with coef_, intercept_ and std_errors_, such as MMRegressor, RankRegressor, ScoreMatchingRegressor or the robust GLMs, all with the same predictors.

  • labels (sequence of str, optional) – Model names; the class names by default.

  • X (array-like, optional) – Data for the least squares reference.

  • y (array-like, optional) – Data for the least squares reference.

  • feature_names (sequence of str, optional) – Coefficient names; x0, x1, ... by default.

  • intercept (bool, default False) – Also show the intercepts.

  • level (float in (0, 1), default 0.95)

  • ax (matplotlib Axes, optional)

Return type:

matplotlib.axes.Axes

dd_plot(X1, X2, *, labels=('Sample 1', 'Sample 2'), random_state=None, ax=None)[source]

Depth-depth plot of two samples (Liu, Parelius & Singh 1999).

Each observation of both samples is placed at its halfspace depth with respect to the first sample and to the second. When both samples come from one distribution the points lie near the diagonal. A difference in location bends them into a lens away from the diagonal, a difference in spread puts them on one side of it, and skewness or shape differences give other departures. kde_test() gives the corresponding test.

Parameters:
  • X1 (array-like of shape (n1, n_features) and (n2, n_features)) – One-dimensional arrays are samples of one variable.

  • X2 (array-like of shape (n1, n_features) and (n2, n_features)) – One-dimensional arrays are samples of one variable.

  • labels (pair of str, default ("Sample 1", "Sample 2"))

  • random_state (int, numpy.random.Generator, numpy.random.RandomState or None) – Seed of the directions of the approximate depth for three or more variables.

  • ax (matplotlib Axes, optional)

Return type:

matplotlib.axes.Axes

References

Liu, R. Y., Parelius, J. M. and Singh, K. (1999). Multivariate analysis by data depth: descriptive statistics, graphics and inference. The Annals of Statistics, 27(3), 783-858.

ctl_curves(X, *, n_clusters=(1, 2, 3, 4), alphas=(0.0, 0.04, 0.08, 0.12, 0.16, 0.2), restr_factor=50.0, n_init=500, random_state=None, ax=None)[source]

Classification trimmed likelihood curves for choosing n_clusters and alpha.

Fits TClust for each number of clusters k and trimming level alpha and draws the trimmed classification log-likelihood against alpha, one curve per k, as R’s ctlcurves (García-Escudero, Gordaliza, Matrán & Mayo-Iscar 2011). The likelihood always grows with k; choose the smallest k whose curve is close to that of k + 1, and the smallest alpha from which they stay close: a larger gap means an extra cluster, possibly made of outliers that more trimming would remove.

Parameters:
  • X (array-like of shape (n_samples, n_features))

  • n_clusters (sequence of int, default (1, 2, 3, 4))

  • alphas (sequence of float in [0, 1), default six values from 0 to 0.2)

  • restr_factor (float, default 50.0) – Eigenvalue ratio of every fit, R’s default for these curves.

  • n_init (int, default 500) – Random starts of every fit; fewer are faster and less reliable.

  • random_state (int, numpy.random.Generator, numpy.random.RandomState or None)

  • ax (matplotlib Axes, optional)

Return type:

matplotlib.axes.Axes

References

García-Escudero, L. A., Gordaliza, A., Matrán, C. and Mayo-Iscar, A. (2011). Exploring the number of groups in robust model-based clustering. Statistics and Computing, 21(4), 585-599.