[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 810
  • Last Modified:

Calculating best fit lines

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
Link-HRSystems
Asked:
Link-HRSystems
  • 7
  • 7
1 Solution
 
Arthur_WoodCommented:
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
 
Link-HRSystemsAuthor Commented:
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
 
Link-HRSystemsAuthor Commented:
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
Arthur_WoodCommented:
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
 
gabesoCommented:
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
 
Arthur_WoodCommented:
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
 
Link-HRSystemsAuthor Commented:
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
 
Link-HRSystemsAuthor Commented:
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
 
Link-HRSystemsAuthor Commented:
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
 
Arthur_WoodCommented:
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
 
Link-HRSystemsAuthor Commented:
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
 
Arthur_WoodCommented:
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
 
Arthur_WoodCommented:
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
 
Link-HRSystemsAuthor Commented:
*bangs head on desk*

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

Thanks for your help!
0
 
Arthur_WoodCommented:
glad to be of assistance

AW
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 7
  • 7
Tackle projects and never again get stuck behind a technical roadblock.
Join Now