PureBytes Links
Trading Reference Links
|
The following is a public method from my regression class, this should
give you what you are looking for and more. Use what you need.
Andrew
Public Sub CALCLeastSquares(ByRef IndVAR() As Double, ByRef DepVAR() As
Double)
'///////////////////////////////////////////////////////////////////////////////////////
'//
'// Name: LSRegression
'// Type: Sub Routine
'// Scope: Public
'// Author: Andrew S. Peskin
'// Description: This sub routine takes the original data and
calculates
'// a Least Squares Regression, returning the
Y-Intercept
'// and the Slope
'//
'// Parameters: Name: IndVAR()
'// Type: Array of Double
'// Desc: An Array of the Independent (X) Variables
'//
'// Name: DepVAR()
'// Type: Array of Double
'// Desc: An Array of the Dependent (Y) Variables
'//
'// Version History:
'// Revision: 1.0.0
'// Date: Thursday, 20 December 2000
'// Author: Andrew Peskin
'///////////////////////////////////////////////////////////////////////////////////////
Dim Count As Long '// General Counting Variable
Dim DataMAX As Long '// The Last Record in the Input
Data and Output Data Arrays
Dim DataMIN As Long '// The First Record in the Input
Data and Output Data Arrays
Dim SumX As Double '// Sum of X's
Dim SumY As Double '// Sum of Y's
Dim SumXSqr As Double '// Sum of X's Squared
Dim SumYSqr As Double '// Sum of Y's Squared
Dim SumXY As Double '// Sum of XY Product
Dim SSxx As Double
Dim SSyy As Double
Dim SSxy As Double
Dim SSE As Double
Dim sSqr As Double
Dim SlopeStdError As Double '// Standard Error of Slope
Dim YIntStdError As Double '// Y Intercept Standard Error
Dim NUMPoints As Long '// Number of Data Points
'// Calculate the Data Array MAX and MIN record values
DataMAX = UBound(IndVAR())
DataMIN = LBound(IndVAR())
'// Calculate the Number of Data Points
NUMPoints = (DataMAX - DataMIN + 1)
'// Calculate the Least Squares Linear Regression Line
SumX = 0#
SumXSqr = 0#
SumY = 0#
SumYSqr = 0#
SumXY = 0#
For Count = DataMIN To DataMAX
SumX = (SumX + IndVAR(Count))
SumXSqr = (SumXSqr + (IndVAR(Count) ^ 2))
SumY = (SumY + DepVAR(Count))
SumYSqr = (SumYSqr + (DepVAR(Count) ^ 2))
SumXY = (SumXY + (IndVAR(Count) * DepVAR(Count)))
Next Count
SSxx = (SumXSqr - ((SumX ^ 2) / NUMPoints))
SSyy = (SumYSqr - ((SumY ^ 2) / NUMPoints))
SSxy = (SumXY - ((SumX * SumY) / NUMPoints))
m_dSlope = (SSxy / SSxx)
m_dYIntercept = ((SumY / NUMPoints) - (m_dSlope * (SumX /
NUMPoints)))
SSE = ((SumYSqr) - (((SumY) ^ 2) / NUMPoints) - (m_dSlope * SSxy))
'// Check for negative SSE & SSXY & SSYY
If ((SSE < 0) Or (SSxy < 0) Or (SSyy < 0)) Then
SlopeStdError = 0
YIntStdError = 0
m_dPearsonsR = 0
Else
SlopeStdError = Sqr((SSE / (NUMPoints - 2))) / Sqr(SSxx)
YIntStdError = Sqr(SlopeStdError)
m_dPearsonsR = (SSxy / Sqr(SSxx * SSyy))
End If
m_dRSquared = (m_dPearsonsR * m_dPearsonsR)
m_eRegressionTYPE = LeastSquares
m_dSlopeStdERROR = SlopeStdError
m_dYInterceptStdERROR = YIntStdError
End Sub
Craig wrote:
> I'm trying to move away from Excel VBA into VB and found that Visual
> Basic
> lacks statistics functions. Therefore, I need to calculate the values
> from
> scratch, but I'm not having any success.
>
> Here's a snippet calculating the slope and y-intercept for the best
> fit
> line in a scatter x-y plot:
>
> For counter = 0 to 199
> x = xArray(counter)
> y = yArray(counter)
> A = x * y 'x*y
> B = B + A 'sum of x*y
> C = C + x 'sum of x
> D = D+ y 'sum of y
> E = E + x ^ 2 'sum of x squared
> Next counter
>
> Slope = (B* C* D) / E- C^2
>
> Y_intercept = (D*E - C*B) / E - C^2
>
> The slope and Y_intercept are wrong. Would somebody help me?
>
> Thanks,
>
> Craig
|