Perl Standard Deviation function is wrong ! - Mailing list pgsql-hackers

From Andreas Zeugswetter
Subject Perl Standard Deviation function is wrong !
Date
Msg-id 01BD907A.6B7A9F10@zeugswettera.user.lan.at
Whole thread Raw
Responses Re: [HACKERS] Perl Standard Deviation function is wrong !  (Brook Milligan <brook@trillium.NMSU.Edu>)
List pgsql-hackers
Hi,

First of all I would like to thank you for your work on the Statistics Module.
Unfortunately a lot of books differ in their formula for variance and stdev.
In Europe the below corrected definition where stdev is not simply the sqrt of variance
seems to be more popular.
For large populations (>400) the calculation will be almost the same,
but for small populations (like 5) the below calculation will be different.

[Hackers] please forget my last mail to this subject. It was wrong.
Tanx
Andreas Zeugswetter

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});

Most books state:
    $self->{variance}     = $self->{pseudo_variance} / $self->{count};
    $self->{standard_deviation}  = sqrt( $self->{pseudo_variance} / ( $self->{count} - 1 ))

>  }
>  return 1;
>}




pgsql-hackers by date:

Previous
From: Andreas Zeugswetter
Date:
Subject: Re: [HACKERS] Standard Deviation function.
Next
From: "Ulrich Voss"
Date:
Subject: Re: [HACKERS] keeping track of connections