Solved

Calculating best fit lines

Posted on 2006-06-12
15
799 Views
Last Modified: 2012-08-14
This is probably more of a maths question than a programming one, but I can't find a more suitable category.

I'm drawing best fit lines on graphs, and using the information here:

http://misc.connectfree.co.uk/graphs/

That page shows how to calculate the variables for the following lines:

y = a + bx
y = a + bx + cx^2

I've implemented them, and everything works fine. However, now I nee to do the same thing for these lines:

y = a + bx + cx^2 + dx^3
y = a + bx + cx^2 + dx^3 + ex^4
y = ab^x

But I can't find similar equations for calculating the replacements for a-e in those equations. I've found some stuff on the web, but nothing in that kind of format (the "first format" shown - which I can understand, and implement!).

Does anyone know how to calculate these?
0
Comment
Question by:Link-HRSystems
  • 7
  • 7
15 Comments
 
LVL 44

Expert Comment

by:Arthur_Wood
ID: 16884411
y = ab^x  can be converted into an equation of the form Y = Mx + B  by using Logarithms:

log(y) = x Log(b) + log(a)

As for the other polynomial forms:

http://mathworld.wolfram.com/LeastSquaresFittingPolynomial.html
or
http://www.efunda.com/math/leastsquares/lstsqrmdcurve.cfm

AW
0
 

Author Comment

by:Link-HRSystems
ID: 16884496
Those pages seem to show how to derive the equations I want, but it's all a little over my head I'm afraid!

I'm looking for the derived equations, in the format of the first two I have, for calculating a/b/c/d/e for the specific equations listed (similar to the page I noted for the first two). While these pages are more flexible (allowing me to go to the nth degree), I have set requirements, and don't really understand everything on those pages!

Do you know where I can find these already calculated? (I assume the first few must be fairly common?)
0
 

Author Comment

by:Link-HRSystems
ID: 16886267
Also, I'm trying to do, as suggested:

log(y) = x Log(b) + log(a)

in Actionscript, but I'm confused by the whole E/natural log thing, and the fact that there doesn't seem to be an "inverse log" method?

The Actionscript Math object is documented here:

http://livedocs.macromedia.com/labs/1/flex20beta3/langref/Math.html

Any ideas how to get y (not log(y)) given x, b and a in:

log(y) = x Log(b) + log(a)

?

I've tried ever combination of Math.log and Math.exp multiplying and dividing by the log constants there too!
0
 
LVL 44

Expert Comment

by:Arthur_Wood
ID: 16886982
the 'inverse log':

if Natural Log (ln (x)) then e^Ln(x)

if Log base 10, then 10^Log(x)

Take the Log of the X's and Y's, then fit the least squares to the Logs,  that will give you M and B  in  the equation Log(y) = M*Log(x) + B

then to get back to the a and b in y = ax^b===> b = 10^M, a = 10^B

AW
0
 
LVL 9

Expert Comment

by:gabeso
ID: 16888198
An equation such as:

y = a + bx + cx^2 + dx^3 + ex^4

is NOT a line - it is a curve.

What you have are a system of polynomials.

As this system is non-linear you can't use linear algebraic techniques to solve it directly without transformations as discussed above.

However there are numerical methods for doing this.
0
 
LVL 44

Accepted Solution

by:
Arthur_Wood earned 500 total points
ID: 16889304
gabeso,  actually, there is a standard method for getting a LEASR SQUARES best fit to the polynomial.  That is what is discussed in the links that I posted.  It involves soving a system of linear equations, in the set of parameters (in the case you show, a,b,c,d,e, which can be solved and result in equations to the parameters in terms of Sum(x), sum(x^2), sum(xy) sum(x^2*y) etc.  Thus for the 3rd order case:

n*a + Sum(x) * b + sum(x^2) *c + sum(x^3) * d = sum(y)
sum(x)*a + Sum(x^2) * b + sum(x^3) *c + sum(x^4) * d = sum(xy)
sum(x^2)*a + Sum(x^3) * b + sum(x^4) *c + sum(x^5) * d = sum(x^2y)
sum(x^3)*a + Sum(x^4) * b + sum(x^5) *c + sum(x^6) * d = sum(x^3y)

and when these 4 LINEAR equations are soloved (algebraically), the result is the LEAST SQUARES cubic that best fits the set of data points

and for the 4th order:

n*a + Sum(x) * b + sum(x^2) *c + sum(x^3) * d + sum(x^4) * e= sum(y)
sum(x)*a + Sum(x^2) * b + sum(x^3) *c + sum(x^4) * d + sum(x^5) * e= sum(xy)
sum(x^2)*a + Sum(x^3) * b + sum(x^4) *c + sum(x^5) * d + sum(x^6) * e= sum(x^2y)
sum(x^3)*a + Sum(x^4) * b + sum(x^5) *c + sum(x^6) * d + sum(x^7) * e= sum(x^3y)
sum(x^4)*a + Sum(x^5) * b + sum(x^6) *c + sum(x^7) * d + sum(x^8) * e= sum(x^4y)

which is again a system of linear equations in the 5 unknowns a,b,c,d and e.

AW



0
 

Author Comment

by:Link-HRSystems
ID: 16892305
Ok, logs.... I've taken my linear best fit line, and changed all sums to use logs, like this:

foreach (Point p in points)
        {
            sumOfProducts += Math.Log(p.X) * Math.Log(p.Y);
            sumOfX += Math.Log(p.X);
            sumOfY += Math.Log(p.Y);
            sumOfSquaresOfX += Math.Log(p.X) * Math.Log(p.X);
            sumOfSquaresOfY += Math.Log(p.Y) * Math.Log(p.Y);
        }

        double count = emps.Count;

        B =
            ((count * sumOfProducts) - (sumOfX * sumOfY))
            /
            ((count * sumOfSquaresOfX) - (sumOfX * sumOfX));

        A =
            (sumOfY - (B * sumOfX))
            /
            count;


And then to get Y from X, I have:

return Math.pow(Math.LOG2E, Math.log(A + (B * Math.log(x))))

But the curve is way off (not within area of the graph I'm looking at!). I'm a little confused by the equation with M and B and A and B. Can you see something simple there I've done wrong?


And for the other equations, I don't really understand what your big calculation is for, because it contains a/b/c/d/e, and it's those values that I'm trying to calculate. Once I've got those, it's easy to put them into something like y = a + bx + cx^2 + dx^3 + ex^4 ?
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Author Comment

by:Link-HRSystems
ID: 16892335
Gabeso, check the page I linked to:

http://misc.connectfree.co.uk/graphs/

That shows how I'm doing it for:

    y = a + bx + cx^2

Which is also a curve. I just want to do the same thing for the other equations I gave
0
 

Author Comment

by:Link-HRSystems
ID: 16892817
I've sorted the Log one out by looking through the Pascal code of our last app (and now AW's response makes a bit more sense). I still don't have a clue about the others though!
0
 
LVL 44

Expert Comment

by:Arthur_Wood
ID: 16893202
if you work out the solution to the 3rd order linear equations that I gave you, you will get:

a = (sum(y) - b*sum(x) - c*sum(x^2) - d*sum(x^3))/n

b = (n*sum(xy) - sum(x) * sum(y) + c*(sum(x) * sum(x^2) - n*sum(x^3)) + d*(sum(x) * sum(x^3) - n*sum(x^4))/(n*sum(x^2) - (sum(x))^2)

and so on.. The rest is just a little applied algebra, and is actaully quite straight forward.  You have already doe essentiall the same thing, in developing you formulae for the 2nd order case.  Just apply the same algorithm here, and in the 4th order case.

AW
0
 

Author Comment

by:Link-HRSystems
ID: 16893228
It's that "and so on" bit I don't get. I didn't derive the formular for the 2nd order case, I took it from http://misc.connectfree.co.uk/graphs/

I don't understand how you got the equations for a and b that you just posted, but that's exactly what I want (a - d, and a - e for the 3rd and 4th order cases)!
0
 
LVL 44

Expert Comment

by:Arthur_Wood
ID: 16898355
take this equation:

n*a + Sum(x) * b + sum(x^2) *c + sum(x^3) * d = sum(y)

now divide both sides by n:

a + Sum(x) * b/n + sum(x^2) *c/n + sum(x^3) * d/n = sum(y)/n


now subtract from both sides leaving only a on the left:

a = (sum(y)- Sum(x) * b - sum(x^2) *c - sum(x^3) * d ) /n


and for the second equation:

sum(x)*a + Sum(x^2) * b + sum(x^3) *c + sum(x^4) * d = sum(xy)


divide both sides by Sum(X^2):


sum(x)*a/Sum(x^2) +  b + (sum(x^3)/Sum(x^2)) *c + (sum(x^4)/Sum(x^2)) * d = sum(xy)/Sum(x^2)

now subtract from both sided, leaving the b on the left:

b = sum(xy)/Sum(x^2) -sum(x)*a/Sum(x^2)  - (sum(x^3)/Sum(x^2)) *c - (sum(x^4)/Sum(x^2)) * d

and then substitute teh earlier equation for a, and rearrange the terms to get:

b = (n*sum(xy) - sum(x) * sum(y) + c*(sum(x) * sum(x^2) - n*sum(x^3)) + d*(sum(x) * sum(x^3) - n*sum(x^4))/(n*sum(x^2) - (sum(x))^2)


very elementary algebra.

AW
0
 
LVL 44

Expert Comment

by:Arthur_Wood
ID: 16898403
now if you have trouble with all of the Sum(x), sum(x^2)... terms, then express this equation:

n*a + Sum(x) * b + sum(x^2) *c + sum(x^3) * d = sum(y)
sum(x)*a + Sum(x^2) * b + sum(x^3) *c + sum(x^4) * d = sum(xy)
sum(x^2)*a + Sum(x^3) * b + sum(x^4) *c + sum(x^5) * d = sum(x^2y)
sum(x^3)*a + Sum(x^4) * b + sum(x^5) *c + sum(x^6) * d = sum(x^3y)

as

n*a + A* b + B *c + C * d = Y1
A * a + B * b + C *c + D * d = Y2
B * a + C * b + D *c + E * d = Y3
C * a + D * b + E *c + F * d = Y4


where :
A = Sum(x)
B = Sum(x^2)
C = sum(x^3)
D = sum(x^4)
E = sum(x^5)
F = Sum(x^6)

and

Y1 = sum(y)
Y2 = Sum(x * y)
Y3 = sum(x^2 * y)
Y4 = sum(x^3 * y)


this might make the algebra a bit easier.

AW
0
 

Author Comment

by:Link-HRSystems
ID: 16900526
*bangs head on desk*

Vry elementary algebra indeed! It all makes perfect sense now!

Thanks for your help!
0
 
LVL 44

Expert Comment

by:Arthur_Wood
ID: 16906320
glad to be of assistance

AW
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Suggested Solutions

RIA (Rich Internet Application) tools are interactive internet applications which have many of the characteristics of desktop applications. The RIA tools typically deliver output either by the way of a site-specific browser or via browser plug-in. T…
A short article about problems I had with the new location API and permissions in Marshmallow
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now