Re: geometric mean? [message #21626] |
Fri, 08 September 2000 02:37  |
Martin Schultz
Messages: 515 Registered: August 1997
|
Senior Member |
|
|
Andrew wrote:
>
> 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
>
> Sent via Deja.com http://www.deja.com/
> Before you buy.
Hi Andrew,
I couldn't find such a routine either so I decided to hack it
together using the algorithm you suggest but including some error
cehcking and more caution with range limits or negative values. You
can find geomean.pro on my web pages:
http://www.mpimet.mpg.de/~schultz.martin/idl/html/libmartin_ schultz.html
Cheers,
Martin
--
[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[ [[[[[[[
[[ Dr. Martin Schultz Max-Planck-Institut fuer Meteorologie [[
[[ Bundesstr. 55, 20146 Hamburg [[
[[ phone: +49 40 41173-308 [[
[[ fax: +49 40 41173-298 [[
[[ martin.schultz@dkrz.de [[
[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[ [[[[[[[
|
|
|
|
Re: geometric mean? [message #21724 is a reply to message #21626] |
Fri, 08 September 2000 09:59   |
noymer
Messages: 65 Registered: June 1999
|
Member |
|
|
In article <39B8B345.FDF4E45D@dkrz.de>,
Martin Schultz <martin.schultz@dkrz.de> wrote:
> Hi Andrew,
>
> I couldn't find such a routine either so I decided to hack it
> together using the algorithm you suggest but including some error
> cehcking and more caution with range limits or negative values. You
> can find geomean.pro on my web pages:
>
http://www.mpimet.mpg.de/~schultz.martin/idl/html/libmartin_ schultz.html
>
> Cheers,
> Martin
>
Dear Martin,
Thanks!!!
You include checking for negative values, which would mess
up the ALOG function.
Since I am taking geometric means of rates that are by
definition positive, I did not think of negative numbers.
There is a problem, though...
Someone please correct me if this is wrong; I'm not 100% sure.
The way I implemented the geometric mean was not the DEFINITION of
the geomean, but rather a computational SHORTCUT.
The DEFINITION goes something like:
GEOMEAN(Arr)=(PROD(Arr))^(1/n), where n is the number of elements,
and PROD is the product operator. Logging both sides gets rid of the
nasty "nth root" (i.e. ^(1/n)) and turns the product into a sum, which
is also nice. Then exponentiating un-transforms the log.
Clearly we can't log any negative number, but we can product a
bunch of numbers and then take an nth root of the result. And if there
are zero or an even number of negative numbers there will be a real
nth root, hence (I guess), the geomean would exist.
I don't know what the convention is with negative numbers, and
it doesn't affect me because I am using positive numbers, but maybe
someone out there knows:
(1) Is geomean by convention undefined if any numbers in the set
are negative?
(2) Is geomean always the positive nth root? geomean of -2 and -2
is +2?
Cheers,
Andrew
Sent via Deja.com http://www.deja.com/
Before you buy.
|
|
|
Re: geometric mean? [message #21746 is a reply to message #21631] |
Sun, 17 September 2000 00:00  |
Craig Markwardt
Messages: 1869 Registered: November 1996
|
Senior Member |
|
|
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
------------------------------------------------------------ --------------
|
|
|