Mathematical foundations
Vectors, Inner Products, and Norms
Develop the geometric language used for embeddings, gradients, similarity, projections, and regularization.
By the end you can
- Interpret vectors as points, directions, feature collections, and parameter updates
- Compute and explain dot products, angles, projections, and common norms
- Distinguish magnitude-sensitive distance from direction-sensitive similarity
- Choose a geometric comparison that matches the intended semantics
Key idea
High-dimensional distance needs a semantic audit
In many dimensions, distances stop being distinctive. The nearest point and the farthest point converge. That is not a rule of thumb; it was proved in 1999. Beyer and colleagues state it in their abstract: “We show that under a broad set of conditions (much broader than independent and identically distributed dimensions), as dimensionality increases, the distance to the nearest data point approaches the distance to the farthest data point.” The same paper measures the collapse instead of describing it. On a one-million-tuple uniform workload the ratio DMAX/DMIN is on the order of 10^7 in one dimension. At 10 dimensions it is already six orders of magnitude smaller. By 20 dimensions the farthest point is only about four times as far as the closest. In 2001 Aggarwal and colleagues restated the theorem as their own Theorem 1 and put the consequence in a sentence: “the ratio of the distances of the nearest and farthest neighbors to a given target in high dimensional space is almost 1”.
The second failure is stranger than a shrinking gap. Count how often a point turns up in other points' k-nearest-neighbor lists and the count is not evenly spread. Radovanović and colleagues published that result in 2010: “Through theoretical and empirical analysis involving synthetic and real data sets we show that under commonly used assumptions this distribution becomes considerably skewed as dimensionality increases, causing the emergence of hubs, that is, points with very high k-occurrences which effectively represent “popular” nearest neighbors.” Over 50 real data sets they measured a Spearman correlation of 0.62 between dimensionality and the skewness of N10, and of 0.80 between intrinsic dimensionality and that same skewness. A handful of points quietly become everybody's neighbour, and no aggregate retrieval score will say so.
High dimension is not the whole problem. Irrelevant or poorly scaled coordinates dilute useful structure too. Embeddings complicate it further: their geometry is learned from an objective, so proximity reflects training incentives rather than a universal notion of similarity. Before trusting nearest neighbors, inspect examples, compare metrics, test stability under rescaling, and evaluate whether local neighborhoods support the downstream task.
A distance is only as meaningful as the representation and metric that define it.
A vector is a role, not merely a column of numbers
A vector can represent an observation, a direction of motion, a set of model parameters, or a change to those parameters. Its meaning comes from the space and the operation it participates in. The same array may be a point when you compare examples and a direction when you apply a gradient update. Confusion begins when those roles are mixed without anyone saying so.
Vector geometry supplies three recurring ideas: direction, magnitude, and projection. Modern ML uses them everywhere, from cosine similarity in retrieval to norm penalties in optimization. The choice among them is not a matter of taste. As the sections below show, it has been proved about, measured, benchmarked, and written into a federal taxonomy.
Ask what role a vector plays before interpreting any of its coordinates.
Case
A direction between two words answered almost 40% of the analogies
Learned geometry can carry a relation, not only a distance. Subtract one word vector from another and the offset you get is specific to the relationship between them. Mikolov and colleagues reported that at NAACL-HLT in 2013. “King - Man + Woman” lands very close to “Queen”, and on their syntactic analogy test set the word vectors “are able to correctly answer almost 40% of the questions”. The direction between two words had become the object of interest.
What that number measures is a separate question, and Tal Linzen asked it in 2016. The result depends on a scoring rule that hides the query words from the answer pool. Remove the rule and the arithmetic answers itself: “When these words were not excluded, the nearest neighbor of a∗− a + b was b in 93% of the cases and a∗ in 5% of the cases (it was never a).” Without the exclusion, the offset lands back on its own input nineteen times out of twenty.
The baselines make the same point from the other side. On plurals, ONLY-B — take the nearest neighbour of b and ignore the offset entirely — scored .70, and ADD-OPPOSITE scored .45. Reversing the direction of the US-cities analogies dropped accuracy from .69 to .17, which no genuine relation-specific offset should permit. The exclusion rule sits in the original code, as Nissim and colleagues later documented: it “explicitly prevents yielding any term D such that D == B, D == A, or D == C”. The offset is real. Almost 40% is a number about a scoring harness as much as about the geometry.
Analogy
A vector points down an aisle and says how far to go
Each vector is an arrow telling a worker where and how far to move. The direction picks the aisle; the length sets the travel distance. A dot product asks how much one instruction helps move along another. A projection keeps only the part of an instruction aligned with a chosen aisle.
Coordinates such as income, age, and device type correspond to no physical direction, and no arrangement of aisles supplies one. Preprocessing is what gives the geometry a defensible meaning.
Vector operations inherit meaning from the representation; geometry cannot repair a semantically incoherent feature space.
Comparison
L1, L2, and L∞ describe different geometries
The norm determines what counts as a large vector and which perturbations count as nearby. L1 adds absolute coordinate magnitudes, has a diamond-shaped unit ball in two dimensions, and often encourages sparse solutions. L2 uses Euclidean length, has a circular or spherical unit ball, is smooth away from the zero vector, and is rotation-invariant under orthogonal transformations. L∞ uses the largest absolute coordinate. Its unit ball is a square or a cube, and it controls the worst coordinate deviation while treating many smaller changes as irrelevant to the norm.
The claim that L1 is less dominated by one very large coordinate has an exponent attached to it. In 2001 Aggarwal and colleagues proved that the expected gap between the farthest and the nearest point under an L_k norm scales as d^(1/k - 1/2). Absolute contrast grows with dimension for L1, stays flat for L2, and vanishes for k >= 3. They concluded that L1 is preferable to L2 in high dimensions. That recommendation was then tested. Mirkes and colleagues ran it against 25 UCI databases analysed as 37 binary classification problems, comparing eight lp functionals, and reported in 2020: “A systematic comparison shows that the difference of the performance of kNN based on lp for p=2, 1, and 0.5 is statistically insignificant.” An asymptotic contrast argument and a measured k-NN win are different things.
L∞ is the least abstract column of the three, because an entire research field runs on a budget expressed in it. Madry and colleagues defined robustness against an explicit L-infinity ball in 2018, training and evaluating with epsilon = 0.3 on MNIST and epsilon = 8 on CIFAR-10. Their adversarially trained MNIST network scored 98.8% on natural examples. Under the strongest attack they ran, 100-step PGD with 20 restarts, it still scored 89.3%. The choice of norm is now codified by a government agency: “PGD can be applied to the ℓ2 and ℓ∞ distance metrics for measuring the perturbation of adversarial examples.” That is NIST AI 100-2e2025, the adversarial machine learning taxonomy published in March 2025, which cites Madry and colleagues as reference [232].
L1 norm
Adds absolute coordinate magnitudes.
- Diamond-shaped unit ball in two dimensions
- Often encourages sparse solutions
- Less dominated by one very large coordinate than squared L2
- Useful when total absolute change is meaningful
L2 norm
Uses Euclidean length.
- Circular or spherical unit ball
- Smooth away from the zero vector
- Rotation-invariant under orthogonal transformations
- Common for distances, weight decay, and projections
L∞ norm
Uses the largest absolute coordinate.
- Square or cube-shaped unit ball
- Controls the worst coordinate deviation
- Treats many smaller changes as irrelevant to the norm
- Useful for bounded per-feature perturbations
Visual
Four questions vector geometry can answer
Different operations preserve different aspects of a representation. A norm measures magnitude according to a chosen geometry and answers how large. An inner product connects two directions, supports angle calculations, and answers how aligned. A distance compares points, depends on the chosen norm or metric, and answers how far apart. A projection extracts the component aligned with a subspace or vector and answers how much lies along this direction.
The four questions look interchangeable on a whiteboard. They are not. The d^(1/k - 1/2) scaling is a statement about distances. The epsilon = 0.3 ball is a statement about a norm. The ranking behaviour documented for OpenAI's embeddings below is a statement about an inner product under a unit-length constraint. Each result answers its own question, and none of them transfers for free.
How large?
A norm measures magnitude according to a chosen geometry.
How aligned?
An inner product connects two directions and supports angle calculations.
How far apart?
A distance compares points and depends on the chosen norm or metric.
How much lies along this direction?
A projection extracts the component aligned with a subspace or vector.
Magnitude, alignment, separation, and projection are related but not interchangeable.
Example
A projection calculation with two coordinates
Let u = (3, 4) and v = (4, 0). The projection of u onto v isolates the horizontal component.
- Inner product: u·v = 12.
- Squared norm: ‖v‖² = 16.
- Projection coefficient: 12/16 = 0.75.
- Projected vector: 0.75v = (3, 0).
- Residual: u − projᵥu = (0, 4), which is orthogonal to v.
Example
A worked comparison: same direction, different magnitude
Consider x = (3, 4), y = (6, 8), and z = (−4, 3). Three vectors are enough to separate what a norm sees from what an angle sees.
A long document sits far from a short one on the same topic, and the angle is what survives that difference. Microsoft's documentation of the Azure OpenAI embedding models gives the applied reason for preferring it: “This measurement is beneficial, because if two documents are far apart by Euclidean distance because of size, they could still have a smaller angle between them and therefore higher cosine similarity.” The x-and-y pair below is exactly that situation in two coordinates: a document twice as long, pointing the same way.
- Lengths: ||x||₂ = 5 and ||y||₂ = 10, so y has twice the Euclidean magnitude.
- Cosine similarity: x and y have cosine similarity 1 because they point in the same direction.
- Dot product: x·y = 50, which reflects both perfect alignment and their magnitudes.
- Orthogonality: x·z = 0, so z is perpendicular to x even though both have length 5.
- Interpretation: A retrieval system scoring by dot product treats y as twice the match x is for any query aligned with that direction. Cosine similarity treats the two directions as identical. Production embedding APIs remove the choice by shipping vectors of length 1.
The dot product mixes magnitude and alignment
For vectors x and y, the dot product x·y sums coordinate-wise products. Geometrically, x·y = ||x||₂ ||y||₂ cos θ, so the result grows with both vector lengths and with directional agreement. A positive dot product indicates an acute angle, zero indicates orthogonality, and a negative value indicates opposing directions. In high-dimensional models these products often become logits, attention scores, or similarity signals. Because magnitude matters, a large-norm vector can dominate even when its direction is only moderately aligned. Cosine similarity removes that effect by dividing by both norms.
A vendor can also remove it in advance. OpenAI's embeddings developer guide says “We recommend cosine similarity”, and explains why the recommendation costs nothing: “OpenAI embeddings are normalized to length 1, which means that: Cosine similarity can be computed slightly faster using just a dot product”. With both norms equal to one, the factor ||x||₂ ||y||₂ disappears, and cosine similarity, the raw dot product and Euclidean distance produce identical rankings. The ambiguity in the worked example above is settled by the representation rather than by the metric. The same guide gives the default lengths, 1536 for text-embedding-3-small and 3072 for text-embedding-3-large, and notes that a text-embedding-3-large vector shortened to 256 dimensions still outperforms an unshortened 1536-dimension text-embedding-ada-002 on the MTEB benchmark.
The inequality behind that cosine has a tangled name. The integral form was published in 1859, in a monograph on inequalities between integrals, by Viktor Bunyakovsky. The Encyclopedia of Mathematics records the sequence plainly: “Bunyakovskii published his study as early as 1859, whereas in H.A. Schwarz’ work this inequality appeared as late as 1884 (without any reference to the work of Bunyakovskii)”. The MacTutor biography of Viktor Yakovlevich Bunyakovsky says the same: “He is best known for his discovery of the Cauchy - Schwarz inequality, published in a monograph in 1859 on inequalities between integrals. This is twenty-five years before Schwarz’s work.” Textbooks still hand the result to Cauchy and Schwarz alone.
Steps
Choosing a similarity or distance
Use the intended invariances to select the geometry rather than defaulting to Euclidean distance. First define the comparison: state whether magnitude, direction, worst-coordinate deviation, or total change matters. Then normalize deliberately, deciding whether vector length is signal, nuisance, or a consequence of frequency — unit length, OpenAI's answer for its own embeddings, is one defensible position and not the only one. Then inspect neighbors. Look at concrete nearest and farthest pairs, not only aggregate scores, because this is precisely where hubness hides: a Spearman correlation of 0.80 between intrinsic dimensionality and skewness is invisible in a mean recall number and obvious in a list of who keeps showing up. Then stress the metric by rescaling features, perturbing irrelevant coordinates, and comparing neighborhood stability. Finally, validate downstream: measure whether the chosen geometry improves the actual retrieval, clustering, or prediction task.
The last step is not a formality. A norm recommendation derived from a clean asymptotic argument about contrast survived until someone ran it over 25 UCI databases and 37 binary classification problems. At that point the difference between l0.5, l1 and l2 turned out to be statistically insignificant. Choose the geometry from the semantics; keep it only if the task agrees.
1. Define the comparison
State whether magnitude, direction, worst-coordinate deviation, or total change matters.
2. Normalize deliberately
Decide whether vector length is signal, nuisance, or a consequence of frequency.
3. Inspect neighbors
Look at concrete nearest and farthest pairs, not only aggregate scores.
4. Stress the metric
Rescale features, perturb irrelevant coordinates, and compare neighborhood stability.
5. Validate downstream
Measure whether the chosen geometry improves the actual retrieval, clustering, or prediction task.
Key takeaways
- Vectors can represent points, directions, parameters, or updates; the role determines how the coordinates should be read.
- The dot product combines alignment with magnitude, while cosine similarity isolates directional agreement — and when vectors are normalized to length 1, as OpenAI documents its embeddings to be, cosine similarity, the raw dot product and Euclidean distance rank identically.
- L1, L2, and L∞ encode different ideas of size: Aggarwal and colleagues proved contrast scales as d^(1/k - 1/2), but Mirkes and colleagues found the k-NN difference between l0.5, l1 and l2 statistically insignificant across 25 UCI databases and 37 binary classification problems.
- Projection measures the component of a vector that lies along a direction or subspace: u = (3, 4) onto v = (4, 0) gives coefficient 0.75, projected vector (3, 0), and residual (0, 4).
- High-dimensional distance can become uninformative — Beyer and colleagues measured the farthest point at only about four times the distance of the closest by 20 dimensions, and Radovanović and colleagues found a 0.80 Spearman correlation between intrinsic dimensionality and hubness skew over 50 real data sets.
- A metric should be chosen from the semantics and invariances of the task, then validated on concrete downstream behavior; a norm choice can be consequential enough that NIST AI 100-2e2025 writes the ℓ2 and ℓ∞ perturbation metrics into a published taxonomy.