Areas Between Curves
The first application of the definite integral generalises "area under a curve" to the area between two curves. The recipe is short: subtract, find where the curves cross, and integrate the absolute difference, sometimes with respect to instead of . The same calculation measures income inequality (the Gini coefficient) and classifier quality (the area under a ROC curve). For the basics see Area.
The formula
If and are continuous and on , the area of the region between them is
Why. Slice the region into vertical strips of width . A strip at is approximately a rectangle of height . Adding the strips gives a Riemann sum for , and its limit is the integral. The position of the axis does not matter: if both curves are shifted up by , the difference is unchanged. That is why no case analysis about signs of and themselves is needed.
If the curves cross, the "top" curve changes. In general
and to evaluate it by hand, split at the crossing points.
Example: a parabola and a line
Region bounded by and . They meet where , i.e. , so at and . In between the parabola is on top (test : ).
import sympy as sp
x = sp.symbols('x', real=True)
top, bottom = 4 - x**2, x + 2
a, b = sorted(sp.solve(sp.Eq(top, bottom), x))
print("intersections:", a, b)
print("area:", sp.integrate(top - bottom, (x, a, b)))intersections: -2 1
area: 9/2
Curves that cross several times
and on cross at and . Splitting there:
Numerically you can integrate directly. The kinks at the crossings are where adaptive quadrature works hardest, so quad accepts a points list of trouble spots; for this smooth case both calls agree to all printed digits, but with sharper kinks (or a fixed-step rule) the hint matters:
import numpy as np
from scipy.integrate import quad
h = lambda x: abs(np.sin(x) - np.cos(x))
plain, _ = quad(h, 0, 2 * np.pi)
split, _ = quad(h, 0, 2 * np.pi, points=[np.pi / 4, 5 * np.pi / 4])
print(f"quad |sin - cos| = {plain:.12f}")
print(f"quad with crossing points = {split:.12f}")
print(f"exact 4√2 = {4 * np.sqrt(2):.12f}")quad |sin - cos| = 5.656854249492
quad with crossing points = 5.656854249492
exact 4√2 = 5.656854249492
Integrating with respect to y
Some regions are bounded by curves of the form . Then slice horizontally: .
Region between and . They meet where , i.e. and ; the line is to the right in between.
Slicing vertically would need two integrals, because the lower boundary switches from the left half of the parabola to the line at . Choosing the right direction saves work.
Application: the Gini coefficient
The Lorenz curve of an income distribution is the share of total income earned by the poorest fraction of people. Perfect equality is the line . The Gini coefficient is
from 0 (equal) to 1 (one person has everything). With data, the Lorenz curve is piecewise linear and the integral is a trapezoid sum:
import numpy as np
incomes = np.sort(np.array([12, 18, 22, 25, 30, 35, 41, 55, 80, 182], float)) # thousands
p = np.linspace(0, 1, len(incomes) + 1)
L = np.concatenate(([0], np.cumsum(incomes) / incomes.sum()))
area_under_L = float(np.sum((L[:-1] + L[1:]) / 2 * np.diff(p))) # trapezoid rule
gini = 2 * (0.5 - area_under_L)
print("Lorenz curve:", np.round(L, 3))
print(f"Gini coefficient = {gini:.4f}")Lorenz curve: [0. 0.024 0.06 0.104 0.154 0.214 0.284 0.366 0.476 0.636 1. ]
Gini coefficient = 0.4364
In software
- ROC AUC. A classifier's ROC curve plots true-positive rate against false-positive rate; the area under it (AUC) is the probability that a random positive scores above a random negative.
sklearn.metrics.aucis a trapezoid rule. The area between the ROC curve and the diagonal measures skill above chance, and is the Gini coefficient of the classifier. - Diffing curves. The integral of (the distance) compares a model's prediction with measurements, two audio envelopes, or two CDFs (the 1-D Wasserstein distance used in ML is exactly ).
- Polygon area. The shoelace formula for polygon area is the "area between curves" idea applied to straight edges: signed trapezoids under each edge, added up.
Exercises
Exercise 1: Two parabolas
Find the area between and .
Answer
.
Solution
Exercise 2: Crossing curves
Find the total area between and on .
Answer
.
Solution
They cross at . On , and . By odd symmetry the area on is also . Total . (The signed integral is 0.)
import sympy as sp
x = sp.symbols('x', real=True)
print(sp.integrate(sp.Abs(x - x**3), (x, -1, 1)))1/2
Exercise 3: Slice horizontally
Find the area bounded by and the -axis.
Answer
.
Solution
The curve meets at : .
Exercise 4: Gini of a formula
The Lorenz curve of a model economy is . Compute its Gini coefficient.
Answer
.
Solution
.
Test yourself
Two curves cross inside . What does compute?
- The total area between them
- The net (signed) area, with parts where subtracted
- Zero
- The area under
When is integrating with respect to the better choice?
- Never
- Only for circles
- When the curves are polynomials
- When the region's left and right boundaries are single curves , but the top or bottom boundary changes
What does a Gini coefficient of 0 mean?
- One person has all income
- No data
- Perfect equality: the Lorenz curve is the line
- Half the population has no income