Discrete Mathematics for Machine Learning
The mathematics of distinct, countable things: sets, logic, counting, graphs and remainders. It is the foundation every data structure stands on.
Why Does This Exist?
Machine learning runs on two kinds of mathematics. Calculus handles the smooth stuff: gradients, curves, things that flow. But the data itself is almost always discrete: a vocabulary of 50,000 distinct tokens, a graph of users and friendships, a true-or-false label, a remainder when you shard rows across machines.
Discrete mathematics is the toolkit for that world. It collects five ideas that look unrelated until you need them together: sets (collections and what belongs to them), logic (combining true-or-false claims), counting (how many possibilities exist), graphs (things plus connections), and modular arithmetic (remainders). Hashing a feature, masking attention, counting label combinations, walking a knowledge graph: each is one of these five wearing work clothes.
This page maps the territory and shows how the pieces connect. Each piece gets its full treatment on its own page: counting lives in combinatorics and counting, graphs in graph fundamentals.
The one idea you need first
A set is a collection of distinct things, written with braces: . Order does not matter and repeats collapse, so is the same set as . The power set of is the set of all its subsets. That one construction already explains binary features, as the diagram shows.
Think of It Like This
A box of labelled switches
Picture a box with three switches, each labelled and each either on or off. Every distinct pattern of the switches is one configuration of the box.
With one switch you get 2 patterns. Add a second and each old pattern splits in two, giving 4. Add a third and you get 8. Every subset of the three switches corresponds to exactly one pattern: the subset of switches that are on. The power set is just the list of all patterns.
That is why binary features produce possible inputs. The analogy stops where real features start correlating: the box assumes every combination is possible, while real data visits a tiny fraction of the patterns.
How It Actually Works
The five branches, each in one paragraph:
- Sets answer membership questions. Union, intersection and complement describe "either", "both" and "not". Inclusion-exclusion keeps the counting honest when groups overlap: .
- Logic combines claims with and, or and not, governed by propositional logic and checked row by row in truth tables. Every
ifin your pipeline is logic. - Counting says how many outcomes exist before you assign them probabilities. Permutations order things, combinations choose them, and the binomial coefficient counts the choices.
- Graphs model connections: nodes for things, edges for relationships. A social network, a dependency tree, a neural network's wiring diagram.
- Modular arithmetic works with remainders: means divides their difference. Sharding, hashing and cyclic schedules all live here.
Worked example
Take and list every subset by deciding yes or no for each element:
Count them: 8. In general an -element set has subsets, because each of the elements contributes an independent yes-or-no choice and . For binary features that is already 1,024 patterns; for it is about , more than any dataset will ever cover. That gap between possible and observed patterns is why generalisation is hard and why structure (graphs, hierarchies, sparsity) matters.
Code
The point is the check: enumerate the power set and confirm the count is exactly , no more and no fewer.
import itertools
def power_set(items: list[int]) -> list[tuple[int, ...]]: out: list[tuple[int, ...]] = [] for r in range(len(items) + 1): out.extend(itertools.combinations(items, r)) # all subsets of size r return out
subs = power_set([1, 2, 3])print(len(subs)) # -> 8print(2 ** 3 == len(subs)) # -> TrueWatch Out For
Counting ordered picks with the unordered formula
Choosing 2 features out of 10 gives sets. Listing ordered pairs gives . Both numbers are correct answers to different questions, and mixing them doubles or halves a probability computed downstream. Before reaching for a formula, ask whether swapping two picks creates a new outcome or the same one.
Assuming every combination occurs
counts possible patterns, not observed ones. With 100 binary features the space holds patterns and your data holds maybe . Any method that needs to see each pattern (a full contingency table, a saturated interaction model) is dead on arrival. You need assumptions, sharing or structure, which is exactly what the rest of machine learning provides.
The Quick Version
- Discrete mathematics covers countable things: sets, logic, counting, graphs and remainders.
- An -element set has exactly subsets, so binary features span patterns.
- Inclusion-exclusion corrects double counting when groups overlap.
- Remainders (modular arithmetic) sit behind hashing, sharding and cyclic schedules.
- Possible patterns dwarf observed data, which is why models need structure to generalise.