ContentsThe library

Vectors, Distance and Similarity

When Distance and Angle Become the Same Question

Last timeComparing Direction Alone

Give every vector unit length and the two families of measure stop competing: straight-line distance and the angle then rank every pair in exactly the same order.

The previous two lessons set up a choice between measuring distance and measuring

angle. This lesson makes the choice go away, for the price of one division.

FIG 1Normalising a vector
the result: the same direction, with a length of exactly one
the original length, which every coordinate is divided by
Every coordinate is divided by the same positive number, so nothing about the direction changes. Do this to every vector in a collection and all of them now sit on the surface of a single sphere, which is a useful picture even at three hundred dimensions.

What happens to distance

Take two unit vectors and expand the squared distance between them. The

expansion has three terms: the squared length of the first, the squared length of

the second, and twice their dot product with a minus sign. Both squared lengths

are one.

FIG 2Squared distance between two unit vectors
the angle between the two vectors
their cosine similarity, which for unit vectors is just their dot product
Nothing else survives the expansion. The distance between two normalised vectors is a function of their angle alone, which is only to be expected once you picture them both sitting on the same sphere: the only thing that can differ is how far apart they are around it.
FIG 3Distance against cosine, for unit vectors
0.000.631.251.882.50-1.0-0.50.00.51.0cosine similarity
distance
The curve falls all the way from left to right without ever levelling off or turning back. That is the whole argument: a higher cosine is always a smaller distance, so sorting by one produces the same order as sorting by the other, and the two measures cannot disagree about which vector is nearest.

What that is worth in practice

The equivalence is not a curiosity; it is the reason a whole class of systems

works. Fast search structures are almost all built around distance, because

distance has properties they rely on. Similarity, in most applications involving

learned vectors, is wanted as an angle.

Normalising once, at the time of storage, connects the two. The index does

distance, the application means angle, and the relation above guarantees the

answers agree exactly rather than approximately. There is no accuracy cost

anywhere in that argument, which is rare enough to be worth noticing.

The lesson stops here

2 more paragraphs to go

You have read the opening. The rest of the argument, the problems that check whether it landed, and the lines worth keeping at the end all come with a plan.

The first lesson of every course in the library reads the whole way through, free, so you can see exactly what the rest of them are.

See the planThe contents

This is the reading half

Starting the course gives you your own copy of it. Every idea on every page has problems standing under it, marked with a reason rather than a tick, and any sentence you do not believe can be opened and argued with. None of that can happen on a page nobody owns.

The contents

The rest of this course

  1. 01The Same List, Read Two Ways
  2. 02Straight Lines and City Blocksopening only
  3. 03Multiply, Add, and Get an Angleopening only
  4. 04Dividing the Lengths Outopening only
  5. 05When Distance and Angle Become the Same Questionyou are here
  6. 06Everything Is Far Away and Nothing Is Nearbyopening only
  7. 07The Cost of Asking What Is Nearbyopening only
  8. 08Which Question Are You Actually Askingopening only

Read alongside