ROC Analysis Tool Based on DeLong's Method
O(N log N) fast implementation of DeLong's algorithm for comparing correlated ROC curves using mid-ranks, with MATLAB source code and GUI tool.
Background
Originating from radar and sonar detection problems in the early 1950s, receiver operating characteristic (ROC) analysis has become an indispensable tool for tackling two-sample problems across many scientific and engineering fields—such as evaluating diagnostic systems in clinical medicine, benchmarking classifiers in machine learning, and quantifying signal detectability in signal processing.
In essence, ROC analysis is a supervised framework that relies on sample labels and the underlying cumulative distribution functions (CDFs) of the two populations. In practice, the analysis begins by plotting a two-dimensional ROC curve, traced out by pairs of false-positive rates and true-positive rates across varying decision thresholds. From this curve, the area under the curve (AUC) can be computed—either analytically or empirically—as a summary metric that is often more informative than raw accuracy when evaluating classifier performance.
As shown by Bamber, the AUC can be estimated using the Mann–Whitney U statistic (MWUS). Building on this relationship, numerous nonparametric methods have been proposed in the literature. Among algorithms for comparing the areas under two or more correlated ROC curves, DeLong’s method is perhaps the most widely adopted due to its simplicity and practical utility. In our work, we reduced the time complexity of DeLong’s algorithm from quadratic O(N^2) down to linearithmic O(N log N) by exploiting an equivalence between the Heaviside function and sample mid-ranks.
In this post, I share the MATLAB source code for the two core algorithms from our paper, followed by a brief guide to the interactive ROC analysis GUI tool published on GitHub.
Source Codes
Algorithm 1: Procedure of Calculating Mid-ranks
As our method is based on the relationship between the Heaviside function and mid-ranks of samples, the first step is to calculate the mid-ranks. Presented below is a MATLAB function to achieve this. The input to this function, x, is a 1 x N vector, and the output, T, contains the corresponding mid-ranks.
function T = midrank(x)
%MIDRANK
[Z, J] = sort(x);
Z = [Z Z(end) + 1];
N = length(x);
T = zeros(1, N);
i = 1;
while i <= N
a = i;
j = a;
while Z(j) == Z(a)
j = j + 1;
end
b = j - 1;
for k = a : b
T(k) = (a + b) / 2;
end
i = b + 1;
end
T(J) = T;
end
Algorithm 2: Improved DeLong’s Algorithm
With the midrank function, we can now implement the fast version of DeLong’s algorithm, shown below. The input argument, samples, is a MATLAB struct consisting of two elements:
- spsizes is a
2 x 1vector, which represents the sizes of two samples, namely, X and Y. For ease of later reference, let m, n denote these two values. - ratings is a
K x Nmatrix, where each row represents the ratings of one experiment. Note that N must be equal to the sum of m and n, and its first m elements are the ratings corresponding to X, while the last n elements correspond to Y.
There are two outputs: aucs denotes the AUCs corresponding to each row of the ratings, while delongcov is the covariance matrix.
function [aucs, delongcov] = fastDeLong(samples)
%FASTDELONGCOV
%The fast version of DeLong's method for computing the covariance of
%unadjusted AUC.
if sum(samples.spsizes) ~= size(samples.ratings, 2) || numel(samples.spsizes) ~= 2
error('Argument mismatch error');
end
z = samples.ratings;
m = samples.spsizes(1);
n = samples.spsizes(2);
x = z(:, 1 : m);
y = z(:, m + 1 : end);
k = size(z, 1);
tx = zeros(k, m);
ty = zeros(k, n);
tz = zeros(k, m + n);
for r = 1 : k
tx(r, :) = midrank(x(r, :));
ty(r, :) = midrank(y(r, :));
tz(r, :) = midrank(z(r, :));
end
aucs = sum(tz(:, 1 : m), 2) / m / n - (m + 1) / 2 / n;
v01 = (tz(:, 1 : m) - tx(:, :)) / n;
v10 = 1 - (tz(:, m + 1 : end) - ty(:, :)) / m;
sx = cov(v01')';
sy = cov(v10')';
delongcov = sx / m + sy / n;
end
Using the ROC Analysis GUI Tool
I have also published a MATLAB GUI tool for ROC analysis on GitHub. You can download the source code here.
To analyze your own data:
- Save your experiment results as a
.matfile containingspsizesandratings(formatted as described in Algorithm 2) and place it in the same directory as the tool’s source code. - Run
DeLongUserInterfacein MATLAB—your file will appear in the Selected File dropdown menu. - Select your file and click Update Data to plot the ROC curves.
- Choose the two ratings you wish to compare under Rating 1 and Rating 2, then click Analysis to compute the statistical comparison results using our fast DeLong implementation.
Tip: If a button’s text turns red, it indicates that the displayed results are out of sync with the current dropdown selection. Simply click the button again to recompute; the text will return to black once the results are up to date.


Citation
I hope this tool proves helpful for researchers working with ROC/AUC analysis. If you use this code in your published work, please cite our paper (PDF):
X. Sun, W. Xu, Fast implementation of DeLong’s algorithm for comparing the areas under correlated receiver operating characteristic curves, IEEE Signal Processing Letters 21 (11) (2014) 1389-1393.
Here is a BibTeX citation as well:
@article{sun2014fast,
title={Fast Implementation of DeLong's Algorithm for Comparing the Areas Under Correlated Receiver Operating Characteristic Curves},
author={Xu Sun and Weichao Xu},
journal={IEEE Signal Processing Letters},
volume={21},
number={11},
pages={1389-1393},
year={2014},
publisher={IEEE}
}