Skip to content

Commit f01e626

Browse files
committedSep 2, 2020
8252093: formula used to calculate decaying variance in numberSeq
Reviewed-by: sjohanss, kbarrett
1 parent f9e6f26 commit f01e626

File tree

1 file changed

+10
-2
lines changed

1 file changed

+10
-2
lines changed
 

‎src/hotspot/share/utilities/numberSeq.cpp

+10-2
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,17 @@ void AbsSeq::add(double val) {
4141
_dvariance = 0.0;
4242
} else {
4343
// otherwise, calculate both
44-
_davg = (1.0 - _alpha) * val + _alpha * _davg;
44+
// Formula from "Incremental calculation of weighted mean and variance" by Tony Finch
45+
// diff := x - mean
46+
// incr := alpha * diff
47+
// mean := mean + incr
48+
// variance := (1 - alpha) * (variance + diff * incr)
49+
// PDF available at https://fanf2.user.srcf.net/hermes/doc/antiforgery/stats.pdf
50+
// Note: alpha is actually (1.0 - _alpha) in our code
4551
double diff = val - _davg;
46-
_dvariance = (1.0 - _alpha) * diff * diff + _alpha * _dvariance;
52+
double incr = (1.0 - _alpha) * diff;
53+
_davg += incr;
54+
_dvariance = _alpha * (_dvariance + diff * incr);
4755
}
4856
}
4957

0 commit comments

Comments
 (0)