AUC between different size curves
3 views (last 30 days)
Julius Muschaweck on 7 Sep 2021
I would use interp1 to interpolate both functions so they have the same high resolution x support. You can easily replace the 0.01 step by a 1e-6 step size in the code below.
Look for interp1 options, to control the interpolation algorithm (you might prefer 'linear' to avoid overshoots), and to control extrapolation (in your probability distribution case, you might want to set the value for extrapolation to 1 if one of your distributions has shorter x range).
Then use trapz to compute the signed integral of the difference or unsigned area between the curves. See code.
x1 = 0:0.2:1.4;
y1 = x1.^2;
x2 = 0:0.14:1.4;
y2 = sqrt(x2);
xq = 0:0.01:1.4;
y1q = interp1(x1,y1,xq);
y2q = interp1(x2,y2,xq);
my_signed_integral = trapz(xq,y2q-y1q)
my_unsigned_area = trapz(xq,abs(y2q-y1q))
test_signed = trapz(xq,sqrt(xq) - xq.^2)
test_unsigned= trapz(xq,abs(sqrt(xq) - xq.^2))