From dc7f29e2ec7a47a3e425d960af1ee8070fc15ed9 Mon Sep 17 00:00:00 2001 From: Thomas Stastny Date: Wed, 28 Sep 2022 17:48:53 +0200 Subject: [PATCH] welford mean: protect against negative variances --- src/lib/mathlib/math/WelfordMean.hpp | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/lib/mathlib/math/WelfordMean.hpp b/src/lib/mathlib/math/WelfordMean.hpp index ac6af40faa73..80d6b68a6075 100644 --- a/src/lib/mathlib/math/WelfordMean.hpp +++ b/src/lib/mathlib/math/WelfordMean.hpp @@ -58,6 +58,9 @@ class WelfordMean // M2 aggregates the squared distance from the mean // count aggregates the number of samples seen so far _M2 += delta.emult(new_value - _mean); + + // protect against floating point precision causing negative variances + _M2 = matrix::max(_M2, {}); } bool valid() const { return _count > 2; }