SigmaField
Mathematical Foundations for Quant Finance
0 of 256 lessons
      1. 3.3.1 Linear systems and Gaussian eliminationConcept · 30 min
      2. 3.3.2 Rank and the shape of the solution setConcept · 30 min
      3. 3.3.3 Practice set: linear systemsPractice set · 20 min
      4. 3.3.4 Case study: replicating a payoff, and complete marketsCase study · 35 min

Level 3, Linear algebra 3.3 Linear systems Lesson 1 of 4

ConceptLesson 3.3.1 in Level 3, Linear algebra. About 30 minutes.

Linear systems and Gaussian elimination

What a linear system is

A linear equation in the unknowns x1,…,xnx_1, \ldots, x_n multiplies each unknown by a number, adds the results and sets the sum equal to a number. There are no powers, roots or products of unknowns: 2x1−x2=32x_1 - x_2 = 3 is linear, x1x2=3x_1 x_2 = 3 is not.

A linear system is a set of linear equations that must all hold at once. A solution is a list of values for the unknowns that satisfies every equation.

Lesson 1.2.1 solved one already. The lines y=−x+7y = -x + 7 and y=2x+1y = 2x + 1 meet at (2,5)(2, 5). Rename xx and yy as x1x_1 and x2x_2, and bring the unknowns to the left:

x1+x2=7−2x1+x2=1\begin{aligned} x_1 + x_2 &= 7 \\ -2x_1 + x_2 &= 1 \end{aligned}

Lesson 3.2.1 multiplied a matrix by a vector: entry ii of AxA\mathbf{x} is row ii of AA times x\mathbf{x}. So the whole system is one matrix equation:

(11−21)(x1x2)=(71)\begin{pmatrix} 1 & 1 \\ -2 & 1 \end{pmatrix} \begin{pmatrix} x_1 \\ x_2 \end{pmatrix} = \begin{pmatrix} 7 \\ 1 \end{pmatrix}

Rule: A linear system

Ax=bA\mathbf{x} = \mathbf{b}

AA is the coefficient matrix, with one row per equation and one column per unknown. x\mathbf{x} is the vector of unknowns and b\mathbf{b} the right-hand side. Row ii of Ax=bA\mathbf{x} = \mathbf{b} is equation ii.

Two pictures of one system

The row picture. Each equation is a line in the plane of x1x_1 and x2x_2. A solution lies on both lines, so it is the point where they meet: (2,5)(2, 5).

The column picture. Lesson 3.2.1 also read AxA\mathbf{x} as a linear combination of the columns of AA (lesson 3.1.1). The columns of AA are a1=(1,−2)\mathbf{a}_1 = (1, -2) and a2=(1,1)\mathbf{a}_2 = (1, 1), each a column vector. The system asks: how much of each column adds up to b\mathbf{b}?

2(1−2)+5(11)=(71)2\begin{pmatrix} 1 \\ -2 \end{pmatrix} + 5\begin{pmatrix} 1 \\ 1 \end{pmatrix} = \begin{pmatrix} 7 \\ 1 \end{pmatrix}

Rule: The column picture

x1a1+x2a2+⋯+xnan=bx_1\mathbf{a}_1 + x_2\mathbf{a}_2 + \cdots + x_n\mathbf{a}_n = \mathbf{b}

Solving Ax=bA\mathbf{x} = \mathbf{b} means finding the combination of the columns a1,…,an\mathbf{a}_1, \ldots, \mathbf{a}_n of AA that gives b\mathbf{b}. The solution's entries are the amounts of each column.

The same system twice. As rows, two lines meet at (2,5)(2, 5); elimination swaps the second line for x2=5x_2 = 5, dashed, through the same point. As columns, 2 of a1\mathbf{a}_1 and then 5 of a2\mathbf{a}_2 reach b\mathbf{b}.

With three unknowns, each equation is a flat plane in space and the row picture looks for the point on all three. The column picture combines three columns in R3\mathbb{R}^3, the space of lists of three numbers. Lesson 3.3.2 shows what happens when the lines or planes do not meet in one point.

Try it

The first line multiplies AA by x=(2,5)\mathbf{x} = (2, 5) and gets b\mathbf{b}: the row picture. The second adds 2 of column 1 and 5 of column 2: the column picture. A[:, 0] is the first column.

The last line asks NumPy for the solution, with np.linalg.solve (end of this lesson).

⌘/Ctrl + Enter runs the cell

The augmented matrix and three row operations

Elimination works on the numbers alone. Write b\mathbf{b} as an extra column beside AA, behind a bar: this is the augmented matrix [A∣b][A \mid \mathbf{b}]. Each row is one equation.

(117−211)\left(\begin{array}{cc|c} 1 & 1 & 7 \\ -2 & 1 & 1 \end{array}\right)

Write R1,R2,…R_1, R_2, \ldots for the rows. Three row operations change the equations without changing the solutions.

Rule: Row operations

Ri↔RjRi→c Ri, c≠0Ri→Ri−m Rj\begin{gathered} R_i \leftrightarrow R_j \\[4pt] R_i \to c\,R_i,\ c \neq 0 \\[4pt] R_i \to R_i - m\,R_j \end{gathered}

Swap two rows; multiply a row by a number that is not 0; subtract a multiple of one row from another. Each can be undone, by the same kind of operation, so no solution is lost or gained.

To eliminate x1x_1 from the second equation, add 2 times row 1 to row 2. That is R2→R2−(−2)R1R_2 \to R_2 - (-2)R_1. The new row 2 is (−2+2,1+2∣1+14)=(0,3∣15)(-2 + 2, 1 + 2 \mid 1 + 14) = (0, 3 \mid 15), which says 3x2=153x_2 = 15.

In the row picture, the second line has become the flat line x2=5x_2 = 5. It still passes through (2,5)(2, 5), because a row operation keeps the solutions: the dashed line in the figure.

Now solve from the bottom up. Row 2 gives x2=15÷3=5x_2 = 15 \div 3 = 5. Row 1 then gives x1=7−x2=2x_1 = 7 - x_2 = 2.

Try it

M is the augmented matrix, made of decimals (dtype=float) so that its rows can hold fractions. M[1] is row 2: Python counts from 0.

The row operation is one line. The last two lines solve from the bottom up.

⌘/Ctrl + Enter runs the cell

Elimination, pivots and back substitution

Elimination aims for row echelon form: a staircase of zeros below the diagonal. The first nonzero entry of each row is its pivot, and each pivot sits to the right of the pivot in the row above. Rows of zeros, if any, go at the bottom.

Work one column at a time, from left to right. Use the pivot to clear every entry below it.

Rule: One elimination step

m=entry to clearpivotRi→Ri−m Rpivot\begin{gathered} m = \frac{\text{entry to clear}}{\text{pivot}} \\[4pt] R_i \to R_i - m\,R_{\text{pivot}} \end{gathered}

The multiplier mm is the entry below the pivot divided by the pivot. Subtracting mm times the pivot row puts a 0 in that place. Then back substitution solves the last equation first and works upwards.

A 3 × 3 example by hand

Solve this system:

x1+2x2+x3=32x1+5x2+3x3=83x1+4x2+4x3=14\begin{aligned} x_1 + 2x_2 + x_3 &= 3 \\ 2x_1 + 5x_2 + 3x_3 &= 8 \\ 3x_1 + 4x_2 + 4x_3 &= 14 \end{aligned}

Its augmented matrix is

(1213253834414)\left(\begin{array}{ccc|c} 1 & 2 & 1 & 3 \\ 2 & 5 & 3 & 8 \\ 3 & 4 & 4 & 14 \end{array}\right)

The first pivot is the 1 in the top left. Two steps clear column 1, and one more clears column 2 below the new pivot.

stepoperationmultipliernew row of AAnew entry of b\mathbf{b}
1R2−2R1R_2 - 2R_12÷1=22 \div 1 = 20, 1, 10,\ 1,\ 12
2R3−3R1R_3 - 3R_13÷1=33 \div 1 = 30, −2, 10,\ -2,\ 15
3R3−(−2)R2R_3 - (-2)R_2−2÷1=−2-2 \div 1 = -20, 0, 30,\ 0,\ 39

The result is in row echelon form, with pivots 1, 1 and 3 on the diagonal:

(121301120039)\left(\begin{array}{ccc|c} 1 & 2 & 1 & 3 \\ 0 & 1 & 1 & 2 \\ 0 & 0 & 3 & 9 \end{array}\right)

Back substitution reads it from the bottom:

x3=9÷3=3x2=2−x3=−1x1=3−2x2−x3=2\begin{aligned} x_3 &= 9 \div 3 = 3 \\ x_2 &= 2 - x_3 = -1 \\ x_1 &= 3 - 2x_2 - x_3 = 2 \end{aligned}

So x=(2,−1,3)\mathbf{x} = (2, -1, 3). Check it in the third original equation: 3(2)+4(−1)+4(3)=143(2) + 4(-1) + 4(3) = 14.

Try it

The three row operations of the table, then back substitution. Each x uses the ones already found below it.

The last line multiplies the original AA by the answer and gets b\mathbf{b} back.

⌘/Ctrl + Enter runs the cell

A zero pivot: swap rows

Sometimes the pivot place holds a 0, and dividing by it is impossible. Then swap in a row from below that has a nonzero entry in that column: the first row operation.

Take this system:

(121324381324)\left(\begin{array}{ccc|c} 1 & 2 & 1 & 3 \\ 2 & 4 & 3 & 8 \\ 1 & 3 & 2 & 4 \end{array}\right)

Clearing column 1 with R2−2R1R_2 - 2R_1 and R3−R1R_3 - R_1 leaves a 0 where the second pivot should be:

(121300120111)\left(\begin{array}{ccc|c} 1 & 2 & 1 & 3 \\ 0 & 0 & 1 & 2 \\ 0 & 1 & 1 & 1 \end{array}\right)

Row 3 has a 1 in column 2, so swap rows 2 and 3. The matrix is then already in row echelon form. Back substitution gives x3=2x_3 = 2, x2=1−x3=−1x_2 = 1 - x_3 = -1 and x1=3−2x2−x3=3x_1 = 3 - 2x_2 - x_3 = 3.

If every entry at and below the pivot place is 0, no swap helps: that column has no pivot. Lesson 3.3.2 shows that the system then has no solution or infinitely many.

Try it

The cell clears column 1, prints the 0 in the second pivot place, swaps rows 2 and 3, and prints the staircase.

M[[1, 2]] = M[[2, 1]] swaps two rows. The last line solves the swapped system and gets (3,−1,2)(3, -1, 2).

⌘/Ctrl + Enter runs the cell

A tiny pivot needs a swap too

A pivot that is not 0 but very small causes a different problem: rounding. A computer keeps about 16 significant digits (lesson 2.8.1), and a tiny pivot makes a huge multiplier.

Solve 10−20x1+x2=110^{-20}x_1 + x_2 = 1 and x1+x2=2x_1 + x_2 = 2. The answer is very close to (1,1)(1, 1). With the pivot 10−2010^{-20}, the multiplier is 102010^{20}, and row 2 becomes (0, 1−1020∣2−1020)(0,\ 1 - 10^{20} \mid 2 - 10^{20}).

To 16 digits, both 1−10201 - 10^{20} and 2−10202 - 10^{20} round to −1020-10^{20}. So x2=1x_2 = 1, and then row 1 gives x1=(1−1)÷10−20=0x_1 = (1 - 1) \div 10^{-20} = 0: wrong.

Swap the rows first and the multiplier is 10−2010^{-20} instead. Nothing important is lost, and the answer is (1,1)(1, 1).

Rule: Partial pivoting

pivot=the largest ∣aic∣over the rows i≥c\begin{gathered} \text{pivot} = \text{the largest } |a_{ic}| \\[4pt] \text{over the rows } i \ge c \end{gathered}

Before clearing column cc, swap up the row, at or below row cc, whose entry aica_{ic} in that column is largest in size. Every multiplier is then at most 1 in size, which keeps rounding errors small. It also takes care of zero pivots.

Try it

two_by_two eliminates and back-substitutes without any swap. On the original order it returns x1=0x_1 = 0.

M[[1, 0]] is the same matrix with its rows swapped, and gives the right answer. np.linalg.solve gets it right on the original order, because it swaps for itself.

⌘/Ctrl + Enter runs the cell

Solving with NumPy

np.linalg.solve(A, b) returns the solution of Ax=bA\mathbf{x} = \mathbf{b} for a square AA. NumPy passes the work to LAPACK, a library of compiled linear-algebra routines. Its routine runs this lesson's method: elimination with partial pivoting, then back substitution.

Lesson 3.2.3 could also solve the system as A−1bA^{-1}\mathbf{b}, with np.linalg.inv. Computing the whole inverse takes more arithmetic than one elimination and adds rounding, so solve is the better choice.

When elimination finds a column with no pivot, solve stops with LinAlgError: Singular matrix, though rounding can hide a missing pivot (lesson 3.3.2). A matrix that is not square is refused too. Lesson 3.3.2 deals with both kinds of system, and case study 3.3.4 uses solve to copy one asset's payoff with others.

Try it

solve on the 3 × 3 example, then a check: Ax−bA\mathbf{x} - \mathbf{b}. Its entries are 0 or tiny, such as 4.4×10−164.4 \times 10^{-16}: rounding, so the check passes.

The singular matrix has a second row that is twice the first, so elimination leaves a row of zeros and solve raises an error.

⌘/Ctrl + Enter runs the cell

Common mistakes

  1. Apply a row operation to the whole row, right-hand side included. Changing AA and not b\mathbf{b} solves a different system.
  2. Watch the sign of the multiplier. To clear a −2-2 below a pivot of 1, m=−2m = -2, so the step is R2−(−2)R1=R2+2R1R_2 - (-2)R_1 = R_2 + 2R_1.
  3. Never divide by a zero pivot. Swap in a row from below. If every entry below is 0 too, the column has no pivot (lesson 3.3.2).
  4. Multiplying a row by 0 is not a row operation. It throws an equation away and can add false solutions.
  5. Back-substitute from the bottom. The last row has one unknown; each row above adds one more.
  6. A tiny pivot is a trap in code. Swap up the largest entry in size, as np.linalg.solve does.
  7. Prefer solve(A, b) to inv(A) @ b, and check the answer by computing Ax−bA\mathbf{x} - \mathbf{b}.

Linear systems and elimination

0 of 11 correct

  1. 1.
  2. 2.
  3. 3.
  4. 4.
  5. 5.
  6. 6.
  7. 7.
  8. 8.
  9. 9.
  10. 10.
  11. 11.
How to type answers
  • Powers with ^: x^7, 2^(1/3), a^-2.
  • Multiply with * or by writing side by side: 3x^2, 2(x+1).
  • ln(x) is the natural log, log(x, 2) is log base 2, e is Euler's number, sqrt(x) is a square root.
  • sin(x), cos(x) and tan(x) take radians; write pi for π and sin(x)^2 for sin²x.
  • Your first answer to each question is saved: it counts towards your score and brings the question back for review.
  • Exact answers like ln(20)/ln(3) are best. Decimals need about 4 significant figures.

Summary card

  • A linear system is Ax=bA\mathbf{x} = \mathbf{b}. Each row is an equation, a line or a plane; the solution is the combination of the columns of AA that gives b\mathbf{b}.
  • Row operations (swap, scale by a nonzero number, subtract a multiple of another row) change the equations but not the solutions. Work on the augmented matrix [A∣b][A \mid \mathbf{b}].
  • Elimination clears each column below its pivot with multiplier m=entrypivotm = \frac{\text{entry}}{\text{pivot}}, giving row echelon form. Back substitution then solves from the bottom up.
  • A zero pivot needs a row swap. Partial pivoting always swaps up the largest entry in size, which also protects against rounding from tiny pivots.
  • np.linalg.solve(A, b) does elimination with partial pivoting; prefer it to inv(A) @ b. It raises LinAlgError for a singular matrix.

Free further reading

Optional. This lesson covers what you need; these add more examples and practice. All are free to read.