I want to find the area of overlap between two distributions in MATLAB. I found a solution in Python (Calculate overlap area of two functions) but am unfortunately stuck using MATLAB for this problem.
Here is an example of two simple normal distributions (y1, y2). I'd like to calculate the area of overlap between these distributions.
x = linspace(-15, 15);
y1 = normpdf(x, -5, 2.7)*5000;
y2 = normpdf(x, +5, 2.9)*5500;
% PLOT
plot(x, y1)
hold on
plot(x, y2)
As you might see and as illustrated in this answer, the area that you want to calculate is the integral of min(y1,y2)
so first create the vector y
which is a 1x100 double
vector just like y1
and y2
.
y=min(y1,y2)
then based on the Riemann sum concept, you should calculate the length of the interval under each of the elements of vector y
. since the linspace
function in the first line of your code linspace(-15, 15)
has divided an interval with a length of 30
into 100
parts then the interval would be:
IntevalLength = (15-(-15))/size(y,2)
so the area of each of the rectangles in Riemann integral would be calculated as:
AreaOfRectangles = IntevalLength * y;
and the sum of AreaOfRectangles
will give you the area of the green part in the photo.
>> sum(AreaOfRectangles)
ans =
385.6763