I would like to calculate the percentage of overlap between 2 ranges.
for example: I have two lists x and y x ['567.630', '592.927'] y ['593.000', '618.297'] In this case I would like an output of 0 as there is no overlap.
x ['793.843', '802.244'] y ['794.843', '803.244'] In this case I would like an output of 87%.
The total range of x and y are not always the same. It could be that x is 10s and y just 8s.
Could it be that there are some packages that perform these calculations?
Thank you in advance!
CodePudding user response:
This is a bit shorter solution, where you can choose from which list you want to compare the overlap. If you want the overlap of e.g. [0,1] and [0,2] to be 100% you can just do max(x,y) from the output. I changed y list to show this.
x = [793.843, 802.244]
y = [794.843, 900.244]
def overlap_percentage(xlist,ylist):
min1 = min(xlist)
max1 = max(xlist)
min2 = min(ylist)
max2 = max(ylist)
overlap = max(0, min(max1, max2) - max(min1, min2))
length = max1-min1 max2-min2
lengthx = max1-min1
lengthy = max2-min2
return 2*overlap/length , overlap/lengthx , overlap/lengthy
average, x,y = overlap_percentage(x,y)
print("average: ", average,"% x: ", x,"% y: ",y,"%")
output
average: 0.130 % x: 0.880% y: 0.070 %
CodePudding user response:
No needs for extra imports. I assumed the data to be list of float
numbers and not of string. The percentage is computed as overlapping over second interval, for the other case uncomment the line in the implementation.
def overlapping_percentage(interval1: list, interval2: list) -> int:
# min max of each intervals
m1, M1 = min(interval1), max(interval1)
m2, M2 = min(interval2), max(interval2)
# find overlapping boundaries
lb, ub = 0, 0 # lower and upper bound
if m1 <= m2 <= M1:
if M1 >= M2: # i2 is contained in i1
lb, ub = m2, M2
elif M1 < M2:
lb, ub = m2, M1
elif m2 <= m1 <= M2:
if M1 <= M2: # i1 is contained in i2
lb, ub = m1, M1
elif M1 > M2:
lb, ub = m1, M2
# percentage
percent = int((ub - lb) / (interval2[1] - interval2[0]) * 100)
# percent = int((ub - lb) / (interval1[1] - interval1[0]) * 100) # for the other percentage uncomment this line
print('overlapping interval', lb, ub)
print(percent, '%')
return percent
# intervals
#i1, i2 = [567.630, 592.927], [593.000, 618.297]
i1, i2 = [793.843, 802.244], [794.843, 803.244]
overlapping_percentage(i1, i2)
# overlapping interval 794.843 - 802.244
# 88 %
The overlapping part can be further compactified as follow
...
# find overlapping boundaries
lb, ub = 0, 0 # lower and upper bound
if m1 <= m2 <= M1:
lb, ub = m2, min(M1, M2)
elif m2 <= m1 <= M2:
lb, ub = m1, min(M1, M2)
...