Results 1 to 5 of 5

Thread: [RESOLVED] Can someone check my algorithms? (cubic mean and power mean)

  1. #1

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

    Resolved [RESOLVED] Can someone check my algorithms? (cubic mean and power mean)

    I'm working on an averages app.
    I have some algorithms, that i haven't found any online calculators to test with

    Cubic mean
    Code:
    Dim sum As Decimal
    For x As Integer = 0 To numbersAsInteger.GetUpperBound(0)
        sum += CDec(numbersAsInteger(x) * (3 / (x + 1)))
    Next
    Return Math.Pow((sum / numbersAsInteger.Count), 1 / 3)
    Power mean
    Code:
    If frm.ShowDialog = DialogResult.OK Then
        Dim p As Integer = CInt(frm.NumericUpDown1.Value)
        Dim sum As Decimal
        For x As Integer = 0 To numbersAsInteger.GetUpperBound(0)
            sum += CDec(numbersAsInteger(x) * (p / (x + 1)))
        Next
        Return Math.Pow((sum / numbersAsInteger.Count), 1 / p)
    End If
    Are my calculations correct?
    Thanks for any help

  2. #2
    PowerPoster
    Join Date
    Nov 2017
    Posts
    3,116

    Re: Can someone check my algorithms? (cubic mean and power mean)

    Quote Originally Posted by .paul. View Post
    Code:
    Dim sum As Decimal
    For x As Integer = 0 To numbersAsInteger.GetUpperBound(0)
        sum += CDec(numbersAsInteger(x) * (3 / (x + 1)))
    Next
    Return Math.Pow((sum / numbersAsInteger.Count), 1 / 3)
    Unless I'm missing something, the calculations are not correct.

    Use {1, 2, 3} as an example.

    Taking the individual "sum" terms:

    CDec(1 * (3 / (0 + 1))) = 3
    CDec(2 * (3 / (1 + 1))) = 3
    CDec(3 * (3 / (2 + 1))) = 3

    sum = 3+3+3 = 9

    Math.Pow((9 / 3), 1 / 3) = Cube root of 3


    What if the values were in a different order? Surely the result is the same, right? Well...

    Use {3, 1, 2} as an example.

    Taking the individual "sum" terms:

    CDec(3 * (3 / (0 + 1))) = 9
    CDec(1 * (3 / (1 + 1))) = 3/2
    CDec(2 * (3 / (2 + 1))) = 2

    sum = 9+3/2+2 = 12.5

    Math.Pow((12.5 / 3), 1 / 3) = Cube root of 4.16666666666...

    A different result by reordering the data can't be right.


    Pretty sure this:

    Code:
    sum += CDec(numbersAsInteger(x) * (3 / (x + 1)))
    needs to be this:

    Code:
    sum += Math.Pow(numbersAsInteger(x), 3)
    If not, you need to give a generic formula of what you are considering to be a cubic (or other power) mean and give an example or two of a set and the resulting mean.

  3. #3

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

    Re: Can someone check my algorithms? (cubic mean and power mean)

    This is where I found the formulas, but I’m not sure I’ve read them correctly

    https://en.m.wikipedia.org/wiki/Average

  4. #4
    PowerPoster
    Join Date
    Nov 2017
    Posts
    3,116

    Re: Can someone check my algorithms? (cubic mean and power mean)

    Quote Originally Posted by .paul. View Post
    This is where I found the formulas, but I’m not sure I’ve read them correctly

    https://en.m.wikipedia.org/wiki/Average
    Ok. You haven't. Multiplying a term by a value that varies simply by the location of said term in the series (as you are doing when you use the value of x anywhere in your calculation) is wrong.

    A cubic mean from the link is the sum of the cubes of all terms, divide that sum by the number of terms, and then take the cube root of that result. Use the change I suggested.

    Good luck.

  5. #5

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

    Re: Can someone check my algorithms? (cubic mean and power mean)

    Quote Originally Posted by OptionBase1 View Post
    Ok. You haven't. Multiplying a term by a value that varies simply by the location of said term in the series (as you are doing when you use the value of x anywhere in your calculation) is wrong.

    A cubic mean from the link is the sum of the cubes of all terms, divide that sum by the number of terms, and then take the cube root of that result. Use the change I suggested.

    Good luck.
    Ok Thanks. Lucky I checked that..

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