← Lab
ASTERXOR / Lab

Two arrows can disagree about what close means

One comparison cares about where a vector ends. Another cares about the direction it points.

Three schematic arrows share an origin: two point in the same direction at different lengths, and one points at a shallower angle.
A schematic illustration of direction and length. The cover is not the four-vector test fixture; the exact candidates appear in the article figures.
LabASTERXOR / LEON LEUNG

An arrow ten times longer can be a perfect match, depending on the question.

Two playlists might have the same proportions of genres but very different total lengths. Two documents might use similar proportions of words while one is much longer. In both cases, pattern and quantity are separate ideas. Numerical vectors let us describe both, but a similarity measure decides which part survives the comparison. The familiar phrase closest match can hide that choice. A tiny drawing of four arrows makes the disagreement easier to see than a long argument about which metric is best.

Direction keeps a pattern. Length keeps an amount.
THE SETUP

Four arrows, two plausible winners

Our query is (1, 1). Candidate A is (10, 10), B is (1, 0), C is (0.8, 1.2), and D is (-1, -1). A points in exactly the query's direction but extends ten times as far. C ends nearby but tilts a little differently. D points straight the other way. The drawing presents several perfectly reasonable senses in which a candidate could resemble the query.

These coordinates are deliberately plain. The experiment does not need to claim that an arrow understands language. It only needs a situation in which keeping magnitude and ignoring magnitude produce different rankings. Both rankings can be calculated exactly enough to inspect, and neither needs a trained model.

Four labelled candidate arrows start at the origin; A reaches (10, 10), and the query and shorter candidates cluster near the origin.
FIG 01The query is (1, 1). A extends in the same direction; C ends nearby; D points opposite. These are the actual test vectors.
WHERE IT ENDS

Euclidean distance rewards nearby endpoints

Euclidean distance measures the straight-line gap between the ends of two vectors placed at a common origin. C is about 0.2828 away from the query, while B is 1 away, D is about 2.8284 away, and A is about 12.7279 away. C is therefore the nearest by this measure. The fact that A follows the same direction does not compensate for its distant endpoint.

This is a sensible comparison when coordinate amounts matter. If the axes represented actual quantities that must be matched, discarding scale could throw away the very discrepancy you wanted to detect. Euclidean distance also inherits the units and scaling of the coordinates, so it is only as meaningful as the space in which it is measured.

WHERE IT POINTS

Cosine similarity rewards aligned directions

Cosine similarity divides the dot product by the product of the vector lengths. For nonzero vectors it describes the angle between them. A receives a score approximately equal to 1, C about 0.9806, B about 0.7071, and D approximately -1. This time A wins. Its extra length disappears from the comparison because the normalization removes magnitude.

The limits are useful to picture. A score of 1 means the same direction, 0 means perpendicular directions, and -1 means opposite directions in this real-valued setting. A negative score is not a moral judgement or a claim that two people disagree. It is a geometric relationship between the supplied coordinates.

Distance bars are lowest for C, while cosine bars are highest for A and negative for D.
FIG 02Euclidean distance selects C at 0.28284; cosine similarity selects A at approximately 1. Lower and higher scores respectively indicate the preferred match.
A CONNECTION

Normalize first and the two views become related

Scale every nonzero vector to length one and all endpoints lie on a unit circle in this drawing. For unit vectors, squared Euclidean distance equals two minus twice the cosine similarity. Consequently, sorting by increasing Euclidean distance and decreasing cosine gives the same ranking on those normalized vectors. The apparent disagreement came from deciding whether to keep the original lengths.

This relationship is mathematical, not a promise that normalization is right for an application. Total quantity might be useful information. There is also no direction to normalize when a vector is entirely zero. A real implementation needs an explicit policy for that case instead of dividing by zero or quietly presenting an arbitrary score as a meaningful match.

The expanded calculation makes the unit-circle identity concrete. After normalizing, the query’s distances to A, C, B and D are 0, about 0.1971, 0.7654 and 2. Their order now agrees with cosine. D reaches the largest possible separation on the unit circle because its endpoint is opposite the query. Normalization has not discovered a hidden similarity; it has deliberately moved endpoints so that radial size no longer contributes to their separation.

THE LIMIT OF A SCORE

A high number needs a useful representation

A similarity score cannot rescue a representation that omitted the information you care about. If document vectors describe only word counts, they may miss word order, negation or context. Learned representations can capture richer relationships, but their scores still need evaluation on the task where they will be used. The formula itself does not establish semantic understanding.

Even the scale of the output invites overinterpretation. Cosine 0.98 does not automatically mean a 98 percent probability of a match. It is a cosine value. Turning it into a calibrated probability would require a separate model and relevant evidence. The toy arrows demonstrate the geometry, not the reliability of a search engine or a recommendation product.

WHERE ZERO LIVES

Moving the origin changes what an angle means

There is another choice hidden inside an arrow: where its tail begins. Add (100, 0) to the query and to every candidate. All pairwise coordinate differences stay unchanged, so the Euclidean distances are identical and C still wins. Cosine now selects C as well. The geometric configuration of endpoints has merely moved, but the rays drawn from zero have acquired different directions. Cosine is invariant to positive rescaling of individual vectors, not to a shared translation.

Originally, A makes zero degrees with the query, C about 11.310 degrees, B 45 degrees and D 180 degrees. After the translation, the measured gaps are about 4.627, 0.115, 0.567 and 1.146 degrees for A, C, B and D respectively. Even D now points nearly the same way as the query. Neither calculation is a bug. The exercise shows why a meaningful origin belongs to the definition of an angle-based representation, especially if the numbers describe positions rather than amounts measured from a natural zero.

Two bar charts show the cosine winner changing from A to C when the shared origin relationship changes.
FIG 03Actual angular gaps before and after adding (100, 0) to all five vectors. Panel scales differ; all Euclidean endpoint distances remain unchanged.
A PRACTICAL QUESTION

Decide what you are allowed to forget

Before choosing a metric, ask whether multiplying an item's whole vector by ten should change its similarity. If that multiplication represents ten times more of something important, perhaps it should. If it merely reflects a longer document with the same composition, perhaps it should not. This question is more revealing than treating one measure as universally more advanced.

The local checks confirm the aligned and opposite cases and the different winners under the two comparisons. No single example chooses a metric for every problem. What it can do is expose the trade-off clearly: A preserves the query's pattern at a different scale; C preserves its endpoint more closely. The correct winner depends on which of those facts the task actually values.

A practical comparison can ask two invariance questions separately. Should changing overall magnitude leave the answer alone? Should moving every point by the same offset leave it alone? Raw Euclidean distance passes the second test but not the first; cosine passes the first for positive multipliers but not the second. Neither test alone establishes usefulness. They reveal whether the formula respects transformations that the application considers irrelevant, before a polished score tempts us to ignore the representation.

Further reading

scikit-learn: cosine similarity ↗SciPy: Euclidean distance ↗

Original documentation and useful background.

THE THOUGHT TO TAKE AWAY

A useful measure starts with a clear decision about which differences are allowed to disappear.