Today we start the linear-algebra block. By the end you will be able to:
Recognise when a biology problem is a system of linear equations.
Write such a system in the standard form A\mathbf{x} = \mathbf{b}.
Solve small 2\times 2 and 3\times 3 systems by hand using substitution and Gaussian elimination.
Use the determinantad - bc to tell whether a 2\times 2 system has one solution.
Check a candidate solution by plugging it back in.
No prior linear algebra is assumed. We build the ideas from a concrete example and keep all numbers small enough to do with pen and paper.
A bulk sample mixes cell types; we want the amount of each
A tumour biopsy is not one cell type. It is a mixture: some tumour cells, some immune cells, some healthy tissue. Suppose we already know two marker readouts per cell type (say, expression of gene G1 and gene G2), measured per “unit” of each cell type:
Tumour
Immune
Gene G1 signal
2
1
Gene G2 signal
1
3
We sequence a bulk sample and measure total signal: G1 = 8, G2 = 9.
Question: how many units of tumour cells (x) and immune cells (y) were in the sample?
Each measured signal is a weighted sum of the unknown amounts
Each measured signal is the sum of contributions from each cell type:
Two unknowns (x and y), two equations. This is a system of linear equations.
“Linear” means every unknown appears only to the first power — no x^2, no xy, no \sin x. Just numbers times unknowns, added up. More formally, a function is linear if f(x+y)=f(x)+f(y). You can check that f(x)=2x is linear.
A\mathbf{x} = \mathbf{b} separates knowns, unknowns and measurements
We separate the known coefficients from the unknowns and the measurements:
A — the coefficient matrix. Here, the cell-type signatures (one column per cell type, one row per gene).
\mathbf{x} — the unknowns we want (the cell-type amounts).
\mathbf{b} — the right-hand side, our measurements.
The first row of A times \mathbf{x} reproduces the first equation; the second row gives the second. This packaging is what lets us scale to hundreds of genes later.
Strategy 1 — Substitution
Solve one equation for one unknown, then substitute into the other.
From equation 1: \;2x + y = 8 \;\Rightarrow\; y = 8 - 2x.
Put that into equation 2:
x + 3(8 - 2x) = 9 \;\Rightarrow\; x + 24 - 6x = 9 \;\Rightarrow\; -5x = -15
So x = 3, and then y = 8 - 2(3) = 2.
Answer: 3 units of tumour, 2 units of immune cells.
The system
\begin{aligned}
2x + y &= 8 \quad(1)\\
x + 3y &= 9 \quad(2)
\end{aligned}
Always check your answer
Plug x = 3,\; y = 2 back into the original equations:
\begin{aligned}
2x + y &= 8 \quad(1)\\
x + 3y &= 9 \quad(2)
\end{aligned}
Checking takes little effort and catches almost every arithmetic mistake. Get the habit in to avoid losing points on the exam! (or getting the wrong answer in your research)
Strategy 2 — Gaussian elimination
Substitution gets messy with three or more unknowns. Gaussian elimination is the systematic method that scales (it is what R uses internally).
The idea: add multiples of one equation to another to eliminate a variable, until the system is easy to read off.
Three moves are allowed (they never change the solution):
Multiply an equation by a non-zero number.
Add a multiple of one equation to another.
(Swapping order of two equations. Can sometimes make life more convenient)
Back-substitute into R1: 2x + 2 = 8 \Rightarrow x = 3.
Same answer as before. \checkmark
Try it: two equations, two lines
Write the equations as ax + by = c and dx + ey = f. Each is a line; a solution lies on both. Lines cross (one solution), are parallel (none: elimination leaves 0 = a non-zero number) or are the same line (infinitely many: 0 = 0).
Track only the numbers and aim for a staircase of zeros
We do not need to keep writing x and y. Track only the numbers in an augmented matrix[\,A \mid \mathbf{b}\,]:
Why bother? In deconvolution the signatures A stay the same and only the measurements \mathbf{b} change from sample to sample. Invert once, reuse for every sample.
How: put the identity on the right, [\,A \mid I\,], and keep doing row operations until the left side is I. Then the right side is A^{-1}:
So A^{-1} = \begin{pmatrix} 0.6 & -0.2 \\ -0.2 & 0.4 \end{pmatrix}. Check:A^{-1}\mathbf{b} = \begin{pmatrix} 0.6\cdot 8 - 0.2\cdot 9 \\ -0.2\cdot 8 + 0.4\cdot 9 \end{pmatrix} = \begin{pmatrix} 3 \\ 2 \end{pmatrix}, the same answer as before. \checkmark
No inverse? If elimination leaves a row of zeros on the left, you can never reach I: the matrix is singular (its columns point along the same line) and has no inverse.
Try it: the inverse step by step
[\,A \mid I\,] to [\,I \mid A^{-1}\,] one row operation at a time, then the check A\,A^{-1} = I.
The 2\times2 shortcut: the determinant
For A = \begin{pmatrix} a & b \\ c & d \end{pmatrix}, one number decides everything: the determinant
\det A = ad - bc
\det A \neq 0: A has an inverse, and A\mathbf{x} = \mathbf{b} has exactly one solution: A^{-1} = \frac{1}{ad - bc}\begin{pmatrix} d & -b \\ -c & a \end{pmatrix} \qquad \text{(swap } a, d\text{; negate } b, c\text{; divide by } \det A)
\det A = 0: the columns are parallel, there is no inverse, and no unique solution.
In Linear systems in R we let the computer do the elimination. The code is short:
A <-matrix(c(1, 1, 1,0, 2, 1,2, 1, 1), nrow =3, byrow =TRUE)b <-c(6, 7, 8)solve(A, b)# [1] 2 3 1solve(A) # one argument: the inverse
solve() runs Gaussian elimination internally — exactly what you just did by hand, but on any size. With one argument it returns A^{-1}.
Summary
A linear system = weighted sums of unknowns set equal to numbers.
Standard form: A\mathbf{x} = \mathbf{b} (A coefficients, \mathbf{x} unknowns, \mathbf{b} measurements).
Substitution for tiny systems; Gaussian elimination (via the augmented matrix) for anything bigger — eliminate to upper-triangular, then back-substitute.
Inverse: row-reduce [\,A \mid I\,] to [\,I \mid A^{-1}\,]; then \mathbf{x} = A^{-1}\mathbf{b} for any \mathbf{b}. A zero row on the left means no inverse.
Determinant of a 2\times2: ad - bc. Zero means parallel columns, no inverse, no unique solution.
Always check by substituting back into the original equations.
Biology hook: recovering cell-type amounts from bulk measurements.
Next: Linear systems in R — what happens when there is no solution or infinitely many.