Re: mean and sdev [message #11069] |
Tue, 03 March 1998 00:00  |
Martin Schultz
Messages: 515 Registered: August 1997
|
Senior Member |
|
|
David Fanning wrote:
>
> R. Bauer (r.bauer@fz-juelich.de) writes:
>
>> I 'd like to know if a build in function somewhere is available to get
>> means and standard deviations.
>
> I use the MOMENT function for most of these simple statistical
> measurements.
>
> stats = Moment(data, STD=standardDeviation)
> mean = stats[0]
> Print, mean, standardDeviation
>
> Cheers,
>
> David
>
MOMENT just has one big disadvantage in my opinion: it does not like
standard deviations of zero. So I went ahead and added an
/IGNORE_ERROR option that will return the results of mean and sdev
anyhow with skewness and kurtosis set to 0 ...
regards,
Martin.
--
------------------------------------------------------------ -------
Dr. Martin Schultz
Department for Earth&Planetary Sciences, Harvard University
186 Pierce Hall, 29 Oxford St., Cambridge, MA-02138, USA
phone: (617)-496-8318
fax : (617)-495-4551
e-mail: mgs@io.harvard.edu
IDL-homepage: http://www-as.harvard.edu/people/staff/mgs/idl/
------------------------------------------------------------ -------
;$Id: moment.pro,v 1.4 1995/07/25 20:32:55 dave Exp $
;
; Copyright (c) 1994, Research Systems, Inc. All rights reserved.
; Unauthorized reproduction prohibited.
;+
; NAME:
; MOMENT2
;
; PURPOSE:
; This function computes the mean, variance, skewness and kurtosis
; of an n-element vector.
;
; CATEGORY:
; Statistics.
;
; CALLING SEQUENCE:
; Result = Moment(X)
;
; INPUTS:
; X: An n-element vector of type integer, float or double.
;
; KEYWORD PARAMETERS:
; MDEV: Use this keyword to specify a named variable which returns
; the mean absolute deviation of X.
;
; SDEV: Use this keyword to specify a named variable which returns
; the standard deviation of X.
;
; EXAMPLE:
; Define the n-element vector of sample data.
; x = [65, 63, 67, 64, 68, 62, 70, 66, 68, 67, 69, 71, 66, 65, 70]
; Compute the mean, variance, skewness and kurtosis.
; result = moment(x)
; The result should be the 4-element vector:
; [66.7333, 7.06667, -0.0942851, -1.18258]
;
; PROCEDURE:
; MOMENT computes the first four "moments" about the mean of an
; n-element vector of sample data. The computational formulas
; are given in the Univariate Statistics section of the Mathematics
; Guide.
;
; REFERENCE:
; APPLIED STATISTICS (third edition)
; J. Neter, W. Wasserman, G.A. Whitmore
; ISBN 0-205-10328-6
;
; MODIFICATION HISTORY:
; Written by: GGS, RSI, August 1994
; modified by mgs@io.harvard.edu Nov 1997: added /ignore_error option
;-
function moment2, x, mdev = mdev, sdev = sdev,ignore_error=ignore
on_error, 2
nx = n_elements(x)
if nx le 1 then $
message, 'Not defined for scalar inputs.'
mean = total(x) / nx
resid = x - mean
;Mean absolute deviation (returned through the MDEV keyword).
mdev = total(abs(resid)) / nx
r2 = total(resid^2)
var1 = r2 / (nx-1.0)
var2 = (r2 - (total(resid)^2)/nx)/(nx-1.0)
var = (var1 + var2)/2.0
;Standard deviation (returned through the SDEV keyword).
sdev = sqrt(var)
if var ne 0 then begin
skew = total(resid^3) / (nx * sdev ^ 3)
kurt = total(resid^4) / (nx * sdev ^ 4) - 3.0
endif else begin
skew = 0.
kurt = 0.
if(not keyword_set(ignore)) then message, $
'Skewness and Kurtosis not defined for a sample variance of zero.'
endelse
return, [mean, var, skew, kurt]
end
-
Attachment: moment2.pro
(Size: 2.40KB, Downloaded 93 times)
|
|
|
Re: mean and sdev [message #11127 is a reply to message #11069] |
Tue, 10 March 1998 00:00  |
Alejandro C. Frery
Messages: 2 Registered: March 1998
|
Junior Member |
|
|
Hi,
Though David's reply (the use of MOMENT) function is perfectly correct, that
function computes moments up to the fourth order, since it returns the mean,
the variance, the skewness and the kurtosis. If you only want the mean and
standard deviation, my suggestion is use the STDEV function (hidden and
undocumented, I do not know why) .
x = RANDOMU(seed, 1000)
std = STDEV(x, m) ; std will store the sample standard deviation and m the
sample mean (optional)
PRINT, std, m
Cheers,
Alejandro
Alejandro C. Frery UFPE-DI Recife, PE, Brazil
www.di.ufpe.br/~frery
R. Bauer escreveu na mensagem <34F95952.35B20DF0@fz-juelich.de>...
Hi,
I 'd like to know if a build in function somewhere is available to get
means and standard deviations.
I know
it's easy to do something like this:
mean = total(a)/n_elements(a)
but for 100000 values it takes a lot of my time.
Any ideas?
R. Bauer
--
R.Bauer
Institut fuer Stratosphaerische Chemie (ICG-1)
Forschungszentrum Juelich
email: R.Bauer@fz-juelich.de
|
|
|
Re: mean and sdev [message #11130 is a reply to message #11069] |
Mon, 02 March 1998 00:00  |
Dr. G. Scott Lett
Messages: 14 Registered: February 1998
|
Junior Member |
|
|
The MOMENT function is obscure and slow when all you need is themean and
standard deviation, so we're adding the more obvious MEAN,
STDDEV, VARIANCE and other statistics functions for IDL 5.1.
Best wishes,
Scott Lett
> R. Bauer (r.bauer@fz-juelich.de) writes:
>
>> I 'd like to know if a build in function somewhere is available to get
>> means and standard deviations.
>
> I use the MOMENT function for most of these simple statistical
> measurements.
>
> stats = Moment(data, STD=standardDeviation)
> mean = stats[0]
> Print, mean, standardDeviation
>
> Cheers,
>
> David
>
> -----------------------------------------------------------
> David Fanning, Ph.D.
> Fanning Software Consulting
> E-Mail: davidf@dfanning.com
> Phone: 970-221-0438
> Coyote's Guide to IDL Programming: http://www.dfanning.com/
|
|
|
Re: mean and sdev [message #11144 is a reply to message #11130] |
Mon, 02 March 1998 00:00  |
wmc
Messages: 117 Registered: February 1995
|
Senior Member |
|
|
In article f63234456caf9bc98972f@news.frii.com, davidf@dfanning.com (David Fanning) writes:
> R. Bauer (r.bauer@fz-juelich.de) writes:
>> I 'd like to know if a build in function somewhere is available to get
>> means and standard deviations.
> I use the MOMENT function for most of these simple statistical
> measurements.
>
> stats = Moment(data, STD=standardDeviation)
> mean = stats[0]
> Print, mean, standardDeviation
David is of course correct, but if you just want the standard deviation, the
obsolete procedure stdev is available (still in v5.03), and saying
sd=stdev(data) is rather more convenient than the 2-liner version using stats!
I don't understand why stdev has been made obsolete.
- William
---
William M Connolley | wmc@bas.ac.uk | http://www.nbs.ac.uk/public/icd/wmc/
Climate Modeller, British Antarctic Survey | Disclaimer: I speak for myself
|
|
|
Re: mean and sdev [message #11146 is a reply to message #11130] |
Sun, 01 March 1998 00:00  |
davidf
Messages: 2866 Registered: September 1996
|
Senior Member |
|
|
R. Bauer (r.bauer@fz-juelich.de) writes:
> I 'd like to know if a build in function somewhere is available to get
> means and standard deviations.
I use the MOMENT function for most of these simple statistical
measurements.
stats = Moment(data, STD=standardDeviation)
mean = stats[0]
Print, mean, standardDeviation
Cheers,
David
-----------------------------------------------------------
David Fanning, Ph.D.
Fanning Software Consulting
E-Mail: davidf@dfanning.com
Phone: 970-221-0438
Coyote's Guide to IDL Programming: http://www.dfanning.com/
|
|
|