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

I have a data.txt file 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 [...] 

Each pair of values ​​in the file corresponds to one coordinate point (x, y). When plotting, these points generate a curve. I would like to calculate the area under the curve (AUC) of this curve.

So, I am loading the data:

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

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

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

+6
source share
6 answers

The easiest way is to use the trapezoid rule function trapz .

If your data is known to be smooth, you can try using the Simpson rule, but there is nothing built into MATLAB to integrate numeric data using the Simpson rule. (& I'm not sure how to use it for x / y data, where x does not increase steadily)

+3
source

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

+4
source

You can do something like this:

 AUC = sum((Y(1:end-1)+Y(2:end))/2.*... (X(2:end)-X(1:end-1))); 
+1
source

Source: Link

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

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

If you have negative values ​​in y , you can do, for example,

 y=max(y,0) 
+1
source

[~, ~, ~, AUC] = perfcurve (tags, ratings, posclass);

% posclass may be 1

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

+1
source

There are several options for trapz for a person who is ready to do some encodings on their own. This link shows the implementation of the Simpson rule with python code enabled. There is also File Exchange in the simpsons rule.

0
source

Source: https://habr.com/ru/post/904624/


All Articles