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

S_Correlation_Moment user function



PureBytes Links

Trading Reference Links

>From  Bob Brickey's  Scilink  forum

Sincerely,

Pierre Orphelin
www.sirtrade.com
TradeStation Technologies representative in France 


 
  {****************************************************************

	PROGRAM   	: User Function: S_Correlation_Moment

	COMPILER   	: Omega Easy Language

	PROVIDED BY	: Anonymouse

	LAST EDIT	: 12/30/1996
                
	PURPOSE		: Returns the Coefficient of Correlation.
                  For a dependent variable YDEP
                  (usually Data1) it measures how
                  closely related YDEP is to XIND (usually data2)
                  the independent variable. If YDEP increases 
                  directly as XIND increases, YDEP is positively
                  correlated with value 1.0. If YDEP
                  decreases directly as XIND increases then
                  YDEP is negatively correlated at value -1.0
                  If there's no linear relation between the
                  two, the correlation is 0.0.

                  Experimental program only: Use at your own risk.
                  It was originally to illustrates a method for quick 
                  calculation of sums. Do a one-time original 
                  SUM = V0 + V1 + ... +VLen-1,
                  for the next data point, the old V0 becomes V1, 
                  old V1 becomes V2, etc. 
                  so, SUM (new) = SUM (old) + V[0] (new) - V[Len](old).

    METHOD      : PRODUCT MOMENT CORRELATION FORMULA
                  From "Statistics", Murray R. Spiegel, 
                  Schaum Publ. Co. NY, 1961, p245.
                  Also known as Pearson's correlation?
 
                  r =     N SUMXY - SUMX * SUMY
                          ---------------------
             SquareRoot((N*SUMX2 - SUMX*SUMX) * (N*SUMY2 -SUMY*SUMY) )

    HISTORY	    : Jan-01-97: v1.2 No change in logic, only header
                  Dec-31-96: V1.1 Used Temp,Temp2,Temp3,& Cosmetics.
                  Dec-30-96: V1.0 Original
****************************************************************}
 Inputs: 
     YDEP(NumericSeries), {Data1 series (Y), dependent variable,}
     XIND(NumericSeries), {Data2 series (X), independent variable
                          turns quicker than YDEP (Y), Data1}
     XLEAD(Numeric),      {Periods that Data2 (X) leads Data1(Y) at turns}
     Len(Numeric);        {# data periods to compare}
 
 Vars: SUMX(0),SUMY(0),SUMX2(0),SUMY2(0),SUMXY(0),
 K(0),Temp(0),Temp2(0),Temp3(0);
 
 {Initial sums}
 If CurrentBar = 1 then begin
     SUMX = 0;
     SUMY = 0;
     SUMX2= 0;
     SUMY2= 0;
     SUMXY= 0;

     For K = 0 to Len - 1 begin
         Temp = YDEP[K];
         SUMY = SUMY  + Temp;
         SUMY2= SUMY2 + Temp  * Temp;

         Temp2= XIND[K + XLEAD];
         SUMX = SUMX  + Temp2;
         SUMX2= SUMX2 + Temp2 * Temp2;

         SUMXY= SUMXY + Temp2 * Temp;
     End;
 
 End else begin
     {Short cut: new sum = old sum + Data[0] - Data[Len]}

     Temp  = YDEP[Len];  {Y data Len periods back}
     SUMY  = SUMY  + YDEP - Temp;
     SUMY2 = SUMY2 + YDEP * YDEP - Temp * Temp;

     Temp2 = XIND[Len + XLEAD];
     Temp3 = XIND[XLEAD];
     SUMX  = SUMX  + Temp3 - Temp2;   
     SUMX2 = SUMX2 + Temp3 * Temp3 - Temp2 * Temp2;

     SUMXY = SUMXY + Temp3 * YDEP - Temp2 * Temp;

     {Temp below will be a positive divisor later}
     Temp = (Len * SUMX2 - SUMX * SUMX) * (Len * SUMY2 - SUMY * SUMY);

 	 If Temp > 0 then       {So don't divide by 0}
 	    S_Correlation_Moment = (Len * SUMXY -
               SUMX*SUMY)/SquareRoot(Temp)
 	 else
 		S_Correlation_Moment = 0;
End;
{***************************************************************}