Math · Note 6
Reading a real formula
Taking the revenue formula apart mark by mark, the three checks worth running on any formula of that shape, a set of mixed exercises with no new notation, and a cheat sheet of everything in the series.
This is the formula the series opened with:
Take it apart, left to right. is a function: give it a customer, get a number. The is is defined as. The first sigma is the outer loop, running once for each order of that customer. The second is the inner loop, running once for each line of that order. And is the body — quantity times price, for that line.
Said out loud: R of c equals the sum, over every order o in the set of orders belonging to customer c, of the sum, over every line in the set of lines belonging to order o, of quantity times price.
In plain words: a customer's revenue is the total of every line on every order they placed.
The three checks
Run these on any formula of this shape, automatically, before you trust it.
What happens when a set is empty? gives , because the empty sum is zero. Correct — a customer with no orders has no revenue.
Does the inner set depend on the outer index? does, so the sums cannot be separated into a product of two sums. If you catch yourself simplifying that way, stop.
Can I evaluate it two ways? Summing over all customers should equal the total over Line directly. It does — both ways. Two routes to the same number is the cheapest correctness check available, and it is the one people skip.
That is the skill this series was for. Not manipulating symbols — reading them slowly enough to see what loop they describe, and then checking the edges.
The cheat sheet
Sets. is a set, unordered and without duplicates. is membership. is a filter, which is `WHERE`. is a map, which is `SELECT DISTINCT`. is the size, which is `COUNT(*)`. is the empty set. says every element of is in .
Tuples. is ordered and may repeat. is every pair, which is `CROSS JOIN`. A table is a subset of a product, which is what relation means.
Functions. takes an and returns a . is the same as .
Sums and products. sums over a range, over a set, and a condition underneath filters it. , because counting is summing ones. The empty sum is and the empty product is . has identical grammar and multiplies.
Logic. is for all, is there exists, is implies, is maps to, and is a join.
The four things to actually remember
A table is a set of tuples — that is what relational means. Sigma is a loop, and the part underneath says what to loop over. Summing 1 counts, the empty sum is 0, and the empty product is 1. And if the outer index never appears in the body, something is wrong.
Where to go next, if you want to
Not needed for modeling, but the natural next steps are relational algebra's own symbols — for select, for project, for rename — which are just names for the filtering, grouping and joining already covered here; and functional dependencies, written , which is what normal forms are in this notation. Both are small additions to what is in this series, not new subjects.
Exercises
Write and evaluate: the number of orders placed by customers in Leeds.
Answer
.
Write and evaluate the average number of lines per order. Average is a sum divided by a count; write both parts.
Answer
. The denominator is a count, which could also be written .
Write and evaluate the total revenue from SKU A.
Answer
.
Write , the number of distinct SKUs bought by customers in city , and evaluate it for Leeds.
Answer
Define it in layers rather than as one expression: let be the set of lines whose order belongs to a customer in , then . For Leeds that is all four lines, so the SKUs are and . The lesson is the layering, not the answer: a formula needing three nested conditions should be given a name and split, exactly like a function.
Write the constraint "every line belongs to an order that exists".
Answer
.
Someone writes for total revenue. What is wrong with it, and what does it actually compute here?
Answer
The inner sum does not depend on at all, so it computes the total revenue once for every customer — . This is the classic fan-out bug, and in notation it is visible immediately: the outer index never appears in the body. Whenever that happens, either the formula is wrong or the outer sum is just multiplying by a count. Check which.
Questions
How do you read a nested summation formula out loud?
Left to right, naming each part: the function being defined, then the outer sum and what it ranges over, then the inner sum and what it ranges over, then the body. Reading it aloud in full is the fastest way to find out whether you have understood it.
What checks catch most errors in a summation formula?
Three. Ask what happens when a set is empty, since the empty sum is zero. Ask whether the inner bounds depend on the outer index, which decides whether the sums can be separated. And evaluate the result a second way to see if the two agree.
What is the fan-out bug?
Summing over two collections that are not actually related, so the inner total is counted once per outer row. In notation it is visible immediately, because the outer index never appears in the body of the inner sum.