import%20marimo%0A%0A__generated_with%20%3D%20%220.24.0%22%0Aapp%20%3D%20marimo.App(width%3D%22medium%22)%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20import%20marimo%20as%20mo%0A%20%20%20%20import%20numpy%20as%20np%0A%20%20%20%20import%20pandas%20as%20pd%0A%20%20%20%20import%20plotly.graph_objects%20as%20go%0A%20%20%20%20from%20plotly.subplots%20import%20make_subplots%0A%0A%20%20%20%20return%20go%2C%20make_subplots%2C%20mo%2C%20np%2C%20pd%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%20Note%2009%3A%20Condition%20Number%2C%20Matrix%20Sensitivity%2C%20and%20Multicollinearity%0A%0A%20%20%20%20%26larr%3B%20Previous%20Note%3A%20%5B08%20Matrix%20Calculus%5D(08_matrix_calculus_short.py)%20%7C%20Next%20Note%3A%20%5B10%20Chebyshev%20Inequality%5D(10_chebyshev_inequality.py)%20%26rarr%3B%0A%0A%20%20%20%20---%0A%0A%20%20%20%20%23%23%20%5Ba%5D%20Why%20do%20you%20need%20to%20know%20these%20concepts%3F%0A%0A%20%20%20%20Numerical%20computation%20is%20fundamentally%20vulnerable%20to%20error%20amplification.%20In%20theoretical%20mathematics%2C%20a%20matrix%20is%20either%20invertible%20or%20singular.%20In%20numerical%20computing%20and%20applied%20data%20science%2C%20however%2C%20matrices%20exist%20along%20a%20continuum%20of%20sensitivity%20measured%20by%20the%20**condition%20number**%20%24%5Ckappa(%5Cmathbf%7BA%7D)%24.%0A%0A%20%20%20%20Understanding%20condition%20numbers%20and%20multicollinearity%20is%20essential%20across%20multiple%20core%20areas%3A%0A%20%20%20%201.%20**Numerical%20Stability%20of%20Linear%20Solvers%20and%20Inverses**%3A%20When%20solving%20%24%5Cmathbf%7BA%7D%5Cmathbf%7Bx%7D%20%3D%20%5Cmathbf%7Bb%7D%24%2C%20machine%20round-off%20or%20small%20measurement%20noise%20in%20%24%5Cmathbf%7Bb%7D%24%20is%20amplified%20by%20up%20to%20a%20factor%20of%20%24%5Ckappa(%5Cmathbf%7BA%7D)%24%20in%20the%20solution%20%24%5Cmathbf%7Bx%7D%24.%20If%20%24%5Ckappa(%5Cmathbf%7BA%7D)%20%5Capprox%2010%5Ek%24%2C%20one%20can%20lose%20up%20to%20%24k%24%20significant%20digits%20of%20floating-point%20precision.%0A%20%20%20%202.%20**Multicollinearity%20in%20Regression%20and%20Inference**%3A%20When%20predictor%20columns%20in%20a%20feature%20matrix%20%24%5Cmathbf%7BX%7D%24%20are%20nearly%20linearly%20dependent%2C%20the%20Gram%20matrix%20%24%5Cmathbf%7BX%7D%5ET%20%5Cmathbf%7BX%7D%24%20becomes%20ill-conditioned.%20The%20parameter%20covariance%20matrix%20is%20%24%5Ctext%7BVar%7D(%5Chat%7B%5Cboldsymbol%7B%5Cbeta%7D%7D)%20%3D%20%5Csigma%5E2%20(%5Cmathbf%7BX%7D%5ET%20%5Cmathbf%7BX%7D)%5E%7B-1%7D%24.%20High%20condition%20numbers%20cause%20parameter%20variances%20and%20standard%20errors%20to%20explode%2C%20flipping%20regression%20signs%20and%20destroying%20the%20reliability%20of%20hypothesis%20testing%20(%24p%24-values%20and%20confidence%20intervals).%0A%20%20%20%203.%20**Condition%20Squaring%20in%20Normal%20Equations**%3A%20The%20condition%20number%20of%20the%20Gram%20matrix%20is%20squared%3A%20%24%5Ckappa_2(%5Cmathbf%7BX%7D%5ET%20%5Cmathbf%7BX%7D)%20%3D%20(%5Ckappa_2(%5Cmathbf%7BX%7D))%5E2%24.%20A%20feature%20matrix%20with%20a%20manageable%20condition%20number%20of%20%2410%5E4%24%20produces%20a%20Gram%20matrix%20with%20%24%5Ckappa%20%3D%2010%5E8%24%2C%20consuming%20half%20of%20standard%2064-bit%20double-precision%20digits.%20This%20explains%20why%20orthogonal%20factorizations%20(QR%20decomposition%2C%20SVD)%20are%20preferred%20over%20explicit%20matrix%20inversion.%0A%20%20%20%204.%20**Optimization%20Landscapes%20and%20Convergence%20Rates**%3A%20In%20first-order%20optimization%20(Gradient%20Descent)%2C%20the%20convergence%20speed%20on%20a%20quadratic%20loss%20is%20bounded%20by%20%24(%5Cfrac%7B%5Ckappa%20-%201%7D%7B%5Ckappa%20%2B%201%7D)%5E2%24%2C%20where%20%24%5Ckappa%24%20is%20the%20condition%20number%20of%20the%20Hessian%20%24%5Cmathbf%7BH%7D%20%3D%20%5Cnabla%5E2%20%5Cmathcal%7BL%7D%24.%20High%20condition%20numbers%20create%20steep%2C%20narrow%20canyons%20where%20gradients%20oscillate%20orthogonally%20to%20the%20descent%20direction.%0A%20%20%20%205.%20**Tikhonov%20%2F%20Ridge%20Regularization**%3A%20Adding%20an%20%24L_2%24%20penalty%20%24%5Clambda%20%5C%7C%5Cmathbf%7Bw%7D%5C%7C_2%5E2%24%20alters%20the%20spectrum%20to%20%24%5Cmathbf%7BX%7D%5ET%20%5Cmathbf%7BX%7D%20%2B%20%5Clambda%20%5Cmathbf%7BI%7D%24%2C%20shrinking%20the%20condition%20number%20from%20%24%5Cfrac%7B%5Csigma_1%5E2%7D%7B%5Csigma_p%5E2%7D%24%20to%20%24%5Cfrac%7B%5Csigma_1%5E2%20%2B%20%5Clambda%7D%7B%5Csigma_p%5E2%20%2B%20%5Clambda%7D%24%20and%20restoring%20numerical%20stability.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20---%0A%0A%20%20%20%20%23%23%20%5Bb%5D%20Concept%20explanation%20with%20their%20role%20in%20ML%2FAI%2FStats%3F%0A%0A%20%20%20%20%23%23%23%20Definition%20of%20the%20Condition%20Number%0A%0A%20%20%20%20For%20an%20invertible%20square%20matrix%20%24%5Cmathbf%7BA%7D%20%5Cin%20%5Cmathbb%7BR%7D%5E%7Bn%20%5Ctimes%20n%7D%24%20under%20an%20operator%20matrix%20norm%20%24%5C%7C%5Ccdot%5C%7C%24%2C%20the%20**condition%20number**%20is%20defined%20as%3A%0A%0A%20%20%20%20%24%24%0A%20%20%20%20%5Ckappa(%5Cmathbf%7BA%7D)%20%3D%20%5C%7C%5Cmathbf%7BA%7D%5C%7C%20%5Ccdot%20%5C%7C%5Cmathbf%7BA%7D%5E%7B-1%7D%5C%7C%0A%20%20%20%20%24%24%0A%0A%20%20%20%20Under%20the%20standard%20spectral%20norm%20(induced%20%24L_2%24%20norm)%2C%20%24%5C%7C%5Cmathbf%7BA%7D%5C%7C_2%20%3D%20%5Csigma_%7B%5Cmax%7D(%5Cmathbf%7BA%7D)%24%20and%20%24%5C%7C%5Cmathbf%7BA%7D%5E%7B-1%7D%5C%7C_2%20%3D%201%20%2F%20%5Csigma_%7B%5Cmin%7D(%5Cmathbf%7BA%7D)%24%2C%20where%20%24%5Csigma_%7B%5Cmax%7D%24%20and%20%24%5Csigma_%7B%5Cmin%7D%24%20are%20the%20largest%20and%20smallest%20singular%20values%20of%20%24%5Cmathbf%7BA%7D%24%3A%0A%0A%20%20%20%20%24%24%0A%20%20%20%20%5Ckappa_2(%5Cmathbf%7BA%7D)%20%3D%20%5Cfrac%7B%5Csigma_%7B%5Cmax%7D(%5Cmathbf%7BA%7D)%7D%7B%5Csigma_%7B%5Cmin%7D(%5Cmathbf%7BA%7D)%7D%0A%20%20%20%20%24%24%0A%0A%20%20%20%20For%20a%20rectangular%20matrix%20%24%5Cmathbf%7BX%7D%20%5Cin%20%5Cmathbb%7BR%7D%5E%7Bm%20%5Ctimes%20n%7D%24%20with%20%24m%20%5Cgeq%20n%24%20and%20full%20column%20rank%2C%20the%202-norm%20condition%20number%20is%20defined%20analogously%20via%20its%20non-zero%20extreme%20singular%20values%3A%0A%0A%20%20%20%20%24%24%0A%20%20%20%20%5Ckappa_2(%5Cmathbf%7BX%7D)%20%3D%20%5Cfrac%7B%5Csigma_%7B%5Cmax%7D(%5Cmathbf%7BX%7D)%7D%7B%5Csigma_%7B%5Cmin%7D(%5Cmathbf%7BX%7D)%7D%0A%20%20%20%20%24%24%0A%0A%20%20%20%20Since%20singular%20values%20are%20non-negative%20and%20%24%5Csigma_%7B%5Cmax%7D%20%5Cgeq%20%5Csigma_%7B%5Cmin%7D%24%2C%20we%20always%20have%20%24%5Ckappa_2(%5Cmathbf%7BA%7D)%20%5Cgeq%201%24.%20An%20orthogonal%20matrix%20has%20all%20singular%20values%20equal%20to%20%241%24%2C%20yielding%20the%20optimal%20condition%20number%20%24%5Ckappa_2(%5Cmathbf%7BQ%7D)%20%3D%201%24.%0A%0A%20%20%20%20---%0A%0A%20%20%20%20%23%23%23%20The%20Universal%20Perturbation%20Bound%0A%0A%20%20%20%20Consider%20the%20linear%20system%20%24%5Cmathbf%7BA%7D%5Cmathbf%7Bx%7D%20%3D%20%5Cmathbf%7Bb%7D%24.%20Suppose%20the%20right-hand%20side%20vector%20is%20subject%20to%20perturbation%20%24%5CDelta%20%5Cmathbf%7Bb%7D%24%2C%20producing%20a%20perturbed%20solution%20%24%5Cmathbf%7Bx%7D%20%2B%20%5CDelta%20%5Cmathbf%7Bx%7D%24%3A%0A%0A%20%20%20%20%24%24%0A%20%20%20%20%5Cmathbf%7BA%7D(%5Cmathbf%7Bx%7D%20%2B%20%5CDelta%5Cmathbf%7Bx%7D)%20%3D%20%5Cmathbf%7Bb%7D%20%2B%20%5CDelta%5Cmathbf%7Bb%7D%20%5Cimplies%20%5Cmathbf%7BA%7D%5CDelta%5Cmathbf%7Bx%7D%20%3D%20%5CDelta%5Cmathbf%7Bb%7D%20%5Cimplies%20%5CDelta%5Cmathbf%7Bx%7D%20%3D%20%5Cmathbf%7BA%7D%5E%7B-1%7D%5CDelta%5Cmathbf%7Bb%7D%0A%20%20%20%20%24%24%0A%0A%20%20%20%20Taking%20vector%20norms%3A%0A%0A%20%20%20%20%24%24%0A%20%20%20%20%5C%7C%5CDelta%5Cmathbf%7Bx%7D%5C%7C%20%5Cleq%20%5C%7C%5Cmathbf%7BA%7D%5E%7B-1%7D%5C%7C%20%5C%7C%5CDelta%5Cmathbf%7Bb%7D%5C%7C%0A%20%20%20%20%24%24%0A%0A%20%20%20%20Simultaneously%2C%20from%20%24%5Cmathbf%7Bb%7D%20%3D%20%5Cmathbf%7BA%7D%5Cmathbf%7Bx%7D%24%2C%20submultiplicativity%20gives%20%24%5C%7C%5Cmathbf%7Bb%7D%5C%7C%20%5Cleq%20%5C%7C%5Cmathbf%7BA%7D%5C%7C%20%5C%7C%5Cmathbf%7Bx%7D%5C%7C%24%2C%20which%20is%20equivalent%20to%3A%0A%0A%20%20%20%20%24%24%0A%20%20%20%20%5Cfrac%7B1%7D%7B%5C%7C%5Cmathbf%7Bx%7D%5C%7C%7D%20%5Cleq%20%5Cfrac%7B%5C%7C%5Cmathbf%7BA%7D%5C%7C%7D%7B%5C%7C%5Cmathbf%7Bb%7D%5C%7C%7D%0A%20%20%20%20%24%24%0A%0A%20%20%20%20Multiplying%20these%20two%20inequalities%20yields%20the%20**relative%20perturbation%20bound**%3A%0A%0A%20%20%20%20%24%24%0A%20%20%20%20%5Cfrac%7B%5C%7C%5CDelta%5Cmathbf%7Bx%7D%5C%7C%7D%7B%5C%7C%5Cmathbf%7Bx%7D%5C%7C%7D%20%5Cleq%20%5Ckappa(%5Cmathbf%7BA%7D)%20%5Cfrac%7B%5C%7C%5CDelta%5Cmathbf%7Bb%7D%5C%7C%7D%7B%5C%7C%5Cmathbf%7Bb%7D%5C%7C%7D%0A%20%20%20%20%24%24%0A%0A%20%20%20%20This%20inequality%20proves%20that%20%24%5Ckappa(%5Cmathbf%7BA%7D)%24%20acts%20as%20the%20maximum%20relative%20error%20amplification%20factor.%20If%20%24%5CDelta%20%5Cmathbf%7Bb%7D%24%20aligns%20with%20the%20singular%20vector%20corresponding%20to%20%24%5Csigma_%7B%5Cmin%7D%24%2C%20the%20upper%20bound%20is%20achieved%20with%20exact%20equality.%0A%0A%20%20%20%20---%0A%0A%20%20%20%20%23%23%23%20Condition%20Number%20Squaring%20in%20the%20Normal%20Equations%0A%0A%20%20%20%20In%20Ordinary%20Least%20Squares%2C%20the%20analytical%20parameter%20estimate%20is%3A%0A%0A%20%20%20%20%24%24%0A%20%20%20%20%5Chat%7B%5Cboldsymbol%7B%5Cbeta%7D%7D%20%3D%20(%5Cmathbf%7BX%7D%5ET%20%5Cmathbf%7BX%7D)%5E%7B-1%7D%20%5Cmathbf%7BX%7D%5ET%20%5Cmathbf%7By%7D%0A%20%20%20%20%24%24%0A%0A%20%20%20%20Evaluating%20the%20condition%20number%20of%20the%20Gram%20matrix%20%24%5Cmathbf%7BX%7D%5ET%20%5Cmathbf%7BX%7D%24%3A%0A%0A%20%20%20%20%24%24%0A%20%20%20%20%5Ckappa_2(%5Cmathbf%7BX%7D%5ET%20%5Cmathbf%7BX%7D)%20%3D%20%5Cfrac%7B%5Clambda_%7B%5Cmax%7D(%5Cmathbf%7BX%7D%5ET%20%5Cmathbf%7BX%7D)%7D%7B%5Clambda_%7B%5Cmin%7D(%5Cmathbf%7BX%7D%5ET%20%5Cmathbf%7BX%7D)%7D%20%3D%20%5Cfrac%7B%5Csigma_%7B%5Cmax%7D%5E2(%5Cmathbf%7BX%7D)%7D%7B%5Csigma_%7B%5Cmin%7D%5E2(%5Cmathbf%7BX%7D)%7D%20%3D%20(%5Ckappa_2(%5Cmathbf%7BX%7D))%5E2%0A%20%20%20%20%24%24%0A%0A%20%20%20%20This%20quadratic%20squaring%20is%20severe%3A%0A%20%20%20%20*%20If%20%24%5Cmathbf%7BX%7D%24%20has%20condition%20number%20%2410%5E3%24%2C%20the%20Gram%20matrix%20has%20condition%20number%20%2410%5E6%24.%0A%20%20%20%20*%20If%20%24%5Cmathbf%7BX%7D%24%20has%20condition%20number%20%2410%5E8%24%2C%20%24%5Cmathbf%7BX%7D%5ET%20%5Cmathbf%7BX%7D%24%20has%20condition%20number%20%2410%5E%7B16%7D%24%2C%20which%20matches%20the%20limit%20of%20IEEE%20754%20double%20precision%20(%242%5E%7B-53%7D%20%5Capprox%201.11%20%5Ctimes%2010%5E%7B-16%7D%24).%20In%20that%20scenario%2C%20inverting%20%24%5Cmathbf%7BX%7D%5ET%20%5Cmathbf%7BX%7D%24%20results%20in%20total%20loss%20of%20numerical%20significance.%0A%0A%20%20%20%20---%0A%0A%20%20%20%20%23%23%23%20Multicollinearity%20Diagnostics%20in%20Linear%20Models%0A%0A%20%20%20%20Multicollinearity%20arises%20when%20two%20or%20more%20feature%20columns%20in%20the%20design%20matrix%20%24%5Cmathbf%7BX%7D%24%20exhibit%20strong%20linear%20correlations.%0A%0A%20%20%20%20%23%23%23%23%201.%20Condition%20Index%20Diagnostic%0A%20%20%20%20*%20%24%5Ckappa(%5Cmathbf%7BX%7D)%20%3C%2010%24%3A%20Well-conditioned%20design%3B%20weak%20to%20no%20collinearity.%0A%20%20%20%20*%20%2410%20%5Cleq%20%5Ckappa(%5Cmathbf%7BX%7D)%20%5Cleq%2030%24%3A%20Moderate%20collinearity%3B%20potential%20variance%20inflation.%0A%20%20%20%20*%20%24%5Ckappa(%5Cmathbf%7BX%7D)%20%3E%2030%24%3A%20Severe%20multicollinearity%3B%20parameter%20estimates%20and%20standard%20errors%20are%20unstable.%0A%0A%20%20%20%20%23%23%23%23%202.%20Variance%20Inflation%20Factor%20(VIF)%0A%20%20%20%20For%20each%20predictor%20%24x_j%24%2C%20we%20regress%20%24x_j%24%20against%20all%20other%20%24p-1%24%20predictors%20in%20the%20dataset%2C%20obtaining%20the%20coefficient%20of%20determination%20%24R_j%5E2%24.%20The%20**Variance%20Inflation%20Factor**%20is%3A%0A%0A%20%20%20%20%24%24%0A%20%20%20%20%5Ctext%7BVIF%7D_j%20%3D%20%5Cfrac%7B1%7D%7B1%20-%20R_j%5E2%7D%0A%20%20%20%20%24%24%0A%0A%20%20%20%20The%20variance%20of%20the%20estimated%20regression%20coefficient%20%24%5Chat%7B%5Cbeta%7D_j%24%20can%20be%20decomposed%20into%3A%0A%0A%20%20%20%20%24%24%0A%20%20%20%20%5Ctext%7BVar%7D(%5Chat%7B%5Cbeta%7D_j)%20%3D%20%5Cfrac%7B%5Csigma%5E2%7D%7B(n-1)%20s_j%5E2%7D%20%5Ctimes%20%5Ctext%7BVIF%7D_j%0A%20%20%20%20%24%24%0A%0A%20%20%20%20where%20%24s_j%5E2%20%3D%20%5Cfrac%7B1%7D%7Bn-1%7D%20%5Csum_%7Bi%3D1%7D%5En%20(x_%7Bij%7D%20-%20%5Cbar%7Bx%7D_j)%5E2%24%20is%20the%20sample%20variance%20of%20feature%20%24x_j%24.%0A%20%20%20%20*%20If%20%24R_j%5E2%20%3D%200.90%24%2C%20%24%5Ctext%7BVIF%7D_j%20%3D%2010%24%20(the%20variance%20of%20%24%5Chat%7B%5Cbeta%7D_j%24%20is%20inflated%20%2410%5Ctimes%24%2C%20and%20standard%20error%20is%20inflated%20%24%5Csqrt%7B10%7D%20%5Capprox%203.16%5Ctimes%24).%0A%20%20%20%20*%20If%20%24R_j%5E2%20%3D%200.99%24%2C%20%24%5Ctext%7BVIF%7D_j%20%3D%20100%24%20(standard%20error%20inflated%20%2410%5Ctimes%24).%0A%20%20%20%20*%20Rule%20of%20thumb%3A%20%24%5Ctext%7BVIF%7D_j%20%3E%2010%24%20indicates%20critical%20multicollinearity%20requiring%20intervention.%0A%0A%20%20%20%20---%0A%0A%20%20%20%20%23%23%23%20Stabilization%20via%20Ridge%20Regularization%20(Tikhonov)%0A%0A%20%20%20%20Ridge%20regression%20modifies%20the%20objective%20by%20adding%20an%20%24L_2%24%20penalty%3A%0A%0A%20%20%20%20%24%24%0A%20%20%20%20%5Cmathcal%7BL%7D_%7B%5Ctext%7Bridge%7D%7D(%5Cboldsymbol%7B%5Cbeta%7D)%20%3D%20%5C%7C%5Cmathbf%7By%7D%20-%20%5Cmathbf%7BX%7D%5Cboldsymbol%7B%5Cbeta%7D%5C%7C_2%5E2%20%2B%20%5Clambda%20%5C%7C%5Cboldsymbol%7B%5Cbeta%7D%5C%7C_2%5E2%0A%20%20%20%20%24%24%0A%0A%20%20%20%20The%20regularized%20closed-form%20estimator%20is%3A%0A%0A%20%20%20%20%24%24%0A%20%20%20%20%5Chat%7B%5Cboldsymbol%7B%5Cbeta%7D%7D_%7B%5Ctext%7Bridge%7D%7D%20%3D%20(%5Cmathbf%7BX%7D%5ET%20%5Cmathbf%7BX%7D%20%2B%20%5Clambda%20%5Cmathbf%7BI%7D)%5E%7B-1%7D%20%5Cmathbf%7BX%7D%5ET%20%5Cmathbf%7By%7D%0A%20%20%20%20%24%24%0A%0A%20%20%20%20Since%20the%20eigenvalues%20of%20%24%5Cmathbf%7BX%7D%5ET%20%5Cmathbf%7BX%7D%20%2B%20%5Clambda%20%5Cmathbf%7BI%7D%24%20are%20%24%5Csigma_i%5E2%20%2B%20%5Clambda%24%2C%20the%20regularized%20condition%20number%20becomes%3A%0A%0A%20%20%20%20%24%24%0A%20%20%20%20%5Ckappa_2(%5Cmathbf%7BX%7D%5ET%20%5Cmathbf%7BX%7D%20%2B%20%5Clambda%20%5Cmathbf%7BI%7D)%20%3D%20%5Cfrac%7B%5Csigma_%7B%5Cmax%7D%5E2%20%2B%20%5Clambda%7D%7B%5Csigma_%7B%5Cmin%7D%5E2%20%2B%20%5Clambda%7D%0A%20%20%20%20%24%24%0A%0A%20%20%20%20Even%20for%20an%20ill-conditioned%20or%20rank-deficient%20matrix%20where%20%24%5Csigma_%7B%5Cmin%7D%20%5Capprox%200%24%2C%20choosing%20%24%5Clambda%20%3E%200%24%20guarantees%20that%20the%20denominator%20is%20at%20least%20%24%5Clambda%24%2C%20bounding%20the%20condition%20number%20and%20eliminating%20parameter%20variance%20explosion.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20---%0A%0A%20%20%20%20%23%23%20%5Bc%5D%20Interactive%20Visualizations%3A%20Geometric%20Sensitivity%20and%20Regularization%20Dynamics%0A%0A%20%20%20%20The%20interactive%20subplots%20below%20display%20the%20dual%20facets%20of%20conditioning%3A%0A%20%20%20%20*%20**Left%20Panel**%3A%20Geometric%20demonstration%20of%20error%20magnification%20in%20an%20ill-conditioned%20linear%20system%20(%24%5Ckappa%20%5Capprox%2082%24).%20Two%20nearly%20parallel%20lines%20intersect%20at%20%24%5Cmathbf%7Bx%7D%5E*%20%3D%20%5B1.0%2C%201.0%5D%5ET%24.%20An%20imperceptible%20%24%2B2.4%5C%25%24%20perturbation%20to%20%24b_2%24%20causes%20the%20intersection%20point%20to%20fly%20across%20the%20coordinate%20plane%20to%20%24%5Cmathbf%7Bx%7D_%7B%5Ctext%7Bpert%7D%7D%20%3D%20%5B0.0%2C%202.0%5D%5ET%24%2C%20yielding%20a%20displacement%20%24%5C%7C%5CDelta%5Cmathbf%7Bx%7D%5C%7C%20%3D%201.414%24%20(%2429%5Ctimes%24%20amplification).%0A%20%20%20%20*%20**Right%20Panel**%3A%20The%20stabilizing%20power%20of%20Ridge%20Regularization.%20As%20the%20regularization%20penalty%20%24%5Clambda%24%20increases%20on%20a%20logarithmic%20scale%2C%20the%20condition%20number%20%24%5Ckappa_2(%5Cmathbf%7BX%7D%5ET%20%5Cmathbf%7BX%7D%20%2B%20%5Clambda%20%5Cmathbf%7BI%7D)%24%20collapses%20from%20over%20%24900%24%20down%20below%20the%20critical%20threshold%20of%20%24%5Ckappa%20%3D%2030%24%2C%20restoring%20numerical%20stability.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(go%2C%20make_subplots%2C%20np)%3A%0A%20%20%20%20%23%20Left%20Panel%3A%20Ill-conditioned%202x2%20linear%20system%0A%20%20%20%20%23%20Line%201%3A%20x1%20%2B%20x2%20%3D%202.0%20-%3E%20x2%20%3D%202.0%20-%20x1%0A%20%20%20%20%23%20Line%202%3A%20x1%20%2B%201.05%20x2%20%3D%202.05%20-%3E%20x2%20%3D%20(2.05%20-%20x1)%20%2F%201.05%0A%20%20%20%20%23%20Perturbed%20Line%202%3A%20x1%20%2B%201.05%20x2%20%3D%202.10%20(%2B0.05%20perturbation%2C%20%2B2.4%25)%0A%20%20%20%20a_matrix%20%3D%20np.array(%5B%5B1.0%2C%201.0%5D%2C%20%5B1.0%2C%201.05%5D%5D)%0A%20%20%20%20b_vector%20%3D%20np.array(%5B2.0%2C%202.05%5D)%0A%20%20%20%20delta_b_vec%20%3D%20np.array(%5B0.0%2C%200.05%5D)%0A%0A%20%20%20%20cond_a%20%3D%20float(np.linalg.cond(a_matrix))%0A%20%20%20%20x_exact%20%3D%20np.linalg.solve(a_matrix%2C%20b_vector)%0A%20%20%20%20x_perturbed%20%3D%20np.linalg.solve(a_matrix%2C%20b_vector%20%2B%20delta_b_vec)%0A%20%20%20%20delta_x_vec%20%3D%20x_perturbed%20-%20x_exact%0A%20%20%20%20norm_delta_x%20%3D%20float(np.linalg.norm(delta_x_vec))%0A%0A%20%20%20%20x_domain%20%3D%20np.linspace(-1.5%2C%203.5%2C%20200)%0A%20%20%20%20line1_y%20%3D%202.0%20-%20x_domain%0A%20%20%20%20line2_y%20%3D%20(2.05%20-%20x_domain)%20%2F%201.05%0A%20%20%20%20line2_perturbed_y%20%3D%20(2.10%20-%20x_domain)%20%2F%201.05%0A%0A%20%20%20%20%23%20Right%20Panel%3A%20Ridge%20Regularization%20Condition%20Number%20Curve%0A%20%20%20%20%23%20Typical%20collinear%20dataset%20with%20sigma_max%5E2%20%3D%2080.0%2C%20sigma_min%5E2%20%3D%200.088%20(cond%20%3D%20909)%0A%20%20%20%20sigma_max_squared%20%3D%2080.0%0A%20%20%20%20sigma_min_squared%20%3D%200.088%0A%20%20%20%20lambda_grid%20%3D%20np.logspace(-4%2C%203%2C%20200)%0A%20%20%20%20cond_ridge_curve%20%3D%20(sigma_max_squared%20%2B%20lambda_grid)%20%2F%20(sigma_min_squared%20%2B%20lambda_grid)%0A%0A%20%20%20%20fig%20%3D%20make_subplots(%0A%20%20%20%20%20%20%20%20rows%3D1%2C%0A%20%20%20%20%20%20%20%20cols%3D2%2C%0A%20%20%20%20%20%20%20%20subplot_titles%3D%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20f%22Error%20Magnification%20in%20Ill-Conditioned%20System%20(%CE%BA%3D%7Bcond_a%3A.1f%7D)%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22Ridge%20Regularization%3A%20Condition%20Number%20vs%20%CE%BB%22%2C%0A%20%20%20%20%20%20%20%20%5D%2C%0A%20%20%20%20)%0A%0A%20%20%20%20%23%20Left%3A%20Line%201%0A%20%20%20%20fig.add_trace(%0A%20%20%20%20%20%20%20%20go.Scatter(%0A%20%20%20%20%20%20%20%20%20%20%20%20x%3Dx_domain%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20y%3Dline1_y%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20mode%3D%22lines%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20line%3Ddict(color%3D%22%232563eb%22%2C%20width%3D2.5)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20name%3D%22Line%201%3A%20x%E2%82%81%20%2B%20x%E2%82%82%20%3D%202.0%22%2C%0A%20%20%20%20%20%20%20%20)%2C%0A%20%20%20%20%20%20%20%20row%3D1%2C%0A%20%20%20%20%20%20%20%20col%3D1%2C%0A%20%20%20%20)%0A%0A%20%20%20%20%23%20Left%3A%20Line%202%20(Original)%0A%20%20%20%20fig.add_trace(%0A%20%20%20%20%20%20%20%20go.Scatter(%0A%20%20%20%20%20%20%20%20%20%20%20%20x%3Dx_domain%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20y%3Dline2_y%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20mode%3D%22lines%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20line%3Ddict(color%3D%22%23dc2626%22%2C%20width%3D2.5)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20name%3D%22Line%202%3A%20x%E2%82%81%20%2B%201.05x%E2%82%82%20%3D%202.05%22%2C%0A%20%20%20%20%20%20%20%20)%2C%0A%20%20%20%20%20%20%20%20row%3D1%2C%0A%20%20%20%20%20%20%20%20col%3D1%2C%0A%20%20%20%20)%0A%0A%20%20%20%20%23%20Left%3A%20Line%202%20(Perturbed)%0A%20%20%20%20fig.add_trace(%0A%20%20%20%20%20%20%20%20go.Scatter(%0A%20%20%20%20%20%20%20%20%20%20%20%20x%3Dx_domain%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20y%3Dline2_perturbed_y%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20mode%3D%22lines%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20line%3Ddict(color%3D%22%23dc2626%22%2C%20width%3D2.0%2C%20dash%3D%22dash%22)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20name%3D%22Perturbed%20Line%202%20(%2B2.4%25%20%CE%94b)%22%2C%0A%20%20%20%20%20%20%20%20)%2C%0A%20%20%20%20%20%20%20%20row%3D1%2C%0A%20%20%20%20%20%20%20%20col%3D1%2C%0A%20%20%20%20)%0A%0A%20%20%20%20%23%20Left%3A%20Original%20Intersection%20Point%0A%20%20%20%20fig.add_trace(%0A%20%20%20%20%20%20%20%20go.Scatter(%0A%20%20%20%20%20%20%20%20%20%20%20%20x%3D%5Bx_exact%5B0%5D%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20y%3D%5Bx_exact%5B1%5D%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20mode%3D%22markers%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20marker%3Ddict(size%3D12%2C%20color%3D%22%2316a34a%22%2C%20symbol%3D%22circle%22)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20name%3D%22Original%20x*%20%3D%20%5B1.0%2C%201.0%5D%22%2C%0A%20%20%20%20%20%20%20%20)%2C%0A%20%20%20%20%20%20%20%20row%3D1%2C%0A%20%20%20%20%20%20%20%20col%3D1%2C%0A%20%20%20%20)%0A%0A%20%20%20%20%23%20Left%3A%20Perturbed%20Intersection%20Point%0A%20%20%20%20fig.add_trace(%0A%20%20%20%20%20%20%20%20go.Scatter(%0A%20%20%20%20%20%20%20%20%20%20%20%20x%3D%5Bx_perturbed%5B0%5D%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20y%3D%5Bx_perturbed%5B1%5D%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20mode%3D%22markers%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20marker%3Ddict(size%3D12%2C%20color%3D%22%23ea580c%22%2C%20symbol%3D%22diamond%22)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20name%3Df%22Perturbed%20x%20%3D%20%5B%7Bx_perturbed%5B0%5D%3A.1f%7D%2C%20%7Bx_perturbed%5B1%5D%3A.1f%7D%5D%22%2C%0A%20%20%20%20%20%20%20%20)%2C%0A%20%20%20%20%20%20%20%20row%3D1%2C%0A%20%20%20%20%20%20%20%20col%3D1%2C%0A%20%20%20%20)%0A%0A%20%20%20%20%23%20Left%3A%20Error%20Displacement%20Vector%20%CE%94x%0A%20%20%20%20fig.add_trace(%0A%20%20%20%20%20%20%20%20go.Scatter(%0A%20%20%20%20%20%20%20%20%20%20%20%20x%3D%5Bx_exact%5B0%5D%2C%20x_perturbed%5B0%5D%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20y%3D%5Bx_exact%5B1%5D%2C%20x_perturbed%5B1%5D%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20mode%3D%22lines%2Bmarkers%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20line%3Ddict(color%3D%22%237c3aed%22%2C%20width%3D3.5%2C%20dash%3D%22dot%22)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20marker%3Ddict(size%3D6%2C%20color%3D%22%237c3aed%22)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20name%3Df%22Amplified%20Error%20%7C%7C%CE%94x%7C%7C%3D%7Bnorm_delta_x%3A.2f%7D%22%2C%0A%20%20%20%20%20%20%20%20)%2C%0A%20%20%20%20%20%20%20%20row%3D1%2C%0A%20%20%20%20%20%20%20%20col%3D1%2C%0A%20%20%20%20)%0A%0A%20%20%20%20%23%20Right%3A%20Ridge%20Regularization%20Condition%20Number%20Curve%0A%20%20%20%20fig.add_trace(%0A%20%20%20%20%20%20%20%20go.Scatter(%0A%20%20%20%20%20%20%20%20%20%20%20%20x%3Dlambda_grid%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20y%3Dcond_ridge_curve%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20mode%3D%22lines%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20line%3Ddict(color%3D%22%232563eb%22%2C%20width%3D3.0)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20name%3D%22%CE%BA%E2%82%82(X%E1%B5%80X%20%2B%20%CE%BBI)%22%2C%0A%20%20%20%20%20%20%20%20)%2C%0A%20%20%20%20%20%20%20%20row%3D1%2C%0A%20%20%20%20%20%20%20%20col%3D2%2C%0A%20%20%20%20)%0A%0A%20%20%20%20%23%20Right%3A%20Severe%20Collinearity%20Threshold%20(%CE%BA%20%3D%2030)%0A%20%20%20%20fig.add_trace(%0A%20%20%20%20%20%20%20%20go.Scatter(%0A%20%20%20%20%20%20%20%20%20%20%20%20x%3D%5B1e-4%2C%201e3%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20y%3D%5B30.0%2C%2030.0%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20mode%3D%22lines%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20line%3Ddict(color%3D%22%23dc2626%22%2C%20width%3D2.0%2C%20dash%3D%22dash%22)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20name%3D%22Collinearity%20Threshold%20(%CE%BA%3D30)%22%2C%0A%20%20%20%20%20%20%20%20)%2C%0A%20%20%20%20%20%20%20%20row%3D1%2C%0A%20%20%20%20%20%20%20%20col%3D2%2C%0A%20%20%20%20)%0A%0A%20%20%20%20%23%20Right%3A%20Unregularized%20Baseline%20(%CE%BB%20%3D%200)%0A%20%20%20%20fig.add_trace(%0A%20%20%20%20%20%20%20%20go.Scatter(%0A%20%20%20%20%20%20%20%20%20%20%20%20x%3D%5B1e-4%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20y%3D%5Bsigma_max_squared%20%2F%20sigma_min_squared%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20mode%3D%22markers%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20marker%3Ddict(size%3D10%2C%20color%3D%22%23dc2626%22%2C%20symbol%3D%22x%22)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20name%3Df%22Unregularized%20OLS%20(%CE%BA%3D%7Bsigma_max_squared%20%2F%20sigma_min_squared%3A.0f%7D)%22%2C%0A%20%20%20%20%20%20%20%20)%2C%0A%20%20%20%20%20%20%20%20row%3D1%2C%0A%20%20%20%20%20%20%20%20col%3D2%2C%0A%20%20%20%20)%0A%0A%20%20%20%20axis_config_left%20%3D%20dict(%0A%20%20%20%20%20%20%20%20title%3D%22x%E2%82%81%22%2C%0A%20%20%20%20%20%20%20%20range%3D%5B-1.0%2C%203.0%5D%2C%0A%20%20%20%20%20%20%20%20zeroline%3DTrue%2C%0A%20%20%20%20%20%20%20%20zerolinecolor%3D%22%23cbd5e1%22%2C%0A%20%20%20%20%20%20%20%20gridcolor%3D%22%23f1f5f9%22%2C%0A%20%20%20%20)%0A%0A%20%20%20%20fig.update_layout(%0A%20%20%20%20%20%20%20%20template%3D%22plotly_white%22%2C%0A%20%20%20%20%20%20%20%20height%3D520%2C%0A%20%20%20%20%20%20%20%20margin%3Ddict(l%3D40%2C%20r%3D40%2C%20t%3D60%2C%20b%3D40)%2C%0A%20%20%20%20%20%20%20%20xaxis%3Daxis_config_left%2C%0A%20%20%20%20%20%20%20%20yaxis%3Ddict(%0A%20%20%20%20%20%20%20%20%20%20%20%20title%3D%22x%E2%82%82%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20range%3D%5B-0.5%2C%203.0%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20zeroline%3DTrue%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20zerolinecolor%3D%22%23cbd5e1%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20gridcolor%3D%22%23f1f5f9%22%2C%0A%20%20%20%20%20%20%20%20)%2C%0A%20%20%20%20%20%20%20%20xaxis2%3Ddict(type%3D%22log%22%2C%20title%3D%22Ridge%20Penalty%20%CE%BB%20(log%20scale)%22%2C%20gridcolor%3D%22%23f1f5f9%22)%2C%0A%20%20%20%20%20%20%20%20yaxis2%3Ddict(type%3D%22log%22%2C%20title%3D%22Condition%20Number%20%CE%BA%20(log%20scale)%22%2C%20gridcolor%3D%22%23f1f5f9%22)%2C%0A%20%20%20%20%20%20%20%20legend%3Ddict(orientation%3D%22h%22%2C%20yanchor%3D%22bottom%22%2C%20y%3D-0.26%2C%20xanchor%3D%22center%22%2C%20x%3D0.5)%2C%0A%20%20%20%20)%0A%20%20%20%20return%20(fig%2C)%0A%0A%0A%40app.cell%0Adef%20_(fig%2C%20mo)%3A%0A%20%20%20%20mo.ui.plotly(fig)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20---%0A%0A%20%20%20%20%23%23%20%5Bd%5D%20Code%20Examples%0A%0A%20%20%20%20%23%23%23%20Example%201%3A%20Numerical%20Verification%20of%20the%20Relative%20Perturbation%20Bound%0A%0A%20%20%20%20In%20this%20example%2C%20we%20construct%20an%20ill-conditioned%20matrix%20%24%5Cmathbf%7BA%7D%20%5Cin%20%5Cmathbb%7BR%7D%5E%7B4%20%5Ctimes%204%7D%24%20with%20a%20pre-specified%20condition%20number%20%24%5Ckappa_2(%5Cmathbf%7BA%7D)%20%3D%2010%2C000%24%20using%20Singular%20Value%20Decomposition%3A%0A%0A%20%20%20%20%24%24%0A%20%20%20%20%5Cmathbf%7BA%7D%20%3D%20%5Cmathbf%7BU%7D%20%5Cmathbf%7B%5CSigma%7D%20%5Cmathbf%7BV%7D%5ET%2C%20%5Cquad%20%5Cmathbf%7B%5CSigma%7D%20%3D%20%5Ctext%7Bdiag%7D(10.0%2C%202.0%2C%200.5%2C%200.001)%0A%20%20%20%20%24%24%0A%0A%20%20%20%20We%20apply%20five%20distinct%20perturbation%20vectors%20%24%5CDelta%5Cmathbf%7Bb%7D%24%20of%20varying%20magnitudes%20and%20orientations%2C%20solve%20%24%5Cmathbf%7BA%7D(%5Cmathbf%7Bx%7D%20%2B%20%5CDelta%5Cmathbf%7Bx%7D)%20%3D%20%5Cmathbf%7Bb%7D%20%2B%20%5CDelta%5Cmathbf%7Bb%7D%24%2C%20and%20verify%20that%20the%20empirical%20amplification%20factor%20never%20exceeds%20the%20theoretical%20condition%20number%3A%0A%0A%20%20%20%20%24%24%0A%20%20%20%20%5Ctext%7BAmplification%20Ratio%7D%20%3D%20%5Cfrac%7B%5C%7C%5CDelta%5Cmathbf%7Bx%7D%5C%7C%20%2F%20%5C%7C%5Cmathbf%7Bx%7D%5C%7C%7D%7B%5C%7C%5CDelta%5Cmathbf%7Bb%7D%5C%7C%20%2F%20%5C%7C%5Cmathbf%7Bb%7D%5C%7C%7D%20%5Cleq%20%5Ckappa_2(%5Cmathbf%7BA%7D)%0A%20%20%20%20%24%24%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(np)%3A%0A%20%20%20%20rng_p%20%3D%20np.random.default_rng(101)%0A%0A%20%20%20%20%23%20Construct%20ill-conditioned%204x4%20matrix%20with%20kappa%20%3D%2010%2C000%0A%20%20%20%20q_left%2C%20_%20%3D%20np.linalg.qr(rng_p.standard_normal((4%2C%204)))%0A%20%20%20%20q_right%2C%20_%20%3D%20np.linalg.qr(rng_p.standard_normal((4%2C%204)))%0A%20%20%20%20singular_values%20%3D%20np.array(%5B10.0%2C%202.0%2C%200.5%2C%200.001%5D)%0A%20%20%20%20a_ill%20%3D%20q_left%20%40%20np.diag(singular_values)%20%40%20q_right.T%0A%20%20%20%20kappa_exact%20%3D%20float(np.linalg.cond(a_ill))%0A%0A%20%20%20%20%23%20True%20system%20Ax%20%3D%20b%0A%20%20%20%20x_ground_truth%20%3D%20np.array(%5B1.0%2C%201.0%2C%201.0%2C%201.0%5D)%0A%20%20%20%20b_exact%20%3D%20a_ill%20%40%20x_ground_truth%0A%20%20%20%20norm_b%20%3D%20float(np.linalg.norm(b_exact))%0A%20%20%20%20norm_x%20%3D%20float(np.linalg.norm(x_ground_truth))%0A%0A%20%20%20%20perturbation_scenarios%20%3D%20%5B%0A%20%20%20%20%20%20%20%20(%22Random%20Noise%201%20(1e-4)%22%2C%20rng_p.standard_normal(4)%20*%201e-4)%2C%0A%20%20%20%20%20%20%20%20(%22Random%20Noise%202%20(1e-5)%22%2C%20rng_p.standard_normal(4)%20*%201e-5)%2C%0A%20%20%20%20%20%20%20%20(%22Random%20Noise%203%20(1e-3)%22%2C%20rng_p.standard_normal(4)%20*%201e-3)%2C%0A%20%20%20%20%20%20%20%20(%22Aligned%20with%20Largest%20Mode%20(u%E2%82%81)%22%2C%20q_left%5B%3A%2C%200%5D%20*%201e-4)%2C%0A%20%20%20%20%20%20%20%20(%22Worst-Case%20Alignment%20(u%E2%82%84)%22%2C%20q_left%5B%3A%2C%203%5D%20*%201e-4)%2C%0A%20%20%20%20%5D%0A%0A%20%20%20%20bound_verification_records%20%3D%20%5B%5D%0A%0A%20%20%20%20for%20name%2C%20delta_b%20in%20perturbation_scenarios%3A%0A%20%20%20%20%20%20%20%20rel_delta_b%20%3D%20float(np.linalg.norm(delta_b)%20%2F%20norm_b)%0A%20%20%20%20%20%20%20%20x_solved%20%3D%20np.linalg.solve(a_ill%2C%20b_exact%20%2B%20delta_b)%0A%20%20%20%20%20%20%20%20delta_x%20%3D%20x_solved%20-%20x_ground_truth%0A%20%20%20%20%20%20%20%20rel_delta_x%20%3D%20float(np.linalg.norm(delta_x)%20%2F%20norm_x)%0A%20%20%20%20%20%20%20%20amplification%20%3D%20rel_delta_x%20%2F%20rel_delta_b%20if%20rel_delta_b%20%3E%200%20else%200.0%0A%20%20%20%20%20%20%20%20bound_satisfied%20%3D%20amplification%20%3C%3D%20(kappa_exact%20%2B%201e-7)%0A%0A%20%20%20%20%20%20%20%20bound_verification_records.append(%0A%20%20%20%20%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22Perturbation%20Type%22%3A%20name%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22Relative%20%7C%7C%CE%94b%7C%7C%2F%7C%7Cb%7C%7C%22%3A%20f%22%7Brel_delta_b%3A.2e%7D%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22Relative%20%7C%7C%CE%94x%7C%7C%2F%7C%7Cx%7C%7C%22%3A%20f%22%7Brel_delta_x%3A.2e%7D%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22Amplification%20Factor%22%3A%20f%22%7Bamplification%3A.1f%7D%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22Theoretical%20Bound%20%CE%BA%22%3A%20f%22%7Bkappa_exact%3A.1f%7D%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22Bound%20Satisfied%22%3A%20str(bound_satisfied)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20)%0A%20%20%20%20return%20(bound_verification_records%2C)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(bound_verification_records%2C%20mo%2C%20pd)%3A%0A%20%20%20%20df_perturbation%20%3D%20pd.DataFrame(bound_verification_records)%0A%20%20%20%20mo.ui.table(df_perturbation)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20---%0A%0A%20%20%20%20%23%23%23%20Example%202%3A%20Multicollinearity%20Diagnostics%2C%20Variance%20Inflation%2C%20and%20Ridge%20Healing%0A%0A%20%20%20%20In%20this%20example%2C%20we%20generate%20a%20synthetic%20regression%20dataset%20(%24N%20%3D%20100%24)%20with%20predictors%3A%0A%20%20%20%20*%20%24x_1%20%5Csim%20%5Cmathcal%7BN%7D(50%2C%2015%5E2)%24%0A%20%20%20%20*%20%24x_2%20%5Csim%20%5Cmathcal%7BN%7D(30%2C%2010%5E2)%24%0A%20%20%20%20*%20%24x_3%20%3D%200.9%20x_1%20%2B%200.1%20x_2%20%2B%20%5Cepsilon%24%20(an%20artificial%20linear%20duplicate)%0A%0A%20%20%20%20The%20true%20data-generating%20process%20does%20not%20depend%20on%20%24x_3%24%3A%0A%0A%20%20%20%20%24%24%0A%20%20%20%20y%20%3D%205.0%20%2B%202.5%20x_1%20-%201.8%20x_2%20%2B%20%5Cmathcal%7BN%7D(0%2C%201)%0A%20%20%20%20%24%24%0A%0A%20%20%20%20We%20compute%20the%20Variance%20Inflation%20Factors%20(VIF)%20and%20compare%20three%20models%3A%0A%20%20%20%201.%20**Full%20Collinear%20OLS%20(%24x_1%2C%20x_2%2C%20x_3%24)**%3A%20Suffers%20from%20variance%20explosion%3B%20standard%20errors%20blow%20up%20and%20estimated%20coefficients%20become%20unreliable.%0A%20%20%20%202.%20**Pruned%20OLS%20(%24x_1%2C%20x_2%24)**%3A%20Dropping%20the%20redundant%20collinear%20feature%20drops%20VIF%20to%20%24%5Capprox%201%24%2C%20eliminates%20the%20variance%20explosion%2C%20and%20estimates%20true%20coefficients%20accurately.%0A%20%20%20%203.%20**Ridge%20Regression%20(%24%5Clambda%20%3D%205.0%24)**%3A%20Adds%20an%20%24L_2%24%20penalty%20to%20control%20ill-conditioning%2C%20shrinking%20parameter%20variance%20without%20discarding%20features.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(np)%3A%0A%20%20%20%20rng_reg%20%3D%20np.random.default_rng(42)%0A%20%20%20%20n_pts%20%3D%20100%0A%0A%20%20%20%20%23%20Predictors%0A%20%20%20%20x1_val%20%3D%20rng_reg.normal(50%2C%2015%2C%20n_pts)%0A%20%20%20%20x2_val%20%3D%20rng_reg.normal(30%2C%2010%2C%20n_pts)%0A%20%20%20%20%23%20x3%20is%20highly%20collinear%20with%20x1%20and%20x2%0A%20%20%20%20x3_val%20%3D%200.9%20*%20x1_val%20%2B%200.1%20*%20x2_val%20%2B%20rng_reg.normal(0%2C%200.05%2C%20n_pts)%0A%0A%20%20%20%20true_intercept%20%3D%205.0%0A%20%20%20%20true_beta1%20%3D%202.5%0A%20%20%20%20true_beta2%20%3D%20-1.8%0A%20%20%20%20true_beta3%20%3D%200.0%0A%20%20%20%20y_response%20%3D%20true_intercept%20%2B%20true_beta1%20*%20x1_val%20%2B%20true_beta2%20*%20x2_val%20%2B%20rng_reg.normal(0%2C%201.0%2C%20n_pts)%0A%0A%20%20%20%20%23%20Design%20matrices%0A%20%20%20%20x_matrix_raw%20%3D%20np.column_stack(%5Bx1_val%2C%20x2_val%2C%20x3_val%5D)%0A%0A%20%20%20%20%23%20Standardize%20design%20matrix%20for%20condition%20number%20and%20VIF%0A%20%20%20%20x_matrix_std%20%3D%20(x_matrix_raw%20-%20x_matrix_raw.mean(axis%3D0))%20%2F%20x_matrix_raw.std(axis%3D0)%0A%20%20%20%20cond_full_std%20%3D%20float(np.linalg.cond(x_matrix_std))%0A%20%20%20%20cond_pruned_std%20%3D%20float(np.linalg.cond(x_matrix_std%5B%3A%2C%20%3A2%5D))%0A%0A%20%20%20%20%23%20VIF%20computation%20via%20R%5E2%0A%20%20%20%20def%20calc_vif_array(mat)%3A%0A%20%20%20%20%20%20%20%20vifs%20%3D%20%5B%5D%0A%20%20%20%20%20%20%20%20p_cols%20%3D%20mat.shape%5B1%5D%0A%20%20%20%20%20%20%20%20for%20idx%20in%20range(p_cols)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20y_target%20%3D%20mat%5B%3A%2C%20idx%5D%0A%20%20%20%20%20%20%20%20%20%20%20%20other_feats%20%3D%20np.delete(mat%2C%20idx%2C%20axis%3D1)%0A%20%20%20%20%20%20%20%20%20%20%20%20design%20%3D%20np.column_stack(%5Bnp.ones(len(y_target))%2C%20other_feats%5D)%0A%20%20%20%20%20%20%20%20%20%20%20%20beta_hat%2C%20_%2C%20_%2C%20_%20%3D%20np.linalg.lstsq(design%2C%20y_target%2C%20rcond%3DNone)%0A%20%20%20%20%20%20%20%20%20%20%20%20pred%20%3D%20design%20%40%20beta_hat%0A%20%20%20%20%20%20%20%20%20%20%20%20ss_tot%20%3D%20float(np.sum((y_target%20-%20np.mean(y_target))%20**%202))%0A%20%20%20%20%20%20%20%20%20%20%20%20ss_res%20%3D%20float(np.sum((y_target%20-%20pred)%20**%202))%0A%20%20%20%20%20%20%20%20%20%20%20%20r_squared%20%3D%201.0%20-%20(ss_res%20%2F%20ss_tot)%0A%20%20%20%20%20%20%20%20%20%20%20%20vif_val%20%3D%201.0%20%2F%20(1.0%20-%20r_squared)%20if%20(1.0%20-%20r_squared)%20%3E%201e-9%20else%201e9%0A%20%20%20%20%20%20%20%20%20%20%20%20vifs.append(vif_val)%0A%20%20%20%20%20%20%20%20return%20vifs%0A%0A%20%20%20%20vif_full%20%3D%20calc_vif_array(x_matrix_raw)%0A%20%20%20%20vif_pruned%20%3D%20calc_vif_array(x_matrix_raw%5B%3A%2C%20%3A2%5D)%0A%0A%20%20%20%20%23%20Model%201%3A%20Full%20OLS%0A%20%20%20%20x_design_full%20%3D%20np.column_stack(%5Bnp.ones(n_pts)%2C%20x_matrix_raw%5D)%0A%20%20%20%20beta_full_ols%2C%20_%2C%20_%2C%20_%20%3D%20np.linalg.lstsq(x_design_full%2C%20y_response%2C%20rcond%3DNone)%0A%20%20%20%20residuals_full%20%3D%20y_response%20-%20x_design_full%20%40%20beta_full_ols%0A%20%20%20%20sigma_squared_full%20%3D%20float(np.sum(residuals_full**2)%20%2F%20(n_pts%20-%204))%0A%20%20%20%20cov_matrix_full%20%3D%20sigma_squared_full%20*%20np.linalg.inv(x_design_full.T%20%40%20x_design_full)%0A%20%20%20%20se_full_ols%20%3D%20np.sqrt(np.diag(cov_matrix_full))%0A%0A%20%20%20%20%23%20Model%202%3A%20Pruned%20OLS%20(dropping%20x3)%0A%20%20%20%20x_design_pruned%20%3D%20np.column_stack(%5Bnp.ones(n_pts)%2C%20x_matrix_raw%5B%3A%2C%20%3A2%5D%5D)%0A%20%20%20%20beta_pruned_ols%2C%20_%2C%20_%2C%20_%20%3D%20np.linalg.lstsq(x_design_pruned%2C%20y_response%2C%20rcond%3DNone)%0A%20%20%20%20residuals_pruned%20%3D%20y_response%20-%20x_design_pruned%20%40%20beta_pruned_ols%0A%20%20%20%20sigma_squared_pruned%20%3D%20float(np.sum(residuals_pruned**2)%20%2F%20(n_pts%20-%203))%0A%20%20%20%20cov_matrix_pruned%20%3D%20sigma_squared_pruned%20*%20np.linalg.inv(x_design_pruned.T%20%40%20x_design_pruned)%0A%20%20%20%20se_pruned_ols%20%3D%20np.sqrt(np.diag(cov_matrix_pruned))%0A%0A%20%20%20%20%23%20Model%203%3A%20Ridge%20Regression%20(lambda%20%3D%205.0)%0A%20%20%20%20lambda_param%20%3D%205.0%0A%20%20%20%20y_centered%20%3D%20y_response%20-%20np.mean(y_response)%0A%20%20%20%20ridge_gram%20%3D%20x_matrix_std.T%20%40%20x_matrix_std%20%2B%20lambda_param%20*%20np.eye(3)%0A%20%20%20%20beta_ridge_std%20%3D%20np.linalg.solve(ridge_gram%2C%20x_matrix_std.T%20%40%20y_centered)%0A%20%20%20%20beta_ridge_raw%20%3D%20beta_ridge_std%20%2F%20x_matrix_raw.std(axis%3D0)%0A%20%20%20%20intercept_ridge%20%3D%20float(np.mean(y_response)%20-%20np.sum(beta_ridge_raw%20*%20x_matrix_raw.mean(axis%3D0)))%0A%20%20%20%20cond_ridge%20%3D%20float(np.linalg.cond(ridge_gram))%0A%0A%20%20%20%20multicollinearity_comparison%20%3D%20%7B%0A%20%20%20%20%20%20%20%20%22Model%22%3A%20%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20%22True%20Parameters%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%221.%20Full%20OLS%20(with%20collinear%20x3)%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%222.%20Pruned%20OLS%20(x3%20removed)%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%223.%20Ridge%20Regression%20(%CE%BB%3D5.0)%22%2C%0A%20%20%20%20%20%20%20%20%5D%2C%0A%20%20%20%20%20%20%20%20%22Design%20%CE%BA%20(Standardized)%22%3A%20%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20%221.0%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20f%22%7Bcond_full_std%3A.1f%7D%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20f%22%7Bcond_pruned_std%3A.1f%7D%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20f%22%7Bcond_ridge%3A.1f%7D%20(effective)%22%2C%0A%20%20%20%20%20%20%20%20%5D%2C%0A%20%20%20%20%20%20%20%20%22Max%20VIF%22%3A%20%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20%221.0%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20f%22%7Bmax(vif_full)%3A.1f%7D%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20f%22%7Bmax(vif_pruned)%3A.2f%7D%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22Regularized%22%2C%0A%20%20%20%20%20%20%20%20%5D%2C%0A%20%20%20%20%20%20%20%20%22%CE%B2%E2%82%81%20Estimate%20(SE)%20%5BTrue%3A%202.50%5D%22%3A%20%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20%222.500%20(-)%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20f%22%7Bbeta_full_ols%5B1%5D%3A.3f%7D%20(SE%3A%20%7Bse_full_ols%5B1%5D%3A.3f%7D)%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20f%22%7Bbeta_pruned_ols%5B1%5D%3A.3f%7D%20(SE%3A%20%7Bse_pruned_ols%5B1%5D%3A.3f%7D)%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20f%22%7Bbeta_ridge_raw%5B0%5D%3A.3f%7D%20(Regularized)%22%2C%0A%20%20%20%20%20%20%20%20%5D%2C%0A%20%20%20%20%20%20%20%20%22%CE%B2%E2%82%82%20Estimate%20(SE)%20%5BTrue%3A%20-1.80%5D%22%3A%20%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20%22-1.800%20(-)%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20f%22%7Bbeta_full_ols%5B2%5D%3A.3f%7D%20(SE%3A%20%7Bse_full_ols%5B2%5D%3A.3f%7D)%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20f%22%7Bbeta_pruned_ols%5B2%5D%3A.3f%7D%20(SE%3A%20%7Bse_pruned_ols%5B2%5D%3A.3f%7D)%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20f%22%7Bbeta_ridge_raw%5B1%5D%3A.3f%7D%20(Regularized)%22%2C%0A%20%20%20%20%20%20%20%20%5D%2C%0A%20%20%20%20%20%20%20%20%22%CE%B2%E2%82%83%20Estimate%20(SE)%20%5BTrue%3A%200.00%5D%22%3A%20%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20%220.000%20(-)%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20f%22%7Bbeta_full_ols%5B3%5D%3A.3f%7D%20(SE%3A%20%7Bse_full_ols%5B3%5D%3A.3f%7D)%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22Dropped%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20f%22%7Bbeta_ridge_raw%5B2%5D%3A.3f%7D%20(Regularized)%22%2C%0A%20%20%20%20%20%20%20%20%5D%2C%0A%20%20%20%20%7D%0A%20%20%20%20return%20(multicollinearity_comparison%2C)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo%2C%20multicollinearity_comparison%2C%20pd)%3A%0A%20%20%20%20df_comparison%20%3D%20pd.DataFrame(multicollinearity_comparison)%0A%20%20%20%20mo.ui.table(df_comparison)%0A%20%20%20%20return%0A%0A%0Aif%20__name__%20%3D%3D%20%22__main__%22%3A%0A%20%20%20%20app.run()%0A
2083f7f210f58c249ee68242616b22b1