-
Notifications
You must be signed in to change notification settings - Fork 5
/
create_regression_points.cpp
65 lines (59 loc) · 2.06 KB
/
create_regression_points.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
#include <atomic>
#include <random>
//---------------------------------------------------------------------------
#include <udo/UDOperator.hpp>
//---------------------------------------------------------------------------
using namespace std;
//---------------------------------------------------------------------------
/// The output of this operator
struct Output {
/// The x value
double x;
/// The y value
double y;
};
//---------------------------------------------------------------------------
/// A generator for random 2D points that lie on the curve given by:
///
/// y = a + bx + cx^2 + e
///
/// Where a, b, and c are given in the constructor and e is a randomly
/// generated error value that is normally distributed with mean 0 and
/// variance (a + b + c)^2.
/// x will be chosen randomly uniformly distributed in [0, 100].
class CreateRegressionPoints : public udo::UDOperator<udo::EmptyTuple, Output> {
private:
/// The parameter a
double a;
/// The parameter b
double b;
/// The parameter c
double c;
/// The number of points that should be generated
uint64_t numPoints;
/// The counter for the threads that generate the points
atomic<uint64_t> pointsCounter = 0;
public:
/// Constructor
CreateRegressionPoints(double a, double b, double c, uint64_t numPoints)
: a(a), b(b), c(c), numPoints(numPoints) {}
/// Produce the output
bool postProduce(LocalState& /*localState*/) {
auto firstIndex = pointsCounter.fetch_add(10000);
if (firstIndex >= numPoints)
return true;
uint64_t seed = 42 + firstIndex;
mt19937_64 gen(seed);
uniform_real_distribution xDist(0.0, 100.0);
double stddev = a + b + c;
normal_distribution eDist(0.0, stddev);
for (uint64_t i = 0; i < 10000 && firstIndex + i < numPoints; ++i) {
double x = xDist(gen);
double e = eDist(gen);
double y = a + b * x + c * x * x + e;
produceOutputTuple({x, y});
}
return false;
}
};
//---------------------------------------------------------------------------