{"course":{"slug":"maths-you-need","title":"The Maths You Actually Need","url":"https://addaly.com/learn/maths-you-need","level":"intermediate","track":"deep"},"lesson":{"slug":"cosine-versus-euclidean","title":"Cosine or distance, and why it often does not matter","url":"https://addaly.com/learn/maths-you-need/cosine-versus-euclidean","position":5,"of":76,"minutes":9,"answer":"For unit-length vectors, cosine similarity and Euclidean distance are two readings of the same quantity and rank results identically, so the choice only matters when lengths differ.","keyIdea":"For unit-length vectors, cosine similarity and Euclidean distance are two readings of the same quantity and rank results identically, so the choice only matters when lengths differ."},"body":{"format":"markdown","text":"## The identity that settles the argument\n\nTeams argue about whether to use cosine similarity or Euclidean distance in a vector search. Most of the argument disappears once you write down one line of algebra.\n\nFor any two vectors a and b:\n\n```\n||a - b||^2  =  ||a||^2 + ||b||^2 - 2 (a . b)\n```\n\nExpand the squared distance term by term and you get exactly this; it is the law of cosines wearing different clothes.\n\nNow suppose both vectors have been normalised to length 1. Then `||a||^2 = 1` and `||b||^2 = 1`, and the dot product of two unit vectors *is* the cosine of the angle between them. So:\n\n```\n||a - b||^2 = 2 - 2 cos(theta)\n```\n\nDistance squared is a straight, decreasing function of cosine. Bigger cosine, smaller distance, always, with no exceptions. So if every vector in your index is normalised, sorting by cosine descending and sorting by Euclidean distance ascending give **the same order**. Not a similar order. The same one.\n\nThat is worth knowing before you spend an afternoon benchmarking one against the other.\n\n## Worked, with numbers\n\nTake `a = [0.6, 0.8]` and `b = [0, 1]`, both unit length.\n\n```\na . b = 0.6*0 + 0.8*1 = 0.8          so cos(theta) = 0.8, theta = 36.9 degrees\na - b = [0.6, -0.2]\n||a - b||^2 = 0.36 + 0.04 = 0.40\ncheck: 2 - 2(0.8) = 2 - 1.6 = 0.40   matches\n```\n\nTry a third vector `c = [1, 0]`: `a . c = 0.6`, and `||a − c||² = 0.16 + 0.64 = 0.80 = 2 − 2(0.6)`. Cosine says b beats c; distance says b is closer. They agree, and the identity guarantees they always will.\n\n## When they differ\n\nThe identity depends on the lengths being equal to one. Drop that and the two measures pull apart, because `||a||^2` and `||b||^2` re-enter the equation.\n\nConsider two documents about the same subject, one a 200-word note and one a 4,000-word article, represented as raw word-count vectors. Their directions are similar (same vocabulary, similar proportions) so the cosine is high. Their Euclidean distance is large, because one vector's entries are roughly twenty times the other's. Cosine calls them near-duplicates. Euclidean calls them far apart.\n\nWhich is right depends on the question. For \"are these about the same thing\", cosine. For \"is this the same size of thing\", the raw numbers matter and you should not have thrown length away.\n\nThree practical cases where length carries meaning:\n\n- **Counts and frequencies.** A word appearing 40 times is different evidence from it appearing twice, even at the same proportion.\n- **Physical measurements.** Height, weight, price. Two flats with the same shape of features but doubled everything are not the same flat.\n- **Model activations you are inspecting.** The magnitude of an activation is often the interesting part; normalising it away deletes what you were looking at.\n\n## The similarity numbers people quote\n\nCosine runs from −1 (opposite) through 0 (perpendicular) to 1 (identical direction). People report thresholds (\"we accept matches above 0.8\"), and those thresholds do not transfer between models. One embedding model may put unrelated sentence pairs around 0.1; another routinely puts them at 0.6 because its embeddings are squeezed into a narrow cone of the space, a property called anisotropy. A 0.7 in one model can mean less than a 0.4 in another.\n\nThe only defensible way to set a threshold is empirical: take a few hundred pairs you have labelled as matching or not matching, compute the scores with your actual model, and read the threshold off the distribution. Copying a number from a blog post about a different model is guessing.\n\n## What both measures share, and cannot fix\n\nNeither cosine nor Euclidean distance knows anything about meaning. They measure agreement between two lists of numbers produced by a model, and they inherit everything that model got wrong. If the embedding model was trained mostly on English web text, two Hindi sentences that mean opposite things may sit close together because the model represents both weakly. The geometry will report a confident 0.91 and be entirely wrong, because the metric is doing its job perfectly on numbers that were bad before it saw them.\n\n## The rule to keep\n\nNormalise on insert and on query, then use dot product: it is the cheapest of the three to compute, it equals cosine once vectors are unit length, and it ranks identically to Euclidean distance. Depart from that only when you have a stated reason why length matters, and write the reason down next to the code."},"check":{"question":"An engineer benchmarks cosine similarity against Euclidean distance on a vector index whose entries are all normalised to unit length, and reports that cosine gives \"noticeably better recall@10\". What is the most likely explanation?","options":[{"text":"Cosine handles high-dimensional spaces better, because it ignores magnitude which becomes unreliable as dimensions grow.","correct":false,"tempting":"States a real property of cosine, but it cannot produce a difference here: with unit vectors, magnitude has already been removed from both measures."},{"text":"There is a bug or an inconsistency in the benchmark: with unit-length vectors the two produce identical rankings, so recall@10 cannot differ.","correct":true},{"text":"Euclidean distance is being computed without the square root, which changes the ordering.","correct":false,"tempting":"Squared distance is a monotone function of distance, so omitting the square root leaves every ranking exactly as it was."},{"text":"Recall@10 measures a different quantity from similarity, so the two metrics are not comparable in the first place.","correct":false,"tempting":"Recall@10 is computed from a ranking; both measures produce a ranking, so they are perfectly comparable."}]},"prev":{"slug":"norms-and-distance","title":"How long, and how far","url":"https://addaly.com/learn/maths-you-need/norms-and-distance"},"next":{"slug":"angles-orthogonality-and-projection","title":"Projection, and the art of removing a direction","url":"https://addaly.com/learn/maths-you-need/angles-orthogonality-and-projection"},"authorship":"Founder and series editor: Dr Mustafa Mun. Written by AI models to a written standard. Source and assessment review continues. Academic review invited.","review":{"status":"Draft, open for academic review"},"cite":"Addaly (2026) 'Cosine or distance, and why it often does not matter', in The Maths You Actually Need. Edited by M. Mun. Addaly. Available at: https://addaly.com/learn/maths-you-need/cosine-versus-euclidean (Accessed: 29 November 2026).","licence":{"name":"CC BY-NC-SA 4.0","url":"https://creativecommons.org/licenses/by-nc-sa/4.0/"},"reader":{"schema":1,"title":"Cosine or distance, and why it often does not matter","courseTitle":"The Maths You Actually Need","module":{"index":1,"slug":"vectors-and-space","title":"Vectors, and the space they live in"},"answer":null,"keyIdea":"For unit-length vectors, cosine similarity and Euclidean distance are two readings of the same quantity and rank results identically, so the choice only matters when lengths differ.","hero":{"kind":"plate","image":null,"alt":null,"caption":null},"blocks":[{"t":"h2","id":"the-identity-that-settles-the-argument","runs":[{"text":"The identity that settles the argument"}]},{"t":"p","runs":[{"text":"Teams argue about whether to use cosine similarity or Euclidean distance in a vector search. Most of the argument disappears once you write down one line of algebra."}]},{"t":"p","runs":[{"text":"For any two vectors a and b:"}]},{"t":"code","lang":"","label":"Text","text":"||a - b||^2  =  ||a||^2 + ||b||^2 - 2 (a . b)"},{"t":"p","runs":[{"text":"Expand the squared distance term by term and you get exactly this; it is the law of cosines wearing different clothes."}]},{"t":"p","runs":[{"text":"Now suppose both vectors have been normalised to length 1. Then "},{"text":"||a||^2 = 1","code":true},{"text":" and "},{"text":"||b||^2 = 1","code":true},{"text":", and the dot product of two unit vectors "},{"text":"is","i":true},{"text":" the cosine of the angle between them. So:"}]},{"t":"code","lang":"","label":"Text","text":"||a - b||^2 = 2 - 2 cos(theta)"},{"t":"p","runs":[{"text":"Distance squared is a straight, decreasing function of cosine. Bigger cosine, smaller distance, always, with no exceptions. So if every vector in your index is normalised, sorting by cosine descending and sorting by Euclidean distance ascending give "},{"text":"the same order","b":true},{"text":". Not a similar order. The same one."}]},{"t":"p","runs":[{"text":"That is worth knowing before you spend an afternoon benchmarking one against the other."}]},{"t":"h2","id":"worked-with-numbers","runs":[{"text":"Worked, with numbers"}]},{"t":"p","runs":[{"text":"Take "},{"text":"a = [0.6, 0.8]","code":true},{"text":" and "},{"text":"b = [0, 1]","code":true},{"text":", both unit length."}]},{"t":"code","lang":"","label":"Text","text":"a . b = 0.6*0 + 0.8*1 = 0.8          so cos(theta) = 0.8, theta = 36.9 degrees\na - b = [0.6, -0.2]\n||a - b||^2 = 0.36 + 0.04 = 0.40\ncheck: 2 - 2(0.8) = 2 - 1.6 = 0.40   matches"},{"t":"p","runs":[{"text":"Try a third vector "},{"text":"c = [1, 0]","code":true},{"text":": "},{"text":"a . c = 0.6","code":true},{"text":", and "},{"text":"||a − c||² = 0.16 + 0.64 = 0.80 = 2 − 2(0.6)","code":true},{"text":". Cosine says b beats c; distance says b is closer. They agree, and the identity guarantees they always will."}]},{"t":"h2","id":"when-they-differ","runs":[{"text":"When they differ"}]},{"t":"p","runs":[{"text":"The identity depends on the lengths being equal to one. Drop that and the two measures pull apart, because "},{"text":"||a||^2","code":true},{"text":" and "},{"text":"||b||^2","code":true},{"text":" re-enter the equation."}]},{"t":"p","runs":[{"text":"Consider two documents about the same subject, one a 200-word note and one a 4,000-word article, represented as raw word-count vectors. Their directions are similar (same vocabulary, similar proportions) so the cosine is high. Their Euclidean distance is large, because one vector’s entries are roughly twenty times the other’s. Cosine calls them near-duplicates. Euclidean calls them far apart."}]},{"t":"p","runs":[{"text":"Which is right depends on the question. For “are these about the same thing”, cosine. For “is this the same size of thing”, the raw numbers matter and you should not have thrown length away."}]},{"t":"p","runs":[{"text":"Three practical cases where length carries meaning:"}]},{"t":"ul","items":[[{"text":"Counts and frequencies.","b":true},{"text":" A word appearing 40 times is different evidence from it appearing twice, even at the same proportion."}],[{"text":"Physical measurements.","b":true},{"text":" Height, weight, price. Two flats with the same shape of features but doubled everything are not the same flat."}],[{"text":"Model activations you are inspecting.","b":true},{"text":" The magnitude of an activation is often the interesting part; normalising it away deletes what you were looking at."}]]},{"t":"h2","id":"the-similarity-numbers-people-quote","runs":[{"text":"The similarity numbers people quote"}]},{"t":"p","runs":[{"text":"Cosine runs from −1 (opposite) through 0 (perpendicular) to 1 (identical direction). People report thresholds (“we accept matches above 0.8”), and those thresholds do not transfer between models. One embedding model may put unrelated sentence pairs around 0.1; another routinely puts them at 0.6 because its embeddings are squeezed into a narrow cone of the space, a property called anisotropy. A 0.7 in one model can mean less than a 0.4 in another."}]},{"t":"p","runs":[{"text":"The only defensible way to set a threshold is empirical: take a few hundred pairs you have labelled as matching or not matching, compute the scores with your actual model, and read the threshold off the distribution. Copying a number from a blog post about a different model is guessing."}]},{"t":"h2","id":"what-both-measures-share-and-cannot-fix","runs":[{"text":"What both measures share, and cannot fix"}]},{"t":"p","runs":[{"text":"Neither cosine nor Euclidean distance knows anything about meaning. They measure agreement between two lists of numbers produced by a model, and they inherit everything that model got wrong. If the embedding model was trained mostly on English web text, two Hindi sentences that mean opposite things may sit close together because the model represents both weakly. The geometry will report a confident 0.91 and be entirely wrong, because the metric is doing its job perfectly on numbers that were bad before it saw them."}]},{"t":"h2","id":"the-rule-to-keep","runs":[{"text":"The rule to keep"}]},{"t":"p","runs":[{"text":"Normalise on insert and on query, then use dot product: it is the cheapest of the three to compute, it equals cosine once vectors are unit length, and it ranks identically to Euclidean distance. Depart from that only when you have a stated reason why length matters, and write the reason down next to the code."}]}],"check":{"question":[{"text":"An engineer benchmarks cosine similarity against Euclidean distance on a vector index whose entries are all normalised to unit length, and reports that cosine gives “noticeably better recall@10”. What is the most likely explanation?"}],"options":[{"text":[{"text":"Cosine handles high-dimensional spaces better, because it ignores magnitude which becomes unreliable as dimensions grow."}],"correct":false,"tempting":[{"text":"States a real property of cosine, but it cannot produce a difference here: with unit vectors, magnitude has already been removed from both measures."}]},{"text":[{"text":"There is a bug or an inconsistency in the benchmark: with unit-length vectors the two produce identical rankings, so recall@10 cannot differ."}],"correct":true,"tempting":null},{"text":[{"text":"Euclidean distance is being computed without the square root, which changes the ordering."}],"correct":false,"tempting":[{"text":"Squared distance is a monotone function of distance, so omitting the square root leaves every ranking exactly as it was."}]},{"text":[{"text":"Recall@10 measures a different quantity from similarity, so the two metrics are not comparable in the first place."}],"correct":false,"tempting":[{"text":"Recall@10 is computed from a ranking; both measures produce a ranking, so they are perfectly comparable."}]}],"order":[2,3,0,1],"why":null},"prev":{"slug":"norms-and-distance","title":"How long, and how far","minutes":8},"next":{"slug":"angles-orthogonality-and-projection","title":"Projection, and the art of removing a direction","minutes":9}}}