bugfix: TimeSeries: fix for calculation of standard deviation

git-svn-id: https://svn.fhem.de/fhem/trunk/fhem@22980 2b470e98-0d58-463d-a4d8-8e2adae1ed80
This commit is contained in:
borisneubert 2020-10-17 09:21:43 +00:00
parent 42d6c2bb62
commit c9c4489183
2 changed files with 7 additions and 2 deletions

View File

@ -1,5 +1,6 @@
# Add changes at the top of the list. Keep it in ASCII, and 80-char wide.
# Do not insert empty lines here, update check depends on it.
- bugfix: TimeSeries: fix for calculation of standard deviation
- feature: 76_SMAPortal: new relative time arguments for attr balanceDay,
balanceMonth, balanceYear, new attribute
useRelativeNames

View File

@ -44,6 +44,9 @@
# modified: method elapsed reverted to version from 2015-01-31 to provide downsampling and buffering through fhem.pl
# modified: method _housekeeping does not reset time series if hold time is specified
#
# 17.10.2020 Boris Neubert
# modified: fix for calculation of standard deviation
#
##############################################################################
package TimeSeries;
@ -240,12 +243,13 @@ sub trimToHoldTime() {
sub _updatestat($$) {
my ($self, $V)= @_;
# see Donald Knuth, The Art of Computer Programming, ch. 4.2.2, formulas 14ff.
# see Donald Knuth, The Art of Computer Programming, ch. 4.2.2, p. 232ff, formulas 14ff.
# https://doc.lagout.org/science/0_Computer%20Science/2_Algorithms/The%20Art%20of%20Computer%20Programming%20%28vol.%202_%20Seminumerical%20Algorithms%29%20%283rd%20ed.%29%20%5BKnuth%201997-11-14%5D.pdf
my $n= ++$self->{n};
if($n> 1) {
my $M= $self->{_M};
$self->{_M}= $M + ($V - $M) / $n;
$self->{_S}= $self->{_S} + ($V - $M) * ($V - $M);
$self->{_S}= $self->{_S} + ($V - $M) * ($V - $self->{_M});
$self->{integral}+= $V;
#main::Debug("V= $V M= $M _M= ".$self->{_M}." _S= " .$self->{_S}." int= ".$self->{integral});
} else {