apaContours(Xseries, Yseries, OutlierCutoff, nContours, BandwidthAdjust, IsEmpiricalOtherwiseGaussian, IsCopulaScale)
=apaContours(Xseries, Yseries, OutlierCutoff, nContours, BandwidthAdjust, IsEmpiricalOtherwiseGaussian, IsCopulaScale)Result
Calculates either the contour lines of the bivariate Gaussian kernel density of two series or the Gaussian ellipses implied by their sample means, variances and correlation, together with the observations outside the outermost contour, as inputs for an X-Y scatter chart with density contours, i.e. a scatter plot overlaid with seaborn.kdeplot contours.
The result is a matrix that can be charted directly as an X-Y scatter chart. The first row contains the series names: X, one column per contour line labeled “Contour m” with the probability mass m it encloses, from the outermost to the innermost contour, and Outliers. The first column contains the x values of all points; each further column contains the y values of its series in the rows of its points. Each contour line is followed by a row of #N/A; the outliers follow in consecutive rows. All remaining cells show #N/A.
Syntax
Xseries: n x 1 vector of observations shown on the horizontal axis
Yseries: n x 1 vector of observations shown on the vertical axis, with the same number of observations as Xseries
OutlierCutoff: optional; probability mass enclosed by the outermost contour; the observations outside it are returned as outliers, e.g. 0.98 for the outer 2% of the probability mass; must be > 0 and <= 1; default: 0.98
nContours: optional; number of contour lines; contour k = 1, …, nContours encloses the probability mass OutlierCutoff × k / nContours, so each band between two neighbouring contours holds OutlierCutoff / nContours of the mass; default: 10
BandwidthAdjust: optional; multiplier of the Scott’s rule bandwidth factor n^(−1/6), e.g. 2 for smoother contours; must be > 0; not used for Gaussian contours; default: 1
IsEmpiricalOtherwiseGaussian: optional; TRUE for the empirical kernel density contours, FALSE for the Gaussian ellipses implied by the sample means, variances and correlation (denominator n − 1); default: TRUE
IsCopulaScale: optional; TRUE to draw the contours on the copula scale: Xseries and Yseries are converted to pseudo-observations rank / (n + 1) in (0, 1), the contours are estimated on the corresponding normal scores and mapped back to the unit square; FALSE to use the observations as they are; default: FALSE
Algorithm
The density is estimated with a Gaussian kernel whose covariance matrix equals the sample covariance matrix of Xseries and Yseries (denominator n − 1) multiplied by (BandwidthAdjust × n^(−1/6))², the bivariate Scott’s rule. It is evaluated on a 200 x 200 grid that extends the data range by three kernel standard deviations on each side.
Contour k encloses the probability mass m = OutlierCutoff × k / nContours. In empirical mode, m is converted to the density value at which the grid points with higher density hold the share m of the total density mass. The contour lines at these density values are traced with the marching squares algorithm and linear interpolation along the grid edges. The results correspond to seaborn.kdeplot with gridsize 200, cut 3, levels = nContours + 1, thresh = 1 − OutlierCutoff and bw_adjust = BandwidthAdjust. A contour that would enclose the whole mass (m = 1) is not drawn.
If IsEmpiricalOtherwiseGaussian is FALSE, the contours are the ellipses of the bivariate normal distribution with the sample means and the sample covariance matrix (denominator n − 1). The ellipse for the mass m consists of the points with squared Mahalanobis distance −2 ln(1 − m) from the means, so that exactly the share m of the normal probability mass lies inside it. Each ellipse is drawn with 200 segments. Comparing both modes shows where the empirical distribution deviates from the normal one, e.g. through fat tails, skewness or asymmetric dependence.
Outliers are the observations outside the outermost contour under the selected model. In empirical mode, these are the observations whose kernel density, evaluated exactly at the observation, is below the density value of the outermost contour. In Gaussian mode, these are the observations whose squared Mahalanobis distance from the means exceeds −2 ln(1 − OutlierCutoff). Hence no outlier lies inside the outermost contour.
If IsCopulaScale is TRUE, each series is first replaced by its pseudo-observations u = rank / (n + 1), with tied values receiving their average rank, and then by the normal scores z = Φ⁻¹(u). The density, the contour levels, the contour lines and the outliers are computed on the normal scores exactly as described above, in empirical or Gaussian mode. Finally, every contour vertex is mapped back with the standard normal distribution function Φ, and the outliers are returned as pseudo-observations. Because this transformation is monotone, each contour still encloses the probability mass m, and the contours lie inside the unit square without the boundary leakage of a kernel density estimated directly on values in [0, 1] (probit transformation estimator). In Gaussian mode, the contours are those of the Gaussian copula with the correlation of the normal scores.
Remarks
To draw the chart, select the whole result including the first row and insert an X-Y scatter chart with straight lines and markers. Remove the markers of the contour series and the line of the Outliers series. In Hidden and Empty Cells, select Show #N/A as an empty cell and show empty cells as Gaps, so that the #N/A rows separate the contour lines. The observations themselves can be added as a further marker series.
Outlier labels such as dates can be computed in a column next to the result, e.g. by matching the x and y values of the Outliers rows with Xseries and Yseries, and attached to the Outliers series with Data Labels, Value From Cells.
The number of outliers is in general not (1 − OutlierCutoff) × n, because the share refers to the probability mass of the model, not to the observations. In empirical mode, the kernel smoothing spreads mass beyond the data, so usually fewer observations lie outside; in Gaussian mode, fat tails lead to more observations outside the outermost ellipse than the normal distribution implies.
Use IsCopulaScale = TRUE to analyse the dependence between two series independently of their marginal distributions, e.g. when Xseries and Yseries are empirical distribution function values or returns with different volatilities. The result is the same whether returns or their empirical distribution function values are passed, because only the ranks are used. To overlay the observations, plot them as rank / (n + 1). The contours enclose equal probability mass on the normal-score scale and are not level sets of the copula density; tail dependence shows as contours that reach further into the lower-left and upper-right corners than the Gaussian copula contours.
The function is non-volatile.
Pressing Esc aborts the calculation; the function then returns "Calculation aborted by user."
