How do I take the average and Standard Deviation from an array of long integers?
Printable View
How do I take the average and Standard Deviation from an array of long integers?
Here is one simple way of doing the average
Code:Dim lngMyNumb() As Long
Dim lngTot As Long
Dim i As Integer
ReDim lngMyNumb(3)
lngMyNumb(0) = 5
lngMyNumb(1) = 5
lngMyNumb(2) = 5
lngMyNumb(3) = 45
For i = 0 To UBound(lngMyNumb)
lngTot = lngTot + lngMyNumb(i)
Next
MsgBox "avg is " & lngTot / UBound(lngMyNumb)
Slight addition and expansion:
...I think...Code:Dim lngMyNumb() As Long
Dim lngTot As Long
Dim lngTotSq As Long
Dim i As Integer
ReDim lngMyNumb(3)
lngMyNumb(0) = 5
lngMyNumb(1) = 5
lngMyNumb(2) = 5
lngMyNumb(3) = 45
For i = 0 To UBound(lngMyNumb)
lngTot = lngTot + lngMyNumb(i)
lngTotSq = lngTotSq + (lngMyNumb(i)^2)
Next
MsgBox "avg is " & lngTot / UBound(lngMyNumb)
MsgBox "stddev is " & sqr((lngTotSq / UBound(lngMyNumb)) - ((lngTot / UBound(lngMyNumb))^2))
[Edited by parksie on 11-27-2000 at 05:02 PM]
Thanks parksie, I would have included the standard deviation in my answer but I didn't know the formula. BTW I assume your reference to sqrt is just a typo and you meant Sqr.
Oops...yes. (sorry - I've been using Excel all day :()
Hey, guys: 0 to UBound is (UBound + 1 ) values. Perhaps (UBound + 1) should be the divsior.
You are absolutly right!!! How embarrasing, I even supplied numbers for the average function that were easy to test
The computation for Standard Deveiation does not look right to me. Perhaps the following.Code:Dim FirstValue As Integer
Dim LastValue As Integer
Dim NumberValues As Integer
Dim J As Integer
Dim Total As Double
Dim Average As Double
Dim StandardDev As Double
FirstValue = LBound(MyArray,1)
LastValue = UBound(MyArray,1)
NumberValues = UBound - LBound + 1
Total = 0
For J = FirstValue to LastValue
Total = Total + MyArray(J)
Next J
Average = Total / NumberValues
Total = 0
For J = FirstValue to LastValue
Total = Total + (Average - MyArray(J))^2
Next J
StandardDev = sqr(Total / NumberValues)
Both of the posted computations of the STD Dev are correct. The first is more efficient computationally, Use it. There could be some quibbeling about whether to divide by n or (n-1) but which ever the difference will be small.
Guys,
Many Thanks! I love this Forum. I have yet to ask a question and not have it answered intellegently! THANK YOU!