-
Mar 21st, 2021, 06:18 AM
#1
[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
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Mar 21st, 2021, 08:56 AM
#2
Re: Can someone check my algorithms? (cubic mean and power mean)
Originally Posted by .paul.
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.
-
Mar 21st, 2021, 09:07 AM
#3
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
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Mar 21st, 2021, 10:01 AM
#4
Re: Can someone check my algorithms? (cubic mean and power mean)
Originally Posted by .paul.
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.
-
Mar 21st, 2021, 03:30 PM
#5
Re: Can someone check my algorithms? (cubic mean and power mean)
Originally Posted by OptionBase1
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..
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|