Integrals
On this page
Where derivatives measure rates of change, integrals measure accumulation: total distance from a speed, total CPU time from a utilization graph, total probability from a density. Differentiation and integration were developed as separate ideas, but one of the most important results in mathematics, the Fundamental Theorem of Calculus, shows that they are inverse operations. Read "Derivatives & Gradient Descent" first, because everything here leans on the derivative rules.
Antiderivatives
An antiderivative reverses differentiation: is an antiderivative of when . Since , an antiderivative of is . This is written with the integral sign :
The says which variable you integrate with respect to. This form, without limits, is called an indefinite integral.
The Constant Of Integration
Why the ? The derivatives of , and are all , because constants disappear when you differentiate. Going backwards, you cannot know which constant was there, so you add an unknown constant of integration to represent all of them at once. Forgetting is technically wrong for indefinite integrals.
The Power Rule For Integration
Reverse the power rule for derivatives: instead of "multiply by the exponent, then subtract one", do "add one to the exponent, then divide by the new exponent".
The exception exists because it would mean dividing by zero ( is , outside this course). You can always check an antiderivative by differentiating it back.
| Integral | Result | Check by differentiating |
|---|---|---|
Definite Integrals
A definite integral has limits: it computes the accumulated quantity between and , which is the area under the graph of between those two points (counting area below the x-axis as negative).
Here is the lower limit and the upper limit. A definite integral gives a number, not a function, so there is no .
Evaluating With Brackets
To evaluate, find an antiderivative , then compute . The bracket notation is shorthand for exactly that subtraction.
That matches geometry: the region under from to is a triangle with base and height , area .
With limits and : . A quick sanity check for this one: the region is a trapezoid, and if you level the slanted top around its midpoint you get a rectangle of width and height , area .
The Fundamental Theorem Of Calculus
The Fundamental Theorem of Calculus (FTC) states that differentiation and integration are inverse operations. Concretely:
This is what makes the bracket method work. Without it, computing an area under a curved graph would require adding up infinitely many slices. With it, the hard problem "find an area" turns into the much easier problem "find an antiderivative".
Why It Makes Sense
Think of as the total area accumulated from some starting point up to . If you move a tiny bit further, the area grows by a thin strip of height . So the rate at which the area grows is , meaning . The area function is an antiderivative, and the area between and is "total up to minus total up to ". It is the same logic as an odometer: the distance driven between two moments is the reading at the end minus the reading at the start.
Examples
Verifying the FTC for on : satisfies , and . Going the other way, and : you get back exactly where you started.
Riemann Sums
Underneath every integral is a Riemann sum: an approximation of the area by a finite number of rectangles. Computers calculate integrals numerically this way all the time, especially when no antiderivative formula exists.
Lower And Upper Sums
Split the interval into equal strips and draw a rectangle on each strip. For an increasing function like on :
- The lower sum uses the function value at the left end of each strip, so every rectangle sits under the curve and the total underestimates the area.
- The upper sum uses the value at the right end, so every rectangle pokes above the curve and the total overestimates the area.
| Approximation | Area |
|---|---|
| Lower sum, 4 rectangles | |
| Upper sum, 4 rectangles | |
| Lower sum, 8 rectangles | |
| Upper sum, 8 rectangles | |
| Exact integral |
As the rectangles get narrower, the lower and upper sums squeeze together. In the limit where the width goes to zero, both converge to the exact integral. That limit is the definition of the (Riemann) integral.
Riemann Sums In Code
A Riemann sum is a loop, which makes it the natural way for a program to integrate. More rectangles means a better answer.
def riemann(f, a, b, n, side="left"):
width = (b - a) / n
offset = 0 if side == "left" else width
return sum(f(a + i * width + offset) * width for i in range(n))
f = lambda x: x
for n in (4, 8, 100, 10_000):
print(n, riemann(f, 0, 4, n, "left"), riemann(f, 0, 4, n, "right"))
# both columns approach the exact value 8Applications In Computer Science
Integrals are less visible than derivatives in day to day programming, but they sit underneath many standard tools.
Accumulated Resource Usage
If a server uses CPU at rate , the total CPU consumed between times and is . In practice you only have samples, so you use a Riemann sum: each sample times the length of the interval it represents.
Samples of , , and every 15 minutes ( s) give
and the average utilisation over the hour is . This is the "area divided by width" definition of an average value. Cloud providers bill exactly this way: at €0.04 per CPU-hour, samples of , , , give CPU-seconds CPU-hours, costing .
A related trap: a high Linux load average does not mean high CPU usage. Load average counts processes that are running or waiting, so processes blocked on disk I/O, network calls, file locks or database access raise the load while the CPU sits mostly idle.
Probability And Expected Value
For a continuous random variable with probability density function , probabilities are areas under the density:
The expected value is the long-run average. Signal processing uses integrals the same way to compute the total energy and the average value of a signal, and many machine learning models are defined in terms of expectations even though they are trained with derivatives.
AUC In Machine Learning
A classifier (spam or not spam, sick or healthy, click or no click) usually outputs a score between and , and you choose a threshold above which you predict "positive". Every threshold gives a different trade-off, and the ROC curve (Receiver Operating Characteristic) shows all of them at once.
The ROC Curve
The ROC curve plots, for every threshold:
- the True Positive Rate (TPR, the share of real positives you catch) on the vertical axis,
- the False Positive Rate (FPR, the share of real negatives you wrongly flag) on the horizontal axis.
A perfect classifier reaches the corner : all positives caught, no false alarms. A classifier that guesses randomly follows the diagonal .
Area Under The Curve
The AUC is the area under the ROC curve, a single number that summarises the whole curve:
| AUC | Interpretation |
|---|---|
| Perfect classifier | |
| to | Excellent |
| to | Good |
| to | Fair |
| Random guessing |
Computing An AUC
For the ROC curve , expand the integrand first:
So this classifier is "fair". Keep in mind that the ROC curve compares classifiers across all thresholds, but it does not tell you which threshold to use. That depends on the application: a cancer screening test and a spam filter weigh false negatives and false positives very differently.
Worked Exercises
These mirror the integral exercises of the course book.
Definite Integrals
. . .
Requests Processed
A server handles requests per hour ( in hours). In the first two hours it handles requests.
AUC Of Simple Curves
(with the FPR): . That is below , so this classifier does worse than random guessing; flipping its predictions would give AUC .
: .
Quick Recap
- An antiderivative satisfies . Indefinite integrals need because constants vanish when differentiating.
- Power rule: for . Check by differentiating back.
- A definite integral is the (signed) area under the curve, a number with no .
- FTC: . Integration and differentiation are inverses.
- Riemann sums approximate the area with rectangles; lower sums underestimate, upper sums overestimate, and both converge to the integral as the width goes to .
- CS uses: total resource usage from samples, average = area / width, probabilities and expected values for continuous variables.
- AUC = area under the ROC curve (TPR vs FPR). is perfect, is random.