Results 1 to 3 of 3

Thread: [RESOLVED] Weighted Average formula

  1. #1

    Thread Starter
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,464

    Resolved [RESOLVED] Weighted Average formula

    I've written a weighted average function from memory...

    Code:
    Dim numbersAsInteger(numbers.Length) As Integer
    Dim weightings(numbers.Length) As Decimal
    Dim sum As Decimal
    
    For x As Integer = 0 To numbers.GetUpperBound(0)
        Dim parts() As String = numbers(x).ToString.Split(New String() {"   {", "}"}, StringSplitOptions.None)
        numbersAsInteger(x) = CInt(parts(0))
        weightings(x) = CDec(parts(1))
        sum += numbersAsInteger(x) * weightings(x)
    Next
    
    Return sum / weightings.Sum
    numbers is an object array, which will contain something like {1 {1}, 2 {1.5}, 3 {1}, 4 {3.5}, 5 {1}}

    so numbersAsInteger will be {1, 2, 3, 4, 5}
    and the parallel weightings array will be {1, 1.5, 1, 3.5, 1}

    That's my basic understanding of how a weighted average works. Is that correct?

  2. #2
    Fanatic Member Delaney's Avatar
    Join Date
    Nov 2019
    Location
    Paris, France
    Posts
    845

    Re: [RESOLVED] Weighted Average formula

    That's seems correct:

    Weighted Average = sum (Xi*Wi)/Sum(Wi)

    Xi the values
    Wi the weight
    The best friend of any programmer is a search engine
    "Don't wish it was easier, wish you were better. Don't wish for less problems, wish for more skills. Don't wish for less challenges, wish for more wisdom" (J. Rohn)
    “They did not know it was impossible so they did it” (Mark Twain)

  3. #3

    Thread Starter
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,464

    Re: [RESOLVED] Weighted Average formula

    Quote Originally Posted by Delaney View Post
    That's seems correct:

    Weighted Average = sum (Xi*Wi)/Sum(Wi)

    Xi the values
    Wi the weight
    Ok thanks...

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