/** * Test function to compute BSGL values from: * XLALComputeBSGL, from scratch * and from deprecated XLALComputeLineVeto and XLALComputeLineVetoArray * compare the results and exit if tolerance is violated. */ int XLALCompareBSGLComputations ( const REAL4 TwoF, /**< multi-detector Fstat */ const UINT4 numDetectors, /**< number of detectors */ const REAL4Vector *TwoFX, /**< vector of single-detector Fstats */ const REAL4 Fstar0, /**< amplitude prior normalization for lines */ const REAL4 *oLGX, /**< array of single-detector prior line odds ratio, can be NULL */ const REAL4 tolerance /**< tolerance for comparisons */ ) { /* conversions between old (rho) and new (F*) notation, REAL4 and REAL8 */ REAL8 LVrho = exp( 0.25 * ( Fstar0 + log(70.0) ) ); REAL4 oLG = 0.0; REAL8Vector *oLGXREAL8 = NULL; XLAL_CHECK ( (oLGXREAL8 = XLALCreateREAL8Vector ( numDetectors )) != NULL, XLAL_EFUNC ); if ( oLGX ) { for ( UINT4 X = 0; X < numDetectors; X++ ) { oLGXREAL8->data[X] = (REAL8)oLGX[X]; oLG += oLGX[X]; } } else { /* if oLGX == NULL, assume oLGX=1/numDetectors for all X ==> oLG = sumX oLGX = 1*/ oLG = 1.0; for (UINT4 X = 0; X < numDetectors; X++) { oLGXREAL8->data[X] = 1.0/numDetectors; /* need to set this manually, as old functions still assume oLGX=1 instead */ } } // if ( oLGX == NULL ) /* further parameter pre-conversions for XLALComputeLineVetoArray() */ REAL8 logRhoTerm = Fstar0; REAL8 logoLGX[numDetectors]; for (UINT4 X = 0; X < numDetectors; X++) { logoLGX[X] = log(oLGXREAL8->data[X]); } /* compute BSGL "the pedestrian way", from Eq. (40) of Keitel, Prix, Papa, Leaci, Siddiqi, PR D 89, 064023 (2014), * explicit formula for numDet=2: * log10 BSGL = F - log ( e^F* + e^{F1}*oLG1/oLG + e^{F2}*oLG2/oLG ) */ REAL8 BSGL_extcomp_terms[3]; BSGL_extcomp_terms[0] = exp(Fstar0)/oLG; REAL8 BSGL_extcomp_maxterm = BSGL_extcomp_terms[0]; for (UINT4 X = 0; X < numDetectors; X++) { BSGL_extcomp_terms[1+X] = exp(0.5*TwoFX->data[X]); if ( oLGX ) { BSGL_extcomp_terms[1+X] *= oLGX[X]/oLG; } else { /* oLGX=NULL is interpreted as oLGX[X]=1/numDetectors=0.5 for all X ==> oLG=1 */ BSGL_extcomp_terms[1+X] *= 0.5/oLG; } if ( BSGL_extcomp_terms[1+X] > BSGL_extcomp_maxterm ) { BSGL_extcomp_maxterm = BSGL_extcomp_terms[1+X]; } } REAL4 log10BSGL_extcomp_notallterms = 0.5*TwoF - log(BSGL_extcomp_maxterm); REAL8 BSGL_extcomp_denom = 0.0; for (UINT4 X = 0; X < 1+numDetectors; X++) { BSGL_extcomp_denom += BSGL_extcomp_terms[X]; } REAL4 log10BSGL_extcomp_allterms = 0.5*TwoF - log( BSGL_extcomp_denom ); /* these are not the log-Bayes-factor, as computed by XLALComputeBSGL(), so need to correct by log(1+1/oLG) */ log10BSGL_extcomp_allterms += log(1+1/oLG); log10BSGL_extcomp_notallterms += log(1+1/oLG); /* and actually switch to log10 */ log10BSGL_extcomp_allterms *= LAL_LOG10E; log10BSGL_extcomp_notallterms *= LAL_LOG10E; /* faster version: use only the leading term of the BSGL denominator sum */ BSGLSetup *setup_noLogCorrection; XLAL_CHECK ( (setup_noLogCorrection = XLALCreateBSGLSetup ( numDetectors, Fstar0, oLGX, FALSE )) != NULL, XLAL_EFUNC ); REAL4 log10BSGL_XLAL_notallterms = XLALComputeBSGL ( TwoF, TwoFX->data, setup_noLogCorrection ); XLAL_CHECK ( xlalErrno == 0, XLAL_EFUNC, "XLALComputeBSGL() failed with xlalErrno = %d\n", xlalErrno ); XLALFree ( setup_noLogCorrection ); setup_noLogCorrection = NULL; /* more precise version: use all terms of the BSGL denominator sum */ BSGLSetup *setup_withLogCorrection; XLAL_CHECK ( (setup_withLogCorrection = XLALCreateBSGLSetup ( numDetectors, Fstar0, oLGX, TRUE )) != NULL, XLAL_EFUNC ); REAL4 log10BSGL_XLAL_allterms = XLALComputeBSGL ( TwoF, TwoFX->data, setup_withLogCorrection ); XLAL_CHECK ( xlalErrno == 0, XLAL_EFUNC, "XLALComputeBSGL() failed with xlalErrno = %d\n", xlalErrno ); XLALFree ( setup_withLogCorrection ); setup_withLogCorrection = NULL; /* compute relative deviations */ REAL4 diff_allterms = fabs( log10BSGL_XLAL_allterms - log10BSGL_extcomp_allterms ) / ( 0.5 * ( log10BSGL_XLAL_allterms + log10BSGL_extcomp_allterms )); REAL4 diff_notallterms = fabs( log10BSGL_XLAL_notallterms - log10BSGL_extcomp_notallterms ) / ( 0.5 * ( log10BSGL_XLAL_notallterms + log10BSGL_extcomp_notallterms )); /* output results and deviations and return with error when tolerances are violated */ printf ( "Externally recomputed with allterms: log10BSGL=%f\n", log10BSGL_extcomp_allterms ); printf ( "Externally recomputed with !allterms: log10BSGL=%f\n", log10BSGL_extcomp_notallterms ); printf ( "XLALComputeBSGL() with allterms: log10BSGL=%f (rel. dev.: %f)", log10BSGL_XLAL_allterms, diff_allterms ); XLAL_CHECK ( XLALCheckBSGLDifferences ( diff_allterms, tolerance, "XLALComputeBSGL() with useAllTerms=TRUE" ) == XLAL_SUCCESS, XLAL_EFUNC ); printf ( "XLALComputeBSGL() with !allterms: log10BSGL=%f (rel. dev.: %f)", log10BSGL_XLAL_notallterms, diff_notallterms ); XLAL_CHECK ( XLALCheckBSGLDifferences ( diff_notallterms, tolerance, "XLALComputeBSGL() with useAllTerms=TRUE" ) == XLAL_SUCCESS, XLAL_EFUNC ); /* also test against deprecated rho-notation functions for consistency * need to correct for different prior parametrization, * log(BSGL_new)=LV_old+log(1+oLG) */ REAL4 old_LV_corr = log(1+oLG); xlalErrno = 0; REAL4 log10BSGL_XLAL_rho_notallterms = XLALComputeLineVeto ( TwoF, TwoFX, LVrho, oLGXREAL8, FALSE ); XLAL_CHECK ( xlalErrno == 0, XLAL_EFUNC, "XLALComputeLineVeto() failed with xlalErrno = %d\n", xlalErrno ); log10BSGL_XLAL_rho_notallterms += old_LV_corr; log10BSGL_XLAL_rho_notallterms *= LAL_LOG10E; xlalErrno = 0; REAL4 log10BSGL_XLAL_rhoarray_notallterms = XLALComputeLineVetoArray ( TwoF, numDetectors, TwoFX->data, logRhoTerm, logoLGX, FALSE ); XLAL_CHECK ( xlalErrno == 0, XLAL_EFUNC, "XLALComputeLineVetoArray() failed with xlalErrno = %d\n", xlalErrno ); log10BSGL_XLAL_rhoarray_notallterms += old_LV_corr; log10BSGL_XLAL_rhoarray_notallterms *= LAL_LOG10E; xlalErrno = 0; REAL4 log10BSGL_XLAL_rho_allterms = XLALComputeLineVeto ( TwoF, TwoFX, LVrho, oLGXREAL8, TRUE ); XLAL_CHECK ( xlalErrno == 0, XLAL_EFUNC, "XLALComputeLineVeto() failed with xlalErrno = %d\n", xlalErrno ); log10BSGL_XLAL_rho_allterms += old_LV_corr; log10BSGL_XLAL_rho_allterms *= LAL_LOG10E; xlalErrno = 0; REAL4 log10BSGL_XLAL_rhoarray_allterms = XLALComputeLineVetoArray ( TwoF, numDetectors, TwoFX->data, logRhoTerm, logoLGX, TRUE ); XLAL_CHECK ( xlalErrno == 0, XLAL_EFUNC, "XLALComputeLineVetoArray() failed with xlalErrno = %d\n", xlalErrno ); log10BSGL_XLAL_rhoarray_allterms += old_LV_corr; log10BSGL_XLAL_rhoarray_allterms *= LAL_LOG10E; REAL4 diff_rho_allterms = fabs( log10BSGL_XLAL_rho_allterms - log10BSGL_extcomp_allterms ) / ( 0.5 * ( log10BSGL_XLAL_rho_allterms + log10BSGL_extcomp_allterms )); REAL4 diff_rhoarray_allterms = fabs( log10BSGL_XLAL_rhoarray_allterms - log10BSGL_extcomp_allterms ) / ( 0.5 * ( log10BSGL_XLAL_rhoarray_allterms + log10BSGL_extcomp_allterms )); REAL4 diff_rho_notallterms = fabs( log10BSGL_XLAL_rho_notallterms - log10BSGL_extcomp_notallterms ) / ( 0.5 * ( log10BSGL_XLAL_rho_notallterms + log10BSGL_extcomp_notallterms )); REAL4 diff_rhoarray_notallterms = fabs( log10BSGL_XLAL_rhoarray_notallterms - log10BSGL_extcomp_notallterms ) / ( 0.5 * ( log10BSGL_XLAL_rhoarray_notallterms + log10BSGL_extcomp_notallterms )); printf( "Legacy functions with rho-notation, corrected as BSGL_new=LV_old+log(1+oLG)=LV_old+%f:\n", old_LV_corr ); printf ( "XLALComputeLineVeto() with allterms: BSGL=%f (rel. dev.: %f)", log10BSGL_XLAL_rho_allterms, diff_rho_allterms ); XLAL_CHECK ( XLALCheckBSGLDifferences ( diff_rho_allterms, tolerance, "XLALComputeLineVeto() with useAllTerms=TRUE" ) == XLAL_SUCCESS, XLAL_EFUNC ); printf ( "XLALComputeLineVetoArray() with allterms: BSGL=%f (rel. dev.: %f)", log10BSGL_XLAL_rhoarray_allterms, diff_rhoarray_allterms ); XLAL_CHECK ( XLALCheckBSGLDifferences ( diff_rhoarray_allterms, tolerance, "XLALComputeLineVetoArray() with useAllTerms=TRUE" ) == XLAL_SUCCESS, XLAL_EFUNC ); printf ( "XLALComputeLineVeto() with !allterms: BSGL=%f (rel. dev.: %f)", log10BSGL_XLAL_rho_notallterms, diff_rho_notallterms ); XLAL_CHECK ( XLALCheckBSGLDifferences ( diff_rho_notallterms, tolerance, "XLALComputeLineVeto() with useAllTerms=FALSE" ) == XLAL_SUCCESS, XLAL_EFUNC ); printf ( "XLALComputeLineVetoArray() with !allterms: BSGL=%f (rel. dev.: %f)", log10BSGL_XLAL_rhoarray_notallterms, diff_rhoarray_notallterms ); XLAL_CHECK ( XLALCheckBSGLDifferences ( diff_rhoarray_notallterms, tolerance, "XLALComputeLineVetoArray() with useAllTerms=FALSE" ) == XLAL_SUCCESS, XLAL_EFUNC ); XLALDestroyREAL8Vector(oLGXREAL8); return XLAL_SUCCESS; } /* XLALCompareBSGLComputations() */
/** * Test function to compute LV-stat values both from scratch and by XLALComputeLineVeto, * compare the results and exit if tolerance is violated. */ int XLALCompareLVComputations ( const REAL4 TwoF, /**< multi-detector Fstat */ const REAL4Vector *TwoFX, /**< vector of single-detector Fstats */ const REAL8 rhomaxline, /**< amplitude prior normalization for lines */ const REAL8Vector *lX, /**< vector of single-detector prior line odds ratio, default to lX=1 for all X if NULL */ const REAL4 tolerance_allterms, /**< tolerance for useAllTerms=TRUE */ const REAL4 tolerance_leadterm /**< tolerance for useAllTerms=FALSE (usually higher) */ ) { /* compute LV-stat "the pedestrian way", explicit formula for numDet=2 */ REAL4 LV_extcomp = 0.5*TwoF; if ( lX ) LV_extcomp -= log( pow(rhomaxline,4)/70.0 + lX->data[0]*exp(0.5*TwoFX->data[0]) + lX->data[1]*exp(0.5*TwoFX->data[1]) ); else /* lX=NULL is interpreted as l[X]=1 for all X */ LV_extcomp -= log( pow(rhomaxline,4)/70.0 + exp(0.5*TwoFX->data[0]) + exp(0.5*TwoFX->data[1]) ); /* faster version: use only the leading term of the LV denominator sum */ BOOLEAN useAllTerms = FALSE; xlalErrno = 0; REAL4 LV_XLAL_leadterm = XLALComputeLineVeto ( TwoF, TwoFX, rhomaxline, lX, useAllTerms ); if ( xlalErrno != 0 ) { XLAL_ERROR ( XLAL_EFUNC, "XLALComputeLineVeto() failed with xlalErrno = %d\n", xlalErrno ); return XLAL_FAILURE; } /* more precise version: use all terms of the LV denominator sum */ useAllTerms = TRUE; xlalErrno = 0; REAL4 LV_XLAL_allterms = XLALComputeLineVeto ( TwoF, TwoFX, rhomaxline, lX, useAllTerms ); if ( xlalErrno != 0 ) { XLAL_ERROR ( XLAL_EFUNC, "XLALComputeLineVeto() failed with xlalErrno = %d\n", xlalErrno ); return XLAL_FAILURE; } /* compute relative deviations */ REAL4 diff_allterms = fabs( LV_XLAL_allterms - LV_extcomp ) / ( 0.5 * ( LV_XLAL_allterms + LV_extcomp )); REAL4 diff_leadterm = fabs( LV_XLAL_leadterm - LV_extcomp ) / ( 0.5 * ( LV_XLAL_leadterm + LV_extcomp )); /* output results and deviations and return with error when tolerances are violated */ printf ("Externally recomputed : LV=%f\n", LV_extcomp); printf ("XLALComputeLineVeto with allterms : LV=%f (rel. dev.: %f)", LV_XLAL_allterms, diff_allterms); if ( fabs(diff_allterms) <= tolerance_allterms ) printf (" ==> OK!\n"); else { printf (" ==> BAD!\n"); XLAL_ERROR ( XLAL_EFAILED, "\nTolerance %f exceeded for useAllTerms=TRUE!\n", tolerance_allterms ); return XLAL_FAILURE; } printf ("XLALComputeLineVeto with !allterms: LV=%f (rel. dev.: %f)", LV_XLAL_leadterm, diff_leadterm); if ( fabs(diff_leadterm) <= tolerance_leadterm ) printf (" ==> OK!\n"); else { printf (" ==> BAD!\n"); XLAL_ERROR ( XLAL_EFAILED, "\nTolerance %f exceeded for useAllTerms=FALSE!\n", tolerance_leadterm ); return XLAL_FAILURE; } return XLAL_SUCCESS; } /* XLALStringVector_TEST() */