Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Math Problem: Finding the nth root (geometric mean) 1

Status
Not open for further replies.

storm75m

Programmer
Apr 18, 2001
81
US
Alright all you "brainiacs" out there...
I need to find the geometric mean of a group of numbers in a data array (double). The geometric mean is multiplying all of the numbers together, then taking the nth root of that number, "n" being the total number of data elements. (Sort of like an average) Does anyone have any idea how to do this? I can easily find the average by looping through the elements in the array adding each one, then dividing by the count of data elements, but I don't know how to find the nth root using code. There is a GEOMEAN function in Excel that does what I want, but how do I use it in Access? Is there a reference that I can set? Any help would be much appreciated, thanks in advance.
 
Not sure if I would use an array because then one needs to define the number of elements before it can be used. Here is an example using an array with 4 elements...

Dim Array_lobj() As Variant
ReDim Open_Arg_Value(4)

Open_Arg_Value(1) = 1
Open_Arg_Value(2) = 2
Open_Arg_Value(3) = 3
Open_Arg_Value(4) = 4

Dim i As Integer
i = 1
Dim Count_ll As Long
Count_ll = 0
Dim Geo_ld As Double

Dim Sum_ll As Long
Sum_ll = 1

For i = 1 To 4
Count_ll = Count_ll + 1
Sum_ll = Sum_ll * Val(Open_Arg_Value(i))
Next i

Geo_ld = Sum_ll / Count_ll
Debug.Print Geo_ld
 

Here's a possible solution:

n = count of items
log() = log base 10. Your program should have this function, in one form or another.
10^(#) - 10 to the power of (#).

Code:
(pseudocode)
dim sum as double
dim mean as double
dim i as integer
sum = 0

for i = 1 to count
    sum = sum + log(x)
next i
    sum = sum / n
    mean = 10^(sum)

This goes on the principles that
log(x*y) = log(x) + log(y), and
log(x^(z)) = (z)*log(x), and so

therefore log( (x1*x2*x3...)^(1/n) ) = (1/n)*(log(x1) + log(x2)...)


Anyway. It will work.
 
Some clarification. 3 Principles:
Code:
1.  log(x*y) = log(x) + log(y)
2.  log(x^z) = z * log(x)
3.  10^(log(x)) = x, or
    log(10^x) = x * log(10) = x * (1) = x

So go from there.
 
hmmmmmmmmmmmm,

minor correction and wraping the procedure (more) properly:


Code:
Public Function basGeoMean(MyAry() As Double) As Double

    'Minor correction to procedure by foolio12 _
     Tek-Tips thread705-486262 for storm75m _
     by Muchael Red 2/26/2003

    Dim Sum As Double
    Dim Idx As Integer

    Idx = 1
    While Idx <= UBound(MyAry)
        Sum = Sum + Log(MyAry(Idx))
        Idx = Idx + 1
    Wend

    Sum = Sum / (Idx - 1)
    basGeoMean = 2.718 ^ (Sum)

End Function

(yes, dorothy, MS does use LOG when the world means LN)


MichaelRed
m.red@att.net

Searching for employment in all the wrong places
 
Thanks MichaelRed and foolio12, looks like I got it from here!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top