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.
- the result: the same direction, with a length of exactly one
- the original length, which every coordinate is divided by
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.
- the angle between the two vectors
- their cosine similarity, which for unit vectors is just their dot product
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 contentsThis 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