Results 1 to 10 of 10

Thread: Standard Deviation and Avg

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Oct 2000
    Posts
    71
    How do I take the average and Standard Deviation from an array of long integers?

  2. #2
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431
    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)

  3. #3
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Slight addition and expansion:
    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))
    ...I think...

    [Edited by parksie on 11-27-2000 at 05:02 PM]
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  4. #4

  5. #5
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Oops...yes. (sorry - I've been using Excel all day )
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  6. #6
    Frenzied Member
    Join Date
    Jul 1999
    Location
    Huntingdon Valley, PA 19006
    Posts
    1,151

    UBound as divisor?

    Hey, guys: 0 to UBound is (UBound + 1 ) values. Perhaps (UBound + 1) should be the divsior.
    Live long & prosper.

    The Dinosaur from prehistoric era prior to computers.

    Eschew obfuscation!
    If a billion people believe a foolish idea, it is still a foolish idea!
    VB.net 2010 Express
    64Bit & 32Bit Windows 7 & Windows XP. I run 4 operating systems on a single PC.

  7. #7

  8. #8
    Frenzied Member
    Join Date
    Jul 1999
    Location
    Huntingdon Valley, PA 19006
    Posts
    1,151

    Std Dev Formula?

    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)


    Live long & prosper.

    The Dinosaur from prehistoric era prior to computers.

    Eschew obfuscation!
    If a billion people believe a foolish idea, it is still a foolish idea!
    VB.net 2010 Express
    64Bit & 32Bit Windows 7 & Windows XP. I run 4 operating systems on a single PC.

  9. #9
    New Member
    Join Date
    Nov 2000
    Location
    Oregon
    Posts
    10

    STD DEV

    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.

  10. #10

    Thread Starter
    Lively Member
    Join Date
    Oct 2000
    Posts
    71
    Guys,
    Many Thanks! I love this Forum. I have yet to ask a question and not have it answered intellegently! THANK YOU!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width