Derivation of Line of Best Fit

Click For Summary
SUMMARY

The discussion focuses on deriving the coefficients A and B for the line of best fit using the least squares method. The objective is to minimize the sum of squared vertical distances, represented by the equation D = ∑[yi - f(xi)]², where f(x) = Ax + B. Participants emphasize the necessity of taking partial derivatives of D with respect to A and B, setting them to zero to find explicit expressions for A and B. The process involves rewriting the equations to isolate A and B, ultimately leading to a solution that minimizes deviation from the data points.

PREREQUISITES
  • Understanding of linear equations and the concept of a line of best fit.
  • Familiarity with calculus, specifically differentiation and partial derivatives.
  • Knowledge of the least squares method for optimization.
  • Ability to manipulate algebraic expressions and solve linear equations.
NEXT STEPS
  • Study the derivation of the least squares method in detail.
  • Learn how to apply partial derivatives in multivariable calculus.
  • Explore the implications of using the least squares method with larger datasets.
  • Investigate alternative methods for fitting lines to data, such as polynomial regression.
USEFUL FOR

Students studying statistics, data analysts, and anyone interested in understanding linear regression and optimization techniques in data fitting.

tmobilerocks
Messages
25
Reaction score
0

Homework Statement


Consider a set of data points: (x1, y1), (x2,y2). One seeks to find the best coefficients A and B such that the sum of squared vertical distances of the data f(x) = Ax + B is minimized. Let D = ∑[yi - f(xi]2. By requiring the derivatives of D respect to both A and B each to vanish, find expressions for the values of A and B in terms of the data points. Why are these derivatives made to vanish?

Homework Equations


Line of best fit is a linear equation: f(x) = Ax + B
D must be a minimum

The Attempt at a Solution


I am totally lost by this question. I do not understand how to differentiate D with respect to these parameters (maybe implicit differentiation?)
 
Physics news on Phys.org
You have ##D(A, B) = [y_1 - f(A, B, x_1)]^2 + [y_2 - f(A, B, x_2)]^2 ##. ##x_1, x_2, y_1, y_2## are all known and constant, ##A, B## are unknown variables.
 
Am I supposed to differentiate D? How would I go about doing that?
 
tmobilerocks said:
Am I supposed to differentiate D? How would I go about doing that?

Yes, that is what you are supposed to do. First, though, you need to express ##D## in more explicit form; just use the equation for ##f(x)## at ##x = x_1## and ##x = x_2## to get a sum that contains ##A, B## in fairly simple form. At that point you are supposed to know what to do next, using what you learned in calculus 101.

Are you sure you copied the problem correctly? For just two points you don't need to bother with this "least squares" method; it works, but is unnecessary. However, if you had more than two points you would soon discover the value of the method. BTW: it is almost as easy to do with 1000 points as with two points; all that happens is that you need to evaluate bigger sums. Try it out to see what I mean.
 
Consider the following: If f(x) = Ax + B and D = ∑[yi - f(xi]2, then

D=\sum_{i=1}^n(y_i-Ax_i-B)^2
Take the partial derivative of D with respect to A and set it equal to zero.
Take the partial derivative of D with respect to B, and set it equal to zero.
Combine the coefficients of A and of B in the resulting equations.
Solve for A and B.

What you are doing is minimizing the deviation of the straight line from the data points (where the data points have experimental error, and thus do not all lie on anyone straight line).
 
Is this correct?

I took the partial of D with respect to A, and set it equal to zero. I obtained the result

0 = -2xiyi + 2Axixi - 2Bxi

I then took the partial of D with respect to B, and set it equal to zero. The result I obtained was

0 = -2yi + 2Axi + 2B

After combining these two expressions, and solving for A and B I obtained:

A = (-xiyi + yi -Bx - B)/(xi-xixi)

B = (-xiyi + yi - Axi + Axixi)/(1+x)
 
I do not see how you solved for A and B if both sides of the equation contain them.
 
Am I on the right track? Is there a simpler way to solve this? Combining both of them makes it nearly impossible to solve
 
What is so impossible about that?

The first equation can be rewritten as ## 0 = p + q A + r B ##, where ##p, q, r ## are all made of the known ##x_i, y_i## values. The second equation can be rewritten as ## 0 = u +v A + w B ## in a similar way. Surely you can then solve two linear equations for A and B.
 
  • #10
tmobilerocks said:
Is this correct?

I took the partial of D with respect to A, and set it equal to zero. I obtained the result

0 = -2xiyi + 2Axixi - 2Bxi

I then took the partial of D with respect to B, and set it equal to zero. The result I obtained was

0 = -2yi + 2Axi + 2B

After combining these two expressions, and solving for A and B I obtained:

A = (-xiyi + yi -Bx - B)/(xi-xixi)

B = (-xiyi + yi - Axi + Axixi)/(1+x)

In the following two equations, you need to have summation signs on each of the terms, where the summations are over all the data points.

0 = -2xiyi + 2Axixi - 2Bxi

0 = -2yi + 2Axi + 2B

Once you get the summations in there, you factor out the A and B, and solve these two equations for the two unknowns A and B.

Chet
 

Similar threads

Replies
2
Views
2K
Replies
3
Views
2K
Replies
22
Views
3K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 24 ·
Replies
24
Views
3K
  • · Replies 1 ·
Replies
1
Views
1K
  • · Replies 14 ·
Replies
14
Views
4K
  • · Replies 2 ·
Replies
2
Views
2K
Replies
17
Views
3K
Replies
12
Views
4K