35 #include <gsl/gsl_math.h>
38 #include <lal/XLALError.h>
39 #include <lal/AVFactories.h>
40 #include <lal/LALMalloc.h>
41 #include <lal/LALStdlib.h>
43 #include <lal/ProbabilityDensity.h>
74 if ( pdf->xTics->length == 1 ) {
75 return pdf->xTics->data[0];
79 if ( pdf->xTics->length == 2 ) {
80 return gsl_ran_flat( rng, pdf->xTics->data[0], pdf->xTics->data[1] );
87 if ( pdf->sampling == NULL ) {
98 REAL8 dx_i = pdf->xTics->data[
i + 1] - pdf->xTics->data[
i];
99 prob[
i] = pdf->probDens->data[
i] * dx_i;
102 if ( ( pdf->sampling = gsl_ran_discrete_preproc(
numBins, prob ) ) == NULL ) {
111 UINT4 ind = gsl_ran_discrete( rng, pdf->sampling );
113 REAL8 x0 = pdf->xTics->data[ind];
114 REAL8 x1 = pdf->xTics->data[ind + 1];
118 return gsl_ran_flat( rng, x0, x1 );
139 if ( ( pdf->xTics == NULL ) || ( pdf->xTics->length == 0 ) || ( pdf->xTics->data == NULL ) ) {
145 if ( ( pdf->xTics->length == 1 ) && ( pdf->probDens == NULL ) ) {
150 if ( ( pdf->xTics->length == 2 ) && ( pdf->probDens == NULL ) ) {
158 if ( pdf->probDens == NULL ) {
162 if ( pdf->probDens->length !=
numBins ) {
166 if ( pdf->probDens->data == NULL ) {
176 REAL8 dx_i = pdf->xTics->data[
i + 1] - pdf->xTics->data[
i];
177 norm += pdf->probDens->data[
i] * dx_i;
180 if ( gsl_fcmp( norm, 1.0, relErr ) != 0 ) {
181 XLALPrintError(
"%s: pdf claims to be normalized, but norm = %.16f differs from 1.0 by more than %g relative error\n",
__func__, norm, relErr );
204 if ( pdf->probDens ) {
208 if ( pdf->sampling ) {
209 gsl_ran_discrete_free( pdf->sampling );
232 if ( ( ret =
XLALCalloc( 1,
sizeof( *ret ) ) ) == NULL ) {
243 ret->xTics->data[0] = x0;
269 if ( ( ret =
XLALCalloc( 1,
sizeof( *ret ) ) ) == NULL ) {
280 ret->xTics->data[0] = xMin;
281 ret->xTics->data[1] = xMax;
314 if ( ( ret =
XLALCalloc( 1,
sizeof( *ret ) ) ) == NULL ) {
336 ret->xTics->data[
i] = xi;
341 memset( ret->probDens->data, 0,
numBins *
sizeof( *ret->probDens->data ) );
365 if ( ( pdf->xTics->length == 1 ) && ( pdf->probDens == NULL ) ) {
370 if ( ( pdf->xTics->length == 2 ) && ( pdf->probDens == NULL ) ) {
375 if ( pdf->isNormalized ) {
384 REAL8 dx_i = pdf->xTics->data[
i + 1] - pdf->xTics->data[
i];
385 norm += pdf->probDens->data[
i] * dx_i;
388 REAL8 invNorm = 1.0 / norm;
390 pdf->probDens->data[
i] *= invNorm;
393 pdf->isNormalized = 1;
420 #define PDF_FMT "%.16g"
429 fprintf(
fp,
"%%%% Probability density function in octave format, as a 2xN matrix, containing value-pairs { x[i], pdf(x[i]) }\n" );
433 if ( ( pdf->xTics->length == 1 ) && ( pdf->probDens == NULL ) ) {
439 if ( ( pdf->xTics->length == 2 ) && ( pdf->probDens == NULL ) ) {
440 REAL8 x0 = pdf->xTics->data[0];
441 REAL8 x1 = pdf->xTics->data[1];
442 REAL8 p = 1.0 / ( x1 - x0 );
465 REAL8 xMid = 0.5 * ( pdf->xTics->data[
i] + pdf->xTics->data[
i + 1] );
508 if ( ( pdf->xTics->length == 1 ) && ( pdf->probDens == NULL ) ) {
509 return pdf->xTics->data[0];
513 if ( ( pdf->xTics->length == 2 ) && ( pdf->probDens == NULL ) ) {
514 return 0.5 * ( pdf->xTics->data[0] + pdf->xTics->data[1] );
522 REAL8 this_p = pdf->probDens->data[
i];
523 if ( this_p > max_p ) {
529 REAL8 xmode = 0.5 * ( pdf->xTics->data[mode_i] + pdf->xTics->data[mode_i + 1] );
#define __func__
log an I/O error, i.e.
pdf1D_t * XLALCreateUniformPDF1D(REAL8 xMin, REAL8 xMax)
Creator function for a uniform 1D pdf over [xMin, xMax].
int XLALOutputPDF1D_to_fp(FILE *fp, const pdf1D_t *pdf, const char *name)
Function to write a pdf1D to given filepointer fp.
pdf1D_t * XLALCreateDiscretePDF1D(REAL8 xMin, REAL8 xMax, UINT4 numBins)
Creator function for a generic discrete 1D pdf over [xMin, xMax], discretized into numBins bins.
int XLALNormalizePDF1D(pdf1D_t *pdf)
Method to normalize the given pdf1D.
REAL8 XLALFindModeOfPDF1D(const pdf1D_t *pdf)
Find the 'mode' of the probabilty distribution, ie.
pdf1D_t * XLALCreateSingularPDF1D(REAL8 x0)
Creator function for a 'singular' 1D pdf, containing a single value with certainty,...
void XLALDestroyPDF1D(pdf1D_t *pdf)
Destructor function for 1-D pdf.
REAL8 XLALDrawFromPDF1D(pdf1D_t *pdf, const gsl_rng *rng)
Function to generate random samples drawn from the given pdf(x) NOTE: if the 'sampling' field is NULL...
int XLALCheckValidPDF1D(const pdf1D_t *pdf)
Checks internal consistency of pdf1D object.
void * XLALCalloc(size_t m, size_t n)
void * XLALMalloc(size_t n)
REAL8Vector * XLALCreateREAL8Vector(UINT4 length)
void XLALDestroyREAL8Vector(REAL8Vector *vector)
#define XLAL_ERROR_REAL8(...)
#define XLAL_ERROR_NULL(...)
int XLALPrintError(const char *fmt,...) _LAL_GCC_PRINTF_FORMAT_(1
p
RUN ANALYSIS SCRIPT ###.