1 #include <utl/RandomSamplerFromCDF.h>
2 #include <utl/ErrorLogger.h>
3 #include <utl/AugerUnits.h>
4 #include <utl/AugerException.h>
5 #include <utl/Function.h>
6 #include <utl/TabulatedFunction.h>
16 fX(cdf.XBegin(), cdf.XEnd()),
17 fY(cdf.YBegin(), cdf.YEnd())
24 fY(cdf.begin(), cdf.end())
26 const int size = cdf.size();
28 const double step = 1. / (size - 1);
29 for (
int i = 0; i < size; ++i)
37 for (map<double, double>::const_iterator it = cdf.begin();
38 it != cdf.end(); ++it) {
39 fX.push_back(it->first);
40 fY.push_back(it->second);
56 const int i1 = i2 - 1;
57 const double x1 =
fX[i1];
58 const double y1 =
fY[i1];
59 return x1 + (
fX[i2] - x1)*(y - y1)/(
fY[i2] - y1);
65 const double min,
const double max,
const int bins)
69 const vector<string> vars = {independentVariableName};
71 const double step = (max - min) / (bins - 1);
72 for (
int i = 0; i < bins; ++i) {
73 const double x = min + i * step;
74 const double y = func(x);
85 const unsigned int n =
fX.size();
86 if (n < 2 || n !=
fY.size())
88 if (
fY.front() != 0) {
90 err <<
"CDF starts with " <<
fY.front() <<
" instead of 0.";
93 vector<double>::iterator it =
fY.begin();
95 for (++it; it !=
fY.end(); ++it) {
96 const double y2 = *it;
98 cerr <<
"CDF is not monotonously increasing: " << y1 <<
" > " << y2 << endl;
105 const double fac = 1 / y1;
106 for (it =
fY.begin(); it !=
fY.end(); ++it)
116 cerr <<
"CDF (x,y) dump:" << endl;
117 const size_t n =
fX.size();
118 for (
size_t i = 0; i < n; ++i)
119 cerr <<
fX.at(i) <<
' ' <<
fY.at(i) << endl;
Base class for exceptions arising because configuration data are not valid.
Class to hold collection (x,y) points and provide interpolation between them.
int GetIndex(const double y) const
double GetInverseCDF(const double y) const
Evaluate functions given in a string. The real work is done by the ExpressionParser class...
void SetFunction(const std::string &function, const std::string &independentVariableName, const double min, const double max, const int bins)
RandomSamplerFromCDF(const std::vector< double > &x, const std::vector< double > &y)
Construct using a CDF defined by X and Y vectors.