From 96aedfc62ced26052c0d6e117e0ec2a880b6ea48 Mon Sep 17 00:00:00 2001 From: Adrien Ehrhardt Date: Tue, 9 Apr 2024 12:00:59 +0200 Subject: [PATCH] Fix heights update in weighted_extended_p_square The heights update rule was not updated to take into account weights. * The update rule is done only if the discrepancy between desired and actual positions is above 1 when positions are in the "weights" scale (which can be arbitrarily small) * The update rule itself only takes into account the sign of the discrepancy when it has to be weighted A simulation shows (see PR) that when pushing observations from the same distribution with varying weights, the estimate is not consistent (well off its true value for w << 1 and w >> 1) --- .../statistics/weighted_extended_p_square.hpp | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/include/boost/accumulators/statistics/weighted_extended_p_square.hpp b/include/boost/accumulators/statistics/weighted_extended_p_square.hpp index f88e9b9..cbd9ae4 100644 --- a/include/boost/accumulators/statistics/weighted_extended_p_square.hpp +++ b/include/boost/accumulators/statistics/weighted_extended_p_square.hpp @@ -211,11 +211,9 @@ namespace impl float_type hp = (this->heights[i + 1] - this->heights[i]) / dp; float_type hm = (this->heights[i - 1] - this->heights[i]) / dm; - if((d >= 1 && dp > 1) || (d <= -1 && dm < -1)) + if((d >= args[weight] && dp > args[weight]) || (d <= -args[weight] && dm < -args[weight])) { - short sign_d = static_cast(d / std::abs(d)); - - float_type h = this->heights[i] + sign_d / (dp - dm) * ((sign_d - dm)*hp + (dp - sign_d) * hm); + float_type h = this->heights[i] + d / (dp - dm) * ((d - dm) * hp + (dp - d) * hm); // try adjusting heights[i] using p-squared formula if(this->heights[i - 1] < h && h < this->heights[i + 1]) @@ -227,14 +225,14 @@ namespace impl // use linear formula if(d > 0) { - this->heights[i] += hp; + this->heights[i] += args[weight] * hp; } if(d < 0) { - this->heights[i] -= hm; + this->heights[i] -= args[weight] * hm; } } - this->actual_positions[i] += sign_d; + this->actual_positions[i] += d; } } }