20#include <gsl/gsl_sort.h>
21#include <gsl/gsl_roots.h>
22#include <gsl/gsl_fit.h>
70 UINT4 numofweights = 0;
71 for (
UINT4 ii = 0; ii <
template->templatedata->length; ii++ )
if ( template->templatedata->data[ii] != 0.0 ) {
75 REAL8 sumofsqweights = 0.0;
76 for (
UINT4 ii = 0; ii < numofweights; ii++ ) {
77 sumofsqweights += (
template->templatedata->data[ii] *
template->templatedata->data[ii] );
79 REAL8 sumofsqweightsinv = 1.0 / sumofsqweights;
89 gsl_rng_set( rng, 0 );
93 for (
UINT4 ii = 0; ii < trials; ii++ ) {
96 for (
UINT4 jj = 0; jj < numofweights; jj++ ) {
97 UINT4 firstfreqbin =
template->pixellocations->data[jj] / numfprbins;
98 UINT4 secfreqbin =
template->pixellocations->data[jj] - firstfreqbin * numfprbins;
101 R += ( noise - ffplanenoise->
data[secfreqbin] * fbinaveratios->
data[firstfreqbin] ) * template->templatedata->data[jj];
103 Rs->
data[ii] = (
REAL4 )( R * sumofsqweightsinv );
110 if (
output->topRvalues == NULL ) {
117 output->distSigma = sigma;
143 XLAL_CHECK(
output != NULL &&
template != NULL && ffplanenoise != NULL && fbinaveratios != NULL && inputParams != NULL && rng != NULL,
XLAL_EINVAL );
149 const gsl_root_fsolver_type *T1 = gsl_root_fsolver_brent;
150 gsl_root_fsolver *s1 = NULL;
153 const gsl_root_fdfsolver_type *
T0 = gsl_root_fdfsolver_newton;
154 gsl_root_fdfsolver *s0 = NULL;
156 gsl_function_fdf FDF;
160 struct gsl_probR_pars params = {
template, ffplanenoise, fbinaveratios, thresh, inputParams, rng, errcode};
174 REAL8 Rlow = 0.0, Rhigh = 10000.0, root = 400.0;
183 INT4 max_iter = 100, jj = 0, max_retries = 10;
185 REAL8 prevroot = 0.0;
186 while (
status == GSL_CONTINUE && ii < max_iter ) {
191 status = gsl_root_fsolver_iterate( s1 );
196 root = gsl_root_fsolver_root( s1 );
197 Rlow = gsl_root_fsolver_x_lower( s1 );
198 Rhigh = gsl_root_fsolver_x_upper( s1 );
199 status = gsl_root_test_interval( Rlow, Rhigh, 0.0, 0.001 );
202 status = gsl_root_fdfsolver_iterate( s0 );
205 root = gsl_root_fdfsolver_root( s0 );
206 status = gsl_root_test_delta( prevroot, root, 0.0, 0.001 );
210 if ( root < 0.0 && jj < max_retries ) {
214 XLAL_CHECK( gsl_root_fdfsolver_set( s0, &FDF, gsl_rng_uniform_pos( rng )*Rhigh ) == GSL_SUCCESS,
XLAL_EFUNC );
215 }
else if ( root < 0.0 && jj == max_retries ) {
223 XLAL_CHECK(
status == GSL_SUCCESS,
XLAL_FAILURE,
"Root finding iteration (%d/%d) failed with failure code %d. Previous root = %f, current root = %f\n", ii, max_iter,
status, prevroot, root );
224 XLAL_CHECK( ii < max_iter,
XLAL_EMAXITER,
"Root finding iteration (%d/%d) failed with failure code %d. Previous root = %f, current root = %f\n", ii, max_iter,
status, prevroot, root );
225 XLAL_CHECK( root != 0.0,
XLAL_ERANGE,
"Root finding iteration (%d/%d) converged to 0.0\n", ii, max_iter );
227 XLAL_CHECK(
status == GSL_SUCCESS,
XLAL_FAILURE,
"Root finding iteration (%d/%d) failed with failure code %d. Previous root = %f, current root = %f\n", ii, max_iter,
status, prevroot, root );
228 XLAL_CHECK( ii < max_iter,
XLAL_EMAXITER,
"Root finding iteration (%d/%d) failed with failure code %d. Previous root = %f, current root = %f\n", ii, max_iter,
status, prevroot, root );
235 output->farerrcode = errcode;
238 gsl_root_fsolver_free( s1 );
239 gsl_root_fdfsolver_free( s0 );
258 REAL8 R1 = ( 1.0 + dR ) * R;
259 REAL8 R2 = ( 1.0 - dR ) * R;
260 INT4 errcode1 = 0, errcode2 = 0, errcode3 = 0;
262 REAL8 prob = (
probR( pars->
template, pars->
ffplanenoise, pars->
fbinaveratios, R, pars->
inputParams, pars->
rng, &errcode1 ) +
probR( pars->
template, pars->
ffplanenoise, pars->
fbinaveratios, R1, pars->
inputParams, pars->
rng, &errcode2 ) +
probR( pars->
template, pars->
ffplanenoise, pars->
fbinaveratios, R2, pars->
inputParams, pars->
rng, &errcode3 ) ) / 3.0;
264 if ( errcode1 != 0 ) {
266 }
else if ( errcode2 != 0 ) {
268 }
else if ( errcode3 != 0 ) {
292 INT4 errcode1 = 0, errcode2 = 0;
295 REAL8 R1 = ( 1.0 + dR ) * R;
296 REAL8 R2 = ( 1.0 - dR ) * R;
301 R1 = ( 1.0 + dR ) * R;
302 R2 = ( 1.0 - dR ) * R;
306 REAL8 diffR = R1 - R2;
307 REAL8 slope = ( prob1 - prob2 ) / diffR;
309 if ( errcode1 != 0 ) {
311 }
else if ( errcode2 != 0 ) {
358 for (
UINT4 ii = 0; ii <
template->templatedata->length; ii++ ) {
359 if ( template->templatedata->data[ii] != 0.0 ) {
361 sumwsq +=
template->templatedata->data[ii] *
template->templatedata->data[ii];
375 for (
UINT4 ii = 0; ii < newweights->
length; ii++ ) {
376 UINT4 firstfreqbin =
template->pixellocations->data[ii] / numfprbins;
377 UINT4 secfreqbin =
template->pixellocations->data[ii] - firstfreqbin * numfprbins;
378 newweights->
data[ii] = 0.5 *
template->templatedata->data[ii] * ffplanenoise->
data[secfreqbin] * fbinaveratios->
data[firstfreqbin] / sumwsq;
379 Rpr +=
template->templatedata->data[ii] * ffplanenoise->
data[secfreqbin] * fbinaveratios->
data[firstfreqbin] / sumwsq;
380 sorting->
data[ii] = ii;
393 REAL8 accuracy = 1.0e-11;
404 REAL8 logprobest = 0.0;
405 INT4 estimatedTheProb = 0;
406 if ( prob <= 1.0e-8 || *errcode != 0 ) {
407 estimatedTheProb = 1;
410 REAL8 probslope = 0.0, tempprob,
c1;
411 REAL8 lowerend = 0.0;
412 REAL8 upperend = Rpr;
417 for (
UINT4 ii = 0; ii < probvals->
length; ii++ ) {
418 c1 = gsl_rng_uniform_pos( rng ) * ( upperend - lowerend ) + lowerend;
421 while ( tempprob <= 1.0e-8 || tempprob >= 1.0e-6 ) {
422 if ( tempprob <= 1.0e-8 ) {
424 }
else if ( tempprob >= 1.0e-6 ) {
427 c1 = gsl_rng_uniform_pos( rng ) * ( upperend - lowerend ) + lowerend;
432 while ( tries < 10 && errcode1 != 0 ) {
434 c1 = gsl_rng_uniform_pos( rng ) * ( upperend - lowerend ) + lowerend;
438 if ( tries >= 10 && errcode1 != 0 ) {
439 fprintf( stderr,
"%s: cdfwchisq_twospect() failed with code %d after making %d tries.\n",
__func__, errcode1, tries );
445 probvals->
data[ii] = log10( tempprob );
446 cvals->data[ii] =
c1;
449 REAL8 yintercept, cov00, cov01, cov11, sumsq;
450 XLAL_CHECK_REAL8( gsl_fit_linear( cvals->data, 1, probvals->
data, 1, cvals->length, &yintercept, &probslope, &cov00, &cov01, &cov11, &sumsq ) == GSL_SUCCESS,
XLAL_EFUNC );
451 logprobest = probslope * Rpr + yintercept;
468 if ( estimatedTheProb == 1 ) {
471 return log10( prob );
#define __func__
log an I/O error, i.e.
REAL8 expRandNum(const REAL8 mu, const gsl_rng *ptrToGenerator)
Create a exponentially distributed noise value.
void sort_double_ascend(REAL8Vector *vector)
Sort a REAL8Vector in ascending order, modifying the input vector.
INT4 calcStddev(REAL4 *sigma, const REAL4VectorAligned *vector)
Compute the standard deviation of a REAL4VectorAligned.
REAL4 calcMean(const REAL4VectorAligned *vector)
Compute the mean value of a REAL4VectorAligned, computed via recursion like in GSL.
REAL8 cdfwchisq_twospect(qfvars *vars, REAL8 sigma, REAL8 acc, INT4 *ifault)
farStruct * createfarStruct(void)
Allocate memory for farStruct.
void gsl_probRandDprobRdR(const REAL8 R, void *param, REAL8 *probabilityR, REAL8 *dprobRdR)
Determine the difference between the probability and log10 of the threshold as well as the slope of t...
REAL8 gsl_dprobRdR(const REAL8 R, void *param)
Determine the slope of the inverse cumulative distribution function.
INT4 estimateFAR(farStruct *output, const TwoSpectTemplate *template, const UINT4 trials, const REAL8 thresh, const REAL4VectorAligned *ffplanenoise, const REAL4VectorAligned *fbinaveratios)
Estimate the FAR of the R statistic from the weights by a number of trials.
REAL8 gsl_probR(const REAL8 R, void *param)
For the root finding, calculating the false alarm probability of R.
REAL8 probR(const TwoSpectTemplate *template, const REAL4VectorAligned *ffplanenoise, const REAL4VectorAligned *fbinaveratios, const REAL8 R, const UserInput_t *params, const gsl_rng *rng, INT4 *errcode)
Analytically calculate the probability of a true signal using the Davies' method.
INT4 numericFAR(farStruct *output, const TwoSpectTemplate *template, const REAL8 thresh, const REAL4VectorAligned *ffplanenoise, const REAL4VectorAligned *fbinaveratios, const UserInput_t *inputParams, const gsl_rng *rng, const INT4 method)
Numerically solve for the FAR of the R statistic from the weights using the Davies algorithm and a ro...
void destroyfarStruct(farStruct *farstruct)
Destroy an farStruct.
void * XLALMalloc(size_t n)
INT4Vector * XLALCreateINT4Vector(UINT4 length)
void XLALDestroyINT4Vector(INT4Vector *vector)
REAL8Vector * XLALCreateREAL8Vector(UINT4 length)
void XLALDestroyREAL8Vector(REAL8Vector *vector)
void XLALDestroyREAL4VectorAligned(REAL4VectorAligned *in)
REAL4VectorAligned * XLALCreateREAL4VectorAligned(const UINT4 length, const UINT4 align)
#define XLAL_ERROR_REAL8(...)
#define XLAL_CHECK(assertion,...)
#define XLAL_CHECK_REAL8(assertion,...)
#define XLAL_CHECK_NULL(assertion,...)
REAL4VectorAligned * topRvalues
const REAL4VectorAligned * fbinaveratios
const UserInput_t * inputParams
const REAL4VectorAligned * ffplanenoise
const TwoSpectTemplate * template
REAL8Vector * noncentrality
alignedREAL8Vector * weights
void destroyAlignedREAL8Vector(alignedREAL8Vector *vector)
alignedREAL8Vector * createAlignedREAL8Vector(UINT4 length, const size_t align)