22#include <lal/AVFactories.h>
23#include <lal/LineRobustStats.h>
24#include <lal/LogPrintf.h>
48 const UINT4 numDetectors,
50 const REAL4 cohFstar0,
53 const REAL4 tolerance,
61 const UINT4 numDetectors,
63 const REAL4 cohFstar0,
74 const REAL4 tolerance,
75 const CHAR *casestring
83int main(
int argc,
char *argv[] )
87 XLAL_CHECK( argc == 1,
XLAL_EINVAL,
"The executable '%s' doesn't support any input arguments right now.\n", argv[0] );
89 printf(
"Starting coherent test...\n" );
98 TwoFX->
data[1] = 12.0;
105 oLtLGXarray[0] = 0.5;
106 oLtLGXarray[1] = 0.8;
107 REAL4 *oLtLGX = oLtLGXarray;
108 printf(
"Computing BSGL for TwoF_multi=%f, TwoFX=(%f,%f), priors cohF*0=%f and oLtLGX=(%f,%f)...\n", TwoF, TwoFX->
data[0], TwoFX->
data[1], cohFstar0, oLtLGX[0], oLtLGX[1] );
109 XLAL_CHECK(
XLALCompareBSGLComputations( TwoF,
numDetectors, TwoFX, cohFstar0, oLtLGX, numSegs, tolerance,
LRS_BSGL, 0.0, NULL ) ==
XLAL_SUCCESS,
XLAL_EFUNC );
115 printf(
"Computing BSGL for TwoF_multi=%f, TwoFX=(%f,%f), priors cohF*0=%f and oLtLGX=NULL...\n", TwoF, TwoFX->
data[0], TwoFX->
data[1], cohFstar0 );
116 XLAL_CHECK(
XLALCompareBSGLComputations( TwoF,
numDetectors, TwoFX, cohFstar0, oLtLGX, numSegs, tolerance,
LRS_BSGL, 0.0, NULL ) ==
XLAL_SUCCESS,
XLAL_EFUNC );
120 printf(
"Starting semi-coherent test...\n" );
128 TwoFl->
data[0] = 4.0;
129 TwoFl->
data[1] = 8.0;
130 TwoFl->
data[2] = 4.0;
132 for (
UINT4 l = 0;
l < numSegs;
l++ ) {
136 TwoFXl->
data[0 + 1] = 32.0;
142 sumTwoFX->
data[X] = 0;
144 for (
UINT4 l = 0;
l < numSegs;
l++ ) {
145 sumTwoF += TwoFl->
data[
l];
147 sumTwoFX->
data[X] += TwoFXl->
data[X * numSegs +
l];
151 printf(
"Computing semi-coherent BSGL for sum_TwoF_multi=%f, sum_TwoFX=(%f,%f), priors cohF*0=%f and oLtLGX=NULL...\n", sumTwoF, sumTwoFX->
data[0], sumTwoFX->
data[1], cohFstar0 );
152 XLAL_CHECK(
XLALCompareBSGLComputations( sumTwoF,
numDetectors, sumTwoFX, cohFstar0, oLtLGX, numSegs, tolerance,
LRS_BSGL, 0.0, NULL ) ==
XLAL_SUCCESS,
XLAL_EFUNC );
158 maxTwoFX->
data[X] = 0;
160 for (
UINT4 l = 0;
l < numSegs;
l++ ) {
161 maxTwoF =
fmax( maxTwoF, TwoFl->
data[
l] );
167 printf(
"Computing semi-coherent BSGLtL for sum_TwoF_multi=%f, sum_TwoFX=(%f,%f), max_TwoF_multi=%f, max_TwoFX=(%f,%f), priors cohF*0=%f and oLtLGX=NULL...\n", sumTwoF, sumTwoFX->
data[0], sumTwoFX->
data[1], maxTwoF, maxTwoFX->
data[0], maxTwoFX->
data[1], cohFstar0 );
168 XLAL_CHECK(
XLALCompareBSGLComputations( sumTwoF,
numDetectors, sumTwoFX, cohFstar0, oLtLGX, numSegs, tolerance,
LRS_BSGLtL, maxTwoF, maxTwoFX ) ==
XLAL_SUCCESS,
XLAL_EFUNC );
170 printf(
"Computing semi-coherent BtSGLtL for sum_TwoF_multi=%f, sum_TwoFX=(%f,%f), max_TwoF_multi=%f, max_TwoFX=(%f,%f), priors cohF*0=%f and oLtLGX=NULL...\n", sumTwoF, sumTwoFX->
data[0], sumTwoFX->
data[1], maxTwoF, maxTwoFX->
data[0], maxTwoFX->
data[1], cohFstar0 );
171 XLAL_CHECK(
XLALCompareBSGLComputations( sumTwoF,
numDetectors, sumTwoFX, cohFstar0, oLtLGX, numSegs, tolerance,
LRS_BtSGLtL, maxTwoF, maxTwoFX ) ==
XLAL_SUCCESS,
XLAL_EFUNC );
173 printf(
"Computing semi-coherent BStSGLtL for sum_TwoF_multi=%f, sum_TwoFX=(%f,%f), max_TwoF_multi=%f, max_TwoFX=(%f,%f), priors cohF*0=%f and oLtLGX=NULL...\n", sumTwoF, sumTwoFX->
data[0], sumTwoFX->
data[1], maxTwoF, maxTwoFX->
data[0], maxTwoFX->
data[1], cohFstar0 );
174 XLAL_CHECK(
XLALCompareBSGLComputations( sumTwoF,
numDetectors, sumTwoFX, cohFstar0, oLtLGX, numSegs, tolerance,
LRS_BStSGLtL, maxTwoF, maxTwoFX ) ==
XLAL_SUCCESS,
XLAL_EFUNC );
200 const UINT4 numDetectors,
202 const REAL4 cohFstar0,
205 const REAL4 tolerance,
217 BSGLSetup *setup_noLogCorrection;
219 REAL4 log10_LRS_XLAL_notallterms = 0.0;
220 char funcname[64] =
"";
221 switch ( LRstat_variant ) {
224 snprintf( funcname,
sizeof( funcname ),
"%s",
"XLALComputeBSGL" );
228 snprintf( funcname,
sizeof( funcname ),
"%s",
"XLALComputeBSGLtL" );
232 snprintf( funcname,
sizeof( funcname ),
"%s",
"XLALComputeBtSGLtL" );
236 snprintf( funcname,
sizeof( funcname ),
"%s",
"XLALComputeBStSGLtL" );
241 setup_noLogCorrection = NULL;
244 BSGLSetup *setup_withLogCorrection;
246 REAL4 log10_LRS_XLAL_allterms = 0.0;
247 switch ( LRstat_variant ) {
261 printf(
"log correction not implemented for GLtL denominator, skipping full-precision test for this LRS variant.\n" );
265 setup_withLogCorrection = NULL;
268 REAL4 diff_allterms = 0.0;
269 if ( LRstat_variant == 0 ) {
270 diff_allterms = fabs( log10_LRS_XLAL_allterms - log10LRS_extcomp_allterms ) / ( 0.5 * ( log10_LRS_XLAL_allterms + log10LRS_extcomp_allterms ) );
272 REAL4 diff_notallterms = fabs( log10_LRS_XLAL_notallterms - log10LRS_extcomp_notallterms ) / ( 0.5 * ( log10_LRS_XLAL_notallterms + log10LRS_extcomp_notallterms ) );
275 printf(
"Externally recomputed with allterms: log10LRS=%f\n", log10LRS_extcomp_allterms );
276 printf(
"Externally recomputed with !allterms: log10LRS=%f\n", log10LRS_extcomp_notallterms );
277 char casestring[256] =
"";
278 if ( LRstat_variant == 0 ) {
279 printf(
"%s() with allterms: log10LRS=%f (rel. dev.: %f)", funcname, log10_LRS_XLAL_allterms, diff_allterms );
280 snprintf( casestring,
sizeof( casestring ),
"%s() with useAllTerms=TRUE", funcname );
283 printf(
"%s() with !allterms: log10LRS=%f (rel. dev.: %f)", funcname, log10_LRS_XLAL_notallterms, diff_notallterms );
284 snprintf( casestring,
sizeof( casestring ),
"%s() with useAllTerms=FALSE", funcname );
298 const UINT4 numDetectors,
300 const REAL4 cohFstar0,
302 const BOOLEAN useLogCorrection,
310 REAL4 log10LRS_extcomp = 0;
324 oLtLGX = oLtLGXarray;
327 REAL4 numerator = 0.0;
329 numerator += exp( 0.5 * TwoF );
332 numerator += exp( 0.5 * maxTwoF + ( numSegs - 1 ) * cohFstar0 ) / numSegs;
339 log10LRS_extcomp =
log( numerator );
341 REAL4 *denomterms = NULL;
345 denomterms = denomterms_GL;
347 denomterms = denomterms_GLtL;
350 denomterms[0] = exp( numSegs * cohFstar0 ) / oLtLG;
351 REAL4 maxterm = denomterms[0];
353 denomterms[1 + X] = oLtLGX[X] * exp( 0.5 * TwoFX->
data[X] ) / oLtLG;
355 denomterms[1 + X] *= 0.5;
357 maxterm =
fmax( maxterm, denomterms[1 + X] );
361 denomterms[1 +
numDetectors + X] = 0.5 * oLtLGX[X] * exp( 0.5 * maxTwoFX->
data[X] + ( numSegs - 1 ) * cohFstar0 ) / ( oLtLG * numSegs );
366 if ( !useLogCorrection ) {
367 log10LRS_extcomp -=
log( maxterm );
369 REAL4 BSGL_extcomp_denom = 0.0;
371 BSGL_extcomp_denom += denomterms[X];
373 log10LRS_extcomp -=
log( BSGL_extcomp_denom );
377 log10LRS_extcomp +=
log( 1 + 1 / oLtLG );
381 return log10LRS_extcomp;
387 const REAL4 tolerance,
388 const CHAR *casestring
392 if ( fabs( diff ) <= tolerance ) {
393 printf(
" ==> OK!\n" );
395 printf(
" ==> BAD!\n" );
410 REAL4 cohFstar0 = 4.37;
414 BSGLSetup *setup_noLogCorr;
415 BSGLSetup *setup_withLogCorr;
422 {14.333578, 18.477274, 19.765395, 18.754920, 22.048903},
423 {14.180436, 15.754341, 17.745453, 14.727934, 18.460679},
424 {21.988492, 15.632081, 18.439836, 19.492070, 16.174584},
438 twoF[
i] += twoFPerDet0[X][
i];
440 twoF[
i] *= attenuate[
i];
445 twoFPerDet[X] = twoFPerDet0[X];
448 REAL4 maxTwoFSeg[
VEC_LEN] = {4.585174, 13.221874, 13.005110, 34.682062, 48.968980};
450 {12.341489, 9.090180, 16.084636, 14.056626, 11.450930},
451 {14.006959, 8.774348, 9.584360, 18.157686, 11.551223},
452 {7.490141, 18.599300, 9.724949, 15.562138, 10.512018},
463 maxTwoFSegPerDet[X] = maxTwoFSegPerDet0[X];
477 printf(
"\nBSGL_log = {" );
479 printf(
"%f%s ", outBSGL_log[
i], (
i <
VEC_LEN - 1 ) ?
"," :
"}" );
482 printf(
"\nBSGL_nolog = {" );
484 printf(
"%f%s ", outBSGL_nolog[
i], (
i <
VEC_LEN - 1 ) ?
"," :
"}" );
487 printf(
"\nBSGLtL = {" );
489 printf(
"%f%s ", outBSGLtL[
i], (
i <
VEC_LEN - 1 ) ?
"," :
"}" );
492 printf(
"\nBtSGLtL = {" );
494 printf(
"%f%s ", outBtSGLtL[
i], (
i <
VEC_LEN - 1 ) ?
"," :
"}" );
499 REAL4 errBSGL_log = 0;
500 REAL4 errBSGL_nolog = 0;
502 REAL4 errBtSGLtL = 0;
508 twoFX[X] = twoFPerDet[X][
i];
509 maxTwoFXl[X] = maxTwoFSegPerDet[X][
i];
515 errBSGL_log = fmaxf( errBSGL_log, fabsf( cmp - outBSGL_log[
i] ) );
516 printf(
"i = %d: err = %g\n",
i, errBSGL_log );
521 errBSGL_nolog = fmaxf( errBSGL_nolog, fabsf( cmp - outBSGL_nolog[
i] ) );
522 printf(
"i = %d: err = %g\n",
i, errBSGL_nolog );
527 errBSGLtL = fmaxf( errBSGLtL, fabsf( cmp - outBSGLtL[
i] ) );
528 printf(
"i = %d: err = %g\n",
i, errBSGLtL );
533 errBtSGLtL = fmaxf( errBtSGLtL, fabsf( cmp - outBtSGLtL[
i] ) );
534 printf(
"i = %d: err = %g\n",
i, errBtSGLtL );
539 XLAL_CHECK( errBSGL_log <= tolerance, XLAL_ETOL, "Error in vector BSGL with log-correction exceeds tolerance, %g > %g\
n", errBSGL_log, tolerance );
540 XLAL_CHECK( errBSGL_nolog <= tolerance, XLAL_ETOL, "Error in
vector BSGL without
log-correction exceeds tolerance, %g > %g\
n", errBSGL_nolog, tolerance );
541 XLAL_CHECK( errBSGLtL <= tolerance, XLAL_ETOL, "Error in
vector BSGLtL exceeds tolerance, %g > %g\
n", errBSGLtL, tolerance );
542 XLAL_CHECK( errBtSGLtL <= tolerance, XLAL_ETOL, "Error in
vector BtSGLtL exceeds tolerance, %g > %g\
n", errBtSGLtL, tolerance );
544 printf( "%
s: success!\
n", __func__ );
546 XLALDestroyBSGLSetup( setup_noLogCorr );
547 XLALDestroyBSGLSetup( setup_withLogCorr );
551} // XLALCheckBSGLVectorFunctions()
void LALCheckMemoryLeaks(void)
int main(int argc, char *argv[])
int XLALCheckBSGLDifferences(const REAL4 diff, const REAL4 tolerance, const CHAR *casestring)
int XLALCompareBSGLComputations(const REAL4 TwoF, const UINT4 numDetectors, const REAL4Vector *TwoFX, const REAL4 cohFstar0, const REAL4 *oLtLGX, const UINT4 numSegs, const REAL4 tolerance, const LRstat_variant_t LRstat_variant, const REAL4 maxTwoF, const REAL4Vector *maxTwoFX)
Test function to compute BSGL values from XLALComputeBSGL against those recomputed from scratch ("ped...
REAL4 XLALComputePedestrianLRStat(const REAL4 TwoF, const UINT4 numDetectors, const REAL4Vector *TwoFX, const REAL4 cohFstar0, const REAL4 *oLtLGX, const BOOLEAN useLogCorrection, const LRstat_variant_t LRstat_variant, const REAL4 maxTwoF, const REAL4Vector *maxTwoFX, const UINT4 numSegs)
compute BSGL "the pedestrian way", from Eq.
int XLALCheckBSGLVectorFunctions(void)
LRstat_variant_t
Enum for LR-statistic variants.
int XLALVectorComputeBSGLtL(REAL4 *outBSGLtL, const REAL4 *twoF, const REAL4 *twoFPerDet[PULSAR_MAX_DETECTORS], const REAL4 *maxTwoFSegPerDet[PULSAR_MAX_DETECTORS], const UINT4 len, const BSGLSetup *setup)
REAL4 XLALComputeBSGLtL(const REAL4 twoF, const REAL4 twoFX[PULSAR_MAX_DETECTORS], const REAL4 maxtwoFlX[PULSAR_MAX_DETECTORS], const BSGLSetup *setup)
Single-bin wrapper of XLALVectorComputeBSGLtL(), provided for backwards compatibility.
REAL4 XLALComputeBtSGLtL(const REAL4 maxtwoFl, const REAL4 twoFX[PULSAR_MAX_DETECTORS], const REAL4 maxtwoFXl[PULSAR_MAX_DETECTORS], const BSGLSetup *setup)
Single-bin wrapper of XLALVectorComputeBtSGLtL(), provided for backwards compatibility.
void XLALDestroyBSGLSetup(BSGLSetup *setup)
int XLALVectorComputeBSGL(REAL4 *outBSGL, const REAL4 *twoF, const REAL4 *twoFPerDet[PULSAR_MAX_DETECTORS], const UINT4 len, const BSGLSetup *setup)
REAL4 XLALComputeBStSGLtL(const REAL4 twoF, const REAL4 maxtwoFl, const REAL4 twoFX[PULSAR_MAX_DETECTORS], const REAL4 maxtwoFXl[PULSAR_MAX_DETECTORS], const BSGLSetup *setup)
BSGLSetup * XLALCreateBSGLSetup(const UINT4 numDetectors, const REAL4 Fstar0sc, const REAL4 oLGX[PULSAR_MAX_DETECTORS], const BOOLEAN useLogCorrection, const UINT4 numSegments)
REAL4 XLALComputeBSGL(const REAL4 twoF, const REAL4 twoFX[PULSAR_MAX_DETECTORS], const BSGLSetup *setup)
Single-bin wrapper of XLALVectorComputeBSGL(), provided for backwards compatibility.
int XLALVectorComputeBtSGLtL(REAL4 *outBtSGLtL, const REAL4 *maxTwoFSeg, const REAL4 *twoFPerDet[PULSAR_MAX_DETECTORS], const REAL4 *maxTwoFSegPerDet[PULSAR_MAX_DETECTORS], const UINT4 len, const BSGLSetup *setup)
#define PULSAR_MAX_DETECTORS
maximal number of detectors we can handle (for static arrays of detector quantities)
REAL4Vector * XLALCreateREAL4Vector(UINT4 length)
void XLALDestroyREAL4Vector(REAL4Vector *vector)
#define XLAL_CHECK(assertion,...)
#define XLAL_CHECK_MAIN(assertion,...)