/* * Copyright (c) 2001, 2014, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions. *
*/
void AbsSeq::add(double val) { if (_num == 0) { // if the sequence is empty, the davg is the same as the value
_davg = val; // and the variance is 0
_dvariance = 0.0;
} else { // otherwise, calculate both // Formula from "Incremental calculation of weighted mean and variance" by Tony Finch // diff := x - mean // incr := alpha * diff // mean := mean + incr // variance := (1 - alpha) * (variance + diff * incr) // PDF available at https://fanf2.user.srcf.net/hermes/doc/antiforgery/stats.pdf double diff = val - _davg; double incr = _alpha * diff;
_davg += incr;
_dvariance = (1.0 - _alpha) * (_dvariance + diff * incr);
}
}
double AbsSeq::variance() const { if (_num <= 1) return 0.0;
double x_bar = avg(); double result = _sum_of_squares / total() - x_bar * x_bar; if (result < 0.0) { // due to loss-of-precision errors, the variance might be negative // by a small bit
// guarantee(-0.1 < result && result < 0.0, // "if variance is negative, it should be very small");
result = 0.0;
} return result;
}
double AbsSeq::sd() const { double var = variance();
guarantee( var >= 0.0, "variance should not be negative" ); return sqrt(var);
}
double AbsSeq::davg() const { return _davg;
}
double AbsSeq::dvariance() const { if (_num <= 1) return 0.0;
double result = _dvariance; if (result < 0.0) { // due to loss-of-precision errors, the variance might be negative // by a small bit
guarantee(-0.1 < result && result < 0.0, "if variance is negative, it should be very small");
result = 0.0;
} return result;
}
double AbsSeq::dsd() const { double var = dvariance();
guarantee( var >= 0.0, "variance should not be negative" ); return sqrt(var);
}
bool NumberSeq::check_nums(NumberSeq *total, int n, NumberSeq **parts) { for (int i = 0; i < n; ++i) { if (parts[i] != NULL && total->num() != parts[i]->num()) returnfalse;
} returntrue;
}
// get the oldest value in the sequence... double old_val = _sequence[_next]; // ...remove it from the sum and sum of squares
_sum -= old_val;
_sum_of_squares -= old_val * old_val;
// ...and update them with the new value
_sum += val;
_sum_of_squares += val * val;
// now replace the old value with the new one
_sequence[_next] = val;
_next = (_next + 1) % _length;
// only increase it if the buffer is not full if (_num < _length)
++_num;
guarantee( variance() > -1.0, "variance should be >= 0" );
}
// can't easily keep track of this incrementally... double TruncatedSeq::maximum() const { if (_num == 0) return 0.0; double ret = _sequence[0]; for (int i = 1; i < _num; ++i) { double val = _sequence[i]; if (val > ret)
ret = val;
} return ret;
}
double TruncatedSeq::oldest() const { if (_num == 0) return 0.0; elseif (_num < _length) // index 0 always oldest value until the array is full return _sequence[0]; else { // since the array is full, _next is over the oldest value return _sequence[_next];
}
}
double TruncatedSeq::predict_next() const { if (_num == 0) return 0.0;
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung ist noch experimentell.