Exercise: Linear systems in R and solution spaces

Goal

After this exercise you will be able to:

  • Build a coefficient matrix \(A\) and right-hand side \(\mathbf{b}\) in R and solve \(A\mathbf{x} = \mathbf{b}\) with solve.
  • Verify a solution with matrix multiplication %*%.
  • Recognise, by hand and in R, which of the three solution spaces a system has — one, none, or infinitely many — and tie that to whether a deconvolution problem is exactly-, over-, or under-determined.
  • Find a best-fit answer for an over-determined system with qr.solve / lm.

Background

A bulk expression sample is a mixture of cell types. With known signatures (columns of \(A\)) and a measured bulk profile \(\mathbf{b}\), the cell-type amounts \(\mathbf{x}\) satisfy \[ A\mathbf{x} = \mathbf{b}. \]

The shape of \(A\) tells you a lot. Let \(A\) have \(m\) rows (genes / equations) and \(n\) columns (cell types / unknowns):

  • \(m = n\), \(A\) non-singular \(\to\) exactly one solution (well-determined).
  • \(m > n\), equations conflict \(\to\) no exact solution (over-determined). Use least squares for the closest fit.
  • \(m < n\), or \(A\) square and singular \(\to\) infinitely many solutions if the equations are consistent, and no solution otherwise. In either case, the coefficients cannot be uniquely determined.

Two key facts:

  • \(A\) (square) is singular when one equation is a combination of the others — it carries no new information. Its columns are then not independent, and solve() stops with an error. Then \(\det A = 0\).
  • The rank of \(A\) is the number of genuinely independent equations. Full rank (\(=n\)) with a square \(A\) is exactly the unique-solution case.
  • More generally, a consistent system has a unique solution exactly when \(\operatorname{rank}(A)=n\) (independent columns). If its rank is smaller than \(n\), it has free variables and infinitely many solutions. Consistency must be checked separately; the shape or rank of \(A\) alone cannot establish it.

By hand

Decide the solution space before running any R. For a \(2\times 2\) system, each equation is a line in the \((x, y)\) plane; the solutions are where the lines meet.

Problem 1 — classify three systems

For each, state whether it has one, none, or infinitely many solutions, and explain in one sentence (lines crossing / parallel / identical).

  1. \(\;x + y = 4,\quad x - y = 0\)

  2. \(\;x + y = 4,\quad 2x + 2y = 7\)

  3. \(\;x + y = 4,\quad 3x + 3y = 12\)

Problem 2 — which deconvolution scenario?

Match each situation to one solution / no solution / infinitely many, and to well- / over- / under-determined:

  1. You measure 5 marker genes to resolve 3 cell types. The three signature columns are independent, and the data is exact and consistent.
  2. You measure 2 marker genes to resolve 3 cell types. The two equations are independent.
  3. You measure 3 marker genes to resolve 3 cell types, signatures all distinct and independent.

Explore

Check your classifications in Problem 1 with the interactive picture: each equation is a line, R1 in blue and R2 in orange. It starts with system (a), written as \(ax + by = c\) (R1) and \(dx + ey = f\) (R2). Change the sliders \(d\), \(e\), \(f\) of R2 to enter (b) and (c). The grey box shows the elimination step \(\text{R2} - m\,\text{R1}\) with the current numbers.

E1. For each of (a), (b) and (c), read the status and the last line of the grey box. Do they agree with your answers to Problem 1?

E2. In Step 2 below, solve() gives the same “singular” error for (b) and (c). Which number in the elimination step tells the two cases apart?

E3. What is the rank of \(A\) in (b) and in (c), that is, how many independent rows does it have? Can the rank alone tell “no solution” from “infinitely many”?

E1. (a) \(\text{R2} - \text{R1}\) leaves \(-2y = -4\), so \(y = 2\) and \(x = 2\): the lines cross once at \((2, 2)\). (b) \(\text{R2} - 2\,\text{R1}\) leaves \(0 = -1\): parallel lines, no solution. (c) \(\text{R2} - 3\,\text{R1}\) leaves \(0 = 0\): the same line, infinitely many solutions.

E2. The right-hand side of the leftover row. In both (b) and (c) the left side becomes \(0x + 0y\), which is why solve() stops in both cases. The right side is \(-1\) in (b), which is impossible, and \(0\) in (c), which is always true and leaves \(y\) free.

E3. Rank 1 in both: row 2 of \(A\) is \(2\times\) row 1 in (b) and \(3\times\) row 1 in (c), as qr(A_bc)$rank shows in Step 3. The rank uses only \(A\), not \(\mathbf{b}\), so it says “not a unique solution” but cannot tell (b) from (c). For that you need \(\mathbf{b}\) as well: is the leftover row \(0 =\) a non-zero number, or \(0 = 0\)?

In R

Step 1 — check your answers from Linear systems by hand

In Linear systems by hand you solved Problems 1–4 with pen and paper. Now let R confirm them.

# Problems 1 and 2 — the 2x2 mixture
A <- matrix(c(3, 1,
              1, 2), nrow = 2, byrow = TRUE)
b <- c(11, 7)

x <- solve(A, b)   # solve A x = b
x
# expected: your Type A and Type B amounts

A %*% x            # check: does it reproduce b?
# Problem 3 — the 3x3 mixture
A3 <- matrix(c(1, 2, 1,
               2, 1, 1,
               1, 1, 3), nrow = 3, byrow = TRUE)
b3 <- c(8, 7, 10)

x3 <- solve(A3, b3)
x3

A3 %*% x3 - b3     # the residual should be (numerically) zero
# Problem 4 — the inverse
Ainv <- solve(A)   # one argument: the inverse
Ainv
Ainv %*% A         # the identity
Ainv %*% c(8, 6)   # the second sample

Note byrow = TRUE: it fills the matrix row by row, so the R code reads in the same order as the equations on paper. (The default, byrow = FALSE, fills column by column — a common source of confusion.)

Step 2 — confirm your by-hand classifications

Run each system from Problem 1 through solve and read the result or the error message.

# (a) one solution
solve(matrix(c(1, 1, 1, -1), 2, byrow = TRUE), c(4, 0))

# (b) no solution (inconsistent, singular A)
solve(matrix(c(1, 1, 2, 2), 2, byrow = TRUE), c(4, 7))
# Error: ... system is exactly singular ...

# (c) infinitely many (consistent, singular A)
solve(matrix(c(1, 1, 3, 3), 2, byrow = TRUE), c(4, 12))
# Error: ... system is exactly singular ...

Note that solve gives the same “singular” error for (b) and (c): it cannot tell “no solution” from “infinitely many”. Use the diagnostics below to distinguish them.

Step 3 — diagnose without solving

A_bc <- matrix(c(1, 1, 2, 2), nrow = 2, byrow = TRUE)
qr(A_bc)$rank    # 1   -> only one independent equation (of 2 unknowns)
det(A_bc)        # 0   -> the same verdict from the determinant

Use qr$rank and dim to characterise a system before trying to solve it.

Step 4 — an over-determined system and least squares

Five genes, two cell types, noisy data: no exact solution, so find the best fit.

A <- matrix(c(2, 1,
              1, 3,
              1, 1,
              3, 2,
              0, 2), nrow = 5, byrow = TRUE)   # 5 genes, 2 cell types
b <- c(8.1, 8.9, 5.2, 10.8, 4.1)               # noisy measurements

# solve(A, b) would error: A is not square. Use least squares:
xhat <- qr.solve(A, b)
xhat

# Same answer via linear regression (no intercept):
coef(lm(b ~ A - 1))

# How far off is each equation? (the residuals)
A %*% xhat - b

We return to least squares and why it is the right notion of “closest” when we cover projection (Vectors, dot product & projection) and regression later.

Hints

Use matrix with byrow = TRUE so the numbers read in the same row order as the equations. Print the matrix and check it against the equations before solving.

Two lines either cross once (one solution), are parallel and distinct (no solution), or are the same line (infinitely many). Compare the coefficient ratios to the right-hand-side ratio: e.g. in (b) the left sides differ by a factor 2 but the right sides (\(4\) vs \(7\)) do not.

Count \(m\) = number of marker genes (equations / rows) and \(n\) = number of cell types (unknowns / columns). Then use the shape rule: \(m > n\) is over-determined, \(m < n\) is under-determined, \(m = n\) with independent signatures is well-determined. “Exact and consistent” data means the extra equations agree, so an over-determined-by-shape system can still land on one exact solution; with noise it would have none and need least squares.

solve errors with “system is exactly singular” for both no-solution and infinitely-many cases, and with “‘a’ must be a square matrix” for any non-square \(A\). The error tells you the system is not well-determined, but not which of the two singular cases it is.

Both have singular \(A\). Check consistency: does \(\mathbf{b}\) lie in the column space? A practical check for these small cases is whether the proportionality of the rows of \(A\) is also matched by \(\mathbf{b}\). In Problem 1, (b) breaks it (\(4 \to 7\) is not \(\times 2\)) so no solution; (c) preserves it (\(4 \to 12\) is \(\times 3\)) so infinitely many.

For an over-determined (tall, non-square) \(A\), use qr.solve(A, b) or fit a no-intercept regression with lm(b ~ A - 1) and read coef. The - 1 drops the intercept so the model is purely \(A\mathbf{x}\).

Solutions

Problem 1

  1. One solution. Adding the equations: \(2x = 4 \Rightarrow x = 2\), then \(y = 2\). The two lines cross once. (Solution \(x = y = 2\).)

  2. No solution. Left sides are proportional (\(2\times\)) but right sides are not (\(4 \times 2 = 8 \ne 7\)). The lines are parallel and distinct — they never meet.

  3. Infinitely many. The second equation is exactly \(3\times\) the first (including the right side: \(3 \times 4 = 12\)). It is the same line; every point on \(x + y = 4\) is a solution.

Problem 2

  1. 5 genes, 3 cell types, independent signature columns and consistent data \(\to\) over-determined by shape, with one solution because the rank is 3, the number of unknowns. With measurement noise the extra equations will generally conflict, giving no exact solution and requiring least squares. Consistency alone would not guarantee uniqueness if the columns were dependent.

  2. 2 genes, 3 cell types and two independent equations \(\to\) under-determined, infinitely many solutions. Rank 2 means every right-hand side is attainable, but one unknown is free. Without independent equations, consistency would need to be checked: there could instead be no solution.

  3. 3 genes, 3 cell types, independent \(\to\) well-determined, one solution.

R, step by step

Step 1 — your by-hand answers. solve agrees with Problems 1–4 of Linear systems by hand: Type A \(= 3\) and Type B \(= 2\), the \(3\times3\) amounts \(9/7\), \(16/7\) and \(15/7\), and the inverse used for the second sample.

A <- matrix(c(3, 1, 1, 2), nrow = 2, byrow = TRUE)
b <- c(11, 7)
solve(A, b)
# [1] 3 2

A3 <- matrix(c(1, 2, 1,
               2, 1, 1,
               1, 1, 3), nrow = 3, byrow = TRUE)
b3 <- c(8, 7, 10)
solve(A3, b3)
# [1] 1.285714 2.285714 2.142857   (= 9/7, 16/7, 15/7)

A3 %*% solve(A3, b3) - b3
# all entries ~0 (tiny floating-point residual)

solve(A)
#      [,1] [,2]
# [1,]  0.4 -0.2
# [2,] -0.2  0.6
solve(A) %*% c(8, 6)
# [1,] 2
# [2,] 2

Step 2 — confirm the classifications. (a) is the one-solution system; adding the two equations gives \(2x = 4\), so \(x = 2\) and \(y = 2\) — solve returns 2 2. (b) and (c) both have a singular \(A\), so solve throws the same “system is exactly singular” error for both and cannot tell them apart.

solve(matrix(c(1, 1, 1, -1), 2, byrow = TRUE), c(4, 0))   # 2 2
# (b) and (c) both throw "system is exactly singular"

Step 3 — diagnostics. For the (b)/(c) matrix, row 2 is \(2\times\) row 1, so only one of the two rows is independent (rank 1). The determinant agrees: \(1\cdot2 - 1\cdot2 = 0\). Rank \(1 < 2\) unknowns confirms the system is not unique without even attempting to solve it.

A_bc <- matrix(c(1, 1, 2, 2), nrow = 2, byrow = TRUE)
qr(A_bc)$rank    # 1   (< 2 unknowns -> not unique)
det(A_bc)        # 0   (same verdict)

Step 4 — over-determined system, least squares. \(A\) is \(5\times 2\) (tall), so solve(A, b) would error (“‘a’ must be a square matrix”). qr.solve returns the least-squares estimate — the \(\mathbf{x}\) minimising \(\|A\mathbf{x}-\mathbf{b}\|^2\) — and lm(b ~ A - 1) gives the same numbers (the - 1 removes the intercept). The residual vector \(A\hat{\mathbf{x}}- \mathbf{b}\) is not all zeros: that is the signature of an over-determined system — no exact fit exists, and least squares returns the closest compromise.

A5 <- matrix(c(2,1, 1,3, 1,1, 3,2, 0,2), nrow = 5, byrow = TRUE)
b5 <- c(8.1, 8.9, 5.2, 10.8, 4.1)

qr.solve(A5, b5)            # 2.508511 2.089362
coef(lm(b5 ~ A5 - 1))       # A51 2.508511   A52 2.089362  (same values)

A5 %*% qr.solve(A5, b5) - b5
# row residuals, not zero:
# -0.9936170 -0.1234043 -0.6021277  0.9042553  0.0787234

Where this leads

  • Vectors, dot product & projection (next unit) gives the geometry: the closest point on a line is a projection. Least squares as projection (week 3) uses it to explain why least squares is the right “closest” answer, and what lm does.
  • The full treatment of regression and constrained (non-negative) deconvolution comes later in the course; today’s unconstrained solve/lm is the stepping-stone, not the production tool.