Comparing whole lists of numbers
Before thisread these first
After thiswhat you will be able to doCalculate the length, dot product, and cosine similarity of two vectors, and explain why cosine similarity ignores their size but cannot validate what their entries mean.
Questionwhat this lesson answersWhen a list becomes one object, how can its arithmetic compare two patterns, and what can that comparison never tell you?
Not coveredwhat this lesson leaves outWe do not prove the general bound that keeps cosine similarity between -1 and 1. We do not derive cosine from circle geometry. We do not decide where useful vector entries should come from.
Imagine two tiny black-and-white pictures, each made from three pixels. Larger numbers mean brighter pixels. The first picture gives 1, 2, 2. The second gives 2, 1, 2.
How alike are the pictures? No single position answers that question. The first positions differ, the second positions differ, and the third positions match. We need arithmetic that keeps those three comparisons together.
The whole ordered list becomes one vector
Treat the whole ordered list as one object. That object is called a vector. The number of entries it holds is called its dimension. Each picture here gives a vector of dimension three.
Order matters. The first entry of one vector matches the first entry of the other. Rearranging one list would change which pixels meet.
Adding and scaling work entry by entry
Add the two picture vectors by adding matching entries:
Multiply one vector by 2 by multiplying every entry by 2:
This second operation is called scaling. One number stretches every entry together.
In two dimensions, can be drawn as an arrow two steps across and one step up. To add , place its tail at the first arrow’s head. The arrow from the original start to the new finish is . Scaling by 2 gives , which points the same way and reaches twice as far.
Past three dimensions, this arrow picture stops being drawable. The arithmetic does not stop working. A vector with 768 entries still adds and scales by the same entry-by-entry rules.
Length extends the Pythagorean recipe
Draw the two-dimensional arrow . It forms a right-angled triangle with horizontal side 3 and vertical side 4. The theorem of Pythagoras says its squared length is the sum of the squared sides:
The raised 2 means multiply a number by itself. The square root of 25 is the non-negative number which, multiplied by itself, gives 25. Here that number is 5.
This length is a theorem about distance in the picture. Apply the same recipe to the three-dimensional vector :
For more entries, the recipe stays unchanged. Square every entry, add the squares, then take the square root. Beyond drawable dimensions, this is a definition chosen because it agrees with visible distance where the picture exists.
The dot product multiplies matching entries
Return to the picture vectors. Multiply entries in matching positions, then add those products:
This multiply-and-add recipe is called the dot product. One number comes out.
It is large and positive when large entries occur in the same places. It is zero when the lists have no overlap in that sense:
It is negative when the lists run in opposite directions:
A raw dot product also measures size
The original dot product is 8. Double every entry in the first vector and repeat the same work:
The pattern did not change. Every entry kept the same proportion to the others. Yet the dot product doubled. A raw dot product mixes up how alike two patterns are with how big they are.
Dividing by both lengths removes size
The two original vectors both have length 3. The doubled first vector has length 6. Divide each dot product by the two lengths involved:
Both results are about 0.89. Doubling changed the dot product from 8 to 16, but the divided result did not move. Any positive scaling of either vector cancels in the same way.
For any two non-zero vectors, the result lies between -1 and 1. The earlier zero-overlap pair gives 0 after division. The opposite pair gives -1. Comparing any non-zero vector with itself gives 1.
This size-free result is called cosine similarity. In two dimensions, it matches the cosine of the angle between the arrows. The word cosine is borrowed from that picture. The circle arithmetic behind the name is a separate lesson.
If every entry is zero, the length is zero. Division by that length is impossible, so cosine similarity is undefined for the zero vector.
Try the arithmetic
Scale one vector and watch two comparisons
Adjust either three-entry vector. The table multiplies matching entries and adds every product. The scale control changes vector A after those entries are chosen.
A after scaling: (2, 4, 4). B: (2, 1, 2).
| Position | A entry | B entry | Product |
|---|---|---|---|
| 1 | 2 | 2 | 2 × 2 = 4 |
| 2 | 4 | 1 | 4 × 1 = 4 |
| 3 | 4 | 2 | 4 × 2 = 8 |
Add the products: 4 + 4 + 8 = 16.
Length of scaled A
sqrt(4 + 16 + 16) = sqrt(36) = 6
Length of B
sqrt(4 + 1 + 4) = sqrt(9) = 3
Cosine similarity
16 / (6 × 3) = 0.889
Raw dot product changes
At 1.0 times: 8. At 2.0 times: 16.
Cosine similarity does not
At 1.0 times: 0.889. At 2.0 times: 0.889.
Scaling A by 2.0 multiplies the raw dot product from 8 to 16. Cosine similarity stays 0.889.
The scale stays positive because a negative scale reverses direction. Zero is available in every entry control so you can see why the zero vector has no similarity score.
Now give the compact version names. Let and name the two vectors. Their entries at position are and . The summation sign adds the result from every position. Writing means multiply those matching entries. The notation names the score produced from both lists.
The expression above the fraction bar is the dot product built by matching, multiplying, and adding. Each square-root expression is one vector length. Their product is the size being divided out. The fraction bar divides the dot product by that product of lengths.
A score cannot explain where its entries came from
Two lists can point in exactly the same direction and mean completely different things. The same three entries could record pixels or survey answers. The arithmetic cannot see those labels.
The numbers had to come from somewhere, and this lesson takes them as given. Whether the similarity tracks something a person cares about must be checked against observations. That claim is empirical and could prove false. It depends entirely on how those entries were produced.
A similarity of 0.9 is not a probability. It is not 90 percent agreement. It reports only how two non-zero vectors line up under this chosen arithmetic.
Doorswhat to read next, and why
- Transforming vectors with linear mapsThis lesson compares vectors. Linear maps ask what happens when one vector is transformed into another while addition and scaling are preserved.
- Going round a circleThe name cosine comes from the angle between two arrows. This lesson uses that connection without building the circle arithmetic behind it.
- Turning words and images into numbersThis lesson defines ways to compare vectors, but it does not explain where token vectors come from or why related words end up near each other.
- Cauchy-Schwarz inequalityThe proof that a dot product cannot exceed the product of two lengths is called the Cauchy-Schwarz inequality. This lesson uses its result without proving it.
Symbolswhat each one means, and whether we defined it, measured it, or just started there
- a and bStatus: defined
- The letters a and b are defined as names for the two whole ordered lists being compared.
- a_i and b_iStatus: door
- The subscripts name the entries of a and b at position i, using notation established in the prerequisite lesson.
- dimensionStatus: defined
- A vector's dimension is defined as its count of entries.
- a + b and caStatus: defined
- Vector addition is defined by adding matching entries. Scaling by c is defined by multiplying every entry by that one number.
- square root of xStatus: defined
- For the non-negative totals used here, the square root of x is defined as the non-negative number which gives x when multiplied by itself.
- length of a = square root of sum_i a_i squaredStatus: defined
- In two dimensions, this recipe follows as a theorem about actual distance from Pythagoras. In 768 dimensions, there is no independent notion of distance to check it against. The definition is chosen because it agrees with the picture where a picture exists.
- a dot b = sum_i a_i b_iStatus: defined
- The dot product recipe is defined by multiplying matching entries and adding every result, so one number comes out.
- similarity(a, b)Status: defined
- Cosine similarity is defined as the dot product divided by both vector lengths. It is undefined when either vector has length zero.
- similarity(ca, b) = similarity(a, b), c > 0Status: defined
- Scaling either vector by a positive number leaves cosine similarity unchanged. This follows from the definition because the same factor enters above and below the division.
- cosineStatus: door
- The name cosine comes from the angle between arrows in two dimensions. Its circle arithmetic is a door because it is not derived here.
- whether similarity tracks something a person cares aboutStatus: empirical
- Whether similarity in a particular set of vectors tracks anything meaningful is empirical and depends entirely on where the numbers came from.
What these classifications mean
- defined
- circular by construction, true because we chose it
- empirical
- a measured claim about the world that could have come out otherwise
- bottoms out
- a primitive of the model, with nothing under it here
- door
- used here, explained elsewhere