File Exchange

image thumbnail

ROC curve

version (16.8 KB) by Giuseppe Cardillo
compute a ROC curve


Updated 27 Jun 2020

From GitHub

View Version History

View license on GitHub

ROC - Receiver Operating Characteristics.
The ROC graphs are a useful technique for organizing classifiers and visualizing their performance. ROC graphs are commonly used in medical decision making.
The input is a Nx2 matrix: in the first column you will put your test values (i.e. glucose blood level); in the second column you will put only 1 or 0 (i.e. 1 if the subject is diabetic; 0 if he/she is healthy).
Run rocdemo to see an example

The function computes and plots the classical ROC curve and curves for Sensitivity, Specificity and Efficiency (see the screenshot).

The function will show 6 cut-off points:
1) Max sensitivity
2) Max specificity
3) Cost effective (Sensitivity=Specificity)
4) Max Efficiency
5) Max PLR
6) Max NLR

ROC requires the Curve fitting toolbox.

Cite As

Giuseppe Cardillo (2021). ROC curve (, GitHub. Retrieved .

Comments and Ratings (94)

wei li

Siva kumar

sorry efficiency is accuracy. can this code extend to find F-score

Siva kumar

hello Giuseppe Cardillo,
Thank u for your code. I have one doubt . Here u r finding Se, Spe and efficiency while plotting the curve.
How u calculate Efficency. whether it is same as F-score. Kindly reply

Min Jung Kim

sorry, i was using different roc curve function while i opened this page. i wanted to remove the comment but i could not find a way. sorry for the confusion.

Min Jung Kim

hello Giuseppe Cardillo,

thank you so much for sharing this function. it is so useful to use. i have a this page, in the overview description, you stated as if input is two column matrix (i am now thinking it might be for the original version of this function) and the first column with value, and the second with its binary identifier (0 or 1). in the current version, now it takes class_1 and class_2. in the function description, you gave example with random number distributions. it implies both class_1 and class_2 could be values unlike previous function's input. if you use the overview example (glucose blood level and its identifier) to the current function (class_1 and class_2), how would you construct inputs? do you separate glucose blood level for diabetic (class_1) and glucose blood level for healthy (class_2). therefore, two values only? if you can clarify this, this would be greatly helpful to understand how to use the function, at least for me. thank you so much for your answer in advance!

Giuseppe Cardillo

On the contrary, the problem are that negative values.
Add a costant to all values so that all of them are positives


Dear Giuseppe,
thank you for the code, it is very helpful!

I get this error:
Exiting fzero: aborting search for an interval containing a sign change
because complex function value encountered during search.
(Function value at -1.96125 is 0.15726+2.4342e-11i.)
Check function or try again with a different starting value.
In an assignment A(:) = B, the number of elements in A and B must be the same.

Error in roc (line 264)
H(9)=plot([CNlr CNlr]+COEFF,[0 1],'marker','none','linestyle','--','color',c(4,:),'linewidth',2);

Can you explain me why? I have negative values in the first column of X, but I don't think it is a problem.

Thank you.

Giuseppe Cardillo

Yes... It'is a typo error. I have just uploaded the correction onto github

Dario Geisinger

Thanks for the great work!

Line 290 and 291:
fprintf('5) Max PLR Cut-off point= %0.4f\n',CPlr)
fprintf('6) Max PLR Cut-off point= %0.4f\n',CNlr)
*** SHOULD BE NLR ?? ****

Giuseppe Cardillo

As I wrote, you can use this function if and only if you have only one predictor. If you have many predictors you should use MatLab function "perfcurve"



Hi Giuseppe and thanks for your nice code and helping , I'm new in MATLAB and AI please be patient with me if my question is simple.

Your codes works great but I have a question and just want to know how can i make x matrix from my data set and labels.
In the help is " the first column you will put your test values " but i cant understand it.

My normalized data set (breast cancer) have 17 columns and one columns for labels as an example:

her2 ki67 ER PR ...... Lables
0.2258 0.5874 0.3258 0.3357 ...... 0
0.2258 0.5874 0.3258 0.3357 ...... 1


I know the second column for input in x matrix are labels but what about the first column?
How can I use my data to make first column?


Giuseppe Cardillo

use help roc

Natsu dragon

im new in matlab and working on change detection with kernels
this code is very important in my research
can anyone explain for me how to use it


Can we use this code for plotting ROC for a classification problem with more than 1000 features and binary decision as the output? Instead of blood glucose level, I have around 4096 features for each image and I want to classify it as 1 or 0.

Is it possible to plot ROC curve for this problem?

Ramesh Paudyal

Nice code

Alberta Ipser

Amazing code! thank you so much for making this!!

your code is very important...Thank you very much!

salih haji

Iam phD student in medical imaging, now iam working in thyroid nodlues , I got im my work confusion martrix, So I have ptr and Npr I mean sensitivity and selectivity. but I dont know how I can plot roc curve by matlab. if it possible to explain to my deataily with my appreciate.

Nurul Hidayah

Hi, may I know how to check the values of my xroc and yroc? For instance, the project that I am currently doing are regarding filtering based on different clustering methods(i.e. K-means, Hierarchical and Fuzzy C-means). However, from the results that I get, how do i get the value of xroc and yroc?

I hope to hear from you soon. Thank you so much! (:


Great code!


Excellent share.Thanks. Great work


Hello Giuseppe, i have performed binary classification of data using support vector machine. I have the results of classification in terms of probability and assigned label. How do i use this code to obtain ROC and EER? Thanks.

riad salehin

MEHRDAD moghbel

excellent code and the only one useful for roc from binary svm, however sometimes you get this error

Error using fit>iFit (line 367)
NaN computed by model function, fitting cannot continue.
Try using or tightening upper and lower bounds on

Error in fit (line 108)
[fitobj, goodness, output, convmsg] = iFit( xdatain,
ydatain, fittypeobj, ...

Error in roc (line 149)
cfit = fit(xroc,yroc,ft_,fo_);

Giuseppe Cardillo

Mark it is very simple: it is a 5 point logistic regression where A=0 and D=1

Mark Pether

Thanks for sharing this excellent piece of code Giuseppe, I have one question: how did you come by the formula 1-1/((1+(x/C)^B)^E for the curve fitting of the ROC?


I am wondering if the performance of the classifier is determined with permutation test.


I found a paper which used this code stated as: "To determine whether the discriminative performances could occur by chance, we employed a non-parametric permutation test. Briefly, an empirical distribution was obtained for the area under curve (AUC) derived from the ROC analysis and the determination coefficient (R2) derived from the logistic regression analysis, respectively, by randomly reallocating all of the patients into two groups (improvers and non-improvers) and re-computing the AUC and R2 based on the two randomized groups (10,000 permutations). The 95th percentile points of the empirical distributions were used as critical values to estimate statistics (P values), which indicate the deviation of the observed discriminative performances from those expected by chance."

Jose M.

Thanks Giuseppe, I was away for some time. I am afraid that it is probably the same error as Fulden. Jose Maria

Thao Tran

fulden cantas

Thanks a lot! It works well :)

Giuseppe Cardillo

because you have negative data in your ROC matrix

cut =


fulden cantas

Hi Giuseppe,
Thanks for the codes and it helped me so much but unfortunately I've got an error this time but don't know how to fix it. Could you help me as soon as possible? The codes are below which gave me error.. Thanks in advance.

cov=[sigma1^2 rho12*sigma1*sigma2 rho13*sigma1*sigma3; rho12*sigma1*sigma2 sigma2^2 rho23*sigma2*sigma3; rho13*sigma1*sigma3 rho23*sigma2*sigma3 sigma3^2];
m=mvnrnd([91.51 48.83 139.69],cov,n); %[mvnrnd[mean1 mean2 mean3],cov,n];
k2=find (mean(S)<S);
data=[m S];

dataa=[data(:,1) data(:,2) data(:,4)];

rocdata=[dataa(:,1) dataa(:,3)]; default.values={rocdata,0,0.05,1};
[rocdata threshold alpha verbose] = deal(default.values{:});

Error using fit>iFit (line 340)
Complex value computed by model function, fitting cannot continue.
Try using or tightening upper and lower bounds on coefficients.

Error in fit (line 108)
[fitobj, goodness, output, convmsg] = iFit( xdatain, ydatain, fittypeobj, ...

Error in roc (line 219)
fitSe = fit(table(:,1),table(:,2),ft_,fo_);

Giuseppe Cardillo

I should see the data to reply. Could you send me the dataset by email?

Jose M.

I have some issues with the code, I am not sure why. Do you have any suggestions?

Error using fit>iFit (line 367)
Complex value computed by model function, fitting
cannot continue.
Try using or tightening upper and lower bounds on
Error in fit (line 108)
[fitobj, goodness, output, convmsg] = iFit(
xdatain, ydatain, fittypeobj, ...
Error in rocc (line 281)
fitSe = fit(table(:,1),table(:,2),ft_,fo_);

Raid Omar

Dear Sir,

Thank you very much about this code. I have sent a message to you. Could you kindly answer me, please?


Jort Gemmeke

Nice. One issue with the verbose option: when set to 0, it doesnt fill the .co part of the output anymore. To fix, switch the lines

m=[table(CSe,1) table(CSp,1) CE table(CEff,1)];

near the end of the code (line 300 or so)

Jort Gemmeke

Giuseppe Cardillo

the Equal Error Rate (EER) is the point on the ROC curve that corresponds to have an equal probability of miss-classifying a positive or negative sample. This point is obtained by intersecting the ROC curve with a diagonal of the unit square.
Anyway, in the results, it should be the "cost-effective" cut-off point.


Hello Cardillo.. I was wondering how to obtain the value of EER from your code. Is the standard error equal to the EER?

Giuseppe Cardillo

Simply... I don't know

AMIT kamra

i have 2 excel is desired result..second is actual results.kindly tell where to add these files in code to get Az value

Giuseppe Cardillo

I have just uploaded a new roc version. You can set the verbose flag and so you will have not plots and results summary, but only the rocout structure


Really nice function. I am wondering how to avoid the plots. Is it possibile?


cool function!

Giuseppe Cardillo

to be honest, I haven't. I run on R2012a and it works. Maybe you haven't curvefit toolbox


Hi, when I test your function without argument, it keeps output error like below:
>> roc
Error using fittype/subsref (line 16)
Cannot access fields of fittype using . notation

Error in fit>params2var (line 65)
evalStr = sprintf('tmp = params.%s;',freeList{i});

Error in fit (line 34)
vars = params2var(params,freeList);

Error in roc (line 156)
cfit = fit(xroc,yroc,ft_,fo_);

I'm using Matlab R2012a. Could you give me some suggestion.

Giuseppe Cardillo

As I previously wrote, the main paper you have to read is Hanley JA, McNeil BJ. The Meaning and Use of the Area under a Receiver Operating Characteristic (ROC) Curve. Radiology. 1982 Apr;143(1):29-36.
Now I think it is quite impossible to find a paper describing each bayesian parameter, so you could email me in private and I could try to help you.

Adding a costant will not affect results.


Great code. Thanks. Some questions:
1) Is there any document explaning the output - what each result means and how is it calculated?
2) We found that a negative value in the data causing an error. Is that true? Will adding a constant to bring all data above zero will affect the results?

Giuseppe Cardillo

By definition, the efficiency is the fraction of subjects that are correctly classified.
TRACE(M) is the sum of the elements on the main diagonal; in our case it is the sum of true positives and negatives.
SUM(M(:)) is the sum of the elements of the matrix and so it is the number of studied subjects.
TRACE(M)/SUM(M(:)) is the efficiency.


In the 25 Sep 2012 version, can you describe/cite how 'trace(M)/sum(M(:))' , where M is a 2x2 of [TP FP;FN TN], results in an efficiency measure at each threshold?

Jorge Amaral

I run the same problem again on matlab 7.8 ( R2009a) and it was perfect. I was using matlab 7.4 before. I fixed the error mentioned by Segun Oshin and run some examples with matlab 7.4 and it was ok. However, when I ran the example above there was an error in 7.4 but not in Mtalab 7.8. Thanks

Giuseppe Cardillo

@Jorge Amaral
Thank you for your comment.
I used your data and this is the result:
Cut-off point Sensivity Specificity
0.9000 0.0000 1.0000
0.8000 0.1000 1.0000
0.7000 0.2000 1.0000
0.6000 0.2000 0.9000
0.5500 0.3000 0.9000
0.5400 0.4000 0.9000
0.5300 0.5000 0.9000
0.5200 0.5000 0.8000
0.5100 0.5000 0.7000
0.5050 0.6000 0.7000
0.4000 0.6000 0.6000
0.3900 0.7000 0.6000
0.3800 0.7000 0.5000
0.3700 0.8000 0.5000
0.3600 0.8000 0.4000
0.3500 0.8000 0.3000
0.3400 0.8000 0.2000
0.3300 0.9000 0.2000
0.3000 0.9000 0.1000
0.1000 1.0000 0.1000


AUC S.E. 95% C.I. Comment
0.68000 0.12186 0.44115 0.91885 Poor test
Standardized AUC 1-tail p-value
1.4771 0.069828 The area is not statistically greater than 0.5

so there is not error. If you want contact me by email and we'll try to better understand and to solve


gud stuff sir.i am doin my final year project.i tried ur codes.its workin gud 4 default values.but i dono 2 feed d input.. wat do u mean by data value?i hav used svm classifier. the output of svm is no of ones and no of shld i feed tis as input..i need tis immediately..can u plz help...

Jorge Amaral

Great work!
I have a question regarding the code. In line 222
(if p<=alpha) , I have partest in the same directory but when I run the code with the matrix:
fawcett_matrix =
0.7000 0
0.5300 0
0.5200 0
0.5050 0
0.3900 0
0.3700 0
0.3600 0
0.3500 0
0.3300 0
0.1000 0
0.9000 1.0000
0.8000 1.0000
0.6000 1.0000
0.5500 1.0000
0.5400 1.0000
0.5100 1.0000
0.4000 1.0000
0.3800 1.0000
0.3400 1.0000
0.3000 1.0000
ROCout=roc(fawcett_matrix,0,0.05,1) the following error occurs:
??? Undefined function or variable "co".

Error in ==> roc at 271;

I think it happens because p is greater than alpha in line 222 and there is no default value. Is that correct?


Giuseppe Cardillo

this is a problem caused by using a new syntax of matlab that is not supported by your version. Simply do this:
1) edit roc
2) change [~,J]=min(d); into [S,J]=min(d);
3) save and exit


Hi Mr.Cardillo, I'd like to run the sample command "roc" but the error appears:

>> roc
??? Error: File: roc.m Line: 225 Column: 11
Expression or statement is incorrect--possibly unbalanced (, {, or [.

Any idea to encounter this?, my matlab version 7.6.0

Thanks in advance.



I appreciate your help, it worked :)

Giuseppe Cardillo

this error shows that something doesnt work on rocdata and so you have not x in your workspace. Now I have changed and uploaded a new version of roc so, if you call roc without arguments, it will run the demo by itself. If you dont want to wait for the FEX updating simply change in the code the default.value in this way

default.values = {[165 1;140 1;154 1;139 1;134 1;154 1;120 1;133 1;150 1;...

146 1;140 1;114 1;128 1;131 1;116 1;128 1;122 1;129 1;145 1;117 1;140 1;...

149 1;116 1;147 1;125 1;149 1;129 1;157 1;144 1;123 1;107 1;129 1;152 1;...

164 1;134 1;120 1;148 1;151 1;149 1;138 1;159 1;169 1;137 1;151 1;141 1;...

145 1;135 1;135 1;153 1;125 1;159 1;148 1;142 1;130 1;111 1;140 1;136 1;...

142 1;139 1;137 1;187 1;154 1;151 1;149 1;148 1;157 1;159 1;143 1;124 1;...

141 1;114 1;136 1;110 1;129 1;145 1;132 1;125 1;149 1;146 1;138 1;151 1;...

147 1;154 1;147 1;158 1;156 1;156 1;128 1;151 1;138 1;193 1;131 1;127 1;...

129 1;120 1;159 1;147 1;159 1;156 1;143 1;149 1;160 1;126 1;136 1;150 1;...

136 1;151 1;140 1;145 1;140 1;134 1;140 1;138 1;144 1;140 1;140 1;159 0;...

136 0;149 0;156 0;191 0;169 0;194 0;182 0;163 0;152 0;145 0;176 0;122 0;...

141 0;172 0;162 0;165 0;184 0;239 0;178 0;178 0;164 0;185 0;154 0;164 0;...

140 0;207 0;214 0;165 0;183 0;218 0;142 0;161 0;168 0;181 0;162 0;166 0;...

150 0;205 0;163 0;166 0;176 0;],0,0.05,1};


This error happens,
load rocdata

??? Error using ==> cell
Size vector must be a row vector with real elements.

Error in ==> roc at 44

Giuseppe Cardillo

typing 'load rocdata' you already have your matrix x into the workspace. if you will type x you will see all the data of the matrix. now type roc(x)


Data is on rocdata.mat, true?
so I write load rocdata.mat but I should put it into x, how can I do that?

Giuseppe Cardillo

maybe giving us some informations we could try to help you...


Hey guys, I couldn't run the program, any help plz?

Ali Ali

Thanks a lot

Giuseppe Cardillo

Dear Benjamin, I think that Pythagora don't care if you acknowledge him or not :-).
For the standard error I used an equation described in: Hanley JA, McNeil BJ. Radiology 1982 143 29-36. The meaning and use of the area under the Receiver Operating Characteristic (ROC) curve.

Please cite me only if you use all my function: if you took pieces of code, you can decide to cite me or not.

Lastly, I prefer to use quantile and not a fixed size step because real data usually are not equally spaced.


I was also going to suggest adding a varagin to delineate a step-size, ex:
%add a varagin, in this case I am calling it step which can describe the distance between thresholds to be calculated
if(nargin<2 )
%find unique values in z
elseif length(step)==1 % the fixed step size is being requested
% later in guiseppe code just do labels=step

Also, Guiseppe, I implemented your standard error and pythagoras into my code which generated data that will probably used in an upcoming paper. Do you mind being acknowledged or are there any actual articles to cite? Your call. And lastly, I have a GUI that is pretty beta, but works.


Thanks for the update.
P.S. I work with images of 2000x2000 pixels, so... ;)

Giuseppe Cardillo

Dear Jay, thank you for your comment. I don't agree so much with you. If you look at the code:
1) all vectors are preallocated;
2) True and false positive and negative are computed using logical indexing.
So the computations are very fast.
Anyway, I introduced your suggest and now you can choose if you want to use all or 3<=N<all unique values as thresholds. I have just uploaded the file.


hey there, nice program!

However, you use each element of the data as a threshold and you calculate the fpr tpr etc. This means a very very long time and many many points on the curve for a large vector (which makes your program useless). To avoid this, I suggest you let the user to choose the number of thresholds.


Thanks, that fixed it and I now understand the difference with SE.

Giuseppe Cardillo

Perhaps you are right: I uploaded the file at July to fix the bug by Segun Oshin; it is clear that somewhat in the upload went wrong. I have just reupload the file.
If you are using my roc dataset, you will see that 0's and 1's are not in the same proportion. If you invert 0's and 1's the curve is slight different and so the SE is quite different.


Thanks for answering all of my questions, I really do appreciate it. I still have an issue with your answer for number 3.

First, I was using an inverted data set when I stated the answer should be 151 not 150 (previous post). Second, using the download available on this page right now, running roc(x) gives a cutoff of 153. As you state, the correct answer is in fact 152. Therefore, I am not sure if you changed something and didn't update, since the cutoff value is still using the row of the minimum distance from xroc,yroc and grabbing that rows value from 'labels', hence the wrong answer of 153. (lines 184-186)

To confirm this, run the download from here and see what cutoff you get. Maybe it is something on my end?

As for the standard error calculation (#4), I was playing around and found that if I inverted the 1's and 0's before running, I would get a different Serror for the AUC, which I assumed should be the same regardless of whether they were inverted. The Serror of the sample data is 0.02713, and if I invert the observations, it becomes 0.0364. This is probably trivial.

Thanks again for your excellent responses.

Giuseppe Cardillo

I'll try to answer the questions by Benjamin.
1) The SE of the area is calculated using this equation from Hanley JA, McNeil BJ. Radiology 1982 143 29-36.
2) I haven't project to implement a GUI. Anyway this function is under GPL license, so you can modify and redistribute it without any problems but correct citations.
3) I took in account that there are 2 more points in xroc and yroc arrays than labels array. If you look deeper in the code (line 138):
table=[labels'; yroc(2:end-1)'; 1-xroc(2:end-1)';]';
As you can see, the displayed xroc and yroc points go from 2 to end-1 (and so the points 0,0 and 1,1 are excluded). Anyway, using the demo dataset the cut-off point is 152 (that is the closest to green line)...
4) The standard error of the area is a function of the area and points used to draw the ROC curve: if you have two ROC curves, the first with 10 points and the second with 100 points the first will have a greater SE than the second. hbar and ubar are used to correctly compute the false and true positives and negatives. Their values don't influence the SE computation.


Also, when hbar>ubar, I think values in standard error calculations should be changed. Otherwise, you can get to different standard error values from the same area under the curve depending on whether healthy average is higher than disease average.

Sorry to keep bugging you here, but this is the best way I can see to make suggestions. As you can tell, I have been digging into this lately.


I think there may be an issue within the code, but I could be wrong. When you create xroc and yroc using
xroc=flipud([1; 1-a(:,2); 0]) , the additional two rows are not also added to labels. For instance, in your example data, this yields 72 paired points for the ROC curve (# rows in xroc or yroc) but only 70 thresholds (# rows in labels). This causes issues when reporting the threshold value since this is determined by a row reference back to labels (in the example, the threshold by your math should be 151 not 150 (using 'labels' and 'a').

If this doesn't make sense, or I am wrong, please let me know. Its really not a big deal with large datasets with many points on the curve, but becomes an issue with smaller sets where points are farther apart.



First off, great code, really. I was wondering if you used a specific citable method to calculate the standard error for the AUC, which is then used for the CI?

Second, and more trivial, have you thought about implementing this as a GUI or stand alone? My next side project is to make one for my boss to use (without Matlab).

Thanks again for the great code.

Segun Oshin

Hi, the code is very good. However, I encounter an error where the cut-off point is set, on line 186,

??? Attempted to access labels(7397); index out of bounds because numel(labels)=7396.

Error in ==> roc at 186
co=labels(J); %Set the cut-off point

Is there a way to fix this?
Kind regards!

Segun Oshin


Giuseppe, I find your code useful. I was keen to calculate Equal Error Rate based on your code. Do you have any suggestions as to how I can do this easily. This will be appreciated.


Jens Kaftan

Hi Giuseppe.
I have had a look at the new release today and I think it is still not perfectly correct. I have validated the scripts using the example data of Hanley and McNeil's 1982 paper: "The Meaning and Use of the Area under a Receiver Operating Characteristic (ROC) Curve", which seems to be the basis for the calculations (such as the approximation of Q_1 and Q_2) anyways. To my opinion the problem is that when integrating over the ROC curve to compute the AUC, the data point (sensitivity=1, specificity=0) is not considered when using the trapezoidal rule. Consequently the AUC value (and all AUC dependent measures) differ slightly from the example in the mentioned article (which becomes more severe for non-continous tests with only a few cut-off points).



I tested the new release and it is agreeing with other codes now. Michael, you may also wish to verify that the new version is working correctly.

I also think adding the cut off points as an additional x or y axis would be very useful to understand the trade off between sensitivity and sensibility.


Agree with cabrego, this algorithm does not work correctly. Depending on the input data, it generates ROC curves with specificity and sensitivity backward. I believe this is because elements that fall below a cutoff value (I in the code) are called "true positives" when they should be "false positives". The convention is that higher values of a test are abnormal (positive).

I confirmed that other software (online ROC calculator, ROCR in R, STATA) does not behave this way with the same input data and all others produce correct results.

Use at your own risk.


Nice function, but I think it may have a bug, I am getting different results for significantly overlaping distributions when I compare to medcalc, and online calculators.

email me for more details:

Phong Vo

Thank you very much!


Günther Eibl

easy to use!

Diego García Bascuñán

Good function

Truc Phan


Good stuff.

MATLAB Release Compatibility
Created with R2014b
Compatible with any release
Platform Compatibility
Windows macOS Linux

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!