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

fast and accurate Linear Regression Value, Slope and Standard Error for sparse data


  • To: omega list <omega-list@xxxxxxxxxx>
  • Subject: fast and accurate Linear Regression Value, Slope and Standard Error for sparse data
  • From: Leslie Walko <lesliewalko@xxxxxxxxx>
  • Date: Fri, 14 Mar 2008 09:49:48 -0700 (PDT)

PureBytes Links

Trading Reference Links

Hello List!

Would anyone have a fast and ACCURATE calculation for
Linear Regression that returns the LR Value, the Slope
and the Standard Error all at the same time?

I've tried but messed up.  See code below. I am a bit
baffled why it does not work.  Can anyone help?

Thank you.
Leslie

The code keeps a BIG internal array with an OCCUR
switch so that it can compute over sparse data (and
save some time by skipping needless computation).  

{ FUNCTION } 
{ L. Walko 2008 March 14 }
	
{ buggy ! }
 
{ known limitation: not truly accurate for very large
/ very small numbers}

input: price(numericSimple), occur(truefalseSeries),
Nbars(numeric), 
	o_slope(numericRef), 
	o_yintercept(numericRef), 
	o_stdErr(numericRef) ;
	
Var: obs(0), lastObs(0), Count(0),keepY(0), 
		SumXY(0),SumX(0),SumY(0),SumXSqr(0), 
		y_intercept(0),
		slope(0);

Var: SumResidSqr(0),SigmaRegress(0),stdErr(0),
YProj(0) ;
Array: Y_array[10000](0);


if occur then begin
	obs =obs+1;
	y_array[obs] = price ; 

	if obs >= Nbars then begin
		sumXY 	= 0;
		sumX	= 0;
		sumY	= 0;
		sumXsqr = 0;
	
		lastObs = (obs+Nbars) ;
		For count	= obs to lastObs begin
			SumXY	= SumXY	+	(Count * Y_array[Count]);
		  	SumX	= SumX	+	Count ;
			SumY	= SumY	+	Y_array[Count] ;
			SumXSqr	= SumXSqr +	Count * Count ;
		end;
	
		slope = (SumXY - ((SumX/Nbars)*SumY)) / (SumXSqr -
square(SumX)/Nbars);
		 
		y_intercept = (SumY / Nbars) - slope * SumX/Nbars;
		
		For Count = obs to lastObs begin
			YProj = y_intercept + (slope * Count);
			SumResidSqr = SumResidSqr + Square(Y_array[Count] -
YProj);
		end;
	
	 	SigmaRegress =	SquareRoot(SumResidSqr/(Nbars-2));
	 
		stdErr	= SigmaRegress / SquareRoot(SumXSqr);
	
		o_slope 	 = slope ;
		o_yintercept = y_intercept ;
		o_stdErr	 = stdErr ;
	
		_LinRegSlopeIntErr_a = y_intercept + slope*Nbars ;
	end else
		_LinRegSlopeIntErr_a = 0 ;
end;

{ test with Indicator } 

input: price(c), occur(0=0), Nbars(50) ;

vars: o_slope(0), o_yintercept(0), o_stdErr(0),
intercept(0), slope(0) ;


value1 = _LinRegSlopeIntErr_a(price, occur, Nbars,
o_slope, o_yintercept, o_stdErr) ;
intercept = o_yintercept ;
slope	  = o_slope ; 

if value1 >  -1  then begin
	plot1(intercept + (slope * (Nbars + CurrentBar)),
"LinReg") ;
	plot2(plot1 + 2*o_stdErr, "topBand") ;
end;