Re: [HACKERS] Standard Deviation function.

From: Andreas Zeugswetter <andreas(dot)zeugswetter(at)telecom(dot)at>
To: "'David Gould'" <dg(at)illustra(dot)com>
Cc: "'hackers(at)postgresql(dot)org'" <hackers(at)postgresql(dot)org>
Subject: Re: [HACKERS] Standard Deviation function.
Date: 1998-06-05 08:43:24
Message-ID: 01BD906E.FAFE3D10@zeugswettera.user.lan.at
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

David Gould wrote:
>The Perl Module "Statistics/Descriptive" has on the fly variance calculation.
>
>sub add_data {
> 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).
It should read:
$self->{standard_deviation} = sqrt( $self->{pseudo_variance} / $self->{count} )
Note: The - 1 is missing

> }
> return 1;
>}

Browse pgsql-hackers by date

  From Date Subject
Next Message Maarten Boekhold 1998-06-05 08:43:40 Re: [HACKERS] NEW POSTGRESQL LOGOS
Previous Message David Gould 1998-06-05 08:40:33 Re: [HACKERS] NEW POSTGRESQL LOGOS