Commit 4584a76e authored by Ken MacKay's avatar Ken MacKay Committed by Commit Bot

[Chromecast] Update weighted mean to accept any numeric value

Change-Id: I288e3b7becaf926bef5fb61cb0bdd1c215cca31a
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2108986Reviewed-by: default avatarYuchen Liu <yucliu@chromium.org>
Commit-Queue: Kenneth MacKay <kmackay@chromium.org>
Cr-Commit-Position: refs/heads/master@{#751561}
parent d7ca7f8b
...@@ -14,7 +14,7 @@ WeightedMean::WeightedMean() ...@@ -14,7 +14,7 @@ WeightedMean::WeightedMean()
sum_weights_(0), sum_weights_(0),
sum_squared_weights_(0) {} sum_squared_weights_(0) {}
void WeightedMean::AddSample(int64_t value, double weight) { void WeightedMean::AddDelta(double delta, double weight) {
double old_sum_weights = sum_weights_; double old_sum_weights = sum_weights_;
sum_weights_ += weight; sum_weights_ += weight;
// Use std::abs() to handle negative weights (ie, removing a sample). // Use std::abs() to handle negative weights (ie, removing a sample).
...@@ -23,7 +23,6 @@ void WeightedMean::AddSample(int64_t value, double weight) { ...@@ -23,7 +23,6 @@ void WeightedMean::AddSample(int64_t value, double weight) {
weighted_mean_ = 0; weighted_mean_ = 0;
variance_sum_ = 0; variance_sum_ = 0;
} else { } else {
double delta = value - weighted_mean_;
double mean_change = delta * weight / sum_weights_; double mean_change = delta * weight / sum_weights_;
weighted_mean_ += mean_change; weighted_mean_ += mean_change;
variance_sum_ += old_sum_weights * delta * mean_change; variance_sum_ += old_sum_weights * delta * mean_change;
......
...@@ -23,9 +23,14 @@ class WeightedMean { ...@@ -23,9 +23,14 @@ class WeightedMean {
// Adds |value| to the mean if |weight| is positive. Removes |value| from // Adds |value| to the mean if |weight| is positive. Removes |value| from
// the mean if |weight| is negative. Has no effect if |weight| is 0. // the mean if |weight| is negative. Has no effect if |weight| is 0.
void AddSample(int64_t value, double weight); template <typename T>
void AddSample(T value, double weight) {
AddDelta(value - weighted_mean_, weight);
}
private: private:
void AddDelta(double delta, double weight);
double weighted_mean_; double weighted_mean_;
double variance_sum_; double variance_sum_;
double sum_weights_; double sum_weights_;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment