Contents
(for )
Deep Breakdown
What it tells me (Situation Awareness): The definite integral computes the exact area enclosed between the curve and the x-axis from to , provided throughout. It is the limit of summing infinitely many thin rectangle strips of height and width as (Riemann sum). The result is a single signed number: positive when , negative when , and a mix otherwise. In DA/DS, this appears whenever you need total accumulation over a continuous range: the probability that a random variable falls between two values (area under its PDF), the AUC of an ROC curve, total revenue over a time window, or the expected value of a continuous variable. Mastering this formula is the bridge from derivative calculus to probability, statistics, and the mathematics of expectations.
Flowchart
Real-world use
Where it is used
Whenever a DA/DS problem requires the total amount accumulated over a continuous range. The most common cases are: computing probabilities from probability density functions (PDFs), calculating the Area Under the ROC Curve (AUC-ROC) for classifier evaluation, computing expected values (), and measuring cumulative KPIs (total sessions between two dates modeled as a continuous rate function).
Problem it solves
Given a rate, density, or height function , we need a single number summarizing the total accumulation from to . Discrete summation is limited by the number of data points; the definite integral gives the exact theoretical answer for any continuous function.
How it solves the problem
By the Fundamental Theorem of Calculus (FTC): find any antiderivative (a function whose derivative is ), then . This converts an infinite sum of infinitely thin strips into a two-step evaluation. The constant of integration cancels: .
Example in a business
A fraud detection model outputs predicted probabilities. The ROC curve plots True Positive Rate (TPR) vs. False Positive Rate (FPR) across all thresholds. The AUC-ROC is literally : the area under the ROC curve. A random model gives AUC = 0.5 (area under the diagonal); a perfect model gives AUC = 1.0. sklearn.metrics.roc_auc_score computes this via the trapezoidal rule, which is a numerical approximation of the definite integral.
The formula, piece by piece
Each symbol:
- : the resulting area, a real number (positive if throughout)
- : the integral sign, an elongated "S" for "sum"
- : lower limit of integration — where counting starts on the x-axis
- : upper limit of integration — where counting ends ()
- : the integrand, the height of the curve at position
- : the infinitesimal width of each strip; tells us is the variable of integration Assembled: "The sum of infinitely many strips of height and infinitesimal width , evaluated from to , equals ."
The formula in words
"Chop the interval into equal slices of width . At each slice, the rectangle has height and area . Add all rectangles: . Take the limit as (strips become infinitely thin). The result equals , where is any antiderivative of ."
Critical Concepts
- Fundamental Theorem of Calculus (FTC Part 2): , where . This is the engine behind every definite integral calculation.
- The cancels: In the definite integral, any antiderivative works because the constant drops out.
- Signed area: If on part of , the integral subtracts that area. For total unsigned area, use (see the "Area absolute" formula).
- Reversing limits flips sign: .
- Splitting: for any .
- Probability connection: If is a PDF of a continuous random variable, then .
- Numerical methods: When no closed-form antiderivative exists, use trapezoidal rule, Simpson's rule, or
scipy.integrate.quad.
Step-by-step calculation (tiny example)
Compute .
Step 1: Identify the integrand and bounds , , . Check: everywhere, so this is a true area.
Step 2: Find the antiderivative Power rule for integration: . (we can ignore here, it cancels).
Step 3: Apply FTC — evaluate at bounds and subtract
Step 4: Interpret The area of the region bounded by , the x-axis, , and is exactly square units.
Code examples
Beginner example (Python)
import numpy as np
from scipy import integrate
def f(x: float) -> float:
"""The curve whose area we want: x^2."""
return x**2 # Numerical integration via scipy (Gaussian quadrature under the hood)
area_numerical, error_estimate = integrate.quad(f, 1, 3) # Analytic: F(x) = x^3 / 3 -> F(3) - F(1) = 27/3 - 1/3 = 26/3
area_analytic = 3**3 / 3 - 1**3 / 3 # = 8.6667
print(f"Numerical area (scipy.quad): {area_numerical:.6f}") # 8.666667
print(f"Analytic area (FTC): {area_analytic:.6f}") # 8.666667
print(f"Error estimate from quad: {error_estimate:.2e}") # ~9.6e-14 # Riemann sum approximation (to see the strips idea)
N_STRIPS = 1_000
x_values = np.linspace(1, 3, N_STRIPS + 1)
delta_x = (3 - 1) / N_STRIPS
midpoints = (x_values[:-1] + x_values[1:]) / 2
riemann_sum = float(np.sum(f(midpoints) * delta_x))
print(f"Riemann sum (N={N_STRIPS}): {riemann_sum:.6f}") # ~8.6667Beginner example (R)
# Area under x^2 from 1 to 3
f <- function(x) x^2 # Numerical integration
result <- integrate(f, lower = 1, upper = 3)
cat("Numerical area:", result$value, "\n") # 8.666667
cat("Absolute error:", result$abs.error, "\n") # very small # Analytic: F(x) = x^3/3
ftc_result <- (3^3 / 3) - (1^3 / 3)
cat("Analytic (FTC):", ftc_result, "\n") # 8.666667 # Riemann sum illustration
n_strips <- 1000L
x_left <- seq(1, 3, length.out = n_strips + 1L)[-(n_strips + 1L)]
delta_x <- (3 - 1) / n_strips
midpoints <- x_left + delta_x / 2
riemann_sum <- sum(f(midpoints) * delta_x)
cat("Riemann sum (N=1000):", riemann_sum, "\n") # ~8.6667Professional example (Python)
"""
AUC-ROC as a definite integral. The ROC curve is TPR as a function of FPR across thresholds.
AUC = integral_0^1
TPR(FPR)
d(FPR).
sklearn uses the trapezoidal rule (a numerical definite integral).
We verify that the trapezoidal result matches scipy.integrate.quad
on the interpolated ROC curve.
"""
import numpy as np
from scipy import integrate
from scipy.interpolate import interp1d
from sklearn.datasets import make_classification
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import roc_auc_score, roc_curve
from sklearn.model_selection import train_test_split
RANDOM_SEED = 42 # Reproducible binary classification dataset
x_data, y_data = make_classification( n_samples=2_000, n_features=10, n_informative=6, random_state=RANDOM_SEED,
)
x_train, x_test, y_train, y_test = train_test_split( x_data, y_data, test_size=0.3, random_state=RANDOM_SEED ) # Fit model and get probability scores
model = LogisticRegression(random_state=RANDOM_SEED, max_iter=500)
model.fit(x_train, y_train)
y_scores = model.predict_proba(x_test)[:, 1] # sklearn AUC (trapezoidal rule on discrete FPR/TPR points)
auc_sklearn = roc_auc_score(y_test, y_scores) # Build continuous ROC curve and integrate with scipy.quad
fpr_vals, tpr_vals, _ = roc_curve(y_test, y_scores)
# Interpolate TPR as a function of FPR (piecewise linear)
tpr_of_fpr = interp1d(fpr_vals, tpr_vals, kind="linear", bounds_error=False, fill_value=(0.0, 1.0)) # Definite integral: A = int_0^1 TPR(fpr) d(fpr)
auc_quad, integration_error = integrate.quad(tpr_of_fpr, 0.0, 1.0)
print(f"AUC-ROC (sklearn trapz): {auc_sklearn:.6f}")
print(f"AUC-ROC (scipy.quad): {auc_quad:.6f}")
print(f"Difference: {abs(auc_sklearn - auc_quad):.2e}")
# Both should agree to ~4 decimal places (quad is more precise on the interpolant)
# Show what AUC means: probability that a random positive outscores a random negative
# This is the probabilistic interpretation of the area under the ROC curve
print(f"\nInterpretation: P(score_positive > score_negative) ≈ {auc_sklearn:.4f}")Source: domain knowledge