Re: newbie MOMENT question [message #6556] |
Tue, 16 July 1996 00:00 |
David Foster
Messages: 341 Registered: January 1996
|
Senior Member |
|
|
demott@denali (Charlotte DeMott) wrote:
>
> Hi,
>
> I want to compute the mean and variance of a set of data points, and
> I'm using the MOMENT procedure to do it. With some fields, the
> variance is quite small (i.e., o(e-03)), so I get floating point
> underflow errors when MOMENT tries to compute the skewness and
> kurtosis. since i don't really care about the skewness and kurtosis,
> how can i get IDL to not worry about this, and continue with the
> rest of the procedure?
Try using
junk = CHECK_MATH( trap=0 )
to disable math traps.
Dave Foster
UCSD Brain Image Analysis Lab
foster@bial1.ucsd.edu
|
|
|
Re: newbie MOMENT question [message #6558 is a reply to message #6556] |
Mon, 15 July 1996 00:00  |
Liam Gumley
Messages: 473 Registered: November 1994
|
Senior Member |
|
|
Charlotte DeMott wrote:
> I want to compute the mean and variance of a set of data points, and
> I'm using the MOMENT procedure to do it. With some fields, the
> variance is quite small (i.e., o(e-03)), so I get floating point
> underflow errors when MOMENT tries to compute the skewness and
> kurtosis. since i don't really care about the skewness and kurtosis,
> how can i get IDL to not worry about this, and continue with the
> rest of the procedure? online help suggests that some combination
> of ON_ERROR and/or CATCH would appropriate, but i haven't been
> able to figure out exactly how to use these two yet. any suggestions?
The following works well for me.
Cheers,
Liam.
pro stddev, array, mean, variance, sigma
;+
; Purpose:
; To compute the mean, variance, and standard deviation
; (also known as RMS, root-mean-square) values for a given dataset.
;
; Usage:
; stddev, array, mean, variance, sigma
;
; Input:
; array array of data values (must have more than one element)
;
; Output:
; mean data mean
; variance data variance
; sigma data standard deviation
;-
;- check number of elements
n = n_elements( array )
if ( n lt 1 ) then message, 'Number of elements less than 1 in STDDEV'
;- compute mean and variance
mean = total( double( array ) ) / double( n )
variance = total( double( array ) ^ 2 ) / double( n ) - mean ^ 2
;- compute sigma if precision ok, otherwise return zero
sigma = 0.0d
test = 1.0d + variance
if ( test gt 1.0d ) then sigma = sqrt( variance )
end
|
|
|