Skip site navigation (1) Skip section navigation (2)

Perl Standard Deviation function is wrong !

From: Andreas Zeugswetter <andreas(dot)zeugswetter(at)telecom(dot)at>
To: "'colink(at)latticesemi(dot)com'" <colink(at)latticesemi(dot)com>, "'jason(at)wagner(dot)com'" <jason(at)wagner(dot)com>
Cc: "'dg(at)illustra(dot)com'" <dg(at)illustra(dot)com>, "'hackers(at)postgresql(dot)org'" <hackers(at)postgresql(dot)org>
Subject: Perl Standard Deviation function is wrong !
Date: 1998-06-05 10:05:18
Message-ID: 01BD907A.6B7A9F10@zeugswettera.user.lan.at (view raw or flat)
Thread:
Lists: 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;
>}




Responses

pgsql-hackers by date

Next:From: Ulrich VossDate: 1998-06-05 12:31:24
Subject: Re: [HACKERS] keeping track of connections
Previous:From: Maarten BoekholdDate: 1998-06-05 08:43:40
Subject: Re: [HACKERS] NEW POSTGRESQL LOGOS

Privacy Policy | About PostgreSQL
Copyright © 1996-2014 The PostgreSQL Global Development Group