Loading [MathJax]/extensions/TeX/AMSsymbols.js
LALPulsar 7.1.1.1-b246709
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
LineRobustStatsTest.c
Go to the documentation of this file.
1/*
2 * Copyright (C) 2011, 2014 David Keitel
3 * Copyright (C) 2017 Reinhard Prix
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with with program; see the file COPYING. If not, write to the
17 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
18 * MA 02110-1301 USA
19 */
20
21/* ---------- Includes -------------------- */
22#include <lal/AVFactories.h>
23#include <lal/LineRobustStats.h>
24#include <lal/LogPrintf.h>
25
26/******************************************************
27 * Error codes and messages.
28 */
29
30/* ---------- Defines -------------------- */
31#define TRUE (1==1)
32#define FALSE (1==0)
33
34/**
35 * Enum for LR-statistic variants
36 */
37typedef enum {
41 LRS_BStSGLtL = 3
43
44
45/*---------- internal prototypes ----------*/
46int
48 const UINT4 numDetectors,
49 const REAL4Vector *TwoFX,
50 const REAL4 cohFstar0,
51 const REAL4 *oLtLGX,
52 const UINT4 numSegs,
53 const REAL4 tolerance,
54 const LRstat_variant_t LRstat_variant,
55 const REAL4 maxTwoF,
56 const REAL4Vector *maxTwoFX
57 );
58
61 const UINT4 numDetectors,
62 const REAL4Vector *TwoFX,
63 const REAL4 cohFstar0,
64 const REAL4 *oLtLGX,
65 const BOOLEAN useLogCorrection,
66 const LRstat_variant_t LRstat_variant,
67 const REAL4 maxTwoF,
68 const REAL4Vector *maxTwoFX,
69 const UINT4 numSegs
70 );
71
72int
74 const REAL4 tolerance,
75 const CHAR *casestring
76 );
77
78int
80
81/* ################################### MAIN ################################### */
82
83int main( int argc, char *argv[] )
84{
85
86 /* sanity check for input arguments */
87 XLAL_CHECK( argc == 1, XLAL_EINVAL, "The executable '%s' doesn't support any input arguments right now.\n", argv[0] );
88
89 printf( "Starting coherent test...\n" );
90 UINT4 numSegs = 1;
91
92 /* set up single- and multi-IFO F-stat input */
93 REAL4 TwoF = 7.0;
95 REAL4Vector *TwoFX = NULL;
97 TwoFX->data[0] = 4.0;
98 TwoFX->data[1] = 12.0;
99 /* maximum allowed difference between recalculated and XLAL result */
100 REAL4 tolerance = 1e-06;
101
102 /* compute and compare the results for one set of Fstar0, oLtLGX values */
103 REAL4 cohFstar0 = -LAL_REAL4_MAX; /* prior from BSGL derivation, -Inf means pure line veto, +Inf means pure multi-Fstat */
104 REAL4 oLtLGXarray[numDetectors]; /* per-IFO prior odds ratio for line vs. Gaussian noise */
105 oLtLGXarray[0] = 0.5; /* pick some arbitrary values first */
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 );
110
111 /* change the priors to catch more possible problems */
112 cohFstar0 = 10.0;
113 oLtLGX = NULL; /* NULL is interpreted as oLtLGX[X]=1 for all X (in most general case includes both L and tL) */
114
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 );
117
118 XLALDestroyREAL4Vector( TwoFX );
119
120 printf( "Starting semi-coherent test...\n" );
121 numSegs = 3;
122
123 /* initialize some per-segment F-stats */
124 REAL4Vector *TwoFl = NULL;
125 REAL4Vector *TwoFXl = NULL;
126 XLAL_CHECK( ( TwoFl = XLALCreateREAL4Vector( numSegs ) ) != NULL, XLAL_EFUNC );
127 XLAL_CHECK( ( TwoFXl = XLALCreateREAL4Vector( numSegs * numDetectors ) ) != NULL, XLAL_EFUNC );
128 TwoFl->data[0] = 4.0;
129 TwoFl->data[1] = 8.0;
130 TwoFl->data[2] = 4.0;
131 for ( UINT4 X = 0; X < numDetectors; X++ ) {
132 for ( UINT4 l = 0; l < numSegs; l++ ) {
133 TwoFXl->data[X * numSegs + l] = TwoFl->data[l] / sqrt( numDetectors );
134 }
135 }
136 TwoFXl->data[0 + 1] = 32.0;
137
138 REAL4 sumTwoF = 0.0;
139 REAL4Vector *sumTwoFX = NULL;
140 XLAL_CHECK( ( sumTwoFX = XLALCreateREAL4Vector( numDetectors ) ) != NULL, XLAL_EFUNC );
141 for ( UINT4 X = 0; X < numDetectors; X++ ) {
142 sumTwoFX->data[X] = 0;
143 }
144 for ( UINT4 l = 0; l < numSegs; l++ ) {
145 sumTwoF += TwoFl->data[l];
146 for ( UINT4 X = 0; X < numDetectors; X++ ) {
147 sumTwoFX->data[X] += TwoFXl->data[X * numSegs + l];
148 }
149 }
150
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 );
153
154 REAL4 maxTwoF = 0.0;
155 REAL4Vector *maxTwoFX = NULL;
156 XLAL_CHECK( ( maxTwoFX = XLALCreateREAL4Vector( numDetectors ) ) != NULL, XLAL_EFUNC );
157 for ( UINT4 X = 0; X < numDetectors; X++ ) {
158 maxTwoFX->data[X] = 0;
159 }
160 for ( UINT4 l = 0; l < numSegs; l++ ) {
161 maxTwoF = fmax( maxTwoF, TwoFl->data[l] );
162 for ( UINT4 X = 0; X < numDetectors; X++ ) {
163 maxTwoFX->data[X] = fmax( maxTwoFX->data[X], TwoFXl->data[X * numSegs + l] );
164 }
165 }
166
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 );
169
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 );
172
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 );
175
176 /* free memory */
177 XLALDestroyREAL4Vector( TwoFl );
178 XLALDestroyREAL4Vector( TwoFXl );
179 XLALDestroyREAL4Vector( sumTwoFX );
180 XLALDestroyREAL4Vector( maxTwoFX );
181
182
183 // check vector versions of B*SGL* function
185
187
188 return XLAL_SUCCESS;
189
190} /* main */
191
192
193/**
194 * Test function to compute BSGL values from XLALComputeBSGL
195 * against those recomputed from scratch ("pedestrian" formula);
196 * compare the results and exit if tolerance is violated.
197 */
198int
199XLALCompareBSGLComputations( const REAL4 TwoF, /**< multi-detector Fstat */
200 const UINT4 numDetectors, /**< number of detectors */
201 const REAL4Vector *TwoFX, /**< vector of single-detector Fstats */
202 const REAL4 cohFstar0, /**< amplitude prior normalization for lines */
203 const REAL4 *oLtLGX, /**< array of single-detector prior line odds ratio, can be NULL */
204 const UINT4 numSegs, /**< number of segments */
205 const REAL4 tolerance, /**< tolerance for comparisons */
206 const LRstat_variant_t LRstat_variant, /**< which statistic variant to use */
207 const REAL4 maxTwoF, /**< multi-detector maximum Fstat over segments*/
208 const REAL4Vector *maxTwoFX /**< vector of single-detector maximum Fstats over segments*/
209 )
210{
211
212 /* pedestrian version, with and without log corrections */
213 REAL4 log10LRS_extcomp_notallterms = XLALComputePedestrianLRStat( TwoF, numDetectors, TwoFX, cohFstar0, oLtLGX, FALSE, LRstat_variant, maxTwoF, maxTwoFX, numSegs );
214 REAL4 log10LRS_extcomp_allterms = XLALComputePedestrianLRStat( TwoF, numDetectors, TwoFX, cohFstar0, oLtLGX, TRUE, LRstat_variant, maxTwoF, maxTwoFX, numSegs );
215
216 /* faster version: use only the leading term of the BSGL denominator sum */
217 BSGLSetup *setup_noLogCorrection;
218 XLAL_CHECK( ( setup_noLogCorrection = XLALCreateBSGLSetup( numDetectors, numSegs * cohFstar0, oLtLGX, FALSE, numSegs ) ) != NULL, XLAL_EFUNC );
219 REAL4 log10_LRS_XLAL_notallterms = 0.0;
220 char funcname[64] = "";
221 switch ( LRstat_variant ) {
222 case LRS_BSGL :
223 log10_LRS_XLAL_notallterms = XLALComputeBSGL( TwoF, TwoFX->data, setup_noLogCorrection );
224 snprintf( funcname, sizeof( funcname ), "%s", "XLALComputeBSGL" );
225 break;
226 case LRS_BSGLtL :
227 log10_LRS_XLAL_notallterms = XLALComputeBSGLtL( TwoF, TwoFX->data, maxTwoFX->data, setup_noLogCorrection );
228 snprintf( funcname, sizeof( funcname ), "%s", "XLALComputeBSGLtL" );
229 break;
230 case LRS_BtSGLtL :
231 log10_LRS_XLAL_notallterms = XLALComputeBtSGLtL( maxTwoF, TwoFX->data, maxTwoFX->data, setup_noLogCorrection );
232 snprintf( funcname, sizeof( funcname ), "%s", "XLALComputeBtSGLtL" );
233 break;
234 case LRS_BStSGLtL :
235 log10_LRS_XLAL_notallterms = XLALComputeBStSGLtL( TwoF, maxTwoF, TwoFX->data, maxTwoFX->data, setup_noLogCorrection );
236 snprintf( funcname, sizeof( funcname ), "%s", "XLALComputeBStSGLtL" );
237 break;
238 }
239 XLAL_CHECK( xlalErrno == 0, XLAL_EFUNC, "XLALComputeBSGL() failed with xlalErrno = %d\n", xlalErrno );
240 XLALDestroyBSGLSetup( setup_noLogCorrection );
241 setup_noLogCorrection = NULL;
242
243 /* more precise version: use all terms of the BSGL denominator sum */
244 BSGLSetup *setup_withLogCorrection;
245 XLAL_CHECK( ( setup_withLogCorrection = XLALCreateBSGLSetup( numDetectors, numSegs * cohFstar0, oLtLGX, TRUE, numSegs ) ) != NULL, XLAL_EFUNC );
246 REAL4 log10_LRS_XLAL_allterms = 0.0;
247 switch ( LRstat_variant ) {
248 case LRS_BSGL :
249 log10_LRS_XLAL_allterms = XLALComputeBSGL( TwoF, TwoFX->data, setup_withLogCorrection );
250 break;
251// case LRS_BSGLtL :
252// log10_LRS_XLAL_allterms = XLALComputeBSGLtL ( TwoF, TwoFX->data, maxTwoFX->data, setup_withLogCorrection );
253// break;
254// case LRS_BtSGLtL :
255// log10_LRS_XLAL_allterms = XLALComputeBtSGLtL ( maxTwoF, TwoFX->data, maxTwoFX->data, setup_withLogCorrection );
256// break;
257// case LRS_BStSGLtL :
258// log10_LRS_XLAL_allterms = XLALComputeBStSGLtL ( TwoF, maxTwoF, TwoFX->data, maxTwoFX->data, setup_withLogCorrection );
259// break;
260 default :
261 printf( "log correction not implemented for GLtL denominator, skipping full-precision test for this LRS variant.\n" );
262 }
263 XLAL_CHECK( xlalErrno == 0, XLAL_EFUNC, "%s() failed with xlalErrno = %d\n", funcname, xlalErrno );
264 XLALDestroyBSGLSetup( setup_withLogCorrection );
265 setup_withLogCorrection = NULL;
266
267 /* compute relative deviations */
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 ) );
271 }
272 REAL4 diff_notallterms = fabs( log10_LRS_XLAL_notallterms - log10LRS_extcomp_notallterms ) / ( 0.5 * ( log10_LRS_XLAL_notallterms + log10LRS_extcomp_notallterms ) );
273
274 /* output results and deviations and return with error when tolerances are violated */
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 );
281 XLAL_CHECK( XLALCheckBSGLDifferences( diff_allterms, tolerance, casestring ) == XLAL_SUCCESS, XLAL_EFUNC );
282 }
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 );
285 XLAL_CHECK( XLALCheckBSGLDifferences( diff_notallterms, tolerance, casestring ) == XLAL_SUCCESS, XLAL_EFUNC );
286
287 return XLAL_SUCCESS;
288
289} /* XLALCompareBSGLComputations() */
290
291/**
292 * compute BSGL "the pedestrian way", from Eq. (40) of Keitel, Prix, Papa, Leaci, Siddiqi, PR D 89, 064023 (2014),
293 * explicit formula for numDet=2:
294 * log10 BSGL = F - log ( e^F* + e^{F1}*oLtLG1/oLtLG + e^{F2}*oLtLG2/oLtLG )
295 */
296REAL4
297XLALComputePedestrianLRStat( const REAL4 TwoF, /**< multi-detector Fstat */
298 const UINT4 numDetectors, /**< number of detectors */
299 const REAL4Vector *TwoFX, /**< vector of single-detector Fstats */
300 const REAL4 cohFstar0, /**< amplitude prior normalization for lines */
301 const REAL4 *oLtLGX, /**< array of single-detector prior line odds ratio, can be NULL (in most general case includes both L and tL) */
302 const BOOLEAN useLogCorrection, /**< include log-term correction or not */
303 const LRstat_variant_t LRstat_variant, /**< which statistic variant to compute */
304 const REAL4 maxTwoF, /**< multi-detector maximum Fstat over segments*/
305 const REAL4Vector *maxTwoFX, /**< vector of single-detector maximum Fstats over segments*/
306 const UINT4 numSegs /**< number of segments */
307 )
308{
309
310 REAL4 log10LRS_extcomp = 0;
311
312 /* sum up overall prior odds */
313 REAL4 oLtLGXarray[numDetectors];
314 REAL4 oLtLG = 0.0;
315 if ( oLtLGX ) {
316 for ( UINT4 X = 0; X < numDetectors; X++ ) {
317 oLtLG += oLtLGX[X];
318 }
319 } else { /* if oLtLGX == NULL, assume oLtLGX=1/numDetectors for all X ==> oLtLG = sumX oLtLGX = 1*/
320 oLtLG = 1.0;
321 for ( UINT4 X = 0; X < numDetectors; X++ ) {
322 oLtLGXarray[X] = 1.0 / numDetectors;
323 }
324 oLtLGX = oLtLGXarray;
325 } // if ( oLtLGX == NULL )
326
327 REAL4 numerator = 0.0;
328 if ( LRstat_variant != LRS_BtSGLtL ) {
329 numerator += exp( 0.5 * TwoF );
330 }
331 if ( ( LRstat_variant == LRS_BtSGLtL ) || ( LRstat_variant == LRS_BStSGLtL ) ) {
332 numerator += exp( 0.5 * maxTwoF + ( numSegs - 1 ) * cohFstar0 ) / numSegs; /* extra 1/numSegs frome qual splitting of prior odds over segments */
333 }
334
335 if ( LRstat_variant == LRS_BStSGLtL ) {
336 numerator *= 0.5; /* assume equal splitting of prior odds between S and tS */
337 }
338
339 log10LRS_extcomp = log( numerator );
340
341 REAL4 *denomterms = NULL;
342 REAL4 denomterms_GL[1 + numDetectors];
343 REAL4 denomterms_GLtL[1 + 2 * numDetectors];
344 if ( LRstat_variant == LRS_BSGL ) {
345 denomterms = denomterms_GL;
346 } else {
347 denomterms = denomterms_GLtL;
348 }
349
350 denomterms[0] = exp( numSegs * cohFstar0 ) / oLtLG;
351 REAL4 maxterm = denomterms[0];
352 for ( UINT4 X = 0; X < numDetectors; X++ ) {
353 denomterms[1 + X] = oLtLGX[X] * exp( 0.5 * TwoFX->data[X] ) / oLtLG;
354 if ( LRstat_variant != LRS_BSGL ) {
355 denomterms[1 + X] *= 0.5; /* assume equal splitting of prior odds between L and tL */
356 }
357 maxterm = fmax( maxterm, denomterms[1 + X] );
358 }
359 if ( LRstat_variant != LRS_BSGL ) { /* NOTE: this is the loudest-only (per detector) version only! */
360 for ( UINT4 X = 0; X < numDetectors; X++ ) {
361 denomterms[1 + numDetectors + X] = 0.5 * oLtLGX[X] * exp( 0.5 * maxTwoFX->data[X] + ( numSegs - 1 ) * cohFstar0 ) / ( oLtLG * numSegs ); /* 0.5 from assuming equal splitting of prior odds between L and tL and 1/numSegs from equal splitting over segments */
362 maxterm = fmax( maxterm, denomterms[1 + numDetectors + X] );
363 }
364 }
365
366 if ( !useLogCorrection ) {
367 log10LRS_extcomp -= log( maxterm );
368 } else {
369 REAL4 BSGL_extcomp_denom = 0.0;
370 for ( UINT4 X = 0; X < 1 + numDetectors; X++ ) {
371 BSGL_extcomp_denom += denomterms[X];
372 }
373 log10LRS_extcomp -= log( BSGL_extcomp_denom );
374 }
375
376 /* this is not yet the log-Bayes-factor, as computed by XLALComputeBSGL(), so need to correct by log(1+1/oLtLG) */
377 log10LRS_extcomp += log( 1 + 1 / oLtLG );
378 /* and actually switch to log10 */
379 log10LRS_extcomp *= LAL_LOG10E;
380
381 return log10LRS_extcomp;
382
383} /* XLALComputePedestrianLRStat() */
384
385int
387 const REAL4 tolerance,
388 const CHAR *casestring
389 )
390{
391
392 if ( fabs( diff ) <= tolerance ) {
393 printf( " ==> OK!\n" );
394 } else {
395 printf( " ==> BAD!\n" );
396 XLAL_ERROR( XLAL_EFAILED, "\nTolerance %f exceeded for %s!\n", tolerance, casestring );
397 return XLAL_FAILURE;
398 }
399
400 return XLAL_SUCCESS;
401
402} /* XLALCheckBSGLDifferences() */
403
404int
406{
407#define VEC_LEN 5
408#define NUM_SEGS 3
409
410 REAL4 cohFstar0 = 4.37; // 10% false-alarm: invFalseAlarm_chi2 ( 0.1, 4 * 3 ) / 3
411 UINT4 numDet = 3;
412 REAL4 oLtLGX[PULSAR_MAX_DETECTORS] = {0.1, 0.8, 1.5}; /* per-IFO prior odds ratio for line vs. Gaussian noise */
413
414 BSGLSetup *setup_noLogCorr;
415 BSGLSetup *setup_withLogCorr;
416 XLAL_CHECK( ( setup_noLogCorr = XLALCreateBSGLSetup( numDet, NUM_SEGS * cohFstar0, oLtLGX, FALSE, NUM_SEGS ) ) != NULL, XLAL_EFUNC );
417 XLAL_CHECK( ( setup_withLogCorr = XLALCreateBSGLSetup( numDet, NUM_SEGS * cohFstar0, oLtLGX, TRUE, NUM_SEGS ) ) != NULL, XLAL_EFUNC );
418
419 // generate some 'reasonable' fake Fstat numbers
420 // trying to span some range of 'favoring noise' to 'favoring signal'
421 REAL4 twoFPerDet0[PULSAR_MAX_DETECTORS][VEC_LEN] = { // printf ( "{%f, %f, %f, %f, %f},\n", 3 * unifrnd ( 4, 8, [3,5] ) )
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},
425 {0, 0, 0, 0, 0},
426 {0, 0, 0, 0, 0},
427 {0, 0, 0, 0, 0},
428 {0, 0, 0, 0, 0},
429 {0, 0, 0, 0, 0},
430 {0, 0, 0, 0, 0},
431 {0, 0, 0, 0, 0}
432 };
433 REAL4 twoF[VEC_LEN];
434 XLAL_INIT_MEM( twoF );
435 REAL4 attenuate[VEC_LEN] = { 0.1, 0.3, 0.5, 0.7, 0.9 };
436 for ( UINT4 i = 0; i < VEC_LEN; i ++ ) {
437 for ( UINT4 X = 0; X < PULSAR_MAX_DETECTORS; X ++ ) {
438 twoF[i] += twoFPerDet0[X][i];
439 }
440 twoF[i] *= attenuate[i];
441 }
442
443 const REAL4 *twoFPerDet[PULSAR_MAX_DETECTORS];
444 for ( UINT4 X = 0; X < PULSAR_MAX_DETECTORS; X ++ ) {
445 twoFPerDet[X] = twoFPerDet0[X];
446 }
447
448 REAL4 maxTwoFSeg[VEC_LEN] = {4.585174, 13.221874, 13.005110, 34.682062, 48.968980}; // maxTwoFSeg = unifrnd ( twoF ./ 3, twoF ); printf ( "{%f, %f, %f, %f, %f};\n", maxTwoFSeg );
449 REAL4 maxTwoFSegPerDet0[PULSAR_MAX_DETECTORS][VEC_LEN] = { // maxTwoFSegPerDet = unifrnd ( twoFPerDet ./ 3, twoFPerDet ); printf ( "{%f, %f, %f, %f, %f},\n", maxTwoFSegPerDet );
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},
453 {0, 0, 0, 0, 0},
454 {0, 0, 0, 0, 0},
455 {0, 0, 0, 0, 0},
456 {0, 0, 0, 0, 0},
457 {0, 0, 0, 0, 0},
458 {0, 0, 0, 0, 0},
459 {0, 0, 0, 0, 0}
460 };
461 const REAL4 *maxTwoFSegPerDet[PULSAR_MAX_DETECTORS];
462 for ( UINT4 X = 0; X < PULSAR_MAX_DETECTORS; X ++ ) {
463 maxTwoFSegPerDet[X] = maxTwoFSegPerDet0[X];
464 }
465
466 REAL4 outBSGL_log[VEC_LEN];
467 REAL4 outBSGL_nolog[VEC_LEN];
468 REAL4 outBSGLtL[VEC_LEN];
469 REAL4 outBtSGLtL[VEC_LEN];
470
471 XLAL_CHECK( XLALVectorComputeBSGL( outBSGL_log, twoF, twoFPerDet, VEC_LEN, setup_withLogCorr ) == XLAL_SUCCESS, XLAL_EFUNC );
472 XLAL_CHECK( XLALVectorComputeBSGL( outBSGL_nolog, twoF, twoFPerDet, VEC_LEN, setup_noLogCorr ) == XLAL_SUCCESS, XLAL_EFUNC );
473
474 XLAL_CHECK( XLALVectorComputeBSGLtL( outBSGLtL, twoF, twoFPerDet, maxTwoFSegPerDet, VEC_LEN, setup_noLogCorr ) == XLAL_SUCCESS, XLAL_EFUNC );
475 XLAL_CHECK( XLALVectorComputeBtSGLtL( outBtSGLtL, maxTwoFSeg, twoFPerDet, maxTwoFSegPerDet, VEC_LEN, setup_noLogCorr ) == XLAL_SUCCESS, XLAL_EFUNC );
476
477 printf( "\nBSGL_log = {" );
478 for ( UINT4 i = 0; i < VEC_LEN; i ++ ) {
479 printf( "%f%s ", outBSGL_log[i], ( i < VEC_LEN - 1 ) ? "," : "}" );
480 }
481 printf( "\n" );
482 printf( "\nBSGL_nolog = {" );
483 for ( UINT4 i = 0; i < VEC_LEN; i ++ ) {
484 printf( "%f%s ", outBSGL_nolog[i], ( i < VEC_LEN - 1 ) ? "," : "}" );
485 }
486 printf( "\n" );
487 printf( "\nBSGLtL = {" );
488 for ( UINT4 i = 0; i < VEC_LEN; i ++ ) {
489 printf( "%f%s ", outBSGLtL[i], ( i < VEC_LEN - 1 ) ? "," : "}" );
490 }
491 printf( "\n" );
492 printf( "\nBtSGLtL = {" );
493 for ( UINT4 i = 0; i < VEC_LEN; i ++ ) {
494 printf( "%f%s ", outBtSGLtL[i], ( i < VEC_LEN - 1 ) ? "," : "}" );
495 }
496 printf( "\n" );
497
498 // compute these alternatively with the single-value function versions
499 REAL4 errBSGL_log = 0;
500 REAL4 errBSGL_nolog = 0;
501 REAL4 errBSGLtL = 0;
502 REAL4 errBtSGLtL = 0;
503
504 for ( UINT4 i = 0; i < VEC_LEN; i ++ ) {
506 REAL4 maxTwoFXl[PULSAR_MAX_DETECTORS];
507 for ( UINT4 X = 0; X < PULSAR_MAX_DETECTORS; X ++ ) {
508 twoFX[X] = twoFPerDet[X][i];
509 maxTwoFXl[X] = maxTwoFSegPerDet[X][i];
510 }
511 REAL4 cmp;
512 // BSGL_log
513 cmp = XLALComputeBSGL( twoF[i], twoFX, setup_withLogCorr );
514 XLAL_CHECK( xlalErrno == XLAL_SUCCESS, XLAL_EFUNC, "XLALComputeBSGL() failed.\n" );
515 errBSGL_log = fmaxf( errBSGL_log, fabsf( cmp - outBSGL_log[i] ) );
516 printf( "i = %d: err = %g\n", i, errBSGL_log );
517
518 // BSGL_nolog
519 cmp = XLALComputeBSGL( twoF[i], twoFX, setup_noLogCorr );
520 XLAL_CHECK( xlalErrno == XLAL_SUCCESS, XLAL_EFUNC, "XLALComputeBSGL() failed.\n" );
521 errBSGL_nolog = fmaxf( errBSGL_nolog, fabsf( cmp - outBSGL_nolog[i] ) );
522 printf( "i = %d: err = %g\n", i, errBSGL_nolog );
523
524 // BSGLtL
525 cmp = XLALComputeBSGLtL( twoF[i], twoFX, maxTwoFXl, setup_noLogCorr );
526 XLAL_CHECK( xlalErrno == XLAL_SUCCESS, XLAL_EFUNC, "XLALComputeBSGLtL() failed.\n" );
527 errBSGLtL = fmaxf( errBSGLtL, fabsf( cmp - outBSGLtL[i] ) );
528 printf( "i = %d: err = %g\n", i, errBSGLtL );
529
530 // BtSGLtL
531 cmp = XLALComputeBtSGLtL( maxTwoFSeg[i], twoFX, maxTwoFXl, setup_noLogCorr );
532 XLAL_CHECK( xlalErrno == XLAL_SUCCESS, XLAL_EFUNC, "XLALComputeBtSGLtL() failed.\n" );
533 errBtSGLtL = fmaxf( errBtSGLtL, fabsf( cmp - outBtSGLtL[i] ) );
534 printf( "i = %d: err = %g\n", i, errBtSGLtL );
535
536 } // for i < VECL_LEN
537
538 REAL4 tolerance = 5 * LAL_REAL4_EPS;
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 );
543
544 printf( "%s: success!\n", __func__ );
545
546 XLALDestroyBSGLSetup( setup_noLogCorr );
547 XLALDestroyBSGLSetup( setup_withLogCorr );
548
549 return XLAL_SUCCESS;
550
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.
#define TRUE
#define FALSE
#define VEC_LEN
int XLALCheckBSGLVectorFunctions(void)
#define NUM_SEGS
LRstat_variant_t
Enum for LR-statistic variants.
@ LRS_BSGL
@ LRS_BtSGLtL
@ LRS_BSGLtL
@ LRS_BStSGLtL
int l
double e
#define LAL_REAL4_MAX
#define LAL_LOG10E
#define LAL_REAL4_EPS
unsigned char BOOLEAN
#define XLAL_INIT_MEM(x)
char CHAR
uint32_t UINT4
float REAL4
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 xlalErrno
#define XLAL_ERROR(...)
#define XLAL_CHECK(assertion,...)
#define XLAL_CHECK_MAIN(assertion,...)
XLAL_SUCCESS
XLAL_EFUNC
XLAL_EINVAL
XLAL_EFAILED
XLAL_FAILURE
n
size_t numDetectors
REAL4 * data