The Dot Product, Norms, and Angles

The tools for measuring length, distance, and relationships between vectors.

So far, we've treated vectors as directions and data points. We know how to add and scale them. But this leaves us with some fundamental unanswered questions:

  • How long is a vector?
  • What's the distance between two vectors?
  • How can we measure the relationship or "agreement" between two vectors?

To answer these, we need to introduce a new set of tools for measurement. We'll start with the concept of "length," formally known as the norm.

How Long is a Vector? The Norm
In linear algebra, the "length" or "magnitude" of a vector is called its norm. While there are many ways to define a norm, two are overwhelmingly common in data science and finance.

The L2 Norm (The One You Know)

Let's take our vector v=[3,4]v = [3, 4]. When we draw it, what's its length? You probably see the answer instantly. The vector forms the hypotenuse of a right-angled triangle with sides of length 3 and 4. We can use the Pythagorean theorem!

Length² = 3² + 4² = 9 + 16 = 25
Length = √25 = 5

This is the L2 Norm. It's the standard, "as the crow flies" Euclidean distance.

The Formula: L2 Norm

For a vector v=[v1,v2,...,vn]v = [v₁, v₂, ..., vₙ], its L2 norm, written as v2\|v\|_2, is:

v2=v12+v22++vn2\|v\|_2 = \sqrt{v_1^2 + v_2^2 + \dots + v_n^2}

The L1 Norm (The "Manhattan" Distance)

What if you're not a crow? What if you're a taxi driver in Manhattan, forced to travel along a grid? The distance you'd travel for the vector [3,4][3, 4] is simply 3+4=73 + 4 = 7. This is the L1 Norm. You just sum the absolute values of the components.

The Formula: L1 Norm

For a vector vv, its L1 norm, written as v1\|v\|_1, is:

v1=v1+v2++vn\|v\|_1 = |v_1| + |v_2| + \dots + |v_n|
The Dot Product: The Engine of Measurement
Now we come to the most important operation in this lesson: the dot product. On the surface, it looks like a simple calculation, but it is the key that unlocks the relationship between vectors.

The Data Scientist's View (The Calculation)

The dot product of two vectors, vv and ww, is found by multiplying their corresponding components and then summing the results. Let v=[2,1]v = [2, 1] and w=[1,3]w = [1, 3]. The dot product, written vwv \cdot w, is:

vw=(2×1)+(1×3)=2+3=5v \cdot w = (2 \times 1) + (1 \times 3) = 2 + 3 = 5

The Physicist's View (The "Projection" Intuition)

The dot product tells us about the agreement between two vectors. It answers the question: "How much is vector vv pointing in the same direction as vector ww?"

This relationship between the dot product and the angle between vectors is formalized by this crucial equation:

The Geometric Definition of the Dot Product

vw=vwcos(θ)v \cdot w = \|v\| \|w\| \cos(\theta)

Where v\|v\| and w\|w\| are the L2 norms (lengths) of the vectors, and θ\theta (theta) is the angle between them.

Application: Cosine Similarity
We can rearrange that magic formula to solve for what we're often most interested in: the angle. This value is called the Cosine Similarity.
cos(θ)=vwvw\cos(\theta) = \frac{v \cdot w}{\|v\| \|w\|}

It will always be between -1 and 1, and it's one of the most important metrics in all of data science.

  • Value of 1: The vectors point in the exact same direction (angle is 0°).
  • Value of 0: The vectors are orthogonal (angle is 90°).
  • Value of -1: The vectors point in opposite directions (angle is 180°).

Real-World Example: Recommending Movies

Imagine a streaming service. Your taste is a vector, where each component is your rating for a movie:

  • You = [5, 4, 1, ..., 5]
  • Alice = [5, 5, 2, ..., 4]
  • Bob = [2, 1, 5, ..., 1]

To find who is most similar to you, the service computes the cosine similarity between your vector and everyone else's. It then recommends movies that Alice loves but you haven't seen yet. This is the core principle behind many recommendation engines.

Summary: Your Measurement Toolkit

1. Norm (Length)

L2 Norm (v2\|v\|_2): The standard "Euclidean" length. (Pythagorean theorem).

L1 Norm (v1\|v\|_1): The "Manhattan" length. (Sum of absolute values).

2. The Dot Product (vwv \cdot w)

A simple calculation that reveals the geometric relationship between two vectors.

3. Cosine Similarity

A value from -1 to 1 that normalizes the dot product to give a pure measure of directional "agreement." The workhorse of similarity tasks.