# Re: [HACKERS] Standard Deviation function.

From: Andreas Zeugswetter "'David Gould'" "'hackers(at)postgresql(dot)org'" Re: [HACKERS] Standard Deviation function. 1998-06-05 08:43:24 01BD906E.FAFE3D10@zeugswettera.user.lan.at Raw Message | Whole Thread | Download mbox | Resend email 1998-06-05 08:43:24 from Andreas Zeugswetter pgsql-hackers

David Gould wrote:
>The Perl Module "Statistics/Descriptive" has on the fly variance calculation.
>
> my \$self = shift; ##Myself
> my \$oldmean;
> my (\$min,\$mindex,\$max,\$maxdex);
>
> ##Take care of appending to an existing data set
> \$min = (defined (\$self->{min}) ? \$self->{min} : \$_[0]);
> \$max = (defined (\$self->{max}) ? \$self->{max} : \$_[0]);
> \$maxdex = \$self->{maxdex} || 0;
> \$mindex = \$self->{mindex} || 0;
>
> ##Calculate new mean, pseudo-variance, min and max;
> foreach (@_) {
> \$oldmean = \$self->{mean};
> \$self->{sum} += \$_;
> \$self->{count}++;
> if (\$_ >= \$max) {
> \$max = \$_;
> \$maxdex = \$self->{count}-1;
> }
> if (\$_ <= \$min) {
> \$min = \$_;
> \$mindex = \$self->{count}-1;
> }
> \$self->{mean} += (\$_ - \$oldmean) / \$self->{count};
> \$self->{pseudo_variance} += (\$_ - \$oldmean) * (\$_ - \$self->{mean});
> }
>
> \$self->{min} = \$min;
> \$self->{mindex} = \$mindex;
> \$self->{max} = \$max;
> \$self->{maxdex} = \$maxdex;
> \$self->{sample_range} = \$self->{max} - \$self->{min};
> if (\$self->{count} > 1) {
> \$self->{variance} = \$self->{pseudo_variance} / (\$self->{count} -1);
> \$self->{standard_deviation} = sqrt( \$self->{variance});

Wow, this is it. But as I said, the above line is wrong (By the way: this is a very common mistake).