Re: [HACKERS] Standard Deviation function. - Mailing list pgsql-hackers

From Andreas Zeugswetter
Subject Re: [HACKERS] Standard Deviation function.
Date
Msg-id 01BD906E.FAFE3D10@zeugswettera.user.lan.at
Whole thread Raw
Responses Re: [HACKERS] Standard Deviation function.  (Tom Lane <tgl@sss.pgh.pa.us>)
List 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;
>}



pgsql-hackers by date:

Previous
From: Maarten Boekhold
Date:
Subject: Re: [HACKERS] NEW POSTGRESQL LOGOS
Next
From: Andreas Zeugswetter
Date:
Subject: Perl Standard Deviation function is wrong !