Exercise: vectors, dot product & projection

Goal

By the end of this exercise you should be able to, by hand and in R:

  • add and scale vectors,
  • compute a vector’s length (norm),
  • compute a dot product and read off its meaning,
  • compute the angle / cosine similarity between two vectors,
  • decide whether two vectors are orthogonal,
  • project one vector onto another and find the orthogonal leftover.

Do the by-hand problems with pen and paper first, then check with R.

Background

A vector is an ordered list of numbers \(\mathbf{v} = (v_1, \dots, v_n)\). For us, a vector is a sample: a gene-expression profile across \(n\) genes.

Operation Definition
Addition \(\mathbf{u} + \mathbf{v} = (u_1 + v_1,\ \dots,\ u_n + v_n)\)
Scaling \(c\,\mathbf{v} = (c v_1,\ \dots,\ c v_n)\)
Norm (length) \(\|\mathbf{v}\| = \sqrt{v_1^2 + \dots + v_n^2} = \sqrt{\mathbf{v}\cdot\mathbf{v}}\)
Dot product \(\mathbf{u}\cdot\mathbf{v} = u_1 v_1 + \dots + u_n v_n = \sum_i u_i v_i\)
Angle \(\cos\theta = \dfrac{\mathbf{u}\cdot\mathbf{v}}{\|\mathbf{u}\|\,\|\mathbf{v}\|}\)
Orthogonal \(\mathbf{u}\cdot\mathbf{v} = 0\)
Projection (the shadow \(\mathbf{p}\) of \(\mathbf{u}\) on \(\mathbf{v}\)) \(\mathbf{p} = \dfrac{\mathbf{u}\cdot\mathbf{v}}{\mathbf{v}\cdot\mathbf{v}}\,\mathbf{v}\)

The geometric identity that ties dot product to angle: \[\mathbf{u}\cdot\mathbf{v} = \|\mathbf{u}\|\,\|\mathbf{v}\|\cos\theta.\]

By hand

Use these vectors throughout (think of them as 2-gene mini-profiles): \[\mathbf{a} = \begin{pmatrix} 3 \\ 4 \end{pmatrix}, \quad \mathbf{b} = \begin{pmatrix} 4 \\ 0 \end{pmatrix}, \quad \mathbf{c} = \begin{pmatrix} -2 \\ 6 \end{pmatrix}, \quad \mathbf{d} = \begin{pmatrix} 6 \\ 8 \end{pmatrix}\]

B1. Compute \(\mathbf{a} + \mathbf{c}\) and \(2\mathbf{b} - \mathbf{a}\).

B2. Compute the lengths \(\|\mathbf{a}\|\) and \(\|\mathbf{b}\|\). (Both are nice round numbers.)

B3. Compute the dot products \(\mathbf{a}\cdot\mathbf{b}\) and \(\mathbf{a}\cdot\mathbf{c}\).

B4. Using B2 and B3, compute \(\cos\theta\) for the angle between \(\mathbf{a}\) and \(\mathbf{b}\). What is \(\theta\) in degrees?

B5. Show that \(\mathbf{b}\) and \(\begin{pmatrix} 0 \\ 5 \end{pmatrix}\) are orthogonal. Then find a number \(k\) so that \(\begin{pmatrix} 2 \\ k \end{pmatrix}\) is orthogonal to \(\mathbf{a}\).

B6. Compare \(\mathbf{a}\) and \(\mathbf{d}\): compute \(\cos\theta\) between them. What do you notice, and what does it say about the two “samples”? (Hint: look at \(\mathbf{d}\) versus \(\mathbf{a}\) componentwise.)

B7. Project \(\mathbf{a}\) onto \(\mathbf{b}\): compute the shadow \(\mathbf{p} = \dfrac{\mathbf{a}\cdot\mathbf{b}}{\mathbf{b}\cdot\mathbf{b}}\,\mathbf{b}\). Then compute the leftover \(\mathbf{a} - \mathbf{p}\) and verify it is orthogonal to \(\mathbf{b}\).

Explore

Check your by-hand answers with the interactive picture below. Drag the tips of \(\mathbf{u}\) (blue) and \(\mathbf{v}\) (orange); the tips snap to a 0.5 grid. The green arrow is the shadow \(\mathbf{p}\) (the projection of \(\mathbf{u}\) onto \(\mathbf{v}\)) and the dashed line the perpendicular drop. The depth slider replaces \(\mathbf{v}\) by \(k\mathbf{v}\): the same cell sequenced \(k\) times as deep.

E1. The picture starts with the vectors from the by-hand part: the blue arrow \(\mathbf{u}\) is \(\mathbf{a} = (3,4)\) and the orange arrow \(\mathbf{v}\) is \(\mathbf{b} = (4,0)\). Read \(\mathbf{u}\cdot\mathbf{v}\), \(\cos\theta\), \(\theta\) and the shadow \(\mathbf{p}\) off the numbers below the picture. Do they match your answers to B3, B4 and B7?

E2. Keep \(\mathbf{v} = (4,0)\) and drag \(\mathbf{u}\) around. Where must the tip of \(\mathbf{u}\) be for \(\mathbf{u}\cdot\mathbf{v} = 0\)? Where does the shadow point when \(\mathbf{u}\cdot\mathbf{v} < 0\)?

E3. Click parallel (\(\mathbf{u} = (3,4) = \mathbf{a}\), \(\mathbf{v} = (6,8) = \mathbf{d}\), as in B6). Move the depth slider from \(k = 1\) down to \(k = 0.5\). What happens to the distance \(\|\mathbf{u}-\mathbf{v}\|\), and what happens to \(\cos\theta\)?

E4. Click lecture (\(\mathbf{u} = (3,2)\), \(\mathbf{v} = (1,4)\)) and move the depth slider to \(k = 2\). Which of \(\mathbf{u}\cdot\mathbf{v}\), \(\|\mathbf{u}-\mathbf{v}\|\), \(\cos\theta\) and the shadow \(\mathbf{p}\) change? Explain why the shadow stays put.

E1. \(\mathbf{u}\cdot\mathbf{v} = 3\cdot4 + 4\cdot0 = 12\), \(\cos\theta = 0.6\), \(\theta \approx 53.1^\circ\), and the shadow is \(\mathbf{p} = \tfrac{12}{16}(4,0) = (3,0)\), with leftover \((0,4)\) straight up: the same numbers as B3, B4 and B7.

E2. With \(\mathbf{u} = (x, y)\), \(\mathbf{u}\cdot\mathbf{v} = 4x + 0y = 4x\), so it is zero exactly when \(x = 0\): the tip must be on the vertical gene-2 axis, at a right angle to \(\mathbf{v}\). When \(x < 0\) the dot product is negative and the shadow points the other way from \(\mathbf{v}\), onto the negative gene-1 axis.

E3. At \(k = 0.5\), \(\mathbf{v}\) becomes \((3,4) = \mathbf{u}\), so the distance falls from \(5\) to \(0\). The cosine stays at \(1\) the whole time: scaling a profile does not change its direction.

E4. \(\mathbf{v}\) becomes \((2,8)\). The dot product doubles (\(11 \to 22\)) and the distance grows (\(\sqrt{8} \approx 2.83 \to \sqrt{37} \approx 6.08\)), but \(\cos\theta\) stays \(0.740\). The shadow stays \(\mathbf{p} = (0.65, 2.59)\): the coefficient \(\frac{\mathbf{u}\cdot\mathbf{v}}{\mathbf{v}\cdot\mathbf{v}}\) is halved (\(\tfrac{11}{17} \to \tfrac{22}{68}\)) while \(\mathbf{v}\) is doubled, so the shadow only depends on the direction of \(\mathbf{v}\).

In R

Define the vectors and verify your by-hand answers, then scale up to a realistic-sized profile.

a <- c(3, 4)
b <- c(4, 0)
cc <- c(-2, 6)   # 'c' is a reserved function name in R, so use cc
d <- c(6, 8)

# B1 — addition and scaling
a + cc
2*b - a

# B2 — norms
sqrt(sum(a^2))   # = 5
sqrt(sum(b^2))   # = 4

# B3 — dot products
sum(a * b)
sum(a * cc)

# B4 — angle between a and b
cos_ab <- sum(a*b) / (sqrt(sum(a^2)) * sqrt(sum(b^2)))
cos_ab
acos(cos_ab) * 180/pi   # angle in degrees

# B5 — orthogonality check
sum(b * c(0, 5))        # should be 0

# B6 — a vs d
sum(a*d) / (sqrt(sum(a^2)) * sqrt(sum(d^2)))

# B7 — projection of a onto b, and the leftover
proj_ab <- (sum(a*b) / sum(b*b)) * b
proj_ab
leftover <- a - proj_ab
leftover
sum(leftover * b)       # should be 0 (orthogonal)

Scale up: comparing real-ish profiles

Reusable helper functions, then a small “expression matrix” of samples in columns:

norm_vec <- function(v) sqrt(sum(v^2))
cos_sim  <- function(u, v) sum(u*v) / (norm_vec(u) * norm_vec(v))

set.seed(1)
genes <- 200
# two samples of the same "cell type": s2 is s1 at double depth plus a little noise
s1 <- rpois(genes, lambda = 5)
s2 <- 2 * s1 + rpois(genes, lambda = 1)
# an unrelated sample
s3 <- rpois(genes, lambda = 5)

cos_sim(s1, s2)   # close to 1: same direction despite different depth
cos_sim(s1, s3)   # lower: unrelated profiles

# Compare with Euclidean distance, which IS fooled by depth:
norm_vec(s1 - s2)   # large, because s2 is ~twice as big
norm_vec(s1 - s3)   # smaller, even though s3 is unrelated!

R1. Run the block above. Explain in one sentence why cosine similarity ranks s1–s2 as most similar but Euclidean distance does not.

R2. Build a cosine-similarity matrix for all three samples. (Hint: stack them as columns of a matrix and use crossprod, or just call cos_sim on each pair.)

Hints

Use c to build a vector. Plain +, - and * between a scalar and a vector, or between two equal-length vectors, work elementwise — exactly matching the by-hand definitions of addition and scaling.

The norm is sqrt(sum(v^2)). The dot product of u and v is sum(u*v); equivalently u %*% v (see %*%), which returns a \(1\times1\) matrix. Note \(\|\mathbf{v}\|^2 = \mathbf{v}\cdot\mathbf{v} =\) sum(v*v).

R’s trig functions work in radians. Get the angle with acos and convert to degrees by multiplying by 180/pi. Clamp arguments that drift slightly outside \([-1,1]\) from rounding if needed.

Two vectors are orthogonal iff their dot product is exactly 0. For B5, set \(\mathbf{a}\cdot(2, k) = 0\) and solve the resulting linear equation for \(k\).

First compute the scalar coefficient \(\dfrac{\mathbf{u}\cdot\mathbf{v}}{\mathbf{v}\cdot\mathbf{v}}\) = sum(u*v)/sum(v*v), then multiply it by the vector v. The leftover u - proj should dot to 0 with v.

For R1, recall that cosine similarity divides out each vector’s length, so it sees only direction; Euclidean distance [norm_vec(s1 - s2)] keeps the magnitude, so doubling depth inflates it. For R2, the slick route is to normalise every column to unit length with apply, then crossprod(U) computes all pairwise dot products at once — and a dot product of unit vectors is their cosine, so that matrix is the cosine-similarity matrix. Or simply call cos_sim on each of the three pairs.

Solutions

B1. \(\mathbf{a}+\mathbf{c} = (3+(-2),\ 4+6) = (1,\ 10)\). \(2\mathbf{b}-\mathbf{a} = (8,0)-(3,4) = (5,\ -4)\).

B2. \(\|\mathbf{a}\| = \sqrt{3^2+4^2} = \sqrt{25} = 5\). \(\|\mathbf{b}\| = \sqrt{4^2+0^2} = 4\).

B3. \(\mathbf{a}\cdot\mathbf{b} = 3\cdot4 + 4\cdot0 = 12\). \(\mathbf{a}\cdot\mathbf{c} = 3(-2) + 4\cdot6 = -6 + 24 = 18\).

B4. \(\cos\theta = \dfrac{12}{5\cdot4} = \dfrac{12}{20} = 0.6\). So \(\theta = \arccos(0.6) \approx 53.13^\circ\).

B5. \(\mathbf{b}\cdot(0,5) = 4\cdot0 + 0\cdot5 = 0\) → orthogonal. For \((2,k)\): \(\mathbf{a}\cdot(2,k) = 3\cdot2 + 4k = 6 + 4k = 0 \Rightarrow k = -\tfrac{3}{2}\).

B6. Note \(\mathbf{d} = (6,8) = 2\mathbf{a}\). So \(\cos\theta = \dfrac{\mathbf{a}\cdot 2\mathbf{a}}{\|\mathbf{a}\|\,\|2\mathbf{a}\|} = \dfrac{2\|\mathbf{a}\|^2}{2\|\mathbf{a}\|^2} = 1\), i.e. \(\theta = 0^\circ\). \(\mathbf{d}\) points in the same direction as \(\mathbf{a}\) — same “shape” of profile, just twice the magnitude (like the same cell type sequenced at double depth). Cosine similarity is blind to that scaling.

B7. Coefficient \(= \dfrac{\mathbf{a}\cdot\mathbf{b}}{\mathbf{b}\cdot\mathbf{b}} = \dfrac{12}{16} = \tfrac{3}{4}\). So \(\mathbf{p} = \tfrac{3}{4}(4,0) = (3,\ 0)\). Leftover \(= \mathbf{a} - \mathbf{p} = (0,\ 4)\). Check: \((0,4)\cdot(4,0) = 0\) ✓ (orthogonal, as expected — the projection drops a perpendicular onto the line through \(\mathbf{b}\), here the x-axis).

R answers (selected output):

a + cc            # 1 10
2*b - a           # 5 -4
sqrt(sum(a^2))    # 5
sum(a*b)          # 12
cos_ab            # 0.6
acos(0.6)*180/pi  # 53.13010
proj_ab           # 3 0
leftover          # 0 4
sum(leftover*b)   # 0

R1. s2 is essentially 2 * s1, i.e. the same direction in gene space; cosine similarity measures direction and so reports ~1, whereas Euclidean distance measures absolute separation and is inflated by the doubled magnitude. With set.seed(1) the block prints (your numbers match if you used the same seed):

cos_sim(s1, s2)     # 0.9956318   (near 1: same direction)
cos_sim(s1, s3)     # 0.8509409   (lower: unrelated)
norm_vec(s1 - s2)   # 91.50956    (large: s2 is ~twice as big)
norm_vec(s1 - s3)   # 42.95346    (smaller, yet s3 is the unrelated one!)

So cosine ranks s1–s2 as most similar (\(0.996 > 0.851\)), but Euclidean distance ranks them as least similar (\(91.5 > 43.0\)) — fooled by the depth difference.

R2. One compact way — normalise each sample column to unit length, then the matrix of pairwise cross products is exactly the cosine-similarity matrix (a unit-vector dot product is its cosine):

M <- cbind(s1, s2, s3)        # genes in rows, samples in columns
U <- apply(M, 2, function(v) v / norm_vec(v))   # each column now has norm 1
round(crossprod(U), 3)        # 3x3 cosine-similarity matrix
#       s1    s2    s3
# s1 1.000 0.996 0.851
# s2 0.996 1.000 0.857
# s3 0.851 0.857 1.000

The diagonal is 1 (each sample with itself), the s1–s2 entry is near 1, and every entry involving s3 is lower — matching the pairwise cos_sim calls.

Where this leads

  • Matrices as transformations turns the dot product into a building block: every entry of a matrix–vector product is a dot product of a matrix row with the vector. A matrix becomes a machine that transforms whole vectors.
  • Cosine similarity is the standard way to compare cells/samples in single-cell analysis (Week 5).
  • Projection is the engine of PCA (Week 2): a PCA score is the coordinate of a sample projected onto a principal axis, and the orthogonal-leftover idea is how successive components are made independent.