Page 1 of 1

Wrong correlation coefficient

Posted: Fri Apr 06, 2007 12:00 pm
by 5886443
We use TeeChartPro V7 in Borland c++ builder.
On a graph, we display the correlation function, which is a polynom
of degree 1 (f(x)=ax + b
The graph is correct.
The coefficient for degree 1 is correct.
The coefficient for degree 0 is INCORRECT.
We use AnswerMethod to get the coefficients.

Posted: Tue Apr 10, 2007 1:21 pm
by narcis
Hi provis,

Could you please send us a simple example project we can run "as-is" to reproduce the problem here? You can either post your files at news://www.steema.net/steema.public.attachments newsgroup or at our upload page.

Thanks in advance.

Posted: Fri Apr 13, 2007 2:16 pm
by 5886443
hi Narcis,
I will try to prepare a test case.
Until that, could you confirm you are not aware of known bugs
with correlation feature in your component.
thk

Posted: Fri Apr 13, 2007 3:01 pm
by narcis
Hi provis,

Are you using TCustomTeeFunction for that?

Posted: Tue Jul 10, 2007 7:39 am
by 5886443
Hi Narcis,
sorry for the late reply.
To get the coefficient factors, we use the TCurveFittingFunction
and AnswerVector.
I have made another simple test case, which unfortunately I cannot ship because it uses some internal classes.
Here is the description of my test, which you should be able to reproduce easily.
I have 2 series.
Serie 1 contains values 1,2,3,4,5,6
Serie 2 contains values 2000,20,30,40,50,60

We plot serie 2 against serie 1.
We draw the correlation polynom of degree 1.
The drawing is correct (polynom is Y=-274.29X+1326.7).
But when we ask for correlatrion factors using AnswerVector,
coefficient of degree 1 is correct, but coefficient of degree 0 is not.
It returns 1032 instead of 1326.
If I use a polynom of degree 2, only coefficient of degree 2 is correct.
Back to the polynom of degree 1, if I use function GetCurveYValue
with parameter X=0, I get 1326.7, which proves that the polynom
is drawn correctly.
It looks there is only a problem with AnswerVector.
Thanks for your help

Posted: Tue Jul 10, 2007 2:17 pm
by Marjan
Hi.

I think the problem is the AnswerVector allways returns the coefficients of normalized equation.

Normalizing values prior to fitting avoids fp overflows when large x or y values are used. In short, fitting is performed on x=x-xmin and y=y-ymin values. If you take this into the account, AnswerVector will return correct polynomial coefficients.

Posted: Tue Jul 10, 2007 3:18 pm
by 5886443
thanks for the reply but sorry, I did not understand it.
What do you mean by normalized equation ?
What is fp overflow ?
What should I do to get the correct coefficient ?
How come the coefficient for the highest degree is correct and not the others ?
thanks

Posted: Wed Jul 11, 2007 7:13 am
by Marjan
5886443 wrote:What do you mean by normalized equation ?
What should I do to get the correct coefficient ?
Normalized (in this case) means *all* that minimum x value is subtracted from all x values and minimum y value is subtracted from all y values. Then the actual fitting is performed on these normalized values. The actual fitting is therefore not done on:

Code: Select all

y=a(n)*x^n + a(n-1)*x^(n-1) +...  + a0
but

Code: Select all

y-ymin = b(n)*(x-xmin)^n + b(n-1)*(x-xmin)^(n-1) + ... + b0
where the a(n) and b(n) differ. The AnswerVector will allways return b(n) coefficients and if you want to get a(n), use the above relations to do transformation.
5886443 wrote:What is fp overflow ?
Floating point overflows - this happens if the numbers in fitting algorithm get too big.
5886443 wrote: How come the coefficient for the highest degree is correct and not the others ?
See my explanation above.

Posted: Wed Jul 11, 2007 9:16 am
by 5886443
thanks a lot, this is clear now;
If I understand you correctly, to get the expected factors,
I need to modify my series (add xmin to all x, add ymin to all y),
before using AnswerVector ?
Or is there another method/property I could use to get directly
the coefficients used to draw the polynom (the polynom
is not drawn using normalized equation)
thanks

Posted: Mon Jul 16, 2007 6:36 am
by Marjan
Hi.

Not quite. Simply adding xmin and ymax won't do the trick. For example, "normalized" 1st degree equation is:

Code: Select all

y-ymin = b(1)*(x-xmin)+b(0)
By expanding this to:

Code: Select all

y = b(1)*x - b(1)*xmin+b(0)+ymin
you get the usuall a(1) and a(0) coefficients which define equation

Code: Select all

y=a(1)*x+a(0)
...
a(1) = b(1)
a(0) = -b(1)*xmin+b(0)+ymin
a(1) and a(0) are the coefficients you're after. For higher order polynomial you can use the same approach (although the formulas for a(i) will be a bit longer).

Posted: Tue Feb 05, 2008 5:31 pm
by 4213201
Marjan wrote:Hi.

Not quite. Simply adding xmin and ymax won't do the trick. For example, "normalized" 1st degree equation is:

Code: Select all

y-ymin = b(1)*(x-xmin)+b(0)
By expanding this to:

Code: Select all

y = b(1)*x - b(1)*xmin+b(0)+ymin
you get the usuall a(1) and a(0) coefficients which define equation

Code: Select all

y=a(1)*x+a(0)
...
a(1) = b(1)
a(0) = -b(1)*xmin+b(0)+ymin
a(1) and a(0) are the coefficients you're after. For higher order polynomial you can use the same approach (although the formulas for a(i) will be a bit longer).
I've been trying to expand your equations to 5th order but I am getting completely incorrect results. Could you please provide the formula to obtain coefficients a(2), a(3) and a(4). Thank you in advance.

Posted: Wed Feb 06, 2008 10:37 am
by narcis
Hi MikeC,

The equation is different for each polynomial degree. You cannot apply 1st degree polynomial equation to 5th degree polynomial.

You shoud write out the 5th polynomial equation and use the same principle to extract coefficients a(0) to a(4). It's not complicated, but the equations are quite long. We suggest you to use Mathematica to extract a(0)..a(4) from system of equations.

Re: Wrong correlation coefficient

Posted: Mon May 02, 2011 9:36 am
by 15659034
And for whatever it is worth here are the conversions for second and third order polynominals.

Code: Select all

'y = a(2)*x^2 + a(1)*x + a(0)
'y-ymin = b(2)*(x-xmin)^2 + b(1)*(x-xmin) + b(0)

a(2) = b(2)
a(1) = b(1) - 2*b(2)*xmin
a(0) = b(2)*Math.Pow(xmin,2) - b(1)*xmin + b(0) + ymin

Code: Select all

'y = a(3)*x^3 + a(2)*x^2 + a(1)* x + a(0)
'y-ymin = b(3)*(x-xmin)^3 + b(2)*(x-xmin)^2 + b(1)*(x-xmin) + b(0)

a(3) = b(3)
a(2) = b(2) - 3*b(3)*xmin
a(1) = 3*b(3)*Math.Pow(xmin, 2) - 2*b(2)*xmin + b(1)
a(0) = -b(3)*Math.Pow(xmin, 3) + b(2)*Math.Pow(xmin, 2) - b(1)*xmin + b(0) + ymin


Re: Wrong correlation coefficient

Posted: Sat Aug 04, 2012 10:07 am
by 15660198
I use teechartnet2010.

I need to use the polynomial coefficient with a polynomial degree 6.

I understand that your coefficient is for normalised value.

I draw a series point1.

I Apply polyfitting function on this curve. every things is OK.

I change polynomial coefficient by :

double[] tab_temp_poly = new double[polyFitting1.PolyDegree];

xmin = points1.MinXValue;
ymin = points1.MinYValue;

tab_temp_poly[5] = polyFitting1.Coefficient(5);
tab_temp_poly[4] = polyFitting1.Coefficient(4) - 5 * polyFitting1.Coefficient(5) * xmin;
tab_temp_poly[3] = polyFitting1.Coefficient(3) + 10 * polyFitting1.Coefficient(5) * Math.Pow(xmin, 2)
- 4 * polyFitting1.Coefficient(4) * xmin;
tab_temp_poly[2] = polyFitting1.Coefficient(2) - 10 * polyFitting1.Coefficient(5) * Math.Pow(xmin, 3)
+ 4 * polyFitting1.Coefficient(4) * Math.Pow(xmin, 2) - 3 * polyFitting1.Coefficient(3) * xmin;
tab_temp_poly[1] = polyFitting1.Coefficient(1) + 5 * polyFitting1.Coefficient(5) * Math.Pow(xmin,4)
- 4 * polyFitting1.Coefficient(4) * Math.Pow(xmin,3) + 3 * polyFitting1.Coefficient(3) * Math.Pow(xmin,2)
- 2 * polyFitting1.Coefficient(2) * xmin;
tab_temp_poly[0] = polyFitting1.Coefficient(0)
- polyFitting1.Coefficient(5) * Math.Pow(xmin, 5)
+ polyFitting1.Coefficient(4) * Math.Pow(xmin, 4)
- polyFitting1.Coefficient(3) * Math.Pow(xmin, 3)
+ polyFitting1.Coefficient(2) * Math.Pow(xmin, 2)
- polyFitting1.Coefficient(1) * xmin
+ ymin;

I draw the curve with this coefficient and I get bad result.

what do you think of this?

can you help me?

thanks

thierry

Re: Wrong correlation coefficient

Posted: Sat Aug 04, 2012 10:45 am
by 15660198
sorry it's mathematical error
now I do

double[] tab_temp_poly = new double[polyFitting1.PolyDegree];

tab_temp_poly[5] = polyFitting1.Coefficient(5);
tab_temp_poly[4] = polyFitting1.Coefficient(4) - 5 * polyFitting1.Coefficient(5) * xmin;
tab_temp_poly[3] = polyFitting1.Coefficient(3) + 10 * polyFitting1.Coefficient(5) * Math.Pow(xmin, 2)
- 4 * polyFitting1.Coefficient(4) * xmin;
tab_temp_poly[2] = polyFitting1.Coefficient(2) - 10 * polyFitting1.Coefficient(5) * Math.Pow(xmin, 3)
+ 6 * polyFitting1.Coefficient(4) * Math.Pow(xmin, 2) - 3 * polyFitting1.Coefficient(3) * xmin;
tab_temp_poly[1] = polyFitting1.Coefficient(1) + 5 * polyFitting1.Coefficient(5) * Math.Pow(xmin,4)
- 4 * polyFitting1.Coefficient(4) * Math.Pow(xmin,3) + 3 * polyFitting1.Coefficient(3) * Math.Pow(xmin,2)
- 2 * polyFitting1.Coefficient(2) * xmin;
tab_temp_poly[0] = polyFitting1.Coefficient(0)
- polyFitting1.Coefficient(5) * Math.Pow(xmin, 5)
+ polyFitting1.Coefficient(4) * Math.Pow(xmin, 4)
- polyFitting1.Coefficient(3) * Math.Pow(xmin, 3)
+ polyFitting1.Coefficient(2) * Math.Pow(xmin, 2)
- polyFitting1.Coefficient(1) * xmin
+ ymin;

every things are ok.