[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

RE: Precision Errors



PureBytes Links

Trading Reference Links

Clarification:

> Neal was not speaking about Brickey's code.  He was comparing a 
> corellation calculation on TS, Excel, and C++ -- presumably using 
> TS's bogus Correlation function, which DOES NOT accumulate errors.  
> So the CurrentBar was irrelevant.

Neal just responded to me.  He was NOT using Correlation(), but 
coeffP().  That function computes a correlation of the Close (the 
dependent variable) to CurrentBar (the independent variable).  So 
since coeffP() squares the independent & dependent variables, large 
values of CurrentBar *would* affect the precision of the answer.

Interestingly enough, my CorrelationP() function doesn't exactly 
match the coeffP() function.  (You have to pass it BarNumber instead 
of CurrentBar, because CurrentBar[1] is garbage.)  coeffP() uses a 
slightly different equation.  Instead of 

         r =     N SUMXY - SUMX * SUMY
                 ---------------------
   SquareRoot((N*SUMX2 - SUMX*SUMX) * (N*SUMY2 -SUMY*SUMY) )

as used in Brickey's code and my CorrelationP, coeffP uses


         r =     SUMXY - N * AVERAGE(X)*AVERAGE(Y)
                -----------------------------------
    SquareRoot((SUMX2 - N*AVERAGE(X)^2) * (SUMY2 - N*AVERAGE(Y)^2))

(see code below)

That version seems to have been chosen to use as many built-in EL 
functions as possible (and it's a LOT less efficient than Brickey's), 
but I think the equations are equivalent.  But the results can be 
different by several percent, presumably due to single-precision 
errors propagating differently in the two different calculations.

Gary



coeffP code:

UpEQ = Summation(X * Y, Length) - Length * Average(X, Length) * Average(Y, Length);
LowEQ1 = Summation(Square(X), Length) - Length * Square(Average(X, Length));
LowEQ2 = Summation(Square(Y), Length) - Length * Square(Average(Y, Length));

If LowEQ1 * LowEQ2 > 0 Then
	LowEQT = SquareRoot(LowEQ1 * LowEQ2);

If LowEQT <> 0  Then Begin
	R = UpEQ / LowEQT;
	If R <= 1 AND R >= -1 Then
		coeffR = R;
End;