Level 0: Foundation
★☆☆☆☆ Introductory⏱ 5 minGeneral audience. No mathematics required.Sets are boxes of things
Imagine a toy box. Inside are a ball, a teddy bear and a car. In maths, a box of things like this is called a set.
Two simple rules make a set a set:
- No copies. A box with two identical balls counts the ball only once.
- Order doesn’t matter. Ball, bear, car is the same box as car, ball, bear.
You can ask one question about any set: “Is this thing inside?” Is the bear in the box? Yes. Is a banana in the box? No.
Sets can overlap. Some things fly, and some things are alive. A bird is in both groups. A plane only flies. A dog is only alive. A rock is in neither. Try it:
Functions are machines
A function is like a machine: you put something in, and exactly one thing comes out. Put in 3 to a “double it” machine and 6 comes out, every single time.
A machine that sometimes gives 6 and sometimes gives 7 for the same input is not a function. Try the moody machine below and catch it misbehaving:
Relations are lines between things
A relation is a set of connections. Draw a line from each child to every ice cream flavour they like. Some children like many flavours, some like none. That bunch of lines is a relation. A function is a special relation where every child points to exactly one favourite.
Cardinality is “how many?”
The cardinality of a set is just how many things are in it. Here’s a clever trick: you can tell if two groups are the same size without counting. If every child sits on exactly one chair and no chair is left empty, there are as many children as chairs. Mathematicians use this matching trick to compare even infinitely big sets.
An AI that recognises photos is a function. In goes a picture, out comes one answer, like “cat”. The set of all possible pictures is huge, and the set of answers is small.
A robot’s map is often a set: the set of squares on the floor that are blocked. To plan a path, the robot only moves through squares that are not in that set.
- 1. A box holds a red ball, a red ball and a blue ball. As a set, how many different things are in it?
- 2. A machine gives 10 when you put in 5, but later gives 12 for the same 5. Is it a function?
- 3. A bee flies and is alive. Where does it go in the playground?
Level 1: Beginner
★★☆☆☆ Basic⏱ 15 minSecondary-school mathematics: arithmetic and elementary algebra.Writing sets down
We write a set with curly brackets and list its members: . The symbol means “is a member of”, and means “is not”:
| Symbol | Read it as | Example with |
|---|---|---|
| union: in A or B (or both) | ||
| intersection: in A and B | ||
| difference: in A but not in B | ||
| A is a subset of C: everything in A is also in C | ||
| the empty set, with nothing in it | ||
| complement: everything (in the universe ) not in A | if , | |
| cardinality: how many members |
Here’s the same playground, now with the maths names:
Worked example: counting a union
How many numbers are in ? It’s tempting to add , but that counts 3 and 4 twice. Subtract the overlap once:
Check by listing: has 5 members. ✅ This is called the inclusion–exclusion rule.
Functions
A function takes every member of the set (the domain) to exactly one member of (the codomain). For example, on the domain :
| 0 | 1 | 2 | 3 | |
|---|---|---|---|---|
| 1 | 3 | 5 | 7 |
The outputs that actually appear, , are called the range.
Pairs and relations
An ordered pair is two things where order matters: . The Cartesian product is the set of all pairs you can make:
A relation from to is any set of these pairs. “Is less than” on is the relation .
A dataset for machine learning is a set of pairs : an input (say, a photo) and its correct label (say, “cat”). Training a model means finding a function with on those pairs.
- 1. A = {2, 4, 6} and B = {4, 5, 6}. What is A ∩ B?
- 2. Using the same sets, what is |A ∪ B|?
- 3. How many pairs are in {1, 2, 3} × {a, b}?
- 4. f(x) = x² on the domain {−2, −1, 0, 1, 2}. What is the range?
Level 2: Intermediate
★★★☆☆ Intermediate⏱ 35 minUndergraduate students and practising engineers.Formal definitions
Set-builder notation describes a set by a rule: is the set of members of for which the statement is true. For example, .
The standard number sets are (natural numbers; some books start at 1), (integers), (fractions), and (real numbers), with . is the set of lists of real numbers. That’s where data points, robot joint angles, and neural network weights live.
The power set is the set of all subsets of :
The algebra of sets
| Law | Statement |
|---|---|
| Commutative | |
| Associative | |
| Distributive | |
| De Morgan | |
| Difference |
The indicator function turns a set into numbers, which is how computers store sets:
Functions, precisely
A function is a relation such that every appears in exactly one pair . We write .
- Injective (one-to-one): different inputs give different outputs. .
- Surjective (onto): every output is reached. For every there is an with .
- Bijective: both. A bijection has an inverse .
No two inputs share an output.
Every output is hit.
A perfect pairing: it has an inverse.
Counting functions. For finite sets with and , each of the inputs can go to any of outputs, so there are functions . For an injective function, the first input has choices, the next , and so on, giving injective functions when . A relation on is any subset of , so there are relations.
Two more tools you’ll use constantly:
- Composition: . A deep neural network is a long composition of simple functions: .
- Image and preimage: for , . For , . The preimage exists even if has no inverse.
Properties of relations
A relation on a set (written ) can be:
| Property | Meaning | Example |
|---|---|---|
| Reflexive | for all | , “same colour as” |
| Symmetric | “is a sibling of” | |
| Antisymmetric | , | |
| Transitive | , “is an ancestor of” |
- An equivalence relation is reflexive, symmetric and transitive. It splits a set into non-overlapping equivalence classes. “Same remainder when divided by 3” splits into three classes: , , and .
- A partial order is reflexive, antisymmetric and transitive, like on numbers or on sets.
Where this shows up in AI, robotics and data science
| Idea | Where you’ll meet it |
|---|---|
| Set of pairs | A labelled dataset |
| Function | A classifier, a regression model, a robot controller (state → action) |
| Set of functions | A model family (“hypothesis class”), such as all linear models |
| Preimage | The decision region where the model says “cat” |
| Equivalence classes | Clusters, class labels, robot poses that differ by a full 360° turn |
| Intersection over union | The main score for image segmentation and object detection |
| Set difference | The free space a robot can move in: workspace minus obstacles |
SQL JOIN, UNION, INTERSECT |
Relational databases are built on relations and set operations |
Sets in Python
Python has sets built in. Run this and change it:
Exercise: classify a function
Exercise: Intersection over Union
Segmentation models (for example, finding a rock in a rover camera image) are scored with the Jaccard index, also called IoU:
- 1. f: ℝ → ℝ, f(x) = x². Which is true?
- 2. If |A| = 5, how many subsets does A have?
- 3. 'Is a sibling of' is symmetric. Which other property does it fail?
- 4. By De Morgan, (A ∩ B)ᶜ equals…
Level 3: Advanced
★★★★☆ Advanced⏱ 50 minGraduate level, with full derivations and proofs.How to prove two sets are equal
The standard method is double inclusion: show and . To show , take an arbitrary and prove .
For any sets in a universe : .
Proof. () Let . Then , so is in neither set: and . Hence and , so . () Let . Then and , so , meaning . ∎
If , then .
Proof by bijection. List . Map each subset to its indicator vector . Different subsets give different vectors (injective), and every 0/1 vector defines a subset (surjective). So . ∎
This is why feature selection is hard. With features there are subsets to choose from, far too many to try. It’s also why a binary mask over pixels can encode different shapes.
Inclusion–exclusion in general
For three sets, add the singles, subtract the pairs, add back the triple:
In general, . The same formula works for probabilities, with in place of .
Composition and the pigeonhole principle
If and are injective, then is injective.
Proof. Suppose . Since is injective, . Since is injective, . ∎ (The same pattern works for surjective and bijective.)
If and are finite and , then no function is injective.
A beautiful consequence: no compression program can shrink every file. There are files of bits, but only files shorter than bits. Two files would have to share a compressed version, and then you couldn’t decompress both. This links to information theory (A7).
Equivalence relations are partitions
Every equivalence relation on splits into disjoint, non-empty classes that cover (a partition). Conversely, every partition defines an equivalence relation: “is in the same block as”.
Proof sketch. Let . Reflexivity gives , so classes are non-empty and cover . If and share an element , symmetry and transitivity give , and then . So two classes are either identical or disjoint. ∎
This is exactly what clustering produces, and it’s how mathematicians build new objects. For example, the set of angles is with , which is why robot joint angles “wrap around”.
Infinite cardinality: some infinities are bigger
Two sets have the same cardinality if there’s a bijection between them. This is the chair-matching trick from Level 0, made precise.
- A set is countable if it’s finite or in bijection with . You can list its members as a first, second, third, and so on.
- is countable: . Surprising, since .
- is countable too: list fractions by and skip repeats.
- is countable. The Cantor pairing function is a bijection . It walks through the grid of pairs one diagonal at a time.
is not countable.
Proof (diagonal argument). It’s enough to show the numbers in can’t be listed. Suppose they could: , each written as a decimal . Build a new number with if , and otherwise. Then differs from every in the -th digit, so it isn’t on the list. (Using only the digits 4 and 5 avoids the double-representation problem.) Contradiction. ∎
For every set , there is no surjection . So , and there is an endless ladder of bigger infinities.
Proof. Take any . Let . If for some , then , which is impossible. So is never an output, and isn’t surjective. ∎
The Schröder–Bernstein theorem says that if there are injections and , then there’s a bijection. It’s the standard tool for proving two sets have equal size, for example .
Exercise 3.1: Prove that A ∩ (B ∪ C) = (A ∩ B) ∪ (A ∩ C)
Use double inclusion. () If and , then or . In the first case , in the second . Either way is in the union. () If , then and . The case works the same way. ∎
Exercise 3.2: Show that f: ℝ → (0, ∞), f(x) = eˣ, is a bijection, and give its inverse
Injective: is strictly increasing, so . Surjective: for any , gives . The inverse is . This is why log-probabilities are safe to use in ML: is a bijection, so it never loses information.
Exercise 3.3: Prove that the set of finite binary strings is countable, but the set of infinite binary sequences is not
Finite strings: list all strings of length 0, then length 1, then length 2, and so on. Each length has finitely many strings, so this lists them all. Infinite sequences: run the diagonal argument. Given any list , flip the -th bit of to build a sequence that isn’t on the list. Since infinite sequences correspond to subsets of , this is Cantor’s theorem for .
- 1. Which set is uncountable?
- 2. Why can't a lossless compressor shorten every 1000-bit file?
- 3. In Cantor's proof that A has no surjection onto P(A), what is the 'diagonal set' D?
- 4. g ∘ f is injective. Which must be true?
Level 4: Expert
★★★★★ Expert⏱ 40 minResearchers: original papers and open problems.The foundations crisis: when sets broke mathematics
Early set theory let you form “the set of all with property ” for any . In 1901 Bertrand Russell asked about . If , then , and if , then . This is the same diagonal trick as Cantor’s theorem.
The fix was axiomatic set theory. Zermelo (1908), later refined into ZFC (Zermelo–Fraenkel with the Axiom of Choice), only lets you build sets from existing ones in controlled ways. Nearly all of modern mathematics, including everything on this site, can be written inside ZFC.
Two consequences matter for you as a scientist:
- The Axiom of Choice guarantees things like “every vector space has a basis”. It also implies that some subsets of can’t be given a sensible length (Vitali, 1905). That’s why probability theory only assigns probabilities to sets in a σ-algebra instead of to every subset. You’ll see this in A4.3.
- The Continuum Hypothesis: is there an infinity strictly between and ? Gödel (1940) and Cohen (1963) showed that ZFC can’t decide it either way. Some questions are genuinely independent of the axioms.
Cardinality is the first theory of learning
Here’s where sets meet AI research directly. Suppose your model family is finite, the true rule is in , and you pick any that makes zero mistakes on random training examples.
With probability at least , every such consistent has true error at most
Derivation. Call “bad” if its true error is above . A bad gets one random example right with probability below , so it gets all right with probability below . There are at most bad hypotheses, so by the union bound, the chance that any of them looks perfect is at most . Set this equal to and solve for . ∎
Notice what controls learning: , which is the number of bits needed to name one hypothesis. This is the root of the Occam’s razor and minimum description length ideas (A7).
Reproduce it. The simulation below uses 1,000 threshold classifiers, trains on 200 points, and checks how often the bound fails. Theory says at most 5% of the time.
Try changing m, delta and the size of H. You’ll see the observed failure rate is far below . The bound is correct but loose, because the union bound treats all 1,000 hypotheses as unrelated when neighbouring thresholds make almost the same mistakes.
When cardinality stops working
Most real model families are infinite (all lines, all neural networks with real weights), so and the bound says nothing. Two research answers:
- Count behaviours, not functions. Vapnik and Chervonenkis (1971) asked how many different labellings can produce on points: the growth function. Its largest fully-shattered set size is the VC dimension, an “effective cardinality” for infinite classes (B2).
- Computers are finite anyway. A network with parameters stored as 32-bit floats can represent at most functions, so . This “discretization trick” (Shalev-Shwartz and Ben-David, 2014) gives a real but very loose bound.
Modern deep networks have far more parameters than training examples, yet they generalise well. Zhang et al. (2017) showed that standard networks can perfectly memorise randomly labelled data, so capacity-counting bounds like these can’t explain their success. Explaining why deep learning generalises is still an active research area (B4).
Sets in robotics and space science
- Configuration space. A robot arm’s possible poses form a set . Obstacles define , and motion planning searches the free space (U1).
- Safe sets. In safety-critical control, the safe states are written . A control barrier function guarantees the robot never leaves (Ames et al., 2019). Proving that a set is forward invariant is a core tool for certifying autonomous spacecraft and robots (S6).
- Remote sensing. Satellite analysis combines masks with set operations, for example “pixels that are land and not cloud and burned”. Segmentation accuracy is reported as IoU, which Jaccard introduced in 1912 to compare plant species across alpine regions.
- Databases. E. F. Codd’s relational model (1970) defines a database table as a mathematical relation, a subset of a Cartesian product. SQL is applied set theory (Y2).
Research directions to explore
- Tighter generalization bounds: PAC-Bayes, compression-based bounds (B2, B4).
- Set-valued learning: models whose input is a set must not depend on order, which is the idea behind Deep Sets (Zaheer et al., 2017) and point-cloud networks like PointNet (E18).
- Set-valued predictions with guarantees: conformal prediction outputs a set of labels that contains the truth with a chosen probability (I4).
References
- G. Cantor (1874). “Ueber eine Eigenschaft des Inbegriffes aller reellen algebraischen Zahlen.” Journal für die reine und angewandte Mathematik, 77.
- G. Cantor (1891). “Über eine elementare Frage der Mannigfaltigkeitslehre.” Jahresbericht der DMV, 1. (The diagonal argument.)
- E. Zermelo (1908). “Untersuchungen über die Grundlagen der Mengenlehre I.” Mathematische Annalen, 65.
- P. Jaccard (1912). “The distribution of the flora in the alpine zone.” New Phytologist, 11(2).
- P. Cohen (1963). “The independence of the continuum hypothesis.” PNAS, 50(6).
- E. F. Codd (1970). “A relational model of data for large shared data banks.” Communications of the ACM, 13(6).
- V. Vapnik and A. Chervonenkis (1971). “On the uniform convergence of relative frequencies of events to their probabilities.” Theory of Probability and Its Applications, 16(2).
- L. Valiant (1984). “A theory of the learnable.” Communications of the ACM, 27(11).
- A. Blumer, A. Ehrenfeucht, D. Haussler, M. Warmuth (1987). “Occam’s razor.” Information Processing Letters, 24(6).
- S. Shalev-Shwartz and S. Ben-David (2014). Understanding Machine Learning: From Theory to Algorithms. Cambridge University Press. Chapters 2–3.
- C. Zhang, S. Bengio, M. Hardt, B. Recht, O. Vinyals (2017). “Understanding deep learning requires rethinking generalization.” ICLR.
- M. Zaheer et al. (2017). “Deep Sets.” NeurIPS.
- A. Ames, S. Coogan, M. Egerstedt, G. Notomista, K. Sreenath, P. Tabuada (2019). “Control barrier functions: theory and applications.” European Control Conference.
- P. Halmos (1960). Naive Set Theory. Van Nostrand. (A classic, short first book on the subject.)