Re: [HACKERS] Standard Deviation function.
От | Andreas Zeugswetter |
---|---|
Тема | Re: [HACKERS] Standard Deviation function. |
Дата | |
Msg-id | 01BD906E.FAFE3D10@zeugswettera.user.lan.at обсуждение исходный текст |
Ответы |
Re: [HACKERS] Standard Deviation function.
|
Список | 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 по дате отправления: