1 #include <utl/RandomSamplerFromPDF.h>
2 #include <utl/RandomSamplerFromCDF.h>
3 #include <utl/ErrorLogger.h>
4 #include <utl/AugerUnits.h>
5 #include <utl/AugerException.h>
6 #include <utl/Function.h>
7 #include <utl/TabulatedFunction.h>
18 const vector<double> x(pdf.
XBegin(), pdf.
XEnd());
19 const vector<double> y(pdf.
YBegin(), pdf.
YEnd());
24 RandomSamplerFromPDF::RandomSamplerFromPDF(
const map<double, double>& pdf,
25 const InterpolationType intType)
29 for (map<double, double>::const_iterator it = pdf.begin();
30 it != pdf.end(); ++it) {
31 x.push_back(it->first);
32 y.push_back(it->second);
38 RandomSamplerFromPDF::~RandomSamplerFromPDF()
45 RandomSamplerFromPDF::GetInverseCDF(
const double y)
49 return fCDF->GetInverseCDF(y);
52 return fCDF->GetMinX();
54 return fCDF->GetMaxX();
56 const int i2 = fCDF->GetIndex(y);
57 const int i1 = i2 - 1;
58 const vector<double>& cdfx = fCDF->GetCDFX();
59 const vector<double>& cdfy = fCDF->GetCDFY();
60 const vector<double>& pdf = *fPDF;
61 const double x1 = cdfx[i1];
62 const double y1 = pdf[i1];
63 const double w = y - cdfy[i1];
64 const double k = (pdf[i2] - y1) / (cdfx[i2] - x1);
65 const double r =
sqrt(y1*y1 + 2*k*w);
66 return x1 + 2*w / (r + y1);
71 RandomSamplerFromPDF::SetFunction(
const string&
function,
72 const string& independentVariableName,
73 const double min,
const double max,
const int bins)
80 const double step = (max - min) / (bins - 1);
81 for (
int i = 0; i < bins; ++i) {
82 const double xx = min + i * step;
110 UniformFill(vector<double>& x,
const unsigned int n,
const unsigned int m)
112 const double step = 1. /
m;
113 for (
unsigned int i = 0; i < n; ++i)
122 const unsigned int n = y.size();
139 vector<double> yy(y);
141 Init(x, yy, intType);
152 for (vector<double>::iterator it = v.begin();
159 RandomSamplerFromPDF::InitDiscrete(
const vector<double>& x,
const vector<double>& y)
161 const unsigned int n = x.size();
162 if (n < 1 || n != y.size())
164 const unsigned int n2 = 2*n;
170 for (
unsigned int i = 0; i < n; ++i) {
171 unsigned int i2 = 2*i;
172 const double xx = x[i];
187 RandomSamplerFromPDF::InitStep(
const vector<double>& x,
const vector<double>& y)
189 const unsigned int n = x.size();
190 if (n < 2 || n != y.size())
199 for (
unsigned int i = 1; i < n; ++i) {
211 RandomSamplerFromPDF::InitLinear(
const vector<double>& x,
const vector<double>& y)
213 const unsigned int n = x.size();
214 if (n < 2 || n != y.size())
220 fPDF =
new vector<double>;
222 vector<double>& pdf2 = *fPDF;
224 double x1 = x.front();
227 double y1 = y.front();
229 for (
unsigned int i = 1; i < n; ++i) {
230 const double x2 = x[i];
232 const double y2 = y[i];
234 sum += 0.5 * (x2 - x1) * (y1 + y2);
242 "impossible to normalize");
243 const double fact = 1 / sum;
void Normalize(vector< double > &v, const double fact)
ArrayIterator XEnd()
end of array of X
Base class for exceptions arising because configuration data are not valid.
Class to hold collection (x,y) points and provide interpolation between them.
void Init()
Initialise the registry.
Evaluate functions given in a string. The real work is done by the ExpressionParser class...
ArrayIterator YBegin()
begin of array of Y
void UniformFill(vector< double > &x, const unsigned int n, const unsigned int m)
ArrayIterator XBegin()
begin of array of X
ArrayIterator YEnd()
end of array of Y