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%20plotly.graph_objects%20as%20go%0A%0A%20%20%20%20return%20go%2C%20mo%2C%20np%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.Html(%0A%20%20%20%20%20%20%20%20%22%22%22%0A%20%20%20%20%20%20%20%20%3Cstyle%3E%0A%20%20%20%20%20%20%20%20%40import%20url('https%3A%2F%2Ffonts.googleapis.com%2Fcss2%3Ffamily%3DInter%3Awght%40400%3B500%3B600%26family%3DFira%2BCode%26display%3Dswap')%3B%0A%0A%20%20%20%20%20%20%20%20%2F*%20Core%20Typography%20*%2F%0A%20%20%20%20%20%20%20%20html%2C%20body%2C%20.marimo%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20font-family%3A%20'Inter'%2C%20sans-serif%20!important%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20background-color%3A%20%23ffffff%20!important%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20color%3A%20%231a1a1a%20!important%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20line-height%3A%201.75%20!important%3B%0A%20%20%20%20%20%20%20%20%7D%0A%0A%20%20%20%20%20%20%20%20%2F*%20Headings%20*%2F%0A%20%20%20%20%20%20%20%20h1%2C%20h2%2C%20h3%2C%20h4%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20font-weight%3A%20600%20!important%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20color%3A%20%23000000%20!important%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20letter-spacing%3A%20-0.02em%20!important%3B%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20h1%20%7B%20font-size%3A%202.25rem%20!important%3B%20margin-bottom%3A%201.5rem%20!important%3B%20%7D%0A%20%20%20%20%20%20%20%20h2%20%7B%20font-size%3A%201.5rem%20!important%3B%20margin-top%3A%203rem%20!important%3B%20%7D%0A%0A%20%20%20%20%20%20%20%20%2F*%20Code%20blocks%20*%2F%0A%20%20%20%20%20%20%20%20pre%2C%20code%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20font-family%3A%20'Fira%20Code'%2C%20monospace%20!important%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20font-size%3A%200.9em%20!important%3B%0A%20%20%20%20%20%20%20%20%7D%0A%0A%20%20%20%20%20%20%20%20%2F*%20Whitespace%20and%20margins%20for%20readability%20*%2F%0A%20%20%20%20%20%20%20%20.prose%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20max-width%3A%2065ch%20!important%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20margin%3A%200%20auto%20!important%3B%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20%3C%2Fstyle%3E%0A%20%20%20%20%20%20%20%20%22%22%22%0A%20%20%20%20)%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%23%20Note%2000%3A%20Systems%20of%20Linear%20Equations%20and%20Vector%20Foundations%0A%0A%20%20%20%20Next%20Note%3A%20%5B01%20Inner%20Products%5D(01_inner_product.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%20Linear%20equations%20form%20the%20computational%20bedrock%20of%20data%20science%2C%20machine%20learning%2C%20and%20statistical%20modeling.%20Real-world%20phenomena%20frequently%20involve%20multiple%20interdependent%20variables%20constrained%20by%20simultaneous%20observations.%0A%0A%20%20%20%20Understanding%20how%20to%20express%20these%20relationships%20with%20vectors%20and%20matrices%20allows%20you%20to%3A%0A%0A%20%20%20%201.%20Formulate%20complex%20multidimensional%20problems%20in%20a%20compact%20mathematical%20language.%0A%20%20%20%202.%20Determine%20whether%20a%20unique%20solution%20exists%2C%20whether%20multiple%20solutions%20exist%2C%20or%20whether%20the%20system%20is%20inconsistent.%0A%20%20%20%203.%20Compute%20unknown%20parameters%20efficiently%20using%20standard%20linear%20algebra%20routines.%0A%20%20%20%204.%20Gain%20geometric%20intuition%20for%20how%20data%20points%2C%20hyperplanes%2C%20and%20transformations%20behave%20in%20vector%20spaces.%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%20Vector%20Representation%20of%20Quantities%0A%0A%20%20%20%20Consider%20a%20simple%202D%20coordinate%20system%20tracking%20two%20distinct%20quantities%3A%0A%20%20%20%20*%20%24x%24%3A%20Number%20of%20apples%0A%20%20%20%20*%20%24y%24%3A%20Number%20of%20bananas%0A%0A%20%20%20%20Using%20vector%20notation%2C%20we%20represent%20individual%20units%20along%20coordinate%20axes%3A%0A%0A%20%20%20%20%24%24%0A%20%20%20%20%5Cmathbf%7Bv%7D_%7B%5Ctext%7Bapple%7D%7D%20%3D%20%5Cbegin%7Bbmatrix%7D%201%20%5C%5C%200%20%5Cend%7Bbmatrix%7D%2C%20%5Cquad%0A%20%20%20%20%5Cmathbf%7Bv%7D_%7B%5Ctext%7Bbanana%7D%7D%20%3D%20%5Cbegin%7Bbmatrix%7D%200%20%5C%5C%201%20%5Cend%7Bbmatrix%7D%2C%20%5Cquad%0A%20%20%20%20%5Cmathbf%7Bv%7D_%7B%5Ctext%7Bcombo%7D%7D%20%3D%20%5Cbegin%7Bbmatrix%7D%201%20%5C%5C%201%20%5Cend%7Bbmatrix%7D%0A%20%20%20%20%24%24%0A%0A%20%20%20%20When%20a%20vendor%20packages%20goods%20in%20fixed%20proportions%2C%20say%20two%20apples%20for%20every%20one%20banana%2C%20the%20combo%20follows%20a%20directional%20ray%20through%20the%20origin%3A%0A%0A%20%20%20%20%24%24%0A%20%20%20%20%5Cmathbf%7Bv%7D_%7B%5Ctext%7Bcombo%7D%7D%20%3D%20%5Calpha%20%5Cbegin%7Bbmatrix%7D%202%20%5C%5C%201%20%5Cend%7Bbmatrix%7D%2C%20%5Cquad%20%5Calpha%20%5Cin%20%5Cmathbb%7BR%7D%0A%20%20%20%20%24%24%0A%0A%20%20%20%20%23%23%23%20Pricing%20and%20Matrix%20Representation%0A%0A%20%20%20%20While%20quantities%20count%20discrete%20items%2C%20prices%20provide%20a%20scalar%20weighting%20of%20value.%20Let%3A%0A%20%20%20%20*%20%24x_1%24%3A%20Price%20per%20apple%0A%20%20%20%20*%20%24x_2%24%3A%20Price%20per%20banana%0A%0A%20%20%20%20For%20a%20bundle%20containing%20%24a_1%24%20apples%20and%20%24b_1%24%20bananas%2C%20the%20total%20cost%20%24c_1%24%20is%20a%20linear%20combination%3A%0A%0A%20%20%20%20%24%24%0A%20%20%20%20c_1%20%3D%20a_1%20x_1%20%2B%20b_1%20x_2%0A%20%20%20%20%24%24%0A%0A%20%20%20%20When%20we%20observe%20two%20distinct%20purchase%20scenarios%3A%0A%20%20%20%201.%20%24a_1%24%20apples%20and%20%24b_1%24%20bananas%20sold%20for%20total%20cost%20%24c_1%24%0A%20%20%20%202.%20%24a_2%24%20apples%20and%20%24b_2%24%20bananas%20sold%20for%20total%20cost%20%24c_2%24%0A%0A%20%20%20%20We%20can%20stack%20these%20relationships%20into%20a%20single%20matrix%20equation%3A%0A%0A%20%20%20%20%24%24%0A%20%20%20%20%5Cbegin%7Bbmatrix%7D%20x_1%20%26%20x_2%20%5Cend%7Bbmatrix%7D%0A%20%20%20%20%5Cbegin%7Bbmatrix%7D%20a_1%20%26%20a_2%20%5C%5C%20b_1%20%26%20b_2%20%5Cend%7Bbmatrix%7D%0A%20%20%20%20%3D%20%5Cbegin%7Bbmatrix%7D%20c_1%20%26%20c_2%20%5Cend%7Bbmatrix%7D%0A%20%20%20%20%24%24%0A%0A%20%20%20%20In%20row-vector%20notation%3A%0A%0A%20%20%20%20%24%24%0A%20%20%20%20%5Cmathbf%7Bx%7D_%7B%5Ctext%7Brow%7D%7D%20%5Cmathbf%7BA%7D%20%3D%20%5Cmathbf%7Bc%7D_%7B%5Ctext%7Brow%7D%7D%0A%20%20%20%20%24%24%0A%0A%20%20%20%20Taking%20the%20transpose%20yields%20the%20conventional%20column-vector%20formulation%3A%0A%0A%20%20%20%20%24%24%0A%20%20%20%20%5Cmathbf%7BA%7D%5ET%20%5Cmathbf%7Bx%7D%20%3D%20%5Cmathbf%7Bc%7D%5ET%0A%20%20%20%20%24%24%0A%0A%20%20%20%20%23%23%23%20Solvability%20and%20Determinants%0A%0A%20%20%20%20A%20square%20linear%20system%20has%20a%20unique%20solution%20if%20and%20only%20if%20the%20coefficient%20matrix%20is%20invertible%2C%20which%20requires%20a%20non-zero%20determinant%3A%0A%0A%20%20%20%20%24%24%0A%20%20%20%20%5Cdet(%5Cmathbf%7BA%7D)%20%5Cneq%200%0A%20%20%20%20%24%24%0A%0A%20%20%20%20If%20%24%5Cdet(%5Cmathbf%7BA%7D)%20%3D%200%24%2C%20the%20rows%20(or%20columns)%20are%20linearly%20dependent.%20In%20that%20case%3A%0A%20%20%20%20*%20The%20system%20has%20infinitely%20many%20solutions%20if%20the%20target%20vector%20lies%20within%20the%20column%20span.%0A%20%20%20%20*%20The%20system%20has%20no%20solution%20if%20the%20target%20vector%20lies%20outside%20the%20column%20span.%0A%0A%20%20%20%20%23%23%23%20Role%20in%20ML%2C%20AI%2C%20and%20Statistics%0A%0A%20%20%20%20*%20**Parameter%20Estimation**%3A%20In%20supervised%20learning%2C%20linear%20models%20define%20predictions%20as%20linear%20combinations%20of%20feature%20vectors.%20Solving%20for%20optimal%20weights%20involves%20solving%20or%20approximating%20a%20system%20of%20linear%20equations.%0A%20%20%20%20*%20**Coordinate%20Transformations**%3A%20Every%20linear%20neural%20network%20layer%20performs%20a%20matrix-vector%20product%2C%20transforming%20representations%20from%20one%20coordinate%20basis%20to%20another.%0A%20%20%20%20*%20**Solvability%20and%20Multicollinearity**%3A%20In%20regression%20analysis%2C%20highly%20correlated%20predictors%20lead%20to%20near-singular%20matrices%20with%20determinants%20close%20to%20zero%2C%20causing%20numerical%20instability.%20Understanding%20linear%20independence%20is%20essential%20to%20diagnosing%20and%20resolving%20multicollinearity.%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%20Code%20Examples%0A%0A%20%20%20%20%23%23%23%20Example%201%3A%20Solving%20a%202x2%20System%20(Unique%20Solution)%0A%0A%20%20%20%20A%20vendor%20observes%20two%20purchase%20scenarios%3A%20(40%20apples%2C%206%20bananas)%20totaling%20100%20currency%20units%2C%20and%20(20%20apples%2C%208%20bananas)%20totaling%2080%20currency%20units.%20We%20solve%20for%20the%20unknown%20per-unit%20prices%20%24x_1%24%20(apple)%20and%20%24x_2%24%20(banana)%20using%20the%20matrix%20equation%20%24%5Cmathbf%7BA%7D%5ET%20%5Cmathbf%7Bx%7D%20%3D%20%5Cmathbf%7Bc%7D%24%2C%20verify%20invertibility%20via%20the%20determinant%2C%20and%20visualize%20the%20constraint%20lines%20whose%20intersection%20is%20the%20unique%20solution.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(A%2C%20c%2C%20np)%3A%0A%20%20%20%20%23%20Ensure%20matrix%20A%20is%20invertible%20by%20checking%20its%20determinant%0A%20%20%20%20det_A%20%3D%20np.linalg.det(A)%0A%20%20%20%20if%20det_A%20%3D%3D%200%3A%0A%20%20%20%20%20%20%20%20raise%20ValueError(%22Matrix%20A%20is%20singular%20and%20cannot%20be%20inverted%22)%0A%0A%20%20%20%20%23%20Solve%20the%20system%20of%20linear%20equations%3A%20x_row%20%40%20A%20%3D%20c_row%20%3C%3D%3E%20A.T%20%40%20x%20%3D%20c%0A%20%20%20%20%23%20Using%20np.linalg.solve%20on%20the%20transposed%20system%20is%20numerically%20preferred%0A%20%20%20%20x_solution%20%3D%20np.linalg.solve(A.T%2C%20c)%0A%0A%20%20%20%20%23%20Verification%20via%20matrix%20inversion%0A%20%20%20%20x_inv%20%3D%20c%20%40%20np.linalg.inv(A)%0A%0A%20%20%20%20%7B%22determinant%22%3A%20float(det_A)%2C%20%22solution_solve%22%3A%20x_solution.tolist()%2C%20%22solution_inv%22%3A%20x_inv.tolist()%7D%0A%20%20%20%20return%20(x_solution%2C)%0A%0A%0A%40app.cell%0Adef%20_(np)%3A%0A%20%20%20%20%23%20Coefficient%20matrix%20A%20(quantity%20matrix)%0A%20%20%20%20%23%20Row%201%3A%20scenario%201%20(40%20apples%2C%2020%20bananas)%0A%20%20%20%20%23%20Row%202%3A%20scenario%202%20(6%20apples%2C%208%20bananas)%0A%20%20%20%20A%20%3D%20np.array(%5B%5B40.0%2C%2020.0%5D%2C%20%5B6.0%2C%208.0%5D%5D)%0A%0A%20%20%20%20%23%20Target%20vector%20c%20(total%20costs%20for%20each%20scenario)%0A%20%20%20%20c%20%3D%20np.array(%5B100.0%2C%2080.0%5D)%0A%20%20%20%20return%20A%2C%20c%0A%0A%0A%40app.cell%0Adef%20_(A%2C%20c%2C%20go%2C%20np%2C%20x_solution)%3A%0A%20%20%20%20%23%20Compute%20lines%20for%20visualization%0A%20%20%20%20%23%20Equation%201%3A%20A%5B0%2C%200%5D*x1%20%2B%20A%5B1%2C%200%5D*x2%20%3D%20c%5B0%5D%20%3D%3E%2040*x1%20%2B%206*x2%20%3D%20100%0A%20%20%20%20%23%20Equation%202%3A%20A%5B0%2C%201%5D*x1%20%2B%20A%5B1%2C%201%5D*x2%20%3D%20c%5B1%5D%20%3D%3E%2020*x1%20%2B%208*x2%20%3D%2080%0A%20%20%20%20x1_range%20%3D%20np.linspace(0%2C%204%2C%20200)%0A%20%20%20%20x2_eq1%20%3D%20(c%5B0%5D%20-%20A%5B0%2C%200%5D%20*%20x1_range)%20%2F%20A%5B1%2C%200%5D%0A%20%20%20%20x2_eq2%20%3D%20(c%5B1%5D%20-%20A%5B0%2C%201%5D%20*%20x1_range)%20%2F%20A%5B1%2C%201%5D%0A%0A%20%20%20%20fig%20%3D%20go.Figure()%0A%0A%20%20%20%20%23%20Scenario%201%20constraint%20line%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%3Dx1_range%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20y%3Dx2_eq1%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%20name%3D%22Scenario%201%3A%2040x%E2%82%81%20%2B%206x%E2%82%82%20%3D%20100%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20line%3Ddict(color%3D%22%231f77b4%22%2C%20width%3D2.5)%2C%0A%20%20%20%20%20%20%20%20)%0A%20%20%20%20)%0A%0A%20%20%20%20%23%20Scenario%202%20constraint%20line%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%3Dx1_range%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20y%3Dx2_eq2%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%20name%3D%22Scenario%202%3A%2020x%E2%82%81%20%2B%208x%E2%82%82%20%3D%2080%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20line%3Ddict(color%3D%22%23ff7f0e%22%2C%20width%3D2.5)%2C%0A%20%20%20%20%20%20%20%20)%0A%20%20%20%20)%0A%0A%20%20%20%20%23%20Solution%20point%20(intersection)%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_solution%5B0%5D%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20y%3D%5Bx_solution%5B1%5D%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20mode%3D%22markers%2Btext%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20name%3Df%22Solution%3A%20(%7Bx_solution%5B0%5D%3A.2f%7D%2C%20%7Bx_solution%5B1%5D%3A.2f%7D)%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20text%3D%5Bf%22%20%20Solution%20(%7Bx_solution%5B0%5D%3A.2f%7D%2C%20%7Bx_solution%5B1%5D%3A.2f%7D)%22%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20textposition%3D%22top%20right%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20marker%3Ddict(color%3D%22%23d62728%22%2C%20size%3D11%2C%20symbol%3D%22circle%22)%2C%0A%20%20%20%20%20%20%20%20)%0A%20%20%20%20)%0A%0A%20%20%20%20fig.update_layout(%0A%20%20%20%20%20%20%20%20title%3Ddict(text%3D%22Systems%20of%20Linear%20Equations%3A%20Constraint%20Lines%20and%20Unique%20Solution%22%2C%20font%3Ddict(size%3D14))%2C%0A%20%20%20%20%20%20%20%20xaxis%3Ddict(title%3D%22Price%20per%20Apple%20(x%E2%82%81)%22%2C%20range%3D%5B0%2C%203.5%5D%2C%20zeroline%3DTrue%2C%20gridcolor%3D%22%23e5e5e5%22)%2C%0A%20%20%20%20%20%20%20%20yaxis%3Ddict(title%3D%22Price%20per%20Banana%20(x%E2%82%82)%22%2C%20range%3D%5B0%2C%2012%5D%2C%20zeroline%3DTrue%2C%20gridcolor%3D%22%23e5e5e5%22)%2C%0A%20%20%20%20%20%20%20%20template%3D%22plotly_white%22%2C%0A%20%20%20%20%20%20%20%20legend%3Ddict(x%3D0.55%2C%20y%3D0.98)%2C%0A%20%20%20%20%20%20%20%20width%3D720%2C%0A%20%20%20%20%20%20%20%20height%3D480%2C%0A%20%20%20%20)%0A%0A%20%20%20%20fig%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%23%23%23%20Example%202%3A%20Overdetermined%20System%20(Least-Squares%20Approximation)%0A%0A%20%20%20%20In%20practice%2C%20we%20often%20have%20more%20observations%20(equations)%20than%20unknowns.%20A%20vendor%20records%20**three**%20purchase%20scenarios%20but%20there%20are%20still%20only%20two%20unknown%20prices.%20The%20system%20%24%5Cmathbf%7BA%7D%5ET%20%5Cmathbf%7Bx%7D%20%3D%20%5Cmathbf%7Bc%7D%24%20is%20now%20overdetermined%20and%20generally%20has%20no%20exact%20solution.%0A%0A%20%20%20%20The%20**least-squares%20solution**%20minimizes%20the%20squared%20residual%20%24%5C%7C%5Cmathbf%7BA%7D%5ET%20%5Cmathbf%7Bx%7D%20-%20%5Cmathbf%7Bc%7D%5C%7C_2%5E2%24.%20This%20is%20the%20foundation%20of%20Ordinary%20Least%20Squares%20(OLS)%20regression%3A%0A%0A%20%20%20%20%24%24%0A%20%20%20%20%5Chat%7B%5Cmathbf%7Bx%7D%7D%20%3D%20(%5Cmathbf%7BA%7D%20%5Cmathbf%7BA%7D%5ET)%5E%7B-1%7D%20%5Cmathbf%7BA%7D%20%5C%2C%20%5Cmathbf%7Bc%7D%0A%20%20%20%20%24%24%0A%0A%20%20%20%20NumPy's%20%60np.linalg.lstsq%60%20computes%20this%20via%20the%20numerically%20stable%20SVD%20decomposition%20rather%20than%20forming%20the%20normal%20equations%20explicitly.%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%20%23%20Three%20purchase%20scenarios%20(overdetermined%3A%203%20equations%2C%202%20unknowns)%0A%20%20%20%20%23%20Scenario%201%3A%2040%20apples%2C%206%20bananas%20%3D%20100%0A%20%20%20%20%23%20Scenario%202%3A%2020%20apples%2C%208%20bananas%20%3D%2080%0A%20%20%20%20%23%20Scenario%203%3A%2010%20apples%2C%203%20bananas%20%3D%2049%20(slightly%20noisy%20observation)%0A%20%20%20%20A_over%20%3D%20np.array(%5B%5B40.0%2C%2020.0%2C%2010.0%5D%2C%20%5B6.0%2C%208.0%2C%203.0%5D%5D)%0A%20%20%20%20c_over%20%3D%20np.array(%5B100.0%2C%2080.0%2C%2049.0%5D)%0A%0A%20%20%20%20%23%20Least-squares%20solution%20via%20SVD%0A%20%20%20%20x_lstsq%2C%20residuals%2C%20rank%2C%20sv%20%3D%20np.linalg.lstsq(A_over.T%2C%20c_over%2C%20rcond%3DNone)%0A%0A%20%20%20%20%23%20Normal%20equations%20solution%20for%20comparison%0A%20%20%20%20x_normal%20%3D%20np.linalg.solve(A_over%20%40%20A_over.T%2C%20A_over%20%40%20c_over)%0A%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%22lstsq_solution%22%3A%20x_lstsq.tolist()%2C%0A%20%20%20%20%20%20%20%20%22normal_eq_solution%22%3A%20x_normal.tolist()%2C%0A%20%20%20%20%20%20%20%20%22residual_norm_squared%22%3A%20float(np.sum((A_over.T%20%40%20x_lstsq%20-%20c_over)%20**%202))%2C%0A%20%20%20%20%20%20%20%20%22matrix_rank%22%3A%20int(rank)%2C%0A%20%20%20%20%20%20%20%20%22match%22%3A%20bool(np.allclose(x_lstsq%2C%20x_normal))%2C%0A%20%20%20%20%7D%0A%20%20%20%20return%20A_over%2C%20c_over%0A%0A%0A%40app.cell%0Adef%20_(A_over%2C%20c_over%2C%20go%2C%20np)%3A%0A%20%20%20%20%23%20Least-squares%20solution%20for%20plotting%0A%20%20%20%20_x_ls%2C%20_%2C%20_%2C%20_%20%3D%20np.linalg.lstsq(A_over.T%2C%20c_over%2C%20rcond%3DNone)%0A%0A%20%20%20%20x1_grid%20%3D%20np.linspace(0%2C%205%2C%20200)%0A%0A%20%20%20%20fig_over%20%3D%20go.Figure()%0A%0A%20%20%20%20labels%20%3D%20%5B%0A%20%20%20%20%20%20%20%20f%22Scenario%201%3A%20%7BA_over%5B0%2C0%5D%3A.0f%7Dx%5Cu2081%20%2B%20%7BA_over%5B1%2C0%5D%3A.0f%7Dx%5Cu2082%20%3D%20%7Bc_over%5B0%5D%3A.0f%7D%22%2C%0A%20%20%20%20%20%20%20%20f%22Scenario%202%3A%20%7BA_over%5B0%2C1%5D%3A.0f%7Dx%5Cu2081%20%2B%20%7BA_over%5B1%2C1%5D%3A.0f%7Dx%5Cu2082%20%3D%20%7Bc_over%5B1%5D%3A.0f%7D%22%2C%0A%20%20%20%20%20%20%20%20f%22Scenario%203%3A%20%7BA_over%5B0%2C2%5D%3A.0f%7Dx%5Cu2081%20%2B%20%7BA_over%5B1%2C2%5D%3A.0f%7Dx%5Cu2082%20%3D%20%7Bc_over%5B2%5D%3A.0f%7D%22%2C%0A%20%20%20%20%5D%0A%20%20%20%20colors%20%3D%20%5B%22%231f77b4%22%2C%20%22%23ff7f0e%22%2C%20%22%232ca02c%22%5D%0A%0A%20%20%20%20for%20k%20in%20range(3)%3A%0A%20%20%20%20%20%20%20%20x2_line%20%3D%20(c_over%5Bk%5D%20-%20A_over%5B0%2C%20k%5D%20*%20x1_grid)%20%2F%20A_over%5B1%2C%20k%5D%0A%20%20%20%20%20%20%20%20fig_over.add_trace(%0A%20%20%20%20%20%20%20%20%20%20%20%20go.Scatter(%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20x%3Dx1_grid%2C%20y%3Dx2_line%2C%20mode%3D%22lines%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20name%3Dlabels%5Bk%5D%2C%20line%3Ddict(color%3Dcolors%5Bk%5D%2C%20width%3D2.5)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20)%0A%20%20%20%20%20%20%20%20)%0A%0A%20%20%20%20fig_over.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%5B_x_ls%5B0%5D%5D%2C%20y%3D%5B_x_ls%5B1%5D%5D%2C%20mode%3D%22markers%2Btext%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20name%3Df%22Least-Squares%20Solution%20(%7B_x_ls%5B0%5D%3A.2f%7D%2C%20%7B_x_ls%5B1%5D%3A.2f%7D)%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20text%3D%5Bf%22%20%20LS%20Solution%20(%7B_x_ls%5B0%5D%3A.2f%7D%2C%20%7B_x_ls%5B1%5D%3A.2f%7D)%22%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20textposition%3D%22top%20right%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20marker%3Ddict(color%3D%22%23d62728%22%2C%20size%3D11%2C%20symbol%3D%22diamond%22)%2C%0A%20%20%20%20%20%20%20%20)%0A%20%20%20%20)%0A%0A%20%20%20%20fig_over.update_layout(%0A%20%20%20%20%20%20%20%20title%3Ddict(%0A%20%20%20%20%20%20%20%20%20%20%20%20text%3D%22Overdetermined%20System%3A%20Three%20Constraints%2C%20Least-Squares%20Compromise%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20font%3Ddict(size%3D14)%2C%0A%20%20%20%20%20%20%20%20)%2C%0A%20%20%20%20%20%20%20%20xaxis%3Ddict(title%3D%22Price%20per%20Apple%20(x%5Cu2081)%22%2C%20range%3D%5B0%2C%205%5D%2C%20zeroline%3DTrue%2C%20gridcolor%3D%22%23e5e5e5%22)%2C%0A%20%20%20%20%20%20%20%20yaxis%3Ddict(title%3D%22Price%20per%20Banana%20(x%5Cu2082)%22%2C%20range%3D%5B0%2C%2018%5D%2C%20zeroline%3DTrue%2C%20gridcolor%3D%22%23e5e5e5%22)%2C%0A%20%20%20%20%20%20%20%20template%3D%22plotly_white%22%2C%0A%20%20%20%20%20%20%20%20legend%3Ddict(x%3D0.45%2C%20y%3D0.98)%2C%0A%20%20%20%20%20%20%20%20width%3D720%2C%0A%20%20%20%20%20%20%20%20height%3D480%2C%0A%20%20%20%20)%0A%0A%20%20%20%20fig_over%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%20Takeaway%0A%0A%20%20%20%20*%20**Geometric%20Meaning**%3A%20Each%20linear%20equation%20represents%20a%20hyperplane%20of%20constraints.%20Solving%20the%20system%20corresponds%20to%20finding%20the%20intersection%20point%20satisfying%20all%20constraints%20simultaneously.%0A%20%20%20%20*%20**Matrix%20Formulation**%3A%20Grouping%20quantities%20and%20observations%20into%20matrices%20turns%20a%20set%20of%20simultaneous%20equations%20into%20a%20compact%20matrix%20multiplication%20problem.%0A%20%20%20%20*%20**Invertibility%20and%20Determinants**%3A%20A%20unique%20solution%20exists%20if%20and%20only%20if%20the%20matrix%20determinant%20is%20non-zero%2C%20indicating%20that%20the%20equations%20are%20linearly%20independent.%0A%20%20%20%20*%20**Overdetermined%20Systems**%3A%20When%20there%20are%20more%20equations%20than%20unknowns%2C%20the%20system%20is%20generally%20inconsistent.%20The%20least-squares%20solution%20minimizes%20the%20total%20squared%20error%20across%20all%20constraints%2C%20forming%20the%20mathematical%20foundation%20of%20linear%20regression.%0A%20%20%20%20*%20**Computational%20Practice**%3A%20While%20matrix%20inversion%20(%24%5Cmathbf%7BA%7D%5E%7B-1%7D%24)%20is%20theoretically%20convenient%2C%20numerical%20solvers%20like%20%60np.linalg.solve%60%20and%20%60np.linalg.lstsq%60%20should%20always%20be%20preferred%20in%20practice%20for%20speed%20and%20stability.%0A%0A%20%20%20%20---%0A%0A%20%20%20%20Next%20Note%3A%20%5B01%20Inner%20Products%5D(01_inner_product.py)%20%26rarr%3B%0A%20%20%20%20%22%22%22)%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
2a59cb653b67a0e760f0ef720b3c10d0