-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.cpp
223 lines (185 loc) · 5.95 KB
/
main.cpp
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
#include <algorithm>
#include <cmath>
#include <iostream>
#include <memory>
#include <numeric>
#include <sstream>
#include <vector>
class TKahanAccumulator {
private:
double Sum;
double Addition;
public:
TKahanAccumulator(const double value = 0.)
: Sum(value)
, Addition(0.)
{
}
TKahanAccumulator& operator += (const double value) {
const double y = value - Addition;
const double t = Sum + y;
Addition = (t - Sum) - y;
Sum = t;
return *this;
}
TKahanAccumulator& operator += (const TKahanAccumulator& other) {
return *this += (double)other;
}
operator double() const {
return Sum + Addition;
}
};
class ICovariationCalculator {
private:
public:
virtual void Add(const double x, const double y) = 0;
virtual double Covariation() const = 0;
virtual std::string Name() const = 0;
};
template <class TAccumulatorType>
class TTypedCovariationCalculator : public ICovariationCalculator {
private:
size_t Count = 0;
TAccumulatorType SumX = 0.;
TAccumulatorType SumY = 0.;
TAccumulatorType SumProducts = 0.;
public:
void Add(const double x, const double y) override {
++Count;
SumX += x;
SumY += y;
SumProducts += x * y;
}
double Covariation() const override {
return ((double) SumProducts - (double) SumX * (double) SumY / Count) / Count;
}
std::string Name() const override;
};
using TDummyCovariationCalculator = TTypedCovariationCalculator<long double>;
using TKahanCovariationCalculator = TTypedCovariationCalculator<TKahanAccumulator>;
template <>
std::string TDummyCovariationCalculator::Name() const {
return "Dummy";
};
template <>
std::string TKahanCovariationCalculator::Name() const {
return "Kahan";
};
class TWelfordCovariationCalculator : public ICovariationCalculator {
private:
size_t Count = 0;
double MeanX = 0.;
double MeanY = 0.;
double SumProducts = 0.;
public:
void Add(const double x, const double y) override {
++Count;
MeanX += (x - MeanX) / Count;
SumProducts += (x - MeanX) * (y - MeanY);
MeanY += (y - MeanY) / Count;
}
double Covariation() const override {
return SumProducts / Count;
}
std::string Name() const override {
return "Welford";
}
};
double Error(const double target, const double value) {
return fabs(value - target) / fabs(target);
}
class TPrinter {
private:
std::string Title;
std::vector<std::string> Columns;
std::vector<std::vector<std::string>> Rows;
public:
TPrinter(const std::string& title)
: Title(title)
{
}
void AddColumn(const std::string& name) {
Columns.push_back(name);
}
void AddRow() {
Rows.push_back(std::vector<std::string>());
}
template <class TValue>
void AddToRow(const TValue& value) {
std::stringstream ss;
ss.precision(10);
ss << value;
Rows.back().push_back(ss.str());
}
void Print() const {
auto printRow = [&](const std::vector<std::string>& row){
const size_t columnWidth = 25;
std::stringstream rowStream;
for (const std::string& element : row) {
PrintNextColumn(rowStream, element, columnWidth);
}
printf("%s\n", rowStream.str().c_str());
};
printf("%s\n", Title.c_str());
printRow(Columns);
for (const std::vector<std::string>& row : Rows) {
printRow(row);
}
}
private:
template <class TValue>
void PrintNextColumn(std::stringstream& out, const TValue& value, size_t columnWidth) const {
const size_t startLength = out.str().size();
out << value;
const size_t currentLength = out.str().size();
for (size_t i = currentLength; i < startLength + columnWidth; ++i) {
out << " ";
}
}
};
int main() {
double interestingMeans[] = { 100000, 10000000 };
for (const double mean : interestingMeans) {
const double xMean = mean;
const double yMean = mean;
double xDiff = 1;
double yDiff = 1;
const double actualCovariation = xDiff * yDiff;
std::vector<std::shared_ptr<ICovariationCalculator>> calculators;
calculators.push_back(std::shared_ptr<TDummyCovariationCalculator>(new TDummyCovariationCalculator()));
calculators.push_back(std::shared_ptr<TKahanCovariationCalculator>(new TKahanCovariationCalculator()));
calculators.push_back(std::shared_ptr<TWelfordCovariationCalculator>(new TWelfordCovariationCalculator()));
TPrinter printer("mean: " + std::to_string(mean));
printer.AddColumn("Count");
for (auto&& calculator : calculators) {
printer.AddColumn(calculator->Name());
}
std::vector<double> maxErrors(calculators.size(), 0.);
const size_t count = 10000000;
for (size_t i = 0; i < count; ++i) {
if (i && (i % (count / 100) == 0)) {
printer.AddRow();
printer.AddToRow(i);
for (size_t calculatorIdx = 0; calculatorIdx < calculators.size(); ++calculatorIdx) {
const double calculatedCovariation = calculators[calculatorIdx]->Covariation();
const double error = Error(actualCovariation, calculatedCovariation) * 100;
printer.AddToRow(error);
maxErrors[calculatorIdx] = std::max(maxErrors[calculatorIdx], error);
}
}
xDiff = -xDiff;
yDiff = -yDiff;
for (auto&& calculator : calculators) {
calculator->Add(xMean + xDiff, yMean + yDiff);
}
}
printer.AddRow();
printer.AddToRow("MaxError");
for (size_t i = 0; i < calculators.size(); ++i) {
printer.AddToRow(maxErrors[i]);
}
printer.Print();
printf("\n\n");
}
return 0;
}