blob: e27b1994f6cdd27186830b12c5aaee7c92dc812f (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
|
#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::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::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::Reset() {
*this = TDeviationCalculator();
}
|