Skip to content
AI360Xpert
Beta

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.

A three element set has exactly eight subsets, one per row of yes no choices, which is why n binary features produce 2 to the n patterns
A three element set has exactly eight subsets, one per row of yes no choices, which is why n binary features produce 2 to the n patterns

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: A=1,2,3A = \\{1, 2, 3\\}. Order does not matter and repeats collapse, so 1,2,2,3\\{1, 2, 2, 3\\} is the same set as AA. The power set of AA 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 nn binary features produce 2n2^n 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: ∣A∪B∣=∣A∣+∣B∣−∣A∩B∣|A \cup B| = |A| + |B| - |A \cap B|.
  • Logic combines claims with and, or and not, governed by propositional logic and checked row by row in truth tables. Every if in 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 (nk)\binom{n}{k} 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: a≡b(modm)a \equiv b \pmod m means mm divides their difference. Sharding, hashing and cyclic schedules all live here.

Worked example

Take A=1,2,3A = \\{1, 2, 3\\} and list every subset by deciding yes or no for each element:

∅, 1, 2, 3, 1,2, 1,3, 2,3, 1,2,3\emptyset,\ \\{1\\},\ \\{2\\},\ \\{3\\},\ \\{1,2\\},\ \\{1,3\\},\ \\{2,3\\},\ \\{1,2,3\\}

Count them: 8. In general an nn-element set has 2n2^n subsets, because each of the nn elements contributes an independent yes-or-no choice and 2×2×⋯×2=2n2 \times 2 \times \cdots \times 2 = 2^n. For n=10n = 10 binary features that is already 1,024 patterns; for n=100n = 100 it is about 103010^{30}, 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 2n2^n, 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))  # -> True

Watch Out For

Counting ordered picks with the unordered formula

Choosing 2 features out of 10 gives (102)=45\binom{10}{2} = 45 sets. Listing ordered pairs gives 10×9=9010 \times 9 = 90. 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

2n2^n counts possible patterns, not observed ones. With 100 binary features the space holds 103010^{30} patterns and your data holds maybe 10610^6. 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 nn-element set has exactly 2n2^n subsets, so nn binary features span 2n2^n 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.