WinBatch Tech Support Home

Database Search

If you can't find the information using the categories below, post a question over in our WinBatch Tech Support Forum.

TechHome

Math - Random

Can't find the information you are looking for here? Then leave a message over on our WinBatch Tech Support Forum.

Linear Regression

 Keywords:  

; Regress  Slope
; Performs Linear regression on array A()
; A() must be a 2-dimensional array  Col 0 is X axis data  Col 1 is corresponfing Y axis data
; The slope, XInterxept and YIntercept are passed by reference, and values returned therein
;
; The function sums all rows from 1 to N.  Row 0 is considered a header row
;
; The return value of he function is a status message.  "OK" means Success.





#DefineFunction LinearRegression(a, N, ptrSlope, ptrXIntercept, ptrYIntercept)
SumX=0
SumY=0
SumXY=0
SumXX=0
For i = 1 To N
   x=a[i,0]+ 0.0
   y=a[i,1]+ 0.0
   SumX = SumX + x
   SumY = SumY + y
   SumXY = SumXY + (x * y)
   SumXX = SumXX + (x * X)
Next i

If ((N * SumXX) - (SumX * SumX)) == 0.0 Then Return("ERROR: DIVIDE BY ZERO")

*ptrSlope = ( (N * SumXY) - (SumX * SumY) ) / ( (N * SumXX) - (SumX * SumX) )
*ptrYIntercept = (  (SumY * SumXX) - (SumX * SumXY) ) / (  (N*SumXX) - (SumX * SumX) )
If *ptrSlope==0.0 Then Return("NOTE: SLOPE IS ZERO.  NO XINTERCEPT")
*ptrXIntercept= -(*ptrYIntercept/*ptrSlope)

Return("OK")
#EndFunction


;Test

a= ArrDimension(6,2)  ; ignoring 0   data stats on rom 1
a[1, 0] = 1
a[1, 1] = 1

a[2, 0] = 2
a[2, 1] = 3

a[3, 0] = 3
a[3, 1] = 5

a[4, 0] = 4
a[4, 1] = 7

a[5, 0] = 5
a[5, 1] = 9
MaxRows = 5


status=LinearRegression( a, MaxRows, &Slope, &XIntercept, &YIntercept)
If status!="OK"
   Message("Regress Error",status)
   Exit
EndIf
YText = "Y = %Slope% * X + %YIntercept% "
invslope = 1.0/Slope
XText = "X = %invslope% * y + %XIntercept% " 

Message("Results",StrCat(YText,@CRLF,XText))




Article ID:   W17210
File Created: 2007:07:03:14:28:50
Last Updated: 2007:07:03:14:28:50