Sunday, August 9, 2026

Why cosine similarity better than dot product?

 Yes. This is actually an important next step because it lets you explain why a recommendation system often prefers cosine similarity over a raw dot product.

You can add the following section after your dot-product example.

Dot Product vs. Cosine Similarity

The dot product gives us a useful compatibility score, but there is an important limitation.

The dot product depends on both:

  1. The direction of the vectors — whether the user and movie have similar preferences.
  2. The magnitude (length) of the vectors — how large the numbers are overall.

Cosine similarity, on the other hand, focuses primarily on the direction of the vectors.

ab=axbx+ayby=abcosθ\mathbf a\cdot\mathbf b=a_xb_x+a_yb_y=\lVert\mathbf a\rVert\lVert\mathbf b\rVert\cos\theta
ab=4(1)+1(3)=1\mathbf a\cdot\mathbf b=4(-1)+1(3)=-1
The angle is obtuse, so the vectors point partly apart and the dot product is negative
Inputs
axa_x
aya_y
bxb_x
byb_y
Drag vector a or b
-6-4-2246-6-4-2246xxyyθ = 94.4°a = (4, 1)b = (-1, 3)

The mathematical relationship

For two vectors AA and BB:

AB=ABcos(θ)A \cdot B = \|A\|\|B\|\cos(\theta)

where:

  • ABA \cdot B = dot product
  • A\|A\| = magnitude of vector AA
  • B\|B\| = magnitude of vector BB
  • θ\theta = angle between the two vectors

Cosine similarity removes the magnitude component:

Cosine Similarity(A,B)=ABAB\boxed{ \text{Cosine Similarity}(A,B) = \frac{A\cdot B}{\|A\|\|B\|} }

Therefore:

Cosine Similarity(A,B)=cos(θ)\boxed{ \text{Cosine Similarity}(A,B)=\cos(\theta) }

For normalized vectors, the dot product and cosine similarity become the same value.


Why Does This Matter for Recommendations?

Let's make the difference very concrete.

Suppose we have two users.

User A

This user watches a lot of movies but strongly prefers Action and Sci-Fi:

A=[9,1,8]A=[9,1,8]

User B

This user watches fewer movies but has almost exactly the same preference pattern:

B=[4.5,0.5,4]B=[4.5,0.5,4]

Notice something interesting.

User B's vector is exactly half of User A:

B=0.5AB=0.5A

So the two users have the same preference pattern, but User A has larger values.


Using Dot Product

Calculate:

ABA\cdot B =(9×4.5)+(1×0.5)+(8×4)=(9\times4.5)+(1\times0.5)+(8\times4) =40.5+0.5+32=40.5+0.5+32 73\boxed{73}

The dot product is 73.

Now imagine another user:

C=[1,1,1]C=[1,1,1]

Calculate:

ACA\cdot C =(9×1)+(1×1)+(8×1)=(9\times1)+(1\times1)+(8\times1) =18=18

So:

AB=73A\cdot B=73

while:

AC=18A\cdot C=18

The dot product strongly favors B.

But there is an important question:

Is B really more similar to A, or does B simply have large numerical values in the same dimensions?


Cosine Similarity Removes the Magnitude Problem

First calculate the magnitude of AA:

A=92+12+82\|A\|=\sqrt{9^2+1^2+8^2} =81+1+64=\sqrt{81+1+64} =146=\sqrt{146} 12.08\approx12.08

For B:

B=4.52+0.52+42\|B\|=\sqrt{4.5^2+0.5^2+4^2} =20.25+0.25+16=\sqrt{20.25+0.25+16} =36.5=\sqrt{36.5} 6.04\approx6.04

Now calculate cosine similarity:

Cosine(A,B)=7312.08×6.04\text{Cosine}(A,B) = \frac{73}{12.08\times6.04} 7373\approx\frac{73}{73} Cosine(A,B)1.0\boxed{\text{Cosine}(A,B)\approx1.0}

That makes sense.

The two vectors point in exactly the same direction.

Even though their magnitudes are different, their preference patterns are identical.


An Even More Important Example

Now consider:

D=[90,10,80]D=[90,10,80]

This could represent a user who has watched many more movies, but whose relative preferences are the same.

Notice:

D=10AD=10A

Therefore A and D have exactly the same preference pattern.

Their cosine similarity is:

Cosine(A,D)=1\boxed{\text{Cosine}(A,D)=1}

because they point in exactly the same direction.

But look at the dot product:

ADA\cdot D =(9×90)+(1×10)+(8×80)=(9\times90)+(1\times10)+(8\times80) =810+10+640=810+10+640 =1460=\boxed{1460}

So we have:

ComparisonDot ProductCosine Similarity
A vs B731.00
A vs D14601.00

This is the key difference.

A and B are just as similar as A and D in terms of preference pattern, but the dot product gives D a dramatically larger score because D has a much larger magnitude.

Cosine similarity correctly recognizes that:

"The two users have the same preference direction, regardless of how large their individual values are."


Visual Intuition: Direction vs. Length

Think of each vector as an arrow.

The length of the arrow represents magnitude.

The direction of the arrow represents the pattern of preferences.

Two users could have:

User A:  ───────────────►
User B:  ───────►

They have different magnitudes, but they point in the same direction.

Therefore:

θ=0\theta=0^\circ

and:

cos(0)=1\cos(0^\circ)=1

So their cosine similarity is:

1\boxed{1}

This means maximum similarity.


Why Cosine Similarity Is Often Better for Recommendation

Suppose two users have the following preferences:

User 1

[9,1,8,0,1][9,1,8,0,1]

User 2

[0.9,0.1,0.8,0,0.1][0.9,0.1,0.8,0,0.1]

Their values are very different in magnitude.

But their preference pattern is identical.

Both users essentially say:

"I strongly prefer Action and Sci-Fi, have very little interest in Comedy and Romance."

Cosine similarity identifies this immediately.

The dot product, however, is influenced by the absolute magnitude of the vectors.

This can be problematic when the magnitude represents something unrelated to the actual similarity we want to measure.

For example, magnitude might be affected by:

  • number of movies watched
  • number of ratings given
  • amount of interaction
  • frequency of activity
  • length of a document
  • number of words in a document

In these situations, we often care more about the pattern of preferences than the absolute amount of activity.

That is where cosine similarity becomes particularly useful.


But Is Cosine Similarity Always Better?

No.

This is an important point for a machine-learning explanation.

Cosine similarity isn't universally better than dot product.

They answer slightly different questions.

Dot Product asks:

"How strong is the overall interaction between these two vectors?"

It considers both:

alignment+magnitude\text{alignment} + \text{magnitude}

Cosine Similarity asks:

"How similar is the direction or pattern of these two vectors?"

It primarily considers:

alignment\text{alignment}

So the choice depends on what the numbers represent.


A Simple Real-World Analogy

Imagine two customers buying products.

Customer A

Buys:

  • 10 Action movies
  • 2 Comedy movies
  • 8 Sci-Fi movies
A=[10,2,8]A=[10,2,8]

Customer B

Buys:

  • 1 Action movie
  • 0.2 Comedy movies
  • 0.8 Sci-Fi movies
B=[1,0.2,0.8]B=[1,0.2,0.8]

Customer B has the same preference pattern, but much less activity.

Cosine similarity says:

"Their interests are almost identical."

Dot product says:

"Customer A has a much stronger overall interaction."

Both statements can be useful.

If we're trying to identify similar users based on taste, cosine similarity may be more appropriate.

If we're trying to predict strength of interaction or preference, the magnitude may actually contain valuable information, so a dot product can be preferable.


The Connection to Machine Learning

This distinction becomes even more important when we move from our simple genre example to embeddings.

Suppose a movie is represented by a 768-dimensional embedding:

Movie=[m1,m2,,m768]Movie=[m_1,m_2,\ldots,m_{768}]

and a user is represented by:

User=[u1,u2,,u768]User=[u_1,u_2,\ldots,u_{768}]

The dot product is:

UserMovie=i=1768uimiUser\cdot Movie = \sum_{i=1}^{768}u_i m_i

Cosine similarity is:

i=1768uimii=1768ui2i=1768mi2\boxed{ \frac{ \sum_{i=1}^{768}u_i m_i }{ \sqrt{\sum_{i=1}^{768}u_i^2} \sqrt{\sum_{i=1}^{768}m_i^2} } }

Now the difference becomes very important.

For embedding-based systems such as:

  • semantic search
  • RAG
  • document retrieval
  • recommendation systems
  • image similarity
  • text similarity

we frequently care about whether two embeddings point in similar directions, rather than simply whether their raw values are large.

That's why cosine similarity is so common in embedding-based retrieval.


One Very Important Exception

There is an interesting mathematical shortcut.

If we normalize every vector to length 1, then:

A=1\|A\|=1

and:

B=1\|B\|=1

Therefore:

Cosine Similarity(A,B)=AB1×1\text{Cosine Similarity}(A,B) = \frac{A\cdot B}{1\times1}

which gives:

Cosine Similarity(A,B)=AB\boxed{ \text{Cosine Similarity}(A,B)=A\cdot B }

So after L2 normalization:

Cosine similarity is simply the dot product of the normalized vectors.

This is an extremely useful connection when you later discuss vector databases and embedding search.


The Final Takeaway

You can summarize the entire concept with this comparison:

PropertyDot ProductCosine Similarity
Measures alignment
Considers magnitude
Measures angle/directionIndirectly
Sensitive to vector length
RangeDepends on vectors-1 to 1
Good for preference strengthSometimes
Good for pattern similaritySometimes
Common for embeddings
Same after normalizationYes

The conceptual difference can therefore be stated very simply:

Dot Product=Similarity of direction×Magnitude\boxed{ \text{Dot Product} = \text{Similarity of direction} \times \text{Magnitude} }

while:

Cosine Similarity=Similarity of direction only\boxed{ \text{Cosine Similarity} = \text{Similarity of direction only} }

And for your recommendation-engine example, this gives you a natural progression:

User preferences → Movie features → Dot Product → Problem with magnitude → Cosine Similarity → Normalized embeddings → Vector/embedding search.

That progression will make the mathematics connect nicely to modern recommendation systems, RAG and vector databases.