110 likes | 351 Views
COMP 116: Introduction to Scientific Programming . Lecture 11: Linear Regression. Revisiting linear programs.
E N D
COMP 116: Introduction to Scientific Programming Lecture 11: Linear Regression
Revisiting linear programs • A store has requested a manufacturer to produce pants and sports jackets. For materials, the manufacturer has 750 m2 of cotton textile and 1,000 m2 of polyester. Every pair of pants (1 unit) needs 1 m2 of cotton and 2 m2 of polyester. Every jacket needs 1.5 m2 of cotton and 1 m2 of polyester. The price of the pants is fixed at $50 and the jacket, $40. What is the number of pants and jackets that the manufacturer must give to the stores so that these items obtain a maximum revenue?
Solving linear programs • Design a linear program • What are the unknowns? • What is the cost/objective function? • What are the constraints? • Implement it in Matlab • x=linprog(f,A,b)
Linear Equations • When is there a solution for a m x n matrix A? • A is square (m = n) • A has rank m • Equations are linearly independent • m < n • More unknown than equations • A is underdetermined • m > n • More equations than unknowns • A is overdetermined
Least Squares • Overdetermined systems: too many equations! What to do? • Can’t solve Ax = b exactly • Instead, minimize the “residual” Residual vector: Ax - b Residual square error: (Ax – b)’*(Ax – b) • Matlab command >>x=A\b
Example: Linear Friction • You measured friction in response to a force. What’s the best fitting line to the data?
Example: Linear Friction • You measured friction in response to a force. What’s the best fitting line to the data?
Least Squares % set up and solve matrix % equation A*[m;c] = y A = [x, ones(10,1)]; mc = A\y mc = 2.5121 3.4707 % evaluate error res = A*mc - y; err = res'*res err = 5.8308
Least Squares • In general: Once you have • Mathematical model with parameters • Observed data Fit parameters to data by minimizing sum of squared residuals
Least Squares • Linear least squares if model is linear in the parameters • Written as Ax = b • A, b observed data • x is set of unknowns • Error (scalar): squared residual • (Ax-b)'*(Ax-b)
Same LS principle Minimize sum of squared distance to model (parabola).