17. Line Fitting Least-squares straight-line fitting€¦ · 17. Line Fitting A very common problem...

5
ESS 522 2014 17-1 17. Line Fitting A very common problem in data analysis is looking for relationships between different parameters and fitting lines or surfaces to data. The simplest example is fitting a straight line and we will discuss that here – it is also covered in Chapter 4 of Pal Wessel’s notes. Least-squares straight-line fitting The process of fitting a straight line is one of the simplest examples of an inverse problem. For n pairs of data points X i , Y i , i = 1, 2, … n and fit the data with a relationship y i = a + bX i (17-1) where y i is the predicted value of Y i . We can use the χ 2 statistic to measure the misfit χ 2 a, b ( ) = Y i a bX i σ i 2 i =1 n (17-2) where σ i are the uncertainties (or estimates of the uncertainties which can be set to unity in the absence of better knowledge) in the Y values. Our goal is to find the values of a and b that mimimize χ 2 . To do this we find take the partial derivates of χ 2 with respect to a and b and solve for the values of a and b at which they are both zero χ 2 a = 2 Y i a bX i σ i 2 i =1 n = 0 χ 2 b = 2 X i Y i a bX i σ i 2 i =1 n = 0 (17-3) If we define S = 1 σ i 2 i =1 n , S X = X i σ i 2 i =1 n , S Y = Y i σ i 2 , S XX = X i 2 σ i 2 i =1 n , S XY = X i Y i σ i 2 i =1 n i =1 n (17-4) then equation (7) reduces to aS + bS X = S Y aS X + bS XX = S XY (17-5) The solution is a = S XX S Y S X S XY Δ b = SS XY S X S Y Δ (17-6) with Δ = SS XX S X 2 (17-7) We can also estimate the uncertainties in a and b. To do this we sum the variance in a and b resulting from the variance in each of the Y values. This can be written mathematically

Transcript of 17. Line Fitting Least-squares straight-line fitting€¦ · 17. Line Fitting A very common problem...

Page 1: 17. Line Fitting Least-squares straight-line fitting€¦ · 17. Line Fitting A very common problem in data analysis is looking for relationships between different parameters and

ESS 522 2014

17-1

17. Line Fitting

A very common problem in data analysis is looking for relationships between different parameters and fitting lines or surfaces to data. The simplest example is fitting a straight line and we will discuss that here – it is also covered in Chapter 4 of Pal Wessel’s notes. Least-squares straight-line fitting The process of fitting a straight line is one of the simplest examples of an inverse problem. For n pairs of data points Xi, Yi, i = 1, 2, … n and fit the data with a relationship yi = a + bXi (17-1) where yi is the predicted value of Yi. We can use the χ2 statistic to measure the misfit

χ 2 a,b( ) = Yi − a − bXi

σ i

⎝⎜⎞

⎠⎟

2

i=1

n

∑ (17-2)

where σi are the uncertainties (or estimates of the uncertainties which can be set to unity in the absence of better knowledge) in the Y values. Our goal is to find the values of a and b that mimimize χ 2. To do this we find take the partial derivates of χ 2 with respect to a and b and solve for the values of a and b at which they are both zero

∂χ 2

∂a= −2

Yi − a − bXi

σ i2

⎝⎜⎞

⎠⎟i=1

n

∑ = 0

∂χ 2

∂b= −2 Xi

Yi − a − bXi

σ i2

⎝⎜⎞

⎠⎟i=1

n

∑ = 0

(17-3)

If we define

S = 1

σ i2

i=1

n

∑ , SX =Xi

σ i2

i=1

n

∑ , SY =Yi

σ i2 , SXX =

Xi2

σ i2

i=1

n

∑ , SXY =XiYi

σ i2

i=1

n

∑i=1

n

∑ (17-4)

then equation (7) reduces to

aS + bSX = SY

aSX + bSXX = SXY

(17-5)

The solution is

a =SXX SY − SX SXY

Δ

b =SSXY − SX SY

Δ

(17-6)

with Δ = SSXX − SX

2 (17-7) We can also estimate the uncertainties in a and b. To do this we sum the variance in a and b resulting from the variance in each of the Y values. This can be written mathematically

Page 2: 17. Line Fitting Least-squares straight-line fitting€¦ · 17. Line Fitting A very common problem in data analysis is looking for relationships between different parameters and

ESS 522 2014

17-2

sa2 = σ i

2 ∂a∂Yi

⎝⎜⎞

⎠⎟i=1

n

∑2

sb2 = σ i

2 ∂b∂Yi

⎝⎜⎞

⎠⎟i=1

n

∑2 (17-8)

After substituting derivatives obtained from equation (17-6) and a fair amount of manipulation we get

sa2 =

SXX

Δ

sb2 = S

Δ

(17-9)

We can also estimate the covariance of the uncertainties in a and b

sab2 = σ i

2 ∂a∂Yi

⎝⎜⎞

⎠⎟∂b∂Yi

⎝⎜⎞

⎠⎟= −

SxΔi=1

n

∑ (17-10)

Our estimate of the correlation coefficient between a and b, becomes

r =sabsasb

=−SXSSXX

. (17-11)

If we assume our estimates of the uncertainty in Y are correct, we can check if the fit is adequate (significant) at the α level by comparing our value of χ 2 to the critical χα

2 for n-2 degrees of freedom. Provided it does not exceed this value then the data is fit adequately by the straight line. We can test the significance of the correlation of x and y, by applying the t-statistic with n-2 degrees of freedom to determine whether the slope and our estimate of its uncertainty are significantly different from 0

t =

b − 0( )sb

(17-12)

We can write the 95% confidence limits for b as b ± t0.025sb (17-13) If these limits enclose zero we cannot be confident that x and y are correlated at the 95% level. If we do not know the uncertainty of our data but know that the straight-line model is correct, then we can initially assume an uncertainty of 1 for the purpose of getting a straight line fit and then estimate it from the residuals according to

s2 = 1

n − 2Yi − a − bXi( )2

i=1

n

∑ (17-14)

We can use s2 in place of the population variance σ2 to estimate the slope uncertainties but this will lead to a underestimate of the uncertainty for small n because there is additional uncertainty arising from using an estimate of σ2 and not its true value.

Page 3: 17. Line Fitting Least-squares straight-line fitting€¦ · 17. Line Fitting A very common problem in data analysis is looking for relationships between different parameters and

ESS 522 2014

17-3

(Note that Paul Wessel uses σb etc instead of sb in section 4.1 but this is not consistent with the notation he introduces in Chapter 1 and that we have used since sb is clearly an estimate based on a limited sample of points not the entire population) Line fitting with errors in x and y It is important to note that equation (17-2) assumes that our determinations of x have no uncertainty. In some instances this is a good assumption – for example our determinations of time or spatial coordinate will often have negligible uncertainty. For other instances it is a poor approximation – for example if we plot the concentration of two dissolved chemicals in seawater or two trace elements in a rock, they may both have similar analytical errors. If we have errors in both variables then a better measure of misfit is given by

E =yi −Yiσ y ,i

⎝⎜

⎠⎟

2

+xi − Xiσ x ,i

⎝⎜

⎠⎟

2⎡

⎢⎢

⎥⎥i=1

n

∑ (17-15)

where Xi and Yi are the observed data and xi and yi are the modeled values that are required to lie on a straight line yi = a + bxi (17-16) Our goal is to find the values of a and b that minimize E. To do this we use the method of Lagrange Multipliers. We can write equation (17-16) as fi = a + bxi − yi = 0 (17-17) and since the fi values are constrained to be zero we can write equation (17-15) as

E =yi −Yiσ y ,i

⎝⎜

⎠⎟

2

+xi − Xiσ x ,i

⎝⎜

⎠⎟

2

+ 2λi fi⎡

⎢⎢

⎥⎥i=1

n

∑ (17-18)

where the 2λi values are unknown constant Lagrance multipliers and the factor of 2 is for algebraic convenience. We now set the partial derivatives of E to zero to find the values that give a minimum

∂E∂xi

= ∂E∂yi

= ∂E∂a

= ∂E∂b

= 0

Now if we make the assumption that all the sigma values are equal to unity this gives

∂E∂xi

= ∂∂xi

xi − Xi( )2+ ∂∂xi

λibxi( ) = 2 xi − Xi( ) + 2bλi = 0 (17-19)

∂E∂yi

= ∂∂yi

yi −Yi( )2 − ∂∂yi

λi yi( ) = 2 yi −Yi( )− 2λi = 0 (17-20)

∂E∂a

= 2 ∂∂a

λia( )⎡

⎣⎢

⎦⎥

i=1

n

∑ = 2 λii=1

n

∑ = 0 (17-21)

∂E∂b

= 2 ∂∂a

λibxi( )⎡

⎣⎢

⎦⎥

i=1

n

∑ = 2 λii=1

n

∑ xi = 0 (17-22)

From equations (17-19) and (17-20) we can write

Page 4: 17. Line Fitting Least-squares straight-line fitting€¦ · 17. Line Fitting A very common problem in data analysis is looking for relationships between different parameters and

ESS 522 2014

17-4

xi = Xi − bλi

yi = Yi + λi

(17-23)

Substituting for xi and yi in equation (17-16) yields Yi + λi = a + b Xi − bλi( ) = a + bXi − b2λi (17-24)

Solving for λi yields

λi =a + bXi −Yi1+ b2

(17-25)

Substituting for λi into equation (17-21) yields

a + bXi −Yi1+ b2i=1

n

∑ = 0 (17-26)

Substituting for λi from equations (17-25) and for xi from (17-23) into equation (17-22) yields

a + bXi −Yi1+ b2

⎛⎝⎜

⎞⎠⎟i=1

n

∑ Xi − bλi( ) = aXi + bXi2 −YiX i

1+ b2i=1

n

∑ − ba + bXi −Yi1+ b2

⎛⎝⎜

⎞⎠⎟

2

i=1

n

∑ = 0 (17-27)

We now have reduced the n + 2 equations for a, b and λi to 2 equations (17-26 and 17-27) for a and b. Since the denominator in equation (17-26) cannot reduce to zero, we can write

a = na = Yii=1

n

∑ − b Xii=1

n

∑i=1

n

∑⇒ a = Y − bX

(17-28)

where X and Y are the mean values of the data. We can substitute equation (17-28) into equation (17-27), multiply by (1 + b2)2, and use the variables Ui = Xi - X and Vi = Yi -Y and after a few lines of manipulation get

b2UiVi + b Ui2 −Vi

2( )i−UiVi⎡

⎣⎤⎦

i=1

n

∑ = 0 . (17-29)

This has the solution

b =Vi2

i=1

n

∑ − Ui2

i=1

n

∑ ± Ui2

i=1

n

∑ − Vi2

i=1

n

∑⎛⎝⎜

⎞⎠⎟

2

+ 4 UiVii=1

n

∑⎛⎝⎜⎞⎠⎟

2

2 UiVii=1

n

∑ (17-30)

There are two solutions for b (each with a corresponding value of a from equation 17-28), one that minimizes E and a second that gives a perpendicular line that maximizes E. Robust Line Fitting In a least squares line in which we assume all the data have the same uncertainty we seek to minimize

Page 5: 17. Line Fitting Least-squares straight-line fitting€¦ · 17. Line Fitting A very common problem in data analysis is looking for relationships between different parameters and

ESS 522 2014

17-5

Minimizea,b

E = Yi − a − bXi( )2i=1

n

∑ = ri2

i=1

n

∑ (17-31)

This process is sensitive to outliers, particularly so when the outliers lie near the lower or upper limits of the range of xi. The breakdown point for the least squares line fit (L2 regression) is 1/n. We can overcome this problem to a small extent by minimizing the sum of the absolute misfits (L1 regression)

Minimizea,b

E = rii=1

n

∑ (17-32)

but the L1 norm also has a breakdown point of 1/n. A robust approach with a breakdown point of ½ is to minimize the median misfit. Minimize

a,bmedian ri = median Yi − a − bXi (17-33)

This is equivalent to finding the narrowest strip that encloses half the points. The only way to do this, is by a systematic search through different values of b. For each value of b we calculate Yi - bXi, and then find the value of a that minimizes the median of |Yi - bXi - a|. One then chooses the a and b values that gives the minimum median among all the values of b analyzed. One can use this robust statistical method to find and eliminate outliers

Yi − a − bXi

Median Yi − a − bXi> zcut (17-34)

where a value of zcut =4.45 is equivalent to 3 standard deviations for a normal distribution. Once the outliers are eliminated, one can apply the least squares line fitting approach.