Matlab, how to calculate AUC (Area Under Curve)?
Asked Answered
I

6

7

I have the file data.txt with two columns and N rows, something like this:

0.009943796 0.4667975
0.009795735 0.46777886
0.009623984 0.46897832
0.009564759 0.46941447
0.009546991 0.4703958
0.009428543 0.47224948
0.009375241 0.47475737
0.009298249 0.4767201
[...]

Every couple of values in the file correspond to one point coordinates (x,y). If plotted, this points generate a curve. I would like to calculate the area under curve (AUC) of this curve.

So I load the data:

data = load("data.txt");
X = data(:,1);
Y = data(:,2);

So, X contains all the x coordinates of the points, and Y all the y coordinates.

How could I calculate the area under curve (AUC) ?

Inexpensive answered 28/12, 2011 at 19:43 Comment(3)
It depends. Is the trapezoidal rule good enough for you?Attenborough
mathworks.com/matlabcentral/fileexchange/…Quentinquercetin
@Robert: that looks like it's the area under the curve of a function (Matlab has a whole bunch of quadxxxx() functions). OP is looking for numerical integration of data.Chappie
C
4

Easiest way is the trapezoidal rule function trapz.

If your data is known to be smooth, you could try using Simpson's rule, but there's nothing built-in to MATLAB for integrating numerical data via Simpson's rule. (& I'm not sure how to use it for x/y data where x doesn't increase steadily)

Chappie answered 28/12, 2011 at 19:49 Comment(3)
Thanx guys, I have tried with trapz(), but it strangely gives me always negative values. Why this? If it is an area, it should be always positive... Any idea? Thanx!Inexpensive
If the curve go below 0 the area actually will be decreased. This is just integral, remember. To get the positive AUC you might need to change the baseline. For example, subtract the min(Y) from Y. Or you can use abs(Y) to sum up positive and negative areas.Pigling
Technically, if you use trapz(x,y), the sign of the result depends on the sign of y and the sign of the change in x. (remember: this is integral of y dx) So if your y values are positive but x is decreasing, you'd get a negative number. It's actually a little more complicated than that: for closed curves, the sign should be positive for clockwise encircling and negative for counterclockwise encircling (see en.wikipedia.org/wiki/Green%27s_theorem#Area_Calculation ).Chappie
S
4

just add AUC = trapz(X,Y) to your program and you will get the area under the curve

Somewhere answered 1/3, 2012 at 18:46 Comment(0)
A
2

[~,~,~,AUC] = perfcurve(labels,scores,posclass);

% posclass might be 1

http://www.mathworks.com/matlabcentral/newsreader/view_thread/252131

Annalee answered 16/7, 2013 at 19:6 Comment(0)
B
1

You can do something like that:

AUC = sum((Y(1:end-1)+Y(2:end))/2.*...
  (X(2:end)-X(1:end-1)));
Biogenesis answered 2/1, 2012 at 13:27 Comment(1)
Oli, could you pls add an explanation or link for the equation you used?Besought
G
1

Source: Link

An example in MATLAB to help you get your answer ...

x=[3 10 15 20 25 30];
y=[27 14.5 9.4 6.7 5.3 4.5];
trapz(x,y)

In case you have negative values in y, you can do like,

y=max(y,0)
Granville answered 2/5, 2013 at 11:33 Comment(0)
M
0

There are some options to trapz for the person ready to do some coding by themselves. This link shows the implementation of Simpson's rule, with python code included. There is also a File Exchange on simpsons rule.

Midtown answered 19/6, 2014 at 5:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.