Add a sixth stock that is the second one plus a whisper of noise, and the solver still says info = 0 and returns six weights. At the smallest noise we tried, those weights are wrong in the very first digit. How could we have known before looking at the answer?
The idea in plain words
Where we are. Topic 5 ended with a warning: info = 0 only says that no pivot was exactly zero. This topic puts a number on how much to trust a solution. It comes after the solvers because it is the question you ask of any of them, and before speed because there is no point being fast and wrong. The next two topics ask how fast the good answers can be.
Think of solving A x = b as a machine: put in b, get out x. Real data carry tiny errors, and so does every rounding in the arithmetic, about one part in 10¹⁶ for a double. The condition number says how many times that machine magnifies a small relative error in the worst case. A condition number of 1000 turns an error of 10⁻¹⁶ into one of 10⁻¹³: three digits gone.
Five stocks and a twin
First the covariance matrix of the five stocks. Then we add a sixth stock equal to JPM plus noise of size eps (the list noise has the same spread as a daily return, about 1%), and shrink eps. To judge the answers we make the true solution known: choose x_true = [1, ..., 1], build b = A x_true, solve, and compare.
# The five stocks alone: how much can the covariance matrix magnify an error?blas = Blas()cond = cond1(blas, cov)print("condition number:", round(cond, 2), "| digits left:", round(trust_digits(cond), 1), "| flops", blas.flops)1
condition number: 5.15 | digits left: 15.3 | flops 295
- 1A condition number near 5 loses less than one digit, so the five stocks alone are safe. Computing it through the inverse cost 295 flops, much more than one solve (45).
# Add the twin of JPM with less and less noise, and compare with the known solutionprint(" eps cond forward error digits left")for eps in (1.0, 0.1, 0.01, 0.001, 0.0001, 1e-6, 1e-8): rows = [X[t] + [X[t][1] + eps * noise[t]] for t in range(250)] a = covariance(rows) exact = [1.0] * 6 lu, piv = lu_factor(Blas(), a) computed = lu_solve(Blas(), lu, piv, matvec(a, exact)) c = cond1(Blas(), a) print(f"{eps:7.0e} {c:9.2e} {forward_error(exact, computed):12.2e} {trust_digits(c):8.1f}")1
eps cond forward error digits left 1e+00 1.55e+01 8.88e-16 14.8 1e-01 9.07e+02 1.61e-14 13.0 1e-02 9.00e+04 4.00e-14 11.0 1e-03 9.00e+06 4.32e-10 9.0 1e-04 9.00e+08 6.56e-08 7.0 1e-06 9.00e+12 2.85e-05 3.0 1e-08 1.15e+16 4.64e-01 0.0
- 1Each step of 100 in
epsmultiplies the condition number by 10⁴ and the error follows it up. At 10⁻⁸ the computed weights are off by about 46%.
solve_checked below does what careful code does: measure the condition number first and refuse when it is larger than the limit you accept. In real LAPACK the estimate comes from the LU factors in about n² flops (gecon), far cheaper than the explicit inverse used here for teaching.
# Refuse to solve when the condition number is above what we accepttwin_rows = [X[t] + [X[t][1] + 1e-6 * noise[t]] for t in range(250)]a = covariance(twin_rows)try: solve_checked(Blas(), a, matvec(a, [1.0] * 6), 1e8)except LinAlgError as error: print(error)1weights, cond = solve_checked(Blas(), cov, [1.0] * 5, 1e8)print("five stocks alone: allowed, condition number", round(cond, 2))
ill-conditioned: condition number 9e+12 exceeds 1e+08 five stocks alone: allowed, condition number 5.15
- 1The function refuses before factoring anything, so the 3 digits of an answer from this matrix are never used.
What the tasks ask
- 1`cond1`Factor once, solve for each unit vector to get the inverse's columns, and multiply the two largest column sums. The cost is checked exactly.
- 2`forward_error` and `trust_digits`The two numbers to compare: the error you actually have, and the rule-of-thumb digits you should have left.
- 3`solve_checked`Put them together: measure, refuse above the limit with the exact message, otherwise factor and solve.
Lab · what's graded
4 tasks · 100 pts- Measure the condition number with
cond125 pts - Compare solutions with
forward_error25 pts - Count the digits left with
trust_digits25 pts - Refuse a bad matrix with
solve_checked25 pts