Link-HRSystems
asked on
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?
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?
ASKER
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?)
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?)
ASKER
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!
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!
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
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
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.
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.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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 ?
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 ?
ASKER
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
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
ASKER
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!
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
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
ASKER
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)!
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)!
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
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
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
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
ASKER
*bangs head on desk*
Vry elementary algebra indeed! It all makes perfect sense now!
Thanks for your help!
Vry elementary algebra indeed! It all makes perfect sense now!
Thanks for your help!
glad to be of assistance
AW
AW
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