[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: geometric mean?




Andrew <noymer@my-deja.com> writes:

> I have no response so I assume the answer is to
> roll your own.
> 
> I did:
> 
>  FUNCTION GEOMEAN, arr
>      RETURN, EXP(TOTAL(ALOG(arr))/N_ELEMENTS(arr))
>  END
> 
> - Andrew

I am back from a long trip, so I'll bat a little cleanup here.

Andrew you asked about negative values of ARR.  I think it's fair to
say that the geometric mean is not meaningful (no pun intended) for
negative values.  Therefore I think it would be simplest to take the
absolute value, like this:

FUNCTION GEOMEAN, arr
    RETURN, EXP(TOTAL(ALOG(ABS(arr)))/N_ELEMENTS(arr))
END

To go on to your more general question, the logarithm *is* in fact
defined for negative values, unfortunately it's a complex number.
Also, it's not unique.  To prove that to yourself consider the fact
that Y=EXP(I*X) is an oscillating function like COS and SIN, so a
multitude of X values will give the same Y value (here I is the
complex number COMPLEX(0,1)).  If you really wanted to perform the
geometric mean of negative numbers then be sure to cast them to the
complex type before taking the logarithm.  There will always be some
ambiguity about the sign just as SQRT(X) can formally be either
positive or negative.

Craig

-- 
--------------------------------------------------------------------------
Craig B. Markwardt, Ph.D.         EMAIL:    craigmnet@cow.physics.wisc.edu
Astrophysics, IDL, Finance, Derivatives | Remove "net" for better response
--------------------------------------------------------------------------