σμimplied vol · KKmax(S−K,0)udβρᵢⱼfat tailsPnL · drawdownyield · TdSₜ = μSₜ dt + σSₜ dWₜC = S·N(d₁) − K·e⁻ʳᵀ·N(d₂)σₚ² = wᵀΣwd₁ = [ln(S/K) + (r + σ²⁄2)T] ⁄ σ√T∂V/∂t + ½σ²S²·∂²V/∂S² + rS·∂V/∂S − rV = 0Sharpe = (Rₚ − R_f) ⁄ σₚVaR₉₉ = μ − 2.33σρ = Cov(X,Y) ⁄ σₓσᵧE[Rₚ] = Σ wᵢ·E[Rᵢ]φ(x) = e^(−x²/2) ⁄ √2πE[Rᵢ] = R_f + βᵢ(E[Rₘ] − R_f)dXₜ = θ(μ − Xₜ)dt + σ dWₜσ²ₜ = ω + α·ε²ₜ₋₁ + β·σ²ₜ₋₁C − P = S − K·e⁻ʳᵀΔ = ∂V/∂S Γ = ∂²V/∂S²p = (e^(rΔt) − d) ⁄ (u − d)rₜ = ln(Pₜ ⁄ Pₜ₋₁)E[Xₜ₊₁ | ℱₜ] = Xₜf* = (bp − q) ⁄ bz = (x − μ) ⁄ σP(N=k) = λᵏe^(−λ) ⁄ k!Θ = −∂V/∂Tν = S·φ(d₁)·√Tmin wᵀΣw s.t. wᵀμ = μ*∫Σ∂∞λ
uantCraft AcademyRoadmapsSkills
Ctrl K
AO
+1.2k Learners
LoginJoin ↗
RoadmapsSkills
AO
+1.2k Learners
Login
  • English
  • Français
  • Español
  • العربية
  • Português
  • Deutsch
  • 日本語
  • 中文
σμimplied vol · KKmax(S−K,0)udβρᵢⱼfat tailsPnL · drawdownyield · TdSₜ = μSₜ dt + σSₜ dWₜC = S·N(d₁) − K·e⁻ʳᵀ·N(d₂)σₚ² = wᵀΣwd₁ = [ln(S/K) + (r + σ²⁄2)T] ⁄ σ√T∂V/∂t + ½σ²S²·∂²V/∂S² + rS·∂V/∂S − rV = 0Sharpe = (Rₚ − R_f) ⁄ σₚVaR₉₉ = μ − 2.33σρ = Cov(X,Y) ⁄ σₓσᵧE[Rₚ] = Σ wᵢ·E[Rᵢ]φ(x) = e^(−x²/2) ⁄ √2πE[Rᵢ] = R_f + βᵢ(E[Rₘ] − R_f)dXₜ = θ(μ − Xₜ)dt + σ dWₜσ²ₜ = ω + α·ε²ₜ₋₁ + β·σ²ₜ₋₁C − P = S − K·e⁻ʳᵀΔ = ∂V/∂S Γ = ∂²V/∂S²p = (e^(rΔt) − d) ⁄ (u − d)rₜ = ln(Pₜ ⁄ Pₜ₋₁)E[Xₜ₊₁ | ℱₜ] = Xₜf* = (bp − q) ⁄ bz = (x − μ) ⁄ σP(N=k) = λᵏe^(−λ) ⁄ k!Θ = −∂V/∂Tν = S·φ(d₁)·√Tmin wᵀΣw s.t. wᵀμ = μ*∫Σ∂∞λ
uantCraft AcademyRoadmapsSkills
Ctrl K
AO
+1.2k Learners
LoginJoin ↗
RoadmapsSkills
AO
+1.2k Learners
Login
  • English
  • Français
  • Español
  • العربية
  • Português
  • Deutsch
  • 日本語
  • 中文
BLAS and LAPACK skill›Module 2 · Solving and decomposing›Topic 2.3·~55 minAdvanced

Conditioning: when a solution can be trusted

Skill positionModule 2 of 4 · Topic 3 of 3
2.3 Conditioning: when a solution can be trusted
—Lab score— / 100Total XP—Streak—
Lecture0% read

3 min readCode examples: 3

  1. 01The idea in plain words
  2. 02Five stocks and a twin
  3. 03What the tasks ask

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?

01

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.

cond(A) = |A|₁ · |A⁻¹|₁ digits you can trust ≈ 16 − log₁₀ cond(A)(1)|M|₁ is the largest column sum of absolute values. The rule of thumb is for double precision.
02

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.

main.pyPython
# 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
  1. 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).
main.pyPython
# 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
  1. 1Each step of 100 in eps multiplies the condition number by 10⁴ and the error follows it up. At 10⁻⁸ the computed weights are off by about 46%.
A twin stock with shrinking noiseSix assets, 250 days, known true solution
noise 10⁻² : condition number9.0e411 digits left
noise 10⁻⁴ : condition number9.0e87 digits left
noise 10⁻⁶ : forward error2.9e-53 digits left
noise 10⁻⁸ : forward error0.46nothing left
Digits of the answer you can trust
noise 114.8
noise 10⁻²11
noise 10⁻⁴7
noise 10⁻⁶3
noise 10⁻⁸0
Check before you trust

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.

main.pyPython
# 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
  1. 1The function refuses before factoring anything, so the 3 digits of an answer from this matrix are never used.
03

What the tasks ask

  1. 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. 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. 3
    `solve_checked`Put them together: measure, refuse above the limit with the exact message, otherwise factor and solve.

Key takeaways

  • Accuracy is a property of the matrix No algorithm can rescue a badly conditioned problem: the condition number sets the digits you can trust.
  • Near-duplicate assets are the usual cause Each factor of 100 less noise between two assets costs about 4 digits. Look at the condition number before you trust a minimum-variance portfolio.
  • Trusted now, but how fast? Next: threads. One BLAS call can use several cores, and several calls that each do can fight each other.
Quick check
A matrix has condition number 10¹⁰ and you solve in double precision. About how many digits of the answer can you trust?
Pick an answer.

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
Explorer · lab-6-1
▾ lab-6-1
…
bash · in-browser
learner@quantcraft:~/lab-6-1$
Python not loaded yetPythonLn 1, Col 1Spaces: 4UTF-8

Reset this topic?

Everything in lab-6-1 goes back to the starter files and the terminal is cleared. Your current solution will be lost — this can't be undone.

Will be reset
  • main.py → starter code
  • Terminal history and test output
  • Unsubmitted changes
Will be kept
  • Best lab score
  • Your XP and streak
  • Lecture progress and notes