diff options
author | alex-sh <alex-sh@yandex-team.ru> | 2022-02-10 16:50:03 +0300 |
---|---|---|
committer | Daniil Cherednik <dcherednik@yandex-team.ru> | 2022-02-10 16:50:03 +0300 |
commit | 3196904c9f5bf7aff7374eeadcb0671589581f61 (patch) | |
tree | d13114a178799aeb203a4b3b43dd7fb0c4f6975f /library/cpp/linear_regression/welford.cpp | |
parent | d154d11651ea533127249184148c3f023e2c6d0a (diff) | |
download | ydb-3196904c9f5bf7aff7374eeadcb0671589581f61.tar.gz |
Restoring authorship annotation for <alex-sh@yandex-team.ru>. Commit 1 of 2.
Diffstat (limited to 'library/cpp/linear_regression/welford.cpp')
-rw-r--r-- | library/cpp/linear_regression/welford.cpp | 186 |
1 files changed, 93 insertions, 93 deletions
diff --git a/library/cpp/linear_regression/welford.cpp b/library/cpp/linear_regression/welford.cpp index e27b1994f6..e1539fd506 100644 --- a/library/cpp/linear_regression/welford.cpp +++ b/library/cpp/linear_regression/welford.cpp @@ -1,106 +1,106 @@ -#include "welford.h" +#include "welford.h" #include <util/generic/ymath.h> - -void TMeanCalculator::Multiply(const double value) { - SumWeights *= value; -} - -void TMeanCalculator::Add(const double value, const double weight /*= 1.*/) { - SumWeights += weight; - if (SumWeights.Get()) { - Mean += weight * (value - Mean) / SumWeights.Get(); - } -} - -void TMeanCalculator::Remove(const double value, const double weight /*= 1.*/) { - SumWeights -= weight; - if (SumWeights.Get()) { - Mean -= weight * (value - Mean) / SumWeights.Get(); - } -} - -double TMeanCalculator::GetMean() const { - return Mean; -} - -double TMeanCalculator::GetSumWeights() const { - return SumWeights.Get(); -} - + +void TMeanCalculator::Multiply(const double value) { + SumWeights *= value; +} + +void TMeanCalculator::Add(const double value, const double weight /*= 1.*/) { + SumWeights += weight; + if (SumWeights.Get()) { + Mean += weight * (value - Mean) / SumWeights.Get(); + } +} + +void TMeanCalculator::Remove(const double value, const double weight /*= 1.*/) { + SumWeights -= weight; + if (SumWeights.Get()) { + Mean -= weight * (value - Mean) / SumWeights.Get(); + } +} + +double TMeanCalculator::GetMean() const { + return Mean; +} + +double TMeanCalculator::GetSumWeights() const { + return SumWeights.Get(); +} + void TMeanCalculator::Reset() { *this = TMeanCalculator(); } -void TCovariationCalculator::Add(const double firstValue, const double secondValue, const double weight /*= 1.*/) { - SumWeights += weight; - if (SumWeights.Get()) { - FirstValueMean += weight * (firstValue - FirstValueMean) / SumWeights.Get(); - Covariation += weight * (firstValue - FirstValueMean) * (secondValue - SecondValueMean); - SecondValueMean += weight * (secondValue - SecondValueMean) / SumWeights.Get(); - } -} - -void TCovariationCalculator::Remove(const double firstValue, const double secondValue, const double weight /*= 1.*/) { - SumWeights -= weight; - if (SumWeights.Get()) { - FirstValueMean -= weight * (firstValue - FirstValueMean) / SumWeights.Get(); - Covariation -= weight * (firstValue - FirstValueMean) * (secondValue - SecondValueMean); - SecondValueMean -= weight * (secondValue - SecondValueMean) / SumWeights.Get(); - } -} - -double TCovariationCalculator::GetFirstValueMean() const { - return FirstValueMean; -} - -double TCovariationCalculator::GetSecondValueMean() const { - return SecondValueMean; -} - -double TCovariationCalculator::GetCovariation() const { - return Covariation; -} - -double TCovariationCalculator::GetSumWeights() const { - return SumWeights.Get(); -} - +void TCovariationCalculator::Add(const double firstValue, const double secondValue, const double weight /*= 1.*/) { + SumWeights += weight; + if (SumWeights.Get()) { + FirstValueMean += weight * (firstValue - FirstValueMean) / SumWeights.Get(); + Covariation += weight * (firstValue - FirstValueMean) * (secondValue - SecondValueMean); + SecondValueMean += weight * (secondValue - SecondValueMean) / SumWeights.Get(); + } +} + +void TCovariationCalculator::Remove(const double firstValue, const double secondValue, const double weight /*= 1.*/) { + SumWeights -= weight; + if (SumWeights.Get()) { + FirstValueMean -= weight * (firstValue - FirstValueMean) / SumWeights.Get(); + Covariation -= weight * (firstValue - FirstValueMean) * (secondValue - SecondValueMean); + SecondValueMean -= weight * (secondValue - SecondValueMean) / SumWeights.Get(); + } +} + +double TCovariationCalculator::GetFirstValueMean() const { + return FirstValueMean; +} + +double TCovariationCalculator::GetSecondValueMean() const { + return SecondValueMean; +} + +double TCovariationCalculator::GetCovariation() const { + return Covariation; +} + +double TCovariationCalculator::GetSumWeights() const { + return SumWeights.Get(); +} + void TCovariationCalculator::Reset() { *this = TCovariationCalculator(); } -void TDeviationCalculator::Add(const double value, const double weight /*= 1.*/) { - const double lastMean = MeanCalculator.GetMean(); - MeanCalculator.Add(value, weight); - Deviation += weight * (value - lastMean) * (value - MeanCalculator.GetMean()); -} - -void TDeviationCalculator::Remove(const double value, const double weight /*= 1.*/) { - const double lastMean = MeanCalculator.GetMean(); - MeanCalculator.Remove(value, weight); - Deviation -= weight * (value - lastMean) * (value - MeanCalculator.GetMean()); -} - -double TDeviationCalculator::GetMean() const { - return MeanCalculator.GetMean(); -} - -double TDeviationCalculator::GetDeviation() const { - return Deviation; -} - -double TDeviationCalculator::GetStdDev() const { - const double sumWeights = GetSumWeights(); - if (!sumWeights) { - return 0.; - } - return sqrt(GetDeviation() / sumWeights); -} - -double TDeviationCalculator::GetSumWeights() const { - return MeanCalculator.GetSumWeights(); -} +void TDeviationCalculator::Add(const double value, const double weight /*= 1.*/) { + const double lastMean = MeanCalculator.GetMean(); + MeanCalculator.Add(value, weight); + Deviation += weight * (value - lastMean) * (value - MeanCalculator.GetMean()); +} + +void TDeviationCalculator::Remove(const double value, const double weight /*= 1.*/) { + const double lastMean = MeanCalculator.GetMean(); + MeanCalculator.Remove(value, weight); + Deviation -= weight * (value - lastMean) * (value - MeanCalculator.GetMean()); +} + +double TDeviationCalculator::GetMean() const { + return MeanCalculator.GetMean(); +} + +double TDeviationCalculator::GetDeviation() const { + return Deviation; +} + +double TDeviationCalculator::GetStdDev() const { + const double sumWeights = GetSumWeights(); + if (!sumWeights) { + return 0.; + } + return sqrt(GetDeviation() / sumWeights); +} + +double TDeviationCalculator::GetSumWeights() const { + return MeanCalculator.GetSumWeights(); +} void TDeviationCalculator::Reset() { *this = TDeviationCalculator(); |