Vectors, dot product & projection

Week 1 — Vectors, dot product & projection

Quantitative biology and integrative omics

About this lecture

Here we will introduce a geometric interpretation of the equations. This will give a more intuitive interpretation of what is going on.

  • A vector is both an arrow and a row of numbers — and a gene-expression profile is a vector.
  • Adding and scaling vectors.
  • The length (norm) of a vector.
  • The dot product: one number that measures how two vectors line up.
  • The dot product gives the angle between vectors → cosine similarity, commonly used to compare high-dimensional samples (e.g. single-cell data later on)
  • Orthogonality (a right angle = zero dot product).
  • Projection: the shadow of one vector on another.

A gene-expression profile is a vector

Measure the expression of 3 genes in one tissue sample:

\mathbf{x} = \begin{pmatrix} 12 \\ 0 \\ 7 \end{pmatrix} \quad \begin{array}{l}\text{(Gene A = 12)}\\\text{(Gene B = 0)}\\\text{(Gene C = 7)}\end{array}

  • A vector is just an ordered list of numbers. Here, 3 numbers → one point/arrow in 3-D space.
  • A real RNA-seq sample has ~20 000 genes → a vector in 20 000-dimensional space. We cannot draw it, but every rule we learn in 2-D and 3-D still holds.
  • Two ways to read the same object:
    • as data: “this sample’s measurements”
    • as an arrow: from the origin to the point (12, 0, 7).

Vectors as arrows

In 2-D, the vector \mathbf{v} = \begin{pmatrix} 3 \\ 2 \end{pmatrix} is the arrow from the origin to the point (3, 2).

  • The numbers are the components: move 3 right, 2 up.
  • An arrow has a direction and a length.
  • We can slide the arrow around, but by convention we draw it starting at the origin.
# In R a vector is the most basic object
v <- c(3, 2)
v
# [1] 3 2

Adding vectors = combining profiles

Add component by component:

\begin{pmatrix} 3 \\ 2 \end{pmatrix} + \begin{pmatrix} 1 \\ 4 \end{pmatrix} = \begin{pmatrix} 4 \\ 6 \end{pmatrix}

  • Geometrically: place the tail of the second arrow at the head of the first (“tip-to-tail”).
  • Biological reading: if \mathbf{x} is a baseline profile and \mathbf{d} is the change after a drug, then \mathbf{x} + \mathbf{d} is the treated profile.
c(3, 2) + c(1, 4)
# [1] 4 6

Scaling changes a vector’s length, not its direction

Multiply every component by a scalar:

2 \cdot \begin{pmatrix} 3 \\ 2 \end{pmatrix} = \begin{pmatrix} 6 \\ 4 \end{pmatrix}, \qquad -1 \cdot \begin{pmatrix} 3 \\ 2 \end{pmatrix} = \begin{pmatrix} -3 \\ -2 \end{pmatrix}

  • A scalar > 1 stretches the arrow; between 0 and 1 shrinks it; negative flips it.
  • Scaling never changes the direction (except the flip) — only the length.
  • Biological reading: doubling a library’s sequencing depth scales the whole count vector by ~2; normalisation is exactly choosing a scalar to make samples comparable.
2 * c(3, 2)
# [1] 6 4

A vector’s length (norm) comes from Pythagoras

The length (or norm) of \mathbf{v} comes straight from Pythagoras:

\|\mathbf{v}\| = \sqrt{v_1^2 + v_2^2 + \dots + v_n^2}

For \mathbf{v} = \begin{pmatrix} 3 \\ 4 \end{pmatrix}: \|\mathbf{v}\| = \sqrt{9 + 16} = \sqrt{25} = 5.

v <- c(3, 4)
sqrt(sum(v^2))   # 5
sqrt(sum(v*v))   # same thing
  • A vector of length 1 is a unit vector — pure direction, no magnitude.

The dot product multiplies matching entries and adds them up

The dot product of two vectors multiplies matching components and sums:

\mathbf{u} \cdot \mathbf{v} = u_1 v_1 + u_2 v_2 + \dots + u_n v_n = \sum_{i=1}^{n} u_i v_i

Example: \mathbf{u} = \begin{pmatrix} 3 \\ 2 \end{pmatrix}, \ \mathbf{v} = \begin{pmatrix} 1 \\ 4 \end{pmatrix} \mathbf{u}\cdot\mathbf{v} = 3{\cdot}1 \;+\; 2{\cdot}4 \;=\; 3 \;+\; 8 \;=\; 11

  • The output is a single number (a scalar), not a vector.
  • Note: \mathbf{v}\cdot\mathbf{v} = v_1^2 + \dots + v_n^2 = \|\mathbf{v}\|^2 — the dot product of a vector with itself is its length squared.
u <- c(3, 2); v <- c(1, 4)
sum(u * v)        # 11
u %*% v           # 11 (as a 1x1 matrix)

The dot product measures how much two vectors point the same way

The geometric identity (this is the key formula of the day):

\boxed{\ \mathbf{u} \cdot \mathbf{v} = \|\mathbf{u}\|\,\|\mathbf{v}\|\cos\theta\ }

where \theta is the angle between the two arrows.

  • Same direction (\theta = 0): \cos\theta = 1 → dot product is large & positive.
  • Right angle (\theta = 90^\circ): \cos\theta = 0 → dot product is zero.
  • Opposite (\theta = 180^\circ): \cos\theta = -1 → large & negative.

So the dot product measures how much two vectors point the same way, scaled by their lengths.

Cosine similarity compares samples while ignoring sequencing depth

Rearrange the identity to solve for the angle:

\cos\theta = \frac{\mathbf{u}\cdot\mathbf{v}}{\|\mathbf{u}\|\,\|\mathbf{v}\|}

This is cosine similarity — it ranges from -1 to 1 and ignores overall magnitude (it only cares about direction).

  • Commonly used in biology: two samples of the same cell type have similar shapes of expression, because the ratios of molecules are the same. This compensates for differences in sequencing depth in RNA-seq. Euclidean distance keeps the depth difference.

cos_sim <- function(u, v) sum(u*v) / (sqrt(sum(u^2)) * sqrt(sum(v^2)))
cos_sim(c(12,0,7), c(6,0,3.5))   # 1  -> same direction, different depth

Vectors at a right angle have a dot product of zero

Two vectors are orthogonal (perpendicular) when their dot product is zero:

\mathbf{u}\cdot\mathbf{v} = 0 \quad\text{exactly when}\quad \theta = 90^\circ

Example: \begin{pmatrix} 2 \\ 0 \end{pmatrix} \cdot \begin{pmatrix} 0 \\ 5 \end{pmatrix} = 2\cdot0 + 0\cdot5 = 0. Orthogonal.

  • Orthogonal directions carry independent information — no overlap.
  • This idea is important for PCA later on (the principal components are orthogonal axes).
sum(c(2,0) * c(0,5))   # 0  -> perpendicular

Projection: the shadow of one vector on another

The projection of \mathbf{u} onto \mathbf{v} is the shadow \mathbf{p} that \mathbf{u} casts on the line through \mathbf{v} (imagine light coming straight down):

\mathbf{p} = \frac{\mathbf{u}\cdot\mathbf{v}}{\mathbf{v}\cdot\mathbf{v}}\;\mathbf{v}

  • The fraction \dfrac{\mathbf{u}\cdot\mathbf{v}}{\mathbf{v}\cdot\mathbf{v}} is a scalar: “how many copies of \mathbf{v}”.
  • Multiplying by \mathbf{v} turns that scalar into a vector pointing along \mathbf{v}.
  • The leftover, \mathbf{u} - \mathbf{p}, is orthogonal to \mathbf{v}.
  • Projections are super important in linear algebra, but in this course we will be glossing over it to avoid getting too deep into the math!
proj <- function(u, v) (sum(u*v) / sum(v*v)) * v
proj(c(3,3), c(4,0))   # 3 0  -> shadow on the x-axis

Try it: dot product, angle and projection

Regression, batch removal and PCA are all built on projection

  • Regression / deconvolution: expressing a sample as “so much of profile A plus so much of profile B” is a sequence of projections.
  • Removing an unwanted axis: subtract the projection of every sample onto a technical-artifact direction (e.g. a batch effect) to “regress it out”.
  • PCA preview: projecting all samples onto the top principal axis is exactly what a PCA scatter plot shows.

The dot product, cosine similarity, orthogonality, and projection are all the same machinery viewed from different angles.

Summary

Idea Formula Output Biology
Norm \|\mathbf{v}\| = \sqrt{\mathbf{v}\cdot\mathbf{v}} scalar overall magnitude / depth
Dot product \mathbf{u}\cdot\mathbf{v} = \sum u_i v_i scalar agreement of two profiles
Angle \cos\theta = \dfrac{\mathbf{u}\cdot\mathbf{v}}{\|\mathbf{u}\|\|\mathbf{v}\|} [-1,1] cosine similarity of samples
Orthogonal \mathbf{u}\cdot\mathbf{v}=0 yes/no independent directions
Projection \dfrac{\mathbf{u}\cdot\mathbf{v}}{\mathbf{v}\cdot\mathbf{v}}\mathbf{v} vector shadow / “how much of v”

Next (Matrices as transformations): a matrix is a function that transforms whole vectors at once.