3 #include <utl/AugerException.h>
4 #include <utl/ErrorLogger.h>
17 TabulatedPDF::TabulatedPDF()
24 TabulatedPDF::TabulatedPDF(
25 double xmin,
double xstep,
const std::vector< double > &yValues)
27 FillTable(xmin, xstep, yValues);
41 TabulatedPDF::FillTable(
42 double xmin,
double xstep,
const Array& yValues)
44 const unsigned int size = yValues.size();
50 for (
unsigned int i=0;i<size;++i) PushBack(yValues[i]);
55 TabulatedPDF::PushBack(
double y)
57 const unsigned int size = fY.size();
61 msg <<
"Value " << y <<
" at x = " << fXMin+(size-1)*fXStep <<
" is not a valid probability!";
67 fYSum.push_back(0.5*y*fXStep);
69 const double ysum = fYSum.back() + 0.5*(y+fY.back())*fXStep;
70 fYSum.push_back(ysum);
86 TabulatedPDF::Normalise()
88 if (fYSum.back()==1.0)
return;
90 const double factor = 1./fYSum.back();
92 for (
unsigned int i=0;i<fY.size();++i) {
101 TabulatedPDF::Density(
double x)
104 const unsigned int size = fY.size();
111 if (x < fXMin-fXStep/2)
115 else if (x > fXMin + (size-1)*fXStep)
118 unsigned int index =
static_cast<unsigned int>((x-fXMin)/fXStep);
119 const double x1 = fXMin+index*fXStep;
120 const double& dx = fXStep;
121 const double y1 = fY[index];
122 if (x1 == x)
return y1;
124 const double y2 = fY[index];
126 double prob = y1 + (y2 - y1)*(x - x1)/dx;
133 TabulatedPDF::Integral(
double xmin,
double xmax)
136 const unsigned int size = fY.size();
144 if (xmin > fXMin + (size-1)*fXStep)
150 if (xmax > fXMin + (size-1)*fXStep)
151 xmax = fXMin + (size-2)*fXStep;
153 const unsigned int iBegin =
static_cast<unsigned int>((xmin-fXMin)/fXStep);
154 const unsigned int iEnd =
static_cast<unsigned int>((xmax-fXMin)/fXStep)-1;
155 const double& dx = fXStep;
156 const double x1l = fXMin + iBegin*fXStep;
157 const double y1l = fYSum[iBegin];
158 const double y1u = fYSum[iBegin+1];
160 const double x2l = fXMin + iEnd*fXStep;
161 const double y2l = fYSum[iEnd];
162 const double y2u = fYSum[iEnd+1];
164 const double p1 = y1l + (y1u - y1l)*(xmin - x1l)/dx;
165 const double p2 = y2l + (y2u - y2l)*(xmax - x2l)/dx;
170 TabulatedPDF::Moment(
unsigned short order,
double xmin,
double xmax)
173 const unsigned int size = fY.size();
178 return fY[0]*
pow(fXMin,order);
183 else if (xmin > fXMin + (size-1)*fXStep)
187 else if (xmax > fXMin + (size-1)*fXStep)
188 xmax = fXMin + (size-1)*fXStep;
190 const unsigned int iBegin =
static_cast<unsigned int>((xmin-fXMin)/fXStep);
191 const unsigned int iEnd =
static_cast<unsigned int>((xmax-fXMin)/fXStep);
199 const double& h = fXStep;
200 mom = h*fY[iBegin]*
pow(fXMin+iBegin*fXStep,order);
205 for (
unsigned int i=iBegin+1;i<iEnd;++i)
207 const int weight = iloop ? 4:2 ;
209 mom += weight*h*fY[i]*
pow(fXMin+i*fXStep,order);
210 sum += weight*h*fY[i];
214 mom += h*fY[iEnd-1]*
pow(fXMin+iEnd*fXStep,order);
221 TabulatedPDF::Variance(
double xmin,
double xmax)
224 const unsigned int size = fY.size();
234 else if (xmin > fXMin + (size-1)*fXStep)
238 else if (xmax > fXMin + (size-1)*fXStep)
239 xmax = fXMin + (size-1)*fXStep;
241 const unsigned int iBegin =
static_cast<unsigned int>((xmin-fXMin)/fXStep);
242 const unsigned int iEnd =
static_cast<unsigned int>((xmax-fXMin)/fXStep);
251 const double& h = fXStep;
252 mom1 = h*fY[iBegin]*(fXMin+iBegin*fXStep);
253 mom2 = h*fY[iBegin]*(fXMin+iBegin*fXStep)*(fXMin+iBegin*fXStep);
258 for (
unsigned int i=iBegin+1;i<iEnd;++i)
260 const int weight = iloop ? 4:2 ;
262 mom1 += weight*h*fY[i]*(fXMin+i*fXStep);
263 mom2 += weight*h*fY[i]*(fXMin+i*fXStep)*(fXMin+i*fXStep);
264 sum += weight*h*fY[i];
268 mom1 += h*fY[iEnd]*(fXMin+iEnd*fXStep);
269 mom2 += h*fY[iEnd]*(fXMin+iEnd*fXStep)*(fXMin+iEnd*fXStep);
272 return mom2/sum-
pow(mom1/sum,2);
276 TabulatedPDF::Quantile(
const double psum)
279 const unsigned int size = fY.size();
284 for (
unsigned int i=0;i<size-1;++i)
285 if (fYSum[i] < psum && fYSum[i+1] > psum)
286 { index = i;
break; }
287 const double x0 = fXMin+index*fXStep;
288 const double& dx = fXStep;
289 const double& y0 = fYSum[index];
290 const double& y1 = fYSum[index+1];
291 const double quantile = (psum - y0)/(y1-y0)*dx+x0;
Base class for exceptions trying to access non-existing components.
double pow(const double x, const unsigned int i)
#define ERROR(message)
Macro for logging error messages.
TabulatedPDF::Array Array
std::vector< double > Array