How to calculate overlapping area of two cdf's?

7 views (last 30 days)
Using my data (from vectors ZP and ZM), I used ksdensity to create a kernel pdf and from this I created the CDF's from each of ZP and ZM, which were plotted on the same figure:
%%%%%%%% plots kernel pdf %%%%%%%%%
figure(200); [f1,x1]=ksdensity(ZP); plot(x1,f1); hold on; [f2,x2]=ksdensity(ZM); plot(x2,f2);
%%%%%% plots cdf %%%%%%%%
figure(300); ksdensity(ZP,'support','positive','function','cdf',... 'npoints',1000); hold on; ksdensity(ZM,'support','positive','function','cdf',... 'npoints',1000);
I now have two cdf's (see image) and if I fill the area below each, there will be a small overlapping region. I would like to calculate the area of that overlapping region and would very much appreciate any assistance with this.

Answers (1)

Brendan Hamm
Brendan Hamm on 6 Jul 2015
You could approach this with a Riemann sum or trapezoid rule, but to do this you would want to analyze the density at the same points:
pts = linspace(0,2.5e-3,100); % Based on your plot I choose these points
f1 = ksdensity(ZP,pts,'function','cdf')
f2 = ksdensity(ZM,pts,'function','cdf')
% Cumulative area between curves using trapezoidal rule (assuming f1 > f2 everywhere):
d = cumtrapz(pts,f1-f2);
totalArea = d(end)

Community Treasure Hunt

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

Start Hunting!