Least Squares and Data Fitting Data fitting How do we best fit a - - PowerPoint PPT Presentation
Least Squares and Data Fitting Data fitting How do we best fit a - - PowerPoint PPT Presentation
Least Squares and Data Fitting Data fitting How do we best fit a set of data points? Linear Least Squares 1) Fitting with a line Given data points { ! , ! , , " , " } , we want to find the function =
How do we best fit a set of data points?
Data fitting
Given ๐ data points { ๐ข!, ๐ง! , โฆ , ๐ข", ๐ง" }, we want to find the function
๐ง = ๐ฆ! + ๐ฆ" ๐ข
that best fit the data (or better, we want to find the coefficients ๐ฆ#, ๐ฆ!). Thinking geometrically, we can think โwhat is the line that most nearly passes through all the points?โ
Linear Least Squares
1) Fitting with a line
Given ๐ data points { ๐ข!, ๐ง! , โฆ , ๐ข", ๐ง" }, we want to find ๐ฆ# and ๐ฆ! such that ๐ง$ = ๐ฆ# + ๐ฆ! ๐ข$ โ๐ โ 1, ๐
- r in matrix form:
Note that this system of linear equations has more equations than unknowns โ OVERDETERMINED SYSTEMS ๐ร๐ ๐ร๐ ๐ร๐ 1 ๐ข! โฎ โฎ 1 ๐ข" ๐ฆ# ๐ฆ! = ๐ง! โฎ ๐ง"
๐ฉ ๐ = ๐
We want to find the appropriate linear combination of the columns of ๐ฉ that makes up the vector ๐. If a solution exists that satisfies ๐ฉ ๐ = ๐ then ๐ โ ๐ ๐๐๐๐(๐ฉ)
Linear Least Squares
- In most cases, ๐ โ ๐ ๐๐๐๐(๐ฉ) and ๐ฉ ๐ = ๐ does not have an
exact solution!
- Therefore, an overdetermined system is better expressed as
๐ฉ ๐ โ ๐
Linear Least Squares
- Least Squares: find the solution ๐ that minimizes the residual
๐ = ๐ โ ๐ฉ ๐
- Letโs define the function ๐ as the square of the 2-norm of the residual
๐ ๐ = ๐ โ ๐ฉ ๐ %
%
Linear Least Squares
- Least Squares: find the solution ๐ that minimizes the residual
๐ = ๐ โ ๐ฉ ๐
- Letโs define the function ๐ as the square of the 2-norm of the residual
๐ ๐ = ๐ โ ๐ฉ ๐ %
%
- Then the least squares problem becomes
min
๐ ๐ (๐)
- Suppose ๐: โ" โ โ is a smooth function, then ๐ ๐ reaches a (local)
maximum or minimum at a point ๐โ โ โ" only if โ๐ ๐โ = 0
How to find the minimizer?
- To minimize the 2-norm of the residual vector
min
๐ ๐ ๐ =
๐ โ ๐ฉ ๐ %
%
๐ ๐ = (๐ โ ๐ฉ ๐)((๐ โ ๐ฉ ๐) โ๐ ๐ = 2(๐ฉ( ๐ โ ๐ฉ(๐ฉ ๐) First order necessary condition: โ๐ ๐ = 0 โ ๐ฉ( ๐ โ ๐ฉ(๐ฉ ๐ = ๐ โ ๐ฉ(๐ฉ ๐ = ๐ฉ( ๐ Second order sufficient condition: ๐ธ%๐ ๐ = 2๐ฉ(๐ฉ 2๐ฉ(๐ฉ is a positive semi-definite matrix โ the solution is a minimum Normal Equations โ solve a linear system of equations
Linear Least Squares (another approach)
- Find ๐ = ๐ฉ ๐ which is closest to the vector ๐
- What is the vector ๐ = ๐ฉ ๐ โ ๐ ๐๐๐๐(๐ฉ) that is closest to vector ๐ in
the Euclidean norm?
When ๐ = ๐ โ ๐ = ๐ โ ๐ฉ ๐ is orthogonal to all columns of ๐ฉ, then ๐ is closest to ๐ ๐ฉ๐ผ๐ = ๐ฉ๐ผ(๐ โ ๐ฉ ๐)=0 ๐ฉ"๐ฉ ๐ = ๐ฉ" ๐
Summary:
- ๐ฉ is a ๐ร๐ matrix, where ๐ > ๐.
- ๐ is the number of data pair points. ๐ is the number of parameters of the
โbest fitโ function.
- Linear Least Squares problem ๐ฉ ๐ โ ๐ always has solution.
- The Linear Least Squares solution ๐ minimizes the square of the 2-norm
- f the residual:
min
๐
๐ โ ๐ฉ ๐ %
%
- One method to solve the minimization problem is to solve the system of
Normal Equations ๐ฉ(๐ฉ ๐ = ๐ฉ( ๐
- Letโs see some examples and discuss the limitations of this method.
Example:
Solve: ๐ฉ#๐ฉ ๐ = ๐ฉ# ๐
- Does not need to be a line! For example, here we are fitting the data
using a quadratic curve.
Data fitting - not always a line fit!
Linear Least Squares: The problem is linear in its coefficients!
Another examples
We want to find the coefficients of the quadratic function that best fits the data points:
We would not want our โfitโ curve to pass through the data points exactly as we are looking to model the general trend and not capture the noise.
๐ง = ๐ฆ! + ๐ฆ" ๐ข + ๐ฆ# ๐ข#
Data fitting
1 ๐ข! ๐ข!
"
โฎ โฎ โฎ 1 ๐ข# ๐ข#
"
๐ฆ$ ๐ฆ! ๐ฆ" = ๐ง! โฎ ๐ง#
(๐ข$,๐ง$) Solve: ๐ฉ#๐ฉ ๐ = ๐ฉ# ๐
Which function is not suitable for linear least squares? A) ๐ง = ๐ + ๐ ๐ฆ + ๐ ๐ฆ# + ๐ ๐ฆ% B) ๐ง = ๐ฆ ๐ + ๐ ๐ฆ + ๐ ๐ฆ# + ๐ ๐ฆ% C) ๐ง = ๐ sin ๐ฆ + ๐/ cos ๐ฆ D) ๐ง = ๐ sin ๐ฆ + ๐ฆ/ cos ๐๐ฆ E) ๐ง = ๐ ๐&#' + ๐ ๐#'
Computational Cost
๐ฉ#๐ฉ ๐ = ๐ฉ# ๐
- Compute ๐ฉ(๐ฉ: ๐ ๐๐%
- Factorize ๐ฉ(๐ฉ: LU โ ๐
% 2 ๐2 , Cholesky โ ๐ ! 2 ๐2
- Solve ๐ ๐%
- Since ๐ > ๐ the overall cost is ๐ ๐๐%
Short questions
Given the data in the table below, which of the plots shows the line of best fit in terms of least squares? A) B) C) D)
Short questions
Given the data in the table below, and the least squares model ๐ง = ๐! + ๐% sin ๐ข๐ + ๐2 sin ๐ข๐/2 + ๐3 sin ๐ข๐/4 written in matrix form as determine the entry ๐ต%2 of the matrix ๐ฉ. Note that indices start with 1. A) โ1.0 B) 1.0 C) โ 0.7 D) 0.7 E) 0.0
Solving Linear Least Squares with SVD
๐ฉ is a ๐ร๐ matrix where ๐ > ๐ (more points to fit than coefficient to be determined) Normal Equations: ๐ฉ!๐ฉ ๐ = ๐ฉ! ๐
- The solution ๐ฉ ๐ โ ๐ is unique if and only if ๐ ๐๐๐ ๐ = ๐
(๐ฉ is full column rank)
- ๐ ๐๐๐ ๐ = ๐ โ columns of ๐ฉ are linearly independent โ ๐ non-zero
singular values โ ๐ฉ! ๐ฉ has only positive eigenvalues โ ๐ฉ!๐ฉ is a symmetric and positive definite matrix โ ๐ฉ!๐ฉ is invertible ๐ = ๐ฉ!๐ฉ "๐๐ฉ! ๐
- If ๐ ๐๐๐ ๐ < ๐, then ๐ฉ is rank-deficient, and solution of linear least squares
problem is not unique.
What we have learned so farโฆ
Condition number for Normal Equations
Finding the least square solution of ๐ฉ ๐ โ ๐ (where ๐ฉ is full rank matrix) using the Normal Equations ๐ฉ(๐ฉ ๐ = ๐ฉ( ๐ has some advantages, since we are solving a square system of linear equations with a symmetric matrix (and hence it is possible to use decompositions such as Cholesky Factorization) However, the normal equations tend to worsen the conditioning of the matrix. ๐๐๐๐ ๐ฉ(๐ฉ = (๐๐๐๐ ๐ฉ )% How can we solve the least square problem without squaring the condition of the matrix?
SVD to solve linear least squares problems
We want to find the least square solution of ๐ฉ ๐ โ ๐, where ๐ฉ = ๐ฝ ๐ป ๐พ๐ผ
- r better expressed in reduced form: ๐ฉ = ๐ฝ5 ๐ป๐บ ๐พ๐ผ
๐ฉ = โฎ โฆ โฎ ๐# โฆ ๐$ โฎ โฆ โฎ ๐# โฑ ๐% โฎ โฆ ๐ฐ#
"
โฆ โฎ โฎ โฎ โฆ ๐ฐ%
"
โฆ
๐ฉ is a ๐ร๐ rectangular matrix where ๐ > ๐, and hence the SVD decomposition is given by:
Recall Reduced SVD
๐ฉ = ๐ฝ4 ๐ป๐บ ๐พ๐ผ
๐ร๐ ๐ร๐ ๐ร๐ ๐ร๐
๐ > ๐
SVD to solve linear least squares problems
We want to find the least square solution of ๐ฉ ๐ โ ๐, where ๐ฉ = ๐ฝ$ ๐ป๐บ ๐พ๐ผ Normal equations: ๐ฉ"๐ฉ ๐ = ๐ฉ" ๐ โถ ๐ฝ& ๐ป๐บ ๐พ" " ๐ฝ& ๐ป๐บ ๐พ" ๐ = ๐ฝ& ๐ป๐บ ๐พ" "๐
๐ฉ = โฎ โฆ โฎ ๐! โฆ ๐A โฎ โฆ โฎ ๐! โฑ ๐A โฆ ๐ฐ!
(
โฆ โฎ โฎ โฎ โฆ ๐ฐA
(
โฆ
๐ฉ = ๐ฝ4 ๐ป๐บ ๐พ๐ผ
๐พ ๐ป๐บ๐ฝ&
" ๐ฝ& ๐ป๐บ ๐พ" ๐ = ๐พ ๐ป๐บ๐ฝ& "๐
๐พ ๐ป๐บ ๐ป๐บ ๐พ"๐ = ๐พ ๐ป๐บ๐ฝ&
"๐
๐ป๐บ
( ๐พ"๐ = ๐ป๐บ ๐ฝ& "๐
When can we take the inverse of the singular matrix?
๐ป๐บ
" ๐พ#๐ = ๐ป๐บ ๐ฝ$ #๐
1) Full rank matrix (๐$ โ 0 โ๐):
๐พ#๐ = ๐ป๐บ
%&๐ฝ$ #๐
๐ = ๐พ ๐ป๐บ
"'๐ฝ$ ! ๐
Unique solution:
๐ร๐ ๐ร๐ ๐ร๐ ๐ร1 ๐ร1
rank ๐ฉ = ๐
2) Rank deficient matrix ( rank ๐ฉ = ๐ < ๐ )
Solution is not unique!!
Find solution ๐ such that min
๐ ๐ ๐ =
๐ โ ๐ฉ ๐ "
"
and also ๐ป๐บ
" ๐พ#๐ = ๐ป๐บ ๐ฝ$ #๐
min
๐
๐ ๐
2) Rank deficient matrix (continue)
Change of variables: Set ๐พ#๐ = ๐ and then solve ๐ป๐บ ๐ = ๐ฝ$
#๐ for the variable ๐
๐# โฑ ๐) โฑ ๐ง# โฎ ๐ง) ๐ง)*# โฎ ๐ง% = ๐#
"๐
โฎ ๐)
"๐
๐)*#
"
๐ โฎ ๐%
"๐
๐ง+ = ๐+
"๐
๐+ ๐ = 1,2, โฆ , ๐ What do we do when ๐ > ๐ ? Which choice of ๐ง+ will minimize ๐ ๐ = ๐พ ๐ ๐? ๐ง+ = 0, ๐ = ๐ + 1, โฆ , ๐ Set
We want to find the solution ๐ that satisfies ๐ป๐บ
" ๐พ#๐ = ๐ป๐บ ๐ฝ$ #๐ and also satisfies
min
๐
๐ ๐ Evaluate ๐ = ๐พ๐ = โฎ โฆ โฎ ๐& โฆ ๐) โฎ โฆ โฎ ๐ง& ๐ง" โฎ ๐ง) ๐ = 9
*+& )
๐ง* ๐๐ = 9
*+&
- !./
) (๐* #๐)
๐* ๐๐
Solving Least Squares Problem with SVD (summary)
- Find ๐ that satisfies min
๐
๐ โ ๐ฉ ๐ )
)
- Find ๐ that satisfies min
๐
๐ป๐บ ๐ โ ๐ฝ$
#๐ " "
- Propose ๐ that is solution of ๐ป๐บ ๐ = ๐ฝ5
(๐
- Evaluate: ๐ = ๐ฝ5
(๐
- Set: ๐ง$ = h
O" P" , if ๐$ โ 0
0, otherwise ๐ = 1, โฆ , ๐
- Then compute ๐ = ๐พ ๐
Cost:
๐ ๐ ๐ ๐# Cost of SVD: ๐(๐ ๐#)
- If ๐$โ 0 for โ๐ = 1, โฆ , ๐, then the solution ๐ = ๐พ
๐ป๐บ
Q!๐ฝ5 ( ๐ is
unique (and not a โchoiceโ).
- If at least one of the singular values is zero, then the proposed solution ๐ is
the one with the smallest 2-norm ( ๐ % is minimal ) that minimizes the 2-norm of the residual ๐ป๐บ ๐ โ ๐ฝ5
(๐ %
- Since ๐ % =
๐พ ๐ %= ๐ %, then the solution ๐ is also the one with the smallest 2-norm ( ๐ % is minimal ) for all possible ๐ for which ๐ฉ๐ โ ๐ % is minimal.
Solving Least Squares Problem with SVD (summary)
Solve ๐ฉ ๐ โ ๐ or ๐ฝ> ๐ป๐บ๐พ๐ผ๐ โ ๐ ๐ โ ๐พ ๐ป๐บ A ๐ฝ>
B ๐
Solving Least Squares Problem with SVD (summary)
Consider solving the least squares problem ๐ฉ ๐ โ ๐, where the singular value decomposition of the matrix ๐ฉ = ๐ฝ ๐ป ๐พ๐ผ๐ is: Determine ๐ โ ๐ฉ ๐ (
Example:
Example
Suppose you have ๐ฉ = ๐ฝ ๐ป ๐พ๐ผ๐ calculated. What is the cost of solving min
๐
๐ โ ๐ฉ ๐ %
% ?