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
ppe_init.c
Go to the documentation of this file.
1/*
2* Copyright (C) 2014 Matthew Pitkin
3*
4* This program is free software; you can redistribute it and/or modify
5* it under the terms of the GNU General Public License as published by
6* the Free Software Foundation; either version 2 of the License, or
7* (at your option) any later version.
8*
9* This program is distributed in the hope that it will be useful,
10* but WITHOUT ANY WARRANTY; without even the implied warranty of
11* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12* GNU General Public License for more details.
13*
14* You should have received a copy of the GNU General Public License
15* along with with program; see the file COPYING. If not, write to the
16* Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
17* MA 02110-1301 USA
18*/
19
20/******************************************************************************/
21/* INITIALISATION FUNCTIONS */
22/******************************************************************************/
23
24#include "config.h"
25#include "ppe_init.h"
26
27/** Array for conversion from lowercase to uppercase */
28static const CHAR a2A[256] = {
29 ['a'] = 'A', ['b'] = 'B', ['c'] = 'C', ['d'] = 'D', ['e'] = 'E', ['f'] = 'F', ['g'] = 'G', ['h'] = 'H',
30 ['i'] = 'I', ['j'] = 'J', ['k'] = 'K', ['l'] = 'L', ['m'] = 'M', ['n'] = 'N', ['o'] = 'O', ['p'] = 'P',
31 ['q'] = 'Q', ['r'] = 'R', ['s'] = 'S', ['t'] = 'T', ['u'] = 'U', ['v'] = 'V', ['w'] = 'W', ['x'] = 'X',
32 ['y'] = 'Y', ['z'] = 'Z'
33};
34
35
36/** \brief Convert string to uppercase */
37static void strtoupper( CHAR *s )
38{
39 /* convert everything to uppercase */
40 CHAR c;
41 for ( ; *s; ++s ) {
42 if ( ( c = a2A[( int )( *s )] ) ) {
43 *s = c;
44 }
45 }
46}
47
48
49/**
50 * \brief A wrapper around \c LALInferenceNestedSamplingAlgorithm
51 *
52 * This function just calls \c LALInferenceNestedSamplingAlgorithm, but will time the algorithm
53 * if required.
54 *
55 * \param runState [] A pointer to the \c LALInferenceRunState
56 */
58{
59 /* timing values */
60 struct timeval time1, time2;
61 REAL8 tottime;
62
63 if ( LALInferenceCheckVariable( runState->algorithmParams, "timefile" ) ) {
64 gettimeofday( &time1, NULL );
65 }
66
68
69 if ( LALInferenceCheckVariable( runState->algorithmParams, "timefile" ) ) {
70 gettimeofday( &time2, NULL );
71
72 FILE *timefile = *( FILE ** )LALInferenceGetVariable( runState->algorithmParams, "timefile" );
73 UINT4 timenum = *( UINT4 * )LALInferenceGetVariable( runState->algorithmParams, "timenum" );
74 tottime = ( REAL8 )( ( time2.tv_sec + time2.tv_usec * 1.e-6 ) - ( time1.tv_sec + time1.tv_usec * 1.e-6 ) );
75 fprintf( timefile, "[%d] %s: %.9le secs\n", timenum, __func__, tottime );
76 timenum++;
77 /* get number of likelihood evaluations */
78 UINT4 nlike = 0, ndata = 0;
79 LALInferenceIFOData *tmpdata = runState->data;
80 while ( tmpdata ) {
81 nlike += tmpdata->likeli_counter;
82 ndata++;
83 tmpdata = tmpdata->next;
84 }
85 fprintf( timefile, "[%d] Number of likelihood evaluations: %d\n", timenum, nlike / ndata );
86 timenum++;
87 check_and_add_fixed_variable( runState->algorithmParams, "timenum", &timenum, LALINFERENCE_UINT4_t );
88 }
89}
90
91
92/**
93 * \brief A wrapper around \c LALInferenceSetupLivePointsArray
94 *
95 * This function just calls \c LALInferenceSetupLivePointsArray, but will time the algorithm
96 * if required.
97 *
98 * \param runState [] A pointer to the \c LALInferenceRunState
99 */
101{
102 /* timing values */
103 struct timeval time1, time2;
104 REAL8 tottime;
105
106 if ( LALInferenceCheckVariable( runState->algorithmParams, "timefile" ) ) {
107 gettimeofday( &time1, NULL );
108 }
109
111
112 /* note that this time divided by the number of live points will give the approximate time per likelihood evaluation */
113 if ( LALInferenceCheckVariable( runState->algorithmParams, "timefile" ) ) {
114 gettimeofday( &time2, NULL );
115
116 FILE *timefile = *( FILE ** )LALInferenceGetVariable( runState->algorithmParams, "timefile" );
117 UINT4 timenum = *( UINT4 * )LALInferenceGetVariable( runState->algorithmParams, "timenum" );
118 tottime = ( REAL8 )( ( time2.tv_sec + time2.tv_usec * 1.e-6 ) - ( time1.tv_sec + time1.tv_usec * 1.e-6 ) );
119 fprintf( timefile, "[%d] %s: %.9le secs\n", timenum, __func__, tottime );
120 timenum++;
121 check_and_add_fixed_variable( runState->algorithmParams, "timenum", &timenum, LALINFERENCE_UINT4_t );
122 }
123}
124
125
126/**
127 * \brief Initialises the nested sampling algorithm control
128 *
129 * Memory is allocated for the parameters, priors and proposals. The nested sampling control parameters are set: the
130 * number of live points \c Nlive, the number of points for each MCMC \c Nmcmc, the number of independent runs within
131 * the algorithm \c Nruns, and the stopping criterion \c tolerance.
132 *
133 * The random number generator is initialise (the GSL Mersenne Twister algorithm \c gsl_rng_mt19937) using either a user
134 * defined seed \c randomseed, the system defined \c /dev/urandom file, or the system clock time.
135 *
136 * \param runState [in] A pointer to the \c LALInferenceRunState
137 */
139{
140 ProcessParamsTable *ppt = NULL;
141 ProcessParamsTable *commandLine = runState->commandLine;
142 REAL8 tmp;
143 INT4 tmpi;
144 INT4 randomseed;
145 UINT4 verbose = 0;
146
147 FILE *devrandom = NULL;
148 struct timeval tv;
149
150 /* print out help message */
151 ppt = LALInferenceGetProcParamVal( commandLine, "--help" );
152 if ( ppt ) {
153 fprintf( stderr, USAGE, commandLine->program );
154 exit( 0 );
155 }
156
157 /* Initialise parameters structure */
158 runState->algorithmParams = XLALCalloc( 1, sizeof( LALInferenceVariables ) );
159 runState->priorArgs = XLALCalloc( 1, sizeof( LALInferenceVariables ) );
160 runState->proposalArgs = XLALCalloc( 1, sizeof( LALInferenceVariables ) );
161 /* Initialise threads - single thread */
162 runState->threads = LALInferenceInitThreads( 1 );
163
164 ppt = LALInferenceGetProcParamVal( commandLine, "--verbose" );
165 if ( ppt ) {
167 }
168
169 /* Number of live points */
170 ppt = LALInferenceGetProcParamVal( commandLine, "--Nlive" );
171 if ( ppt ) {
172 tmpi = atoi( LALInferenceGetProcParamVal( commandLine, "--Nlive" )->value );
174 } else {
175 if ( !LALInferenceGetProcParamVal( commandLine, "--inject-only" ) ) {
176 XLAL_ERROR_VOID( XLAL_EIO, "Error... Number of live point must be specified." );
177 }
178 }
179
180 /* Number of points in MCMC chain */
181 ppt = LALInferenceGetProcParamVal( commandLine, "--Nmcmc" );
182 if ( ppt ) {
183 tmpi = atoi( LALInferenceGetProcParamVal( commandLine, "--Nmcmc" )->value );
185 }
186
187 /* set sloppiness! */
188 ppt = LALInferenceGetProcParamVal( commandLine, "--sloppyfraction" );
189 if ( ppt ) {
190 tmp = atof( ppt->value );
191 } else {
192 tmp = 0.0;
193 }
195
196 /* Optionally specify number of parallel runs */
197 ppt = LALInferenceGetProcParamVal( commandLine, "--Nruns" );
198 if ( ppt ) {
199 tmpi = atoi( ppt->value );
201 }
202
203 /* Tolerance of the Nested sampling integrator */
204 ppt = LALInferenceGetProcParamVal( commandLine, "--tolerance" );
205 if ( ppt ) {
206 tmp = strtod( ppt->value, ( char ** )NULL );
208 }
209
210 /* Set cpu_time variable */
211 REAL8 zero = 0.0;
213
214 /* Set up the random number generator */
215 gsl_rng_env_setup();
216 runState->GSLrandom = gsl_rng_alloc( gsl_rng_mt19937 );
217
218 /* (try to) get random seed from command line: */
219 ppt = LALInferenceGetProcParamVal( commandLine, "--randomseed" );
220 if ( ppt != NULL ) {
221 randomseed = atoi( ppt->value );
222 } else { /* otherwise generate "random" random seed: */
223 /*
224 * Note: /dev/random can be slow after the first few accesses, which is why we're using urandom instead.
225 * [Cryptographic safety isn't a concern here at all]
226 */
227 if ( ( devrandom = fopen( "/dev/urandom", "r" ) ) == NULL ) {
228 gettimeofday( &tv, 0 );
229 randomseed = tv.tv_sec + tv.tv_usec;
230 } else {
231 if ( fread( &randomseed, sizeof( randomseed ), 1, devrandom ) != 1 ) {
232 fprintf( stderr, "Error... could not read random seed\n" );
233 exit( 3 );
234 }
235 fclose( devrandom );
236 }
237 }
238
239 gsl_rng_set( runState->GSLrandom, randomseed );
240
241 /* check if we want to time the program */
242 ppt = LALInferenceGetProcParamVal( commandLine, "--time-it" );
243 if ( ppt != NULL ) {
244 FILE *timefile = NULL;
245 UINT4 timenum = 1;
246
247 ppt = LALInferenceGetProcParamVal( commandLine, "--outfile" );
248 if ( !ppt ) {
249 XLAL_ERROR_VOID( XLAL_EFUNC, "Error... no output file is specified!" );
250 }
251
252 CHAR *outtimefile = NULL;
253 outtimefile = XLALStringDuplicate( ppt->value );
254 /* strip the file extension */
255 CHAR *dotloc = strrchr( outtimefile, '.' );
256 CHAR *slashloc = strrchr( outtimefile, '/' );
257 if ( dotloc != NULL ) {
258 if ( slashloc != NULL ) { /* check dot is after any filename seperator */
259 if ( slashloc < dotloc ) {
260 *dotloc = '\0';
261 }
262 } else {
263 *dotloc = '\0';
264 }
265 }
266 outtimefile = XLALStringAppend( outtimefile, "_timings" );
267
268 if ( ( timefile = fopen( outtimefile, "w" ) ) == NULL ) {
269 fprintf( stderr, "Warning... cannot create a timing file, so proceeding without timings\n" );
270 } else {
273 }
274 XLALFree( outtimefile );
275 }
276
277 /* log samples */
279
280 return;
281}
282
283
284/**
285 * \brief Sets the time angle antenna response lookup table
286 *
287 * This function sets up an antenna response lookup table in time for each detector from which data
288 * exists (either real or fake). The time ranges over one sidereal day. The number of bins for the grid
289 * over time can be specified on the command line via \c time-bins, but if this are not given then default
290 * values are used. The data times as a fraction of a sidereal day from the start time will also be
291 * calculated.
292 *
293 * \param runState [in] A pointer to the LALInferenceRunState
294 * \param source [in] A pointer to a LALSource variable containing the source location
295 */
297{
298 /* Set up lookup tables */
299 /* Using psi bins, time bins */
300 ProcessParamsTable *ppt;
301 ProcessParamsTable *commandLine = runState->commandLine;
302 /* Single thread here */
303 LALInferenceThreadState *threadState = &runState->threads[0];
304
305 LALInferenceIFOData *data = runState->data;
306 LALInferenceIFOModel *ifo_model = threadState->model->ifo;
307
308 REAL8 t0;
309 LALDetAndSource detAndSource;
310
311 ppt = LALInferenceGetProcParamVal( commandLine, "--time-bins" );
312 INT4 timeBins;
313 if ( ppt ) {
314 timeBins = atoi( ppt->value );
315 } else {
316 timeBins = TIMEBINS; /* default time bins */
317 }
318
319 while ( ifo_model ) {
320 REAL8Vector *arespT = NULL, *brespT = NULL;
321 REAL8Vector *arespV = NULL, *brespV = NULL;
322 REAL8Vector *arespS = NULL, *brespS = NULL;
323
324 REAL8Vector *sidDayFrac = NULL;
325 REAL8 dt = 0;
326 UINT4 i = 0;
327
328 LALInferenceAddVariable( ifo_model->params, "timeSteps", &timeBins, LALINFERENCE_INT4_t, LALINFERENCE_PARAM_FIXED );
329
330 t0 = XLALGPSGetREAL8( &data->compTimeData->epoch );
331
332 sidDayFrac = XLALCreateREAL8Vector( IFO_XTRA_DATA( ifo_model )->times->length );
333
334 /* set the time in sidereal days since the first data point (mod 1 sidereal day) */
335 for ( i = 0; i < IFO_XTRA_DATA( ifo_model )->times->length; i++ ) {
336 sidDayFrac->data[i] = fmod( XLALGPSGetREAL8( &IFO_XTRA_DATA( ifo_model )->times->data[i] ) - t0, LAL_DAYSID_SI );
337 }
338
339 LALInferenceAddVariable( ifo_model->params, "siderealDay", &sidDayFrac, LALINFERENCE_REAL8Vector_t, LALINFERENCE_PARAM_FIXED );
340
341 detAndSource.pDetector = data->detector;
342 detAndSource.pSource = source;
343
344 arespT = XLALCreateREAL8Vector( timeBins );
345 brespT = XLALCreateREAL8Vector( timeBins );
346 arespV = XLALCreateREAL8Vector( timeBins );
347 brespV = XLALCreateREAL8Vector( timeBins );
348 arespS = XLALCreateREAL8Vector( timeBins );
349 brespS = XLALCreateREAL8Vector( timeBins );
350
351 dt = LALInferenceGetREAL8Variable( ifo_model->params, "dt" );
352
353 response_lookup_table( t0, detAndSource, timeBins, dt, arespT, brespT, arespV, brespV, arespS, brespS );
354
355 LALInferenceAddVariable( ifo_model->params, "a_response_tensor", &arespT, LALINFERENCE_REAL8Vector_t, LALINFERENCE_PARAM_FIXED );
356 LALInferenceAddVariable( ifo_model->params, "b_response_tensor", &brespT, LALINFERENCE_REAL8Vector_t, LALINFERENCE_PARAM_FIXED );
357
358 if ( LALInferenceCheckVariable( ifo_model->params, "nonGR" ) ) {
359 LALInferenceAddVariable( ifo_model->params, "a_response_vector", &arespV, LALINFERENCE_REAL8Vector_t, LALINFERENCE_PARAM_FIXED );
360 LALInferenceAddVariable( ifo_model->params, "b_response_vector", &brespV, LALINFERENCE_REAL8Vector_t, LALINFERENCE_PARAM_FIXED );
361 LALInferenceAddVariable( ifo_model->params, "a_response_scalar", &arespS, LALINFERENCE_REAL8Vector_t, LALINFERENCE_PARAM_FIXED );
362 LALInferenceAddVariable( ifo_model->params, "b_response_scalar", &brespS, LALINFERENCE_REAL8Vector_t, LALINFERENCE_PARAM_FIXED );
363 } else {
364 XLALDestroyREAL8Vector( arespV );
365 XLALDestroyREAL8Vector( brespV );
366 XLALDestroyREAL8Vector( arespS );
367 XLALDestroyREAL8Vector( brespS );
368 }
369
370 data = data->next;
371 ifo_model = ifo_model->next;
372 }
373
374 return;
375}
376
377
378/**
379 * \brief Set up all the allowed variables for a known pulsar search
380 * This functions sets up all possible variables that are possible in a known pulsar search. Parameter values read in
381 * from a .par file and passed in via the \c pars variable will be set.
382 *
383 * \param ini [in] A pointer to a \c LALInferenceVariables type that will be filled in with pulsar parameters
384 * \param pars [in] A \c PulsarParameters type containing pulsar parameters read in from a TEMPO-style .par file
385 */
387{
388 /* amplitude model parameters for l=m=2 harmonic emission */
390 add_variable_parameter( pars, ini, "PHI0", LALINFERENCE_PARAM_FIXED ); /* note that this is rotational phase */
391 add_variable_parameter( pars, ini, "COSIOTA", LALINFERENCE_PARAM_FIXED );
394 add_variable_parameter( pars, ini, "Q22", LALINFERENCE_PARAM_FIXED ); /* mass quadrupole, Q22 */
395
396 /* amplitude model parameters for l=2, m=1 and 2 harmonic emission from Jones (2010) */
399 add_variable_parameter( pars, ini, "LAMBDA", LALINFERENCE_PARAM_FIXED );
400 add_variable_parameter( pars, ini, "COSTHETA", LALINFERENCE_PARAM_FIXED );
402
403 /* amplitude model parameters in phase and amplitude form */
408
409 /***** phase model parameters ******/
410 if ( PulsarCheckParam( pars, "F" ) ) { /* frequency and frequency derivative parameters */
411 UINT4 i = 0;
412 const REAL8Vector *freqs = PulsarGetREAL8VectorParam( pars, "F" );
413 /* add each frequency and derivative value as a seperate parameter (also set a value that is the FIXED value to be used for calculating phase differences) */
414 for ( i = 0; i < freqs->length; i++ ) {
415 CHAR varname[256];
416 snprintf( varname, sizeof( varname ), "F%u", i );
417 REAL8 fval = PulsarGetREAL8VectorParamIndividual( pars, varname );
419 snprintf( varname, sizeof( varname ), "F%u_FIXED", i );
420 LALInferenceAddVariable( ini, varname, &fval, LALINFERENCE_REAL8_t, LALINFERENCE_PARAM_FIXED ); /* add FIXED value */
421 }
422 /* add value with the number of FB parameters given */
424 }
425 add_variable_parameter( pars, ini, "PEPOCH", LALINFERENCE_PARAM_FIXED );
426
427 /* add non-GR parameters */
430 add_variable_parameter( pars, ini, "HCROSS", LALINFERENCE_PARAM_FIXED );
431 add_variable_parameter( pars, ini, "PSITENSOR", LALINFERENCE_PARAM_FIXED );
432 add_variable_parameter( pars, ini, "PHI0TENSOR", LALINFERENCE_PARAM_FIXED );
433 add_variable_parameter( pars, ini, "HSCALARB", LALINFERENCE_PARAM_FIXED );
434 add_variable_parameter( pars, ini, "HSCALARL", LALINFERENCE_PARAM_FIXED );
435 add_variable_parameter( pars, ini, "PSISCALAR", LALINFERENCE_PARAM_FIXED );
436 add_variable_parameter( pars, ini, "PHI0SCALAR", LALINFERENCE_PARAM_FIXED );
437 add_variable_parameter( pars, ini, "HVECTORX", LALINFERENCE_PARAM_FIXED );
438 add_variable_parameter( pars, ini, "HVECTORY", LALINFERENCE_PARAM_FIXED );
439 add_variable_parameter( pars, ini, "PSIVECTOR", LALINFERENCE_PARAM_FIXED );
440 add_variable_parameter( pars, ini, "PHI0VECTOR", LALINFERENCE_PARAM_FIXED );
441 add_variable_parameter( pars, ini, "HPLUS_F", LALINFERENCE_PARAM_FIXED );
442 add_variable_parameter( pars, ini, "HCROSS_F", LALINFERENCE_PARAM_FIXED );
443 add_variable_parameter( pars, ini, "PSITENSOR_F", LALINFERENCE_PARAM_FIXED );
444 add_variable_parameter( pars, ini, "PHI0TENSOR_F", LALINFERENCE_PARAM_FIXED );
445 add_variable_parameter( pars, ini, "HSCALARB_F", LALINFERENCE_PARAM_FIXED );
446 add_variable_parameter( pars, ini, "HSCALARL_F", LALINFERENCE_PARAM_FIXED );
447 add_variable_parameter( pars, ini, "PSISCALAR_F", LALINFERENCE_PARAM_FIXED );
448 add_variable_parameter( pars, ini, "PHI0SCALAR_F", LALINFERENCE_PARAM_FIXED );
449 add_variable_parameter( pars, ini, "HVECTORX_F", LALINFERENCE_PARAM_FIXED );
450 add_variable_parameter( pars, ini, "HVECTORY_F", LALINFERENCE_PARAM_FIXED );
451 add_variable_parameter( pars, ini, "PSIVECTOR_F", LALINFERENCE_PARAM_FIXED );
452 add_variable_parameter( pars, ini, "PHI0VECTOR_F", LALINFERENCE_PARAM_FIXED );
454
455 /* sky position */
456 REAL8 ra = 0.;
457 if ( PulsarCheckParam( pars, "RA" ) ) {
458 ra = PulsarGetREAL8Param( pars, "RA" );
459 } else if ( PulsarCheckParam( pars, "RAJ" ) ) {
460 ra = PulsarGetREAL8Param( pars, "RAJ" );
461 } else {
462 XLAL_ERROR_VOID( XLAL_EINVAL, "No source right ascension specified!" );
463 }
464 REAL8 dec = 0.;
465 if ( PulsarCheckParam( pars, "DEC" ) ) {
466 dec = PulsarGetREAL8Param( pars, "DEC" );
467 } else if ( PulsarCheckParam( pars, "DECJ" ) ) {
468 dec = PulsarGetREAL8Param( pars, "DECJ" );
469 } else {
470 XLAL_ERROR_VOID( XLAL_EINVAL, "No source declination specified!" );
471 }
474
477 add_variable_parameter( pars, ini, "POSEPOCH", LALINFERENCE_PARAM_FIXED );
478
479 /* source distance and parallax */
482
483 /* only add binary system parameters if required */
484 if ( PulsarCheckParam( pars, "BINARY" ) ) {
485 CHAR *binary = XLALStringDuplicate( PulsarGetStringParam( pars, "BINARY" ) );
487
496
502
508
509 add_variable_parameter( pars, ini, "XPBDOT", LALINFERENCE_PARAM_FIXED );
510 add_variable_parameter( pars, ini, "EPS1DOT", LALINFERENCE_PARAM_FIXED );
511 add_variable_parameter( pars, ini, "EPS2DOT", LALINFERENCE_PARAM_FIXED );
517
520 add_variable_parameter( pars, ini, "DTHETA", LALINFERENCE_PARAM_FIXED );
525
526 if ( PulsarCheckParam( pars, "FB" ) ) {
527 UINT4 i = 0;
528 const REAL8Vector *fb = PulsarGetREAL8VectorParam( pars, "FB" );
529 /* add each FB value as a seperate parameter */
530 for ( i = 0; i < fb->length; i++ ) {
531 CHAR varname[256];
532 snprintf( varname, sizeof( varname ), "FB%u", i );
533 REAL8 fbval = PulsarGetREAL8VectorParamIndividual( pars, varname );
535 }
536 /* add value with the number of FB parameters given */
538 }
539 }
540
541 /* check for glitches (searching on glitch epochs GLEP) */
542 if ( PulsarCheckParam( pars, "GLEP" ) ) {
543 UINT4 i = 0, j = 0, glnum = 0;
544 for ( i = 0; i < NUMGLITCHPARS; i++ ) {
545 if ( PulsarCheckParam( pars, glitchpars[i] ) ) {
546 const REAL8Vector *glv = PulsarGetREAL8VectorParam( pars, glitchpars[i] );
547 for ( j = 0; j < glv->length; j++ ) {
548 CHAR varname[300];
549 snprintf( varname, sizeof( varname ), "%s_%u", glitchpars[i], j + 1 );
550 REAL8 glval = PulsarGetREAL8VectorParamIndividual( pars, varname );
552 }
553 if ( glv->length > glnum ) {
554 glnum = glv->length; /* find max number of glitch parameters */
555 }
556 }
557 }
558 /* add value with the number of glitch parameters given */
560 }
561}
562
563
564/**
565 * \brief Sets up the parameters to be searched over and their prior ranges
566 *
567 * This function sets up any parameters that you require the code to search over and specifies the prior range and type
568 * for each. This information is contained in a prior file specified by the command line argument \c prior-file. There
569 * are currently five different allowed prior distributions:
570 * - "uniform": A flat distribution between a minimum and maximum range (with zero probabiility outside that range);
571 * - "gaussian": A Gaussian/Normal distribution defined by a mean and standard devaition;
572 * - "fermidirac": A Fermi-Dirac-like distribution defined by a knee and attenuation width;
573 * - "gmm": A one- or more-dimensional Gaussian Mixture model defined by the means, standard deviations and weights of each mode.
574 * - "loguniform": A flat distribution in log-space (proportional to the inverse of the value in non-log-space)
575 * For all priors the first two columns of the prior definition should be:
576 * -# the name of a parameter to be searched over, and
577 * -# the prior type (e.g. "uniform", "gaussian", "fermidirac", "gmm" or "loguniform".
578 * For the "uniform, "loguniform", "gaussian", "fermidirac" priors the prior file should contain the a further two columns containing:
579 * -# the lower limit ("uniform" and "loguniform"), mean ("gaussian"), or sigma ("fermidirac") of the distribution;
580 * -# the upper limit ("uniform" and "loguniform"), standard deviation ("gaussian"), or r ("fermidirac") of the distribution.
581 * For the "gmm" prior the file should contain at least a further four columns. The third column gives the number of modes
582 * for the mixture. The fourth columns is a list of means for each mode (with sub-lists of means
583 * for each parameter). The fifth column is a list of covariance matrices for each mode. The sixth
584 * column is a list of weights for each mode (weights can be relative weights as they will be
585 * normalised when parsed by the code). Finally, there can be additional lists containing pairs
586 * of values for each parameter giving lower and upper limits of the distribution (note that the
587 * weights given are weights assuming that there are no bounds on the distributions).
588 *
589 * Some examples of files are:
590 * \code
591 * H0 uniform 0 1e-21
592 * PHI0 uniform 0 3.14159265359
593 * COSIOTA uniform -1 1
594 * PSI uniform -0.785398163397448 0.785398163397448
595 * \endcode
596 * or
597 * \code
598 * H0 fermidirac 3.3e-26 9.16
599 * PHI0 uniform 0 3.14159265359
600 * IOTA uniform 0 3.14159265359
601 * PSI uniform -0.785398163397448 0.785398163397448
602 * \endcode
603 * or
604 * \code
605 * H0 gmm 3 [[1e-25],[2e-25],[1e-26]] [[[2e-24]],[[3e-24]],[[1e-25]]] [1.5,2.5,0.2] [0.0,1e-20]
606 * PHI0 uniform 0 3.14159265359
607 * COSIOTA uniform -1 1
608 * PSI uniform -0.785398163397448 0.785398163397448
609 * \endcode
610 *
611 * Any parameter specified in the file will have its vary type set to \c LALINFERENCE_PARAM_LINEAR.
612 *
613 * If a parameter correlation matrix is given by the \c cor-file command then this is used to construct a multi-variate
614 * Gaussian prior for the given parameters (it is assumed that this file is created using TEMPO and the parameters it
615 * contains are the same as those for which a standard deviation is defined in the par file). This overrules the
616 * Gaussian priors that will have been set for these parameters.
617 *
618 * \param runState [in] A pointer to the LALInferenceRunState
619 */
621{
622 CHAR *propfile = NULL;
623 /* Single thread here */
624 LALInferenceThreadState *threadState = &runState->threads[0];
625 ProcessParamsTable *ppt;
626 ProcessParamsTable *commandLine = runState->commandLine;
627
628 CHAR *tempPar = NULL, *tempPrior = NULL;
629 REAL8 low, high;
630
632 if ( !LALInferenceGetProcParamVal( commandLine, "--test-gaussian-likelihood" ) ) {
633 ifo = threadState->model->ifo;
634 }
635
636 CHAR *filebuf = NULL; /* buffer to store prior file */
637 TokenList *tlist = NULL;
638 UINT4 k = 0, nvals = 0;
639
640 /* parameters in correlation matrix */
641 LALStringVector *corParams = NULL;
642 REAL8Array *corMat = NULL;
643
644 INT4 varyphase = 0, varyskypos = 0, varybinary = 0, varyglitch = 0;
645
646 ppt = LALInferenceGetProcParamVal( commandLine, "--prior-file" );
647 if ( ppt ) {
648 propfile = XLALStringDuplicate( LALInferenceGetProcParamVal( commandLine, "--prior-file" )->value );
649 } else {
650 fprintf( stderr, "Error... --prior-file is required.\n" );
651 fprintf( stderr, USAGE, commandLine->program );
652 exit( 0 );
653 }
654
655 /* read in prior file and separate lines */
656 filebuf = XLALFileLoad( propfile );
657 if ( XLALCreateTokenList( &tlist, filebuf, "\n" ) != XLAL_SUCCESS ) {
658 XLAL_ERROR_VOID( XLAL_EINVAL, "Error... could not convert data into separate lines.\n" );
659 }
660
661 /* parse through priors */
662 for ( k = 0; k < tlist->nTokens; k++ ) {
663 UINT4 isthere = 0, i = 0;
665
666 /* check for comment line starting with '#' or '%' */
667 if ( tlist->tokens[k][0] == '#' || tlist->tokens[k][0] == '%' ) {
668 continue;
669 }
670
671 /* check the number of values in the line by counting the whitespace separated values */
672 nvals = 0;
673 TokenList *tline = NULL;
674 XLALCreateTokenList( &tline, tlist->tokens[k], " \t" );
675 nvals = tline->nTokens;
676
677 if ( nvals < 2 ) {
678 fprintf( stderr, "Warning... number of values ('%d') on line '%d' in prior file is different than expected:\n\t'%s'", nvals, k + 1, tlist->tokens[k] );
679 XLALDestroyTokenList( tline );
680 continue;
681 }
682
683 tempPar = XLALStringDuplicate( tline->tokens[0] );
684 strtoupper( tempPar ); /* convert tempPar to all uppercase letters */
685 tempPrior = XLALStringDuplicate( tline->tokens[1] );
686
687 /* check if there is more than one parameter in tempPar, separated by a ':', for us in GMM prior */
688 TokenList *parnames = NULL;
689 XLALCreateTokenList( &parnames, tempPar, ":" ); /* find number of parameters used by GMM (parameters should be ':' separated */
690 UINT4 npars = parnames->nTokens;
691
692 /* gaussian, uniform, loguniform and fermi-dirac priors should all have four values to a line */
693 if ( !strcmp( tempPrior, "uniform" ) || !strcmp( tempPrior, "loguniform" ) || !strcmp( tempPrior, "gaussian" ) || !strcmp( tempPrior, "fermidirac" ) ) {
694 if ( npars > 1 ) {
695 XLAL_ERROR_VOID( XLAL_EINVAL, "Error... 'uniform', 'loguniform', 'gaussian', or 'fermidirac' priors must only be given for single parameters." );
696 }
697
698 if ( nvals != 4 ) {
699 XLAL_ERROR_VOID( XLAL_EINVAL, "Error... 'uniform', 'loguniform', 'gaussian', or 'fermidirac' priors must specify four values." );
700 }
701
702 low = atof( tline->tokens[2] );
703 high = atof( tline->tokens[3] );
704
705 if ( !strcmp( tempPrior, "uniform" ) || !strcmp( tempPrior, "loguniform" ) ) {
706 if ( high < low ) {
707 XLAL_ERROR_VOID( XLAL_EINVAL, "Error... In %s the %s parameters ranges are wrongly set.", propfile, tempPar );
708 }
709 }
710
711 if ( strcmp( tempPrior, "uniform" ) && strcmp( tempPrior, "loguniform" ) && strcmp( tempPrior, "gaussian" ) && strcmp( tempPrior, "fermidirac" ) ) {
712 XLAL_ERROR_VOID( XLAL_EINVAL, "Error... prior type '%s' not recognised", tempPrior );
713 }
714
715 /* Add the prior variables */
716 if ( !strcmp( tempPrior, "uniform" ) ) {
717 LALInferenceAddMinMaxPrior( runState->priorArgs, tempPar, &low, &high, LALINFERENCE_REAL8_t );
718 } else if ( !strcmp( tempPrior, "loguniform" ) ) {
719 LALInferenceAddLogUniformPrior( runState->priorArgs, tempPar, &low, &high, LALINFERENCE_REAL8_t );
720 } else if ( !strcmp( tempPrior, "gaussian" ) ) {
721 LALInferenceAddGaussianPrior( runState->priorArgs, tempPar, &low, &high, LALINFERENCE_REAL8_t );
722 } else if ( !strcmp( tempPrior, "fermidirac" ) ) {
723 LALInferenceAddFermiDiracPrior( runState->priorArgs, tempPar, &low, &high, LALINFERENCE_REAL8_t );
724 }
725 } else if ( !strcmp( tempPrior, "gmm" ) ) {
726 /* check if using a 1D/multi-dimensional Gaussian Mixture Model prior e.g.:
727 * H0:COSIOTA gmm 2 [[1e-23,0.3],[2e-23,0.4]] [[[1e-45,2e-25],[2e-25,0.02]],[[4e-46,2e-24],[2e-24,0.1]]] [0.2,0.4] [0.,1e-22] [-1.,1.]
728 * where the third value is the number of modes followed by: a list of means for each parameter for each mode; the covariance
729 * matrices for each mode; the weights for each mode; and if required sets of pairs of minimum and maximum prior ranges for each
730 * parameter.
731 */
732 if ( nvals < 6 ) {
733 fprintf( stderr, "Warning... number of values ('%d') on line '%d' in prior file is different than expected:\n\t'%s'", nvals, k + 1, tlist->tokens[k] );
734 XLALDestroyTokenList( tline );
735 continue;
736 }
737
738 UINT4 nmodes = ( UINT4 )atoi( tline->tokens[2] ); /* get the number of modes */
739
740 /* get means of modes for each parameter */
741 REAL8Vector **gmmmus;
742 gmmmus = parse_gmm_means( tline->tokens[3], npars, nmodes );
743 if ( !gmmmus ) {
744 XLAL_ERROR_VOID( XLAL_EINVAL, "Error... problem parsing GMM prior mean values for '%s'.", tempPar );
745 }
746
747 /* get the covariance matrices for the modes */
748 gsl_matrix **gmmcovs;
749 gmmcovs = parse_gmm_covs( tline->tokens[4], npars, nmodes );
750 if ( !gmmcovs ) {
751 XLAL_ERROR_VOID( XLAL_EINVAL, "Error... problem parsing GMM prior covariance matrix values for '%s'.", tempPar );
752 }
753
754 /* get weights for the modes */
755 REAL8Vector *gmmweights = NULL;
756 gmmweights = XLALCreateREAL8Vector( nmodes );
757
758 CHAR strpart[8192];
759 CHAR UNUSED *nextpart;
760 nextpart = get_bracketed_string( strpart, tline->tokens[5], '[', ']' );
761 if ( !strpart[0] ) {
762 XLAL_ERROR_VOID( XLAL_EINVAL, "Error... problem parsing GMM prior weights values for '%s'.", tempPar );
763 }
764
765 /* parse comma separated weights */
766 TokenList *weightvals = NULL;
767 XLALCreateTokenList( &weightvals, strpart, "," );
768 if ( weightvals->nTokens != nmodes ) {
769 XLAL_ERROR_VOID( XLAL_EINVAL, "Error... problem parsing GMM prior weights values for '%s'.", tempPar );
770 }
771 for ( UINT4 j = 0; j < nmodes; j++ ) {
772 gmmweights->data[j] = atof( weightvals->tokens[j] );
773 }
774 XLALDestroyTokenList( weightvals );
775
776 REAL8 minval = -INFINITY, maxval = INFINITY;
777 REAL8Vector *minvals = XLALCreateREAL8Vector( npars ), *maxvals = XLALCreateREAL8Vector( npars );
778
779 /* check if minimum and maximum bounds are specified (otherwise set to +/- infinity) */
780 /* there are minimum and maximum values, e.g. [h0min,h0max] [cosiotamin,cosiotamax] */
781 for ( UINT4 j = 0; j < npars; j++ ) {
782 REAL8 thismin = minval, thismax = maxval;
783 if ( tline->nTokens > 6 + j ) {
784 nextpart = get_bracketed_string( strpart, tline->tokens[6 + j], '[', ']' );
785 if ( !strpart[0] ) {
786 XLAL_ERROR_VOID( XLAL_EINVAL, "Error... problem parsing GMM prior limit values for '%s'.", tempPar );
787 }
788 TokenList *minmaxvals = NULL;
789 XLALCreateTokenList( &minmaxvals, strpart, "," );
790 if ( minmaxvals->nTokens == 2 ) {
791 if ( isfinite( atof( minmaxvals->tokens[0] ) ) ) {
792 thismin = atof( minmaxvals->tokens[0] );
793 }
794 if ( isfinite( atof( minmaxvals->tokens[1] ) ) ) {
795 thismax = atof( minmaxvals->tokens[1] );
796 }
797 }
798 XLALDestroyTokenList( minmaxvals );
799 }
800 minvals->data[j] = thismin;
801 maxvals->data[j] = thismax;
802 }
803
804 LALInferenceAddGMMPrior( runState->priorArgs, tempPar, &gmmmus, &gmmcovs, &gmmweights, &minvals, &maxvals );
805 } else {
806 XLAL_ERROR_VOID( XLAL_EINVAL, "Error... prior type '%s' not recognised", tempPrior );
807 }
808
809 /* if there is a phase parameter defined in the proposal then set varyphase to 1 */
810 for ( UINT4 j = 0; j < npars; j++ ) {
811 for ( i = 0; i < NUMAMPPARS; i++ ) {
812 if ( !strcmp( parnames->tokens[j], amppars[i] ) ) {
813 isthere = 1;
814 break;
815 }
816 }
817 if ( !isthere ) {
818 varyphase = 1;
819 }
820
821 /* check if there are sky position parameters that will be searched over */
822 for ( i = 0; i < NUMSKYPARS; i++ ) {
823 if ( !strcmp( parnames->tokens[j], skypars[i] ) ) {
824 varyskypos = 1;
825 break;
826 }
827 }
828
829 /* check if there are any binary parameters that will be searched over */
830 for ( i = 0; i < NUMBINPARS; i++ ) {
831 if ( !strcmp( parnames->tokens[j], binpars[i] ) ) {
832 varybinary = 1;
833 break;
834 }
835 }
836
837 /* check is there are any glitch parameters that will be searched over */
838 for ( i = 0; i < NUMGLITCHPARS; i++ ) {
839 if ( !strncmp( parnames->tokens[j], glitchpars[i], strlen( glitchpars[i] ) * sizeof( CHAR ) ) ) {
840 varyglitch = 1;
841 break;
842 }
843 }
844
845 /* set variable type to LINEAR (as they are initialised as FIXED) */
846 varyType = LALINFERENCE_PARAM_LINEAR;
847 LALInferenceSetParamVaryType( threadState->currentParams, parnames->tokens[j], varyType );
848 }
849
850 XLALDestroyTokenList( parnames );
851 XLALDestroyTokenList( tline );
852 }
853
854 XLALDestroyTokenList( tlist );
855 XLALFree( filebuf );
856
857 LALInferenceIFOModel *ifotemp = ifo;
858 while ( ifotemp ) {
859 /* add in variables to say whether phase, sky position and binary parameter are varying */
860 if ( varyphase ) {
861 LALInferenceAddVariable( ifotemp->params, "varyphase", &varyphase, LALINFERENCE_INT4_t, LALINFERENCE_PARAM_FIXED );
862 }
863 if ( varyskypos ) {
864 LALInferenceAddVariable( ifotemp->params, "varyskypos", &varyskypos, LALINFERENCE_INT4_t, LALINFERENCE_PARAM_FIXED );
865 }
866 if ( varybinary ) {
867 LALInferenceAddVariable( ifotemp->params, "varybinary", &varybinary, LALINFERENCE_INT4_t, LALINFERENCE_PARAM_FIXED );
868 }
869 if ( varyglitch ) {
870 LALInferenceAddVariable( ifotemp->params, "varyglitch", &varyglitch, LALINFERENCE_INT4_t, LALINFERENCE_PARAM_FIXED );
871 }
872
873 ifotemp = ifotemp->next;
874 }
875
876 /* now check for a parameter correlation coefficient matrix file */
877 ppt = LALInferenceGetProcParamVal( runState->commandLine, "--cor-file" );
878 if ( ppt ) {
879 CHAR *corFile = XLALStringDuplicate( ppt->value );
881 dims->data[0] = 1;
882 dims->data[1] = 1;
883
884 corMat = XLALCreateREAL8Array( dims );
885
886 corParams = XLALReadTEMPOCorFile( corMat, corFile );
887
888 /* if the correlation matrix is given then add it as the prior for values with Gaussian errors specified in the par file */
889 add_correlation_matrix( threadState->currentParams, runState->priorArgs, corMat, corParams );
890
892 }
893
894 /* check if using a previous nested sampling file as a prior */
895 samples_prior( runState );
896
897 return;
898}
899
900
901/**
902 * \brief Initialise the MCMC proposal distribution for sampling new points
903 *
904 * There are various proposal distributions that can be used to sample new live points via an MCMC. A combination of
905 * different ones can be used to help efficiency for awkward posterior distributions. Here the proposals that can be
906 * used are:
907 * \c diffev Drawing a new point by differential evolution of two randomly chosen live points. All parameters are
908 * evolved during a single draw.
909 * \c freqBinJump Jumps that are the size of the Fourier frequency bins (can be used if searching over frequency).
910 * \c ensembleStretch Ensemble stretch moves (WARNING: These can lead to long autocorrelation lengths).
911 * \c ensembleWalk Ensemble walk moves. These are used as the default proposal.
912 * \c uniformprop Points for any parameters with uniform priors are drawn from those priors
913 *
914 * This function sets up the relative weights with which each of above distributions is used.
915 *
916 * \param runState [in] A pointer to the run state
917 */
919{
920 ProcessParamsTable *ppt = NULL;
921 UINT4 defrac = 0, freqfrac = 0, esfrac = 0, ewfrac = 0, flatfrac = 0;
922
923 ppt = LALInferenceGetProcParamVal( runState->commandLine, "--diffev" );
924 if ( ppt ) {
925 defrac = atoi( ppt->value );
926 } else {
927 defrac = 0; /* default value */
928 }
929
930 ppt = LALInferenceGetProcParamVal( runState->commandLine, "--freqBinJump" );
931 if ( ppt ) {
932 freqfrac = atoi( ppt->value );
933 }
934
935 ppt = LALInferenceGetProcParamVal( runState->commandLine, "--ensembleStretch" );
936 if ( ppt ) {
937 esfrac = atoi( ppt->value );
938 } else {
939 esfrac = 0;
940 }
941
942 ppt = LALInferenceGetProcParamVal( runState->commandLine, "--ensembleWalk" );
943 if ( ppt ) {
944 ewfrac = atoi( ppt->value );
945 } else {
946 ewfrac = 3;
947 }
948
949 ppt = LALInferenceGetProcParamVal( runState->commandLine, "--uniformprop" );
950 if ( ppt ) {
951 flatfrac = atoi( ppt->value );
952 } else {
953 flatfrac = 1;
954 }
955
956 if ( !defrac && !freqfrac && !ewfrac && !esfrac ) {
957 XLAL_ERROR_VOID( XLAL_EFAILED, "All proposal weights are zero!" );
958 }
959
960 /* Single thread here */
961 LALInferenceThreadState *threadState = &runState->threads[0];
962 threadState->cycle = LALInferenceInitProposalCycle();
963 LALInferenceProposalCycle *cycle = threadState->cycle;
964 /* add proposals */
965 if ( defrac ) {
968 defrac );
969 }
970
971 if ( freqfrac ) {
974 freqfrac );
975 }
976
977 /* Use ensemble moves */
978 if ( esfrac ) {
981 esfrac );
982 }
983
984 if ( ewfrac ) {
987 ewfrac );
988 }
989
990 if ( flatfrac ) {
993 flatfrac );
994 }
995
997
998 /* set proposal */
1000 LALInferenceZeroProposalStats( threadState->cycle );
1001}
1002
1003
1004/**
1005 * \brief Adds a correlation matrix for a multi-variate Gaussian prior
1006 *
1007 * If a TEMPO-style parameter correlation coefficient file has been given, then this function will use it to set the
1008 * prior distribution for the given parameters. It is assumed that the equivalent par file contained standard
1009 * deviations for all parameters given in the correlation matrix file, but if the correlation matrix contains more
1010 * parameters they will be ignored.
1011 */
1013 LALStringVector *parMat )
1014{
1015 UINT4 i = 0, j = 0, k = 0;
1016 LALStringVector *newPars = NULL;
1017 gsl_matrix *corMatg = NULL;
1019 UINT4 corsize = corMat->dimLength->data[0];
1020
1021 /* loop through parameters and find ones that have Gaussian priors set - these should match with parameters in the
1022 * correlation coefficient matrix */
1023 for ( i = 0; i < parMat->length; i++ ) {
1024 UINT4 incor = 0;
1025 LALInferenceVariableItem *checkPrior = ini->head;
1026
1027 for ( ; checkPrior ; checkPrior = checkPrior->next ) {
1028 if ( LALInferenceCheckGaussianPrior( priors, checkPrior->name ) ) {
1029 /* ignore parameter name case */
1030 if ( !XLALStringCaseCompare( parMat->data[i], checkPrior->name ) ) {
1031 incor = 1;
1032
1033 /* add parameter to new parameter string vector */
1034 newPars = XLALAppendString2Vector( newPars, parMat->data[i] );
1035 break;
1036 }
1037 }
1038 }
1039
1040 /* if parameter in the corMat did not match one with a Gaussian defined prior, then remove it from the matrix */
1041 if ( incor == 0 ) {
1042 /* remove the ith row and column from corMat, and the ith name from parMat */
1043 /* shift rows up */
1044 for ( j = i + 1; j < corsize; j++ )
1045 for ( k = 0; k < corsize; k++ ) {
1046 corMat->data[( j - 1 )*corsize + k] = corMat->data[j * corsize + k];
1047 }
1048
1049 /* shift columns left */
1050 for ( k = i + 1; k < corsize; k++ )
1051 for ( j = 0; j < corsize; j++ ) {
1052 corMat->data[j * corsize + k - 1] = corMat->data[j * corsize + k];
1053 }
1054 }
1055 }
1056
1057 XLALDestroyUINT4Vector( dims );
1058
1059 /* return new parameter string vector as old one */
1060 XLALDestroyStringVector( parMat );
1061 parMat = newPars;
1062
1063 /* copy the corMat into a gsl_matrix */
1064 corMatg = gsl_matrix_alloc( parMat->length, parMat->length );
1065 for ( i = 0; i < parMat->length; i++ )
1066 for ( j = 0; j < parMat->length; j++ ) {
1067 gsl_matrix_set( corMatg, i, j, corMat->data[i * corsize + j] );
1068 }
1069
1070 /* re-loop over parameters removing Gaussian priors on those in the parMat and replacing with a correlation matrix */
1071 for ( i = 0; i < parMat->length; i++ ) {
1072 LALInferenceVariableItem *checkPrior = ini->head;
1073
1074 /* allocate global variable giving the list of the correlation matrix parameters */
1076
1077 for ( ; checkPrior ; checkPrior = checkPrior->next ) {
1078 if ( LALInferenceCheckGaussianPrior( priors, checkPrior->name ) ) {
1079 if ( !XLALStringCaseCompare( parMat->data[i], checkPrior->name ) ) {
1080 /* get the mean and standard deviation from the Gaussian prior */
1081 REAL8 mu, sigma;
1082 LALInferenceGetGaussianPrior( priors, checkPrior->name, &mu, &sigma );
1083
1084 /* replace it with the correlation matrix as a gsl_matrix */
1085 LALInferenceAddCorrelatedPrior( priors, checkPrior->name, &corMatg, &mu, &sigma, &i );
1086
1087 /* remove the Gaussian prior */
1088 LALInferenceRemoveGaussianPrior( priors, checkPrior->name );
1089
1090 break;
1091 }
1092 }
1093 }
1094 }
1095}
1096
1097
1098/**
1099 * \brief Calculates the sum of the square of the data and model terms
1100 *
1101 * This function calculates the sum of the square of the data and model terms:
1102 * \f[
1103 * \sum_i^N \Re{d_i}^2 + \Im{d_i}^2, \sum_i^N \Re{h_i}^2, \sum_i^N \Im{h_i}^2, \sum_i^N \Re{d_i}\Re{h_i}, \sum_i^N Im{d_i}\Im{h_i}
1104 * \f]
1105 * for each stationary segment given in the \c chunkLength vector. These value are used in the likelihood calculation in
1106 * \c pulsar_log_likelihood and are precomputed here to speed that calculation up.
1107 *
1108 * \param runState [in] The analysis information structure
1109 */
1111{
1112 LALInferenceIFOData *data = runState->data;
1113 LALInferenceIFOModel *ifomodel = runState->threads[0].model->ifo;
1114
1115 UINT4 gaussianLike = 0, roq = 0, nonGR = 0;
1116
1117 if ( LALInferenceGetProcParamVal( runState->commandLine, "--gaussian-like" ) ) {
1118 gaussianLike = 1;
1119 }
1120 if ( LALInferenceGetProcParamVal( runState->commandLine, "--roq" ) ) {
1121 roq = 1;
1122 }
1123 if ( LALInferenceGetProcParamVal( runState->commandLine, "--nonGR" ) ) {
1124 nonGR = 1;
1125 }
1126
1127 while ( data ) {
1128 REAL8Vector *sumdat = NULL;
1129
1130 /* sums of the antenna pattern functions with themeselves and the data.
1131 * These won't be needed if searching over phase parameters, but there's
1132 * no harm in computing them anyway. */
1133
1134 /* also have "explicitly" whitened (i.e. divided by variance) versions of these for use
1135 * by the function to calculate the signal-to-noise ratio (if using the Gaussian
1136 * likelihood the standard versions of these vectors will also be whitened too) */
1137 REAL8Vector *sumP = NULL; /* sum of tensor antenna pattern function a(t)^2 */
1138 REAL8Vector *sumC = NULL; /* sum of tensor antenna pattern function b(t)^2 */
1139 REAL8Vector *sumPWhite = NULL; /* sum of antenna pattern function a(t)^2 */
1140 REAL8Vector *sumCWhite = NULL; /* sum of antenna pattern function b(t)^2 */
1141
1142 /* non-GR values */
1143 REAL8Vector *sumX = NULL; /* sum of vector antenna pattern function a(t)^2 */
1144 REAL8Vector *sumY = NULL; /* sum of vector antenna pattern function b(t)^2 */
1145 REAL8Vector *sumXWhite = NULL; /* whitened version */
1146 REAL8Vector *sumYWhite = NULL; /* whitened version */
1147
1148 REAL8Vector *sumB = NULL; /* sum of scalar antenna pattern function a(t)^2 */
1149 REAL8Vector *sumL = NULL; /* sum of scalar antenna pattern function b(t)^2 */
1150 REAL8Vector *sumBWhite = NULL; /* whitened version */
1151 REAL8Vector *sumLWhite = NULL; /* whitened version */
1152
1153 COMPLEX16Vector *sumDataP = NULL; /* sum of the data * a(t) */
1154 COMPLEX16Vector *sumDataC = NULL; /* sum of the data * b(t) */
1155 COMPLEX16Vector *sumDataX = NULL; /* sum of the data * a(t) */
1156 COMPLEX16Vector *sumDataY = NULL; /* sum of the data * b(t) */
1157 COMPLEX16Vector *sumDataB = NULL; /* sum of the data * a(t) */
1158 COMPLEX16Vector *sumDataL = NULL; /* sum of the data * b(t) */
1159
1160 /* cross terms */
1161 REAL8Vector *sumPC = NULL, *sumPX = NULL, *sumPY = NULL, *sumPB = NULL, *sumPL = NULL;
1162 REAL8Vector *sumCX = NULL, *sumCY = NULL, *sumCB = NULL, *sumCL = NULL;
1163 REAL8Vector *sumXY = NULL, *sumXB = NULL, *sumXL = NULL;
1164 REAL8Vector *sumYB = NULL, *sumYL = NULL;
1165 REAL8Vector *sumBL = NULL;
1166
1167 /* whitened versions cross terms */
1168 REAL8Vector *sumPCWhite = NULL, *sumPXWhite = NULL, *sumPYWhite = NULL, *sumPBWhite = NULL, *sumPLWhite = NULL;
1169 REAL8Vector *sumCXWhite = NULL, *sumCYWhite = NULL, *sumCBWhite = NULL, *sumCLWhite = NULL;
1170 REAL8Vector *sumXYWhite = NULL, *sumXBWhite = NULL, *sumXLWhite = NULL;
1171 REAL8Vector *sumYBWhite = NULL, *sumYLWhite = NULL;
1172 REAL8Vector *sumBLWhite = NULL;
1173
1174 /* get antenna patterns */
1175 REAL8Vector *arespT = *( REAL8Vector ** )LALInferenceGetVariable( ifomodel->params, "a_response_tensor" );
1176 REAL8Vector *brespT = *( REAL8Vector ** )LALInferenceGetVariable( ifomodel->params, "b_response_tensor" );
1177 REAL8Vector *arespV = NULL, *brespV = NULL, *arespS = NULL, *brespS = NULL;
1178
1179 if ( nonGR ) {
1180 arespV = *( REAL8Vector ** )LALInferenceGetVariable( ifomodel->params, "a_response_vector" );
1181 brespV = *( REAL8Vector ** )LALInferenceGetVariable( ifomodel->params, "b_response_vector" );
1182 arespS = *( REAL8Vector ** )LALInferenceGetVariable( ifomodel->params, "a_response_scalar" );
1183 brespS = *( REAL8Vector ** )LALInferenceGetVariable( ifomodel->params, "b_response_scalar" );
1184 }
1185
1186 INT4 tsteps = *( INT4 * )LALInferenceGetVariable( ifomodel->params, "timeSteps" );
1187
1188 INT4 chunkLength = 0, length = 0, i = 0, j = 0, count = 0;
1189 COMPLEX16 B;
1190 REAL8 aT = 0., bT = 0., aV = 0., bV = 0., aS = 0., bS = 0.;
1191
1192 UINT4Vector *chunkLengths;
1193
1194 REAL8Vector *sidDayFrac = *( REAL8Vector ** )LALInferenceGetVariable( ifomodel->params, "siderealDay" );
1195
1196 chunkLengths = *( UINT4Vector ** )LALInferenceGetVariable( ifomodel->params, "chunkLength" );
1197
1198 length = IFO_XTRA_DATA( ifomodel )->times->length + 1 - chunkLengths->data[chunkLengths->length - 1];
1199
1200 sumdat = XLALCreateREAL8Vector( chunkLengths->length );
1201
1202 if ( !roq ) {
1203 /* allocate memory */
1204 sumP = XLALCreateREAL8Vector( chunkLengths->length );
1205 sumC = XLALCreateREAL8Vector( chunkLengths->length );
1206 sumPC = XLALCreateREAL8Vector( chunkLengths->length );
1207 sumDataP = XLALCreateCOMPLEX16Vector( chunkLengths->length );
1208 sumDataC = XLALCreateCOMPLEX16Vector( chunkLengths->length );
1209
1210 sumPWhite = XLALCreateREAL8Vector( chunkLengths->length );
1211 sumCWhite = XLALCreateREAL8Vector( chunkLengths->length );
1212 sumPCWhite = XLALCreateREAL8Vector( chunkLengths->length );
1213
1214 if ( nonGR ) {
1215 sumX = XLALCreateREAL8Vector( chunkLengths->length );
1216 sumY = XLALCreateREAL8Vector( chunkLengths->length );
1217 sumB = XLALCreateREAL8Vector( chunkLengths->length );
1218 sumL = XLALCreateREAL8Vector( chunkLengths->length );
1219
1220 sumPX = XLALCreateREAL8Vector( chunkLengths->length );
1221 sumPY = XLALCreateREAL8Vector( chunkLengths->length );
1222 sumPB = XLALCreateREAL8Vector( chunkLengths->length );
1223 sumPL = XLALCreateREAL8Vector( chunkLengths->length );
1224 sumCX = XLALCreateREAL8Vector( chunkLengths->length );
1225 sumCY = XLALCreateREAL8Vector( chunkLengths->length );
1226 sumCB = XLALCreateREAL8Vector( chunkLengths->length );
1227 sumCL = XLALCreateREAL8Vector( chunkLengths->length );
1228 sumXY = XLALCreateREAL8Vector( chunkLengths->length );
1229 sumXB = XLALCreateREAL8Vector( chunkLengths->length );
1230 sumXL = XLALCreateREAL8Vector( chunkLengths->length );
1231 sumYB = XLALCreateREAL8Vector( chunkLengths->length );
1232 sumYL = XLALCreateREAL8Vector( chunkLengths->length );
1233 sumBL = XLALCreateREAL8Vector( chunkLengths->length );
1234
1235 sumXWhite = XLALCreateREAL8Vector( chunkLengths->length );
1236 sumYWhite = XLALCreateREAL8Vector( chunkLengths->length );
1237 sumBWhite = XLALCreateREAL8Vector( chunkLengths->length );
1238 sumLWhite = XLALCreateREAL8Vector( chunkLengths->length );
1239
1240 sumPXWhite = XLALCreateREAL8Vector( chunkLengths->length );
1241 sumPYWhite = XLALCreateREAL8Vector( chunkLengths->length );
1242 sumPBWhite = XLALCreateREAL8Vector( chunkLengths->length );
1243 sumPLWhite = XLALCreateREAL8Vector( chunkLengths->length );
1244 sumCXWhite = XLALCreateREAL8Vector( chunkLengths->length );
1245 sumCYWhite = XLALCreateREAL8Vector( chunkLengths->length );
1246 sumCBWhite = XLALCreateREAL8Vector( chunkLengths->length );
1247 sumCLWhite = XLALCreateREAL8Vector( chunkLengths->length );
1248 sumXYWhite = XLALCreateREAL8Vector( chunkLengths->length );
1249 sumXBWhite = XLALCreateREAL8Vector( chunkLengths->length );
1250 sumXLWhite = XLALCreateREAL8Vector( chunkLengths->length );
1251 sumYBWhite = XLALCreateREAL8Vector( chunkLengths->length );
1252 sumYLWhite = XLALCreateREAL8Vector( chunkLengths->length );
1253 sumBLWhite = XLALCreateREAL8Vector( chunkLengths->length );
1254
1255 sumDataX = XLALCreateCOMPLEX16Vector( chunkLengths->length );
1256 sumDataY = XLALCreateCOMPLEX16Vector( chunkLengths->length );
1257 sumDataB = XLALCreateCOMPLEX16Vector( chunkLengths->length );
1258 sumDataL = XLALCreateCOMPLEX16Vector( chunkLengths->length );
1259 }
1260 }
1261
1262 REAL8 tsv = LAL_DAYSID_SI / tsteps, T = 0., timeMin = 0., timeMax = 0.;
1263 REAL8 logGaussianNorm = 0.; /* normalisation constant for Gaussian distribution */
1264
1265 for ( i = 0, count = 0 ; i < length ; i += chunkLength, count++ ) {
1266 chunkLength = chunkLengths->data[count];
1267
1268 sumdat->data[count] = 0.;
1269
1270 if ( !roq ) {
1271 sumP->data[count] = 0.;
1272 sumC->data[count] = 0.;
1273 sumPC->data[count] = 0.;
1274 sumDataP->data[count] = 0.;
1275 sumDataC->data[count] = 0.;
1276
1277 sumPWhite->data[count] = 0.;
1278 sumCWhite->data[count] = 0.;
1279 sumPCWhite->data[count] = 0.;
1280
1281 if ( nonGR ) {
1282 sumX->data[count] = 0.;
1283 sumY->data[count] = 0.;
1284 sumB->data[count] = 0.;
1285 sumL->data[count] = 0.;
1286
1287 sumPX->data[count] = 0.;
1288 sumPY->data[count] = 0.;
1289 sumPB->data[count] = 0.;
1290 sumPL->data[count] = 0.;
1291 sumCX->data[count] = 0.;
1292 sumCY->data[count] = 0.;
1293 sumCB->data[count] = 0.;
1294 sumCL->data[count] = 0.;
1295 sumXY->data[count] = 0.;
1296 sumXB->data[count] = 0.;
1297 sumXL->data[count] = 0.;
1298 sumYB->data[count] = 0.;
1299 sumYL->data[count] = 0.;
1300 sumBL->data[count] = 0.;
1301
1302 sumXWhite->data[count] = 0.;
1303 sumYWhite->data[count] = 0.;
1304 sumBWhite->data[count] = 0.;
1305 sumLWhite->data[count] = 0.;
1306
1307 sumPXWhite->data[count] = 0.;
1308 sumPYWhite->data[count] = 0.;
1309 sumPBWhite->data[count] = 0.;
1310 sumPLWhite->data[count] = 0.;
1311 sumCXWhite->data[count] = 0.;
1312 sumCYWhite->data[count] = 0.;
1313 sumCBWhite->data[count] = 0.;
1314 sumCLWhite->data[count] = 0.;
1315 sumXYWhite->data[count] = 0.;
1316 sumXBWhite->data[count] = 0.;
1317 sumXLWhite->data[count] = 0.;
1318 sumYBWhite->data[count] = 0.;
1319 sumYLWhite->data[count] = 0.;
1320 sumBLWhite->data[count] = 0.;
1321
1322 sumDataX->data[count] = 0.;
1323 sumDataY->data[count] = 0.;
1324 sumDataB->data[count] = 0.;
1325 sumDataL->data[count] = 0.;
1326 }
1327 }
1328
1329 for ( j = i ; j < i + chunkLength ; j++ ) {
1330 REAL8 vari = 1., a0 = 0., a1 = 0., b0 = 0., b1 = 0., timeScaled = 0.;
1331 INT4 timebinMin = 0, timebinMax = 0;
1332
1333 B = data->compTimeData->data->data[j];
1334
1335 /* if using a Gaussian likelihood divide all these values by the variance */
1336 if ( gaussianLike ) {
1337 vari = data->varTimeData->data->data[j];
1338 logGaussianNorm -= log( LAL_TWOPI * vari );
1339 }
1340
1341 /* sum up the data */
1342 sumdat->data[count] += ( creal( B ) * creal( B ) + cimag( B ) * cimag( B ) ) / vari;
1343
1344 if ( !roq ) {
1345 /* set the time bin for the lookup table and interpolate between bins */
1346 T = sidDayFrac->data[j];
1347 timebinMin = ( INT4 )fmod( floor( T / tsv ), tsteps );
1348 timeMin = timebinMin * tsv;
1349 timebinMax = ( INT4 )fmod( timebinMin + 1, tsteps );
1350 timeMax = timeMin + tsv;
1351
1352 /* get values of vector for linear interpolation */
1353 a0 = arespT->data[timebinMin];
1354 a1 = arespT->data[timebinMax];
1355 b0 = brespT->data[timebinMin];
1356 b1 = brespT->data[timebinMax];
1357
1358 /* rescale time for linear interpolation on a unit square */
1359 timeScaled = ( T - timeMin ) / ( timeMax - timeMin );
1360
1361 aT = a0 + ( a1 - a0 ) * timeScaled;
1362 bT = b0 + ( b1 - b0 ) * timeScaled;
1363
1364 /* sum up the other terms */
1365 sumP->data[count] += aT * aT / vari;
1366 sumC->data[count] += bT * bT / vari;
1367 sumPC->data[count] += aT * bT / vari;
1368 sumDataP->data[count] += B * aT / vari;
1369 sumDataC->data[count] += B * bT / vari;
1370
1371 /* non-GR values */
1372 if ( nonGR ) {
1373 a0 = arespV->data[timebinMin];
1374 a1 = arespV->data[timebinMax];
1375 b0 = brespV->data[timebinMin];
1376 b1 = brespV->data[timebinMax];
1377
1378 aV = a0 + ( a1 - a0 ) * timeScaled;
1379 bV = b0 + ( b1 - b0 ) * timeScaled;
1380
1381 a0 = arespS->data[timebinMin];
1382 a1 = arespS->data[timebinMax];
1383 b0 = brespS->data[timebinMin];
1384 b1 = brespS->data[timebinMax];
1385
1386 aS = a0 + ( a1 - a0 ) * timeScaled;
1387 bS = b0 + ( b1 - b0 ) * timeScaled;
1388
1389 sumX->data[count] += aV * aV / vari;
1390 sumY->data[count] += bV * bV / vari;
1391 sumB->data[count] += aS * aS / vari;
1392 sumL->data[count] += bS * bS / vari;
1393
1394 sumPX->data[count] += aT * aV / vari;
1395 sumPY->data[count] += aT * bV / vari;
1396 sumPB->data[count] += aT * aS / vari;
1397 sumPL->data[count] += aT * bS / vari;
1398 sumCX->data[count] += bT * aV / vari;
1399 sumCY->data[count] += bT * bV / vari;
1400 sumCB->data[count] += bT * aS / vari;
1401 sumCL->data[count] += bT * bS / vari;
1402 sumXY->data[count] += aV * bV / vari;
1403 sumXB->data[count] += aV * aS / vari;
1404 sumXL->data[count] += aV * bS / vari;
1405 sumYB->data[count] += bV * aS / vari;
1406 sumYL->data[count] += bV * bS / vari;
1407 sumBL->data[count] += aS * bS / vari;
1408
1409 sumDataX->data[count] += B * aV / vari;
1410 sumDataY->data[count] += B * bV / vari;
1411 sumDataB->data[count] += B * aS / vari;
1412 sumDataL->data[count] += B * bS / vari;
1413 }
1414
1415 /* get "explicitly whitened" versions, i.e. for use in signal-to-noise ratio
1416 * calculations even when not using a Gaussian likelihood */
1417 vari = data->varTimeData->data->data[j];
1418 sumPWhite->data[count] += aT * aT / vari;
1419 sumCWhite->data[count] += bT * bT / vari;
1420 sumPCWhite->data[count] += aT * bT / vari;
1421
1422 /* non-GR values */
1423 if ( nonGR ) {
1424 sumXWhite->data[count] += aV * aV / vari;
1425 sumYWhite->data[count] += bV * bV / vari;
1426 sumBWhite->data[count] += aS * aS / vari;
1427 sumLWhite->data[count] += bS * bS / vari;
1428
1429 sumPXWhite->data[count] += aT * aV / vari;
1430 sumPYWhite->data[count] += aT * bV / vari;
1431 sumPBWhite->data[count] += aT * aS / vari;
1432 sumPLWhite->data[count] += aT * bS / vari;
1433 sumCXWhite->data[count] += bT * aV / vari;
1434 sumCYWhite->data[count] += bT * bV / vari;
1435 sumCBWhite->data[count] += bT * aS / vari;
1436 sumCLWhite->data[count] += bT * bS / vari;
1437 sumXYWhite->data[count] += aV * bV / vari;
1438 sumXBWhite->data[count] += aV * aS / vari;
1439 sumXLWhite->data[count] += aV * bS / vari;
1440 sumYBWhite->data[count] += bV * aS / vari;
1441 sumYLWhite->data[count] += bV * bS / vari;
1442 sumBLWhite->data[count] += aS * bS / vari;
1443 }
1444 }
1445 }
1446 }
1447
1448 /* add all the summed data values - remove if already there, so that sum_data can be called more
1449 * than once if required e.g. if needed in the injection functions */
1450
1451 check_and_add_fixed_variable( ifomodel->params, "sumData", &sumdat, LALINFERENCE_REAL8Vector_t );
1452
1453 if ( !roq ) {
1456 check_and_add_fixed_variable( ifomodel->params, "sumPC", &sumPC, LALINFERENCE_REAL8Vector_t );
1457 check_and_add_fixed_variable( ifomodel->params, "sumDataP", &sumDataP, LALINFERENCE_COMPLEX16Vector_t );
1458 check_and_add_fixed_variable( ifomodel->params, "sumDataC", &sumDataC, LALINFERENCE_COMPLEX16Vector_t );
1459 check_and_add_fixed_variable( ifomodel->params, "sumPWhite", &sumPWhite, LALINFERENCE_REAL8Vector_t );
1460 check_and_add_fixed_variable( ifomodel->params, "sumCWhite", &sumCWhite, LALINFERENCE_REAL8Vector_t );
1461 check_and_add_fixed_variable( ifomodel->params, "sumPCWhite", &sumPCWhite, LALINFERENCE_REAL8Vector_t );
1462
1463 if ( nonGR ) {
1468
1469 check_and_add_fixed_variable( ifomodel->params, "sumPX", &sumPX, LALINFERENCE_REAL8Vector_t );
1470 check_and_add_fixed_variable( ifomodel->params, "sumPY", &sumPY, LALINFERENCE_REAL8Vector_t );
1471 check_and_add_fixed_variable( ifomodel->params, "sumPB", &sumPB, LALINFERENCE_REAL8Vector_t );
1472 check_and_add_fixed_variable( ifomodel->params, "sumPL", &sumPL, LALINFERENCE_REAL8Vector_t );
1473 check_and_add_fixed_variable( ifomodel->params, "sumCX", &sumCX, LALINFERENCE_REAL8Vector_t );
1474 check_and_add_fixed_variable( ifomodel->params, "sumCY", &sumCY, LALINFERENCE_REAL8Vector_t );
1475 check_and_add_fixed_variable( ifomodel->params, "sumCB", &sumCB, LALINFERENCE_REAL8Vector_t );
1476 check_and_add_fixed_variable( ifomodel->params, "sumCL", &sumCL, LALINFERENCE_REAL8Vector_t );
1477 check_and_add_fixed_variable( ifomodel->params, "sumXY", &sumXY, LALINFERENCE_REAL8Vector_t );
1478 check_and_add_fixed_variable( ifomodel->params, "sumXB", &sumXB, LALINFERENCE_REAL8Vector_t );
1479 check_and_add_fixed_variable( ifomodel->params, "sumXL", &sumXL, LALINFERENCE_REAL8Vector_t );
1480 check_and_add_fixed_variable( ifomodel->params, "sumYB", &sumYB, LALINFERENCE_REAL8Vector_t );
1481 check_and_add_fixed_variable( ifomodel->params, "sumYL", &sumYL, LALINFERENCE_REAL8Vector_t );
1482 check_and_add_fixed_variable( ifomodel->params, "sumBL", &sumBL, LALINFERENCE_REAL8Vector_t );
1483
1484 check_and_add_fixed_variable( ifomodel->params, "sumDataX", &sumDataX, LALINFERENCE_COMPLEX16Vector_t );
1485 check_and_add_fixed_variable( ifomodel->params, "sumDataY", &sumDataY, LALINFERENCE_COMPLEX16Vector_t );
1486 check_and_add_fixed_variable( ifomodel->params, "sumDataB", &sumDataB, LALINFERENCE_COMPLEX16Vector_t );
1487 check_and_add_fixed_variable( ifomodel->params, "sumDataL", &sumDataL, LALINFERENCE_COMPLEX16Vector_t );
1488
1489 check_and_add_fixed_variable( ifomodel->params, "sumXWhite", &sumXWhite, LALINFERENCE_REAL8Vector_t );
1490 check_and_add_fixed_variable( ifomodel->params, "sumYWhite", &sumYWhite, LALINFERENCE_REAL8Vector_t );
1491 check_and_add_fixed_variable( ifomodel->params, "sumBWhite", &sumBWhite, LALINFERENCE_REAL8Vector_t );
1492 check_and_add_fixed_variable( ifomodel->params, "sumLWhite", &sumLWhite, LALINFERENCE_REAL8Vector_t );
1493
1494 check_and_add_fixed_variable( ifomodel->params, "sumPXWhite", &sumPXWhite, LALINFERENCE_REAL8Vector_t );
1495 check_and_add_fixed_variable( ifomodel->params, "sumPYWhite", &sumPYWhite, LALINFERENCE_REAL8Vector_t );
1496 check_and_add_fixed_variable( ifomodel->params, "sumPBWhite", &sumPBWhite, LALINFERENCE_REAL8Vector_t );
1497 check_and_add_fixed_variable( ifomodel->params, "sumPLWhite", &sumPLWhite, LALINFERENCE_REAL8Vector_t );
1498 check_and_add_fixed_variable( ifomodel->params, "sumCXWhite", &sumCXWhite, LALINFERENCE_REAL8Vector_t );
1499 check_and_add_fixed_variable( ifomodel->params, "sumCYWhite", &sumCYWhite, LALINFERENCE_REAL8Vector_t );
1500 check_and_add_fixed_variable( ifomodel->params, "sumCBWhite", &sumCBWhite, LALINFERENCE_REAL8Vector_t );
1501 check_and_add_fixed_variable( ifomodel->params, "sumCLWhite", &sumCLWhite, LALINFERENCE_REAL8Vector_t );
1502 check_and_add_fixed_variable( ifomodel->params, "sumXYWhite", &sumXYWhite, LALINFERENCE_REAL8Vector_t );
1503 check_and_add_fixed_variable( ifomodel->params, "sumXBWhite", &sumXBWhite, LALINFERENCE_REAL8Vector_t );
1504 check_and_add_fixed_variable( ifomodel->params, "sumXLWhite", &sumXLWhite, LALINFERENCE_REAL8Vector_t );
1505 check_and_add_fixed_variable( ifomodel->params, "sumYBWhite", &sumYBWhite, LALINFERENCE_REAL8Vector_t );
1506 check_and_add_fixed_variable( ifomodel->params, "sumYLWhite", &sumYLWhite, LALINFERENCE_REAL8Vector_t );
1507 check_and_add_fixed_variable( ifomodel->params, "sumBLWhite", &sumBLWhite, LALINFERENCE_REAL8Vector_t );
1508 }
1509 } else { /* add parameter defining the usage of RQO here (as this is after any injection generation, which would fail if this was set */
1511 }
1512
1513 LALInferenceAddVariable( ifomodel->params, "logGaussianNorm", &logGaussianNorm, LALINFERENCE_REAL8_t, LALINFERENCE_PARAM_FIXED );
1514
1515 data = data->next;
1516 ifomodel = ifomodel->next;
1517 }
1518
1519 return;
1520}
1521
1522/**
1523 * \brief Parse data from a prior file containing Gaussian Mixture Model mean values
1524 *
1525 * If a Gaussian Mixture Model prior has been specified then this function will parse
1526 * the means for each parameter for each mode given. E.g. if the GMM provides multivariate
1527 * Gaussian modes for two parameters, x and y, then the means would be specified in a string
1528 * of the form "[[mux1,muy1],[mux2,muy2],....]". The string should have no whitespace between
1529 * values, and mean values for a given mode must be separated by a comma.
1530 *
1531 * These values are returned in an vector of REAL8Vectors. If an error occurred then NULL will be returned.
1532 *
1533 * \param meanstr [in] A string containing the mean values
1534 * \param npars [in] The number of parameters
1535 * \param nmodes [in] The number of modes
1536 */
1537REAL8Vector **parse_gmm_means( CHAR *meanstr, UINT4 npars, UINT4 nmodes )
1538{
1539 UINT4 modecount = 0;
1540
1541 /* parse mean string */
1542 CHAR *startloc = strchr( meanstr, '[' ); /* find location of first '[' */
1543 if ( !startloc ) {
1544 return NULL;
1545 }
1546
1547 CHAR strpart[16384]; /* string to hold elements */
1548
1549 /* allocate memory for returned value */
1550 REAL8Vector **meanmat;
1551 meanmat = XLALCalloc( nmodes, sizeof( REAL8Vector * ) );
1552
1553 while ( 1 ) {
1554 CHAR *closeloc = get_bracketed_string( strpart, startloc + 1, '[', ']' );
1555 if ( !strpart[0] ) {
1556 break; /* break when no more bracketed items are found */
1557 }
1558
1559 /* get mean values (separated by commas) */
1560 TokenList *meantoc = NULL;
1561 XLALCreateTokenList( &meantoc, strpart, "," );
1562 if ( meantoc->nTokens != npars ) {
1563 XLAL_PRINT_WARNING( "Warning... number of means parameters specified for GMM is not consistent with number of parameters.\n" );
1564 for ( INT4 k = modecount - 1; k > -1; k-- ) {
1565 XLALDestroyREAL8Vector( meanmat[k] );
1566 }
1567 XLALFree( meanmat );
1568 return NULL;
1569 }
1570 meanmat[modecount] = XLALCreateREAL8Vector( npars );
1571 for ( UINT4 j = 0; j < meantoc->nTokens; j++ ) {
1572 meanmat[modecount]->data[j] = atof( meantoc->tokens[j] );
1573 }
1574
1575 startloc = closeloc;
1576 modecount++;
1577 XLALDestroyTokenList( meantoc );
1578 }
1579
1580 if ( modecount != nmodes ) {
1581 XLAL_PRINT_WARNING( "Warning... number of means values specified for GMM is not consistent with number of modes.\n" );
1582 for ( INT4 k = modecount - 1; k > -1; k-- ) {
1583 XLALDestroyREAL8Vector( meanmat[k] );
1584 }
1585 XLALFree( meanmat );
1586 meanmat = NULL;
1587 }
1588
1589 return meanmat;
1590}
1591
1592/**
1593 * \brief Parse data from a prior file containing Gaussian Mixture Model covariance matrix values
1594 *
1595 * If a Gaussian Mixture Model prior has been specified then this function will parse
1596 * the covariance matrices for each mode given. E.g. if the GMM provides multivariate
1597 * Gaussian modes for two parameters, x and y, then the covariances for each mode would
1598 * be specified in a string of the form "[[[covxx1,covxy1][covyx1,covyy1]],[[covxx2,covxy2][covyx2,covyy2]],...]".
1599 * The string should have no whitespace between values, and covariance values for a given mode must be
1600 * separated by a comma.
1601 *
1602 * These values are returned in an array of GSL matrices. If an error occurred then NULL will be returned.
1603 *
1604 * \param covstr [in] A string containing the covariance matrix values
1605 * \param npars [in] The number of parameters
1606 * \param nmodes [in] The number of modes
1607 */
1608gsl_matrix **parse_gmm_covs( CHAR *covstr, UINT4 npars, UINT4 nmodes )
1609{
1610 UINT4 modecount = 0;
1611
1612 /* parse covariance string */
1613 CHAR *startloc = strchr( covstr, '[' ); /* find location of first '[' */
1614 if ( !startloc ) {
1615 return NULL;
1616 }
1617
1618 CHAR strpart[16384]; /* string to hold elements */
1619
1620 /* allocate memory for returned value */
1621 gsl_matrix **covmat;
1622 covmat = XLALCalloc( nmodes, sizeof( gsl_matrix * ) );
1623
1624 while ( 1 ) {
1625 CHAR *openloc = strstr( startloc + 1, "[[" ); /* find next "[[" */
1626 /* break if there are no more opening brackets */
1627 if ( !openloc ) {
1628 break;
1629 }
1630
1631 CHAR *closeloc = strstr( openloc + 1, "]]" ); /* find next "]]" */
1632 if ( !closeloc ) {
1633 break;
1634 }
1635
1636 strncpy( strpart, openloc + 1, ( closeloc - openloc ) ); /* copy string */
1637 strpart[( closeloc - openloc )] = '\0'; /* add null terminating character */
1638
1639 CHAR *newstartloc = strpart;
1640
1641 UINT4 parcount = 0;
1642
1643 covmat[modecount] = gsl_matrix_alloc( npars, npars );
1644
1645 while ( 1 ) {
1646 CHAR newstrpart[8192];
1647 CHAR *newcloseloc = get_bracketed_string( newstrpart, newstartloc, '[', ']' );
1648
1649 if ( !newstrpart[0] ) {
1650 break; /* read all of covariance matrix for this mode */
1651 }
1652
1653 if ( parcount > npars ) {
1654 XLAL_PRINT_WARNING( "Warning... number of covariance parameters specified for GMM is not consistent with number of parameters.\n" );
1655 for ( INT4 k = modecount; k > -1; k-- ) {
1656 gsl_matrix_free( covmat[k] );
1657 }
1658 XLALFree( covmat );
1659 return NULL;
1660 }
1661
1662 newstartloc = newcloseloc;
1663
1664 /* get covariance values (separated by commas) */
1665 TokenList *covtoc = NULL;
1666 XLALCreateTokenList( &covtoc, newstrpart, "," );
1667 if ( covtoc->nTokens != npars ) {
1668 XLAL_PRINT_WARNING( "Warning... number of means parameters specified for GMM is not consistent with number of parameters.\n" );
1669 for ( INT4 k = modecount; k > -1; k-- ) {
1670 gsl_matrix_free( covmat[k] );
1671 }
1672 XLALFree( covmat );
1673 return NULL;
1674 }
1675
1676 for ( UINT4 j = 0; j < covtoc->nTokens; j++ ) {
1677 gsl_matrix_set( covmat[modecount], parcount, j, atof( covtoc->tokens[j] ) );
1678 }
1679 XLALDestroyTokenList( covtoc );
1680
1681 parcount++;
1682 }
1683 startloc = closeloc;
1684 modecount++;
1685 }
1686
1687 if ( modecount != nmodes ) {
1688 XLAL_PRINT_WARNING( "Warning... number of means values specified for GMM is not consistent with number of modes.\n" );
1689 for ( INT4 k = modecount; k > -1; k-- ) {
1690 gsl_matrix_free( covmat[k] );
1691 }
1692 XLALFree( covmat );
1693 covmat = NULL;
1694 }
1695
1696 return covmat;
1697}
1698
1699
1700CHAR *get_bracketed_string( CHAR *dest, const CHAR *bstr, int openbracket, int closebracket )
1701{
1702 /* get positions of opening and closing brackets */
1703 CHAR *openpar = strchr( bstr, openbracket );
1704 CHAR *closepar = strchr( bstr + 1, closebracket );
1705
1706 if ( !openpar || !closepar ) {
1707 dest[0] = 0;
1708 return NULL;
1709 }
1710
1711 strncpy( dest, openpar + 1, ( closepar - openpar ) - 1 );
1712 dest[( closepar - openpar ) - 1] = '\0';
1713
1714 /* return pointer the the location after the closing brackets */
1715 return closepar + 1;
1716}
1717
1718
1720{
1721 INT4 i, randomseed;
1723 for ( i = 0; i < nthreads; i++ ) {
1724 thread = &state->threads[i];
1725 //LALInferenceCopyVariables(thread->model->params, thread->currentParams);
1728 thread->GSLrandom = gsl_rng_alloc( gsl_rng_mt19937 );
1729 randomseed = gsl_rng_get( state->GSLrandom );
1730 gsl_rng_set( thread->GSLrandom, randomseed );
1731
1732 /* Explicitly zero out DE, in case it's not used later */
1733 thread->differentialPoints = NULL;
1734 thread->differentialPointsLength = 0;
1735 thread->differentialPointsSize = 0;
1736 }
1737}
#define __func__
log an I/O error, i.e.
ProcessParamsTable * ppt
int j
int k
const double b1
const double b0
#define c
const REAL8Vector * PulsarGetREAL8VectorParam(const PulsarParameters *pars, const CHAR *name)
Return a REAL8Vector parameter.
int PulsarCheckParam(const PulsarParameters *pars, const CHAR *name)
Check for the existence of the parameter name in the PulsarParameters structure.
LALStringVector * XLALReadTEMPOCorFile(REAL8Array *cormat, CHAR *corfile)
This function will read in a TEMPO-style parameter correlation matrix.
REAL8 PulsarGetREAL8Param(const PulsarParameters *pars, const CHAR *name)
Return a REAL8 parameter.
const CHAR * PulsarGetStringParam(const PulsarParameters *pars, const CHAR *name)
Return a string parameter.
REAL8 PulsarGetREAL8VectorParamIndividual(const PulsarParameters *pars, const CHAR *name)
Return an individual REAL8 value from a REAL8Vector parameter.
UINT2 B
Definition: SFTnaming.c:47
#define fprintf
void XLALDestroyTokenList(TokenList *list)
int XLALCreateTokenList(TokenList **list, const CHAR *string, const CHAR *delimiters)
REAL8Array * XLALCreateREAL8Array(UINT4Vector *)
char * XLALFileLoad(const char *path)
#define LAL_DAYSID_SI
#define LAL_TWOPI
double complex COMPLEX16
double REAL8
char CHAR
uint32_t UINT4
int32_t INT4
void LALInferenceAddVariable(LALInferenceVariables *vars, const char *name, const void *value, LALInferenceVariableType type, LALInferenceParamVaryType vary)
LALInferenceThreadState * LALInferenceInitThreads(INT4 nthreads)
REAL8 LALInferenceGetREAL8Variable(LALInferenceVariables *vars, const char *name)
LALInferenceParamVaryType
void LALInferenceCopyVariables(LALInferenceVariables *origin, LALInferenceVariables *target)
void * LALInferenceGetVariable(const LALInferenceVariables *vars, const char *name)
ProcessParamsTable * LALInferenceGetProcParamVal(ProcessParamsTable *procparams, const char *name)
void LALInferenceSetParamVaryType(LALInferenceVariables *vars, const char *name, LALInferenceParamVaryType vary)
void LALInferenceLogSampleToArray(LALInferenceVariables *algorithmParams, LALInferenceVariables *vars)
int LALInferenceCheckVariable(LALInferenceVariables *vars, const char *name)
LALINFERENCE_PARAM_OUTPUT
LALINFERENCE_PARAM_FIXED
LALINFERENCE_PARAM_LINEAR
LALINFERENCE_string_t
LALINFERENCE_REAL8_t
LALINFERENCE_INT4_t
LALINFERENCE_REAL8Vector_t
LALINFERENCE_void_ptr_t
LALINFERENCE_UINT4_t
LALINFERENCE_COMPLEX16Vector_t
void LALInferenceNestedSamplingAlgorithm(LALInferenceRunState *runState)
void LALInferenceSetupLivePointsArray(LALInferenceRunState *runState)
void LALInferenceAddGaussianPrior(LALInferenceVariables *priorArgs, const char *name, REAL8 *mu, REAL8 *sigma, LALInferenceVariableType type)
void LALInferenceAddGMMPrior(LALInferenceVariables *priorArgs, const char *name, REAL8Vector ***mus, gsl_matrix ***covs, REAL8Vector **weights, REAL8Vector **minrange, REAL8Vector **maxrange)
void LALInferenceAddMinMaxPrior(LALInferenceVariables *priorArgs, const char *name, REAL8 *min, REAL8 *max, LALInferenceVariableType type)
void LALInferenceAddLogUniformPrior(LALInferenceVariables *priorArgs, const char *name, REAL8 *xmin, REAL8 *xmax, LALInferenceVariableType type)
void LALInferenceRemoveGaussianPrior(LALInferenceVariables *priorArgs, const char *name)
int LALInferenceCheckGaussianPrior(LALInferenceVariables *priorArgs, const char *name)
void LALInferenceGetGaussianPrior(LALInferenceVariables *priorArgs, const char *name, REAL8 *mu, REAL8 *sigma)
void LALInferenceAddCorrelatedPrior(LALInferenceVariables *priorArgs, const char *name, gsl_matrix **cor, REAL8 *mu, REAL8 *sigma, UINT4 *idx)
void LALInferenceAddFermiDiracPrior(LALInferenceVariables *priorArgs, const char *name, REAL8 *sigma, REAL8 *r, LALInferenceVariableType type)
REAL8 LALInferenceEnsembleStretchFull(LALInferenceThreadState *thread, LALInferenceVariables *cp, LALInferenceVariables *proposedParams)
const char *const frequencyBinJumpName
REAL8 LALInferenceEnsembleWalkFull(LALInferenceThreadState *thread, LALInferenceVariables *cp, LALInferenceVariables *proposedParams)
const char *const drawFlatPriorName
void LALInferenceAddProposalToCycle(LALInferenceProposalCycle *cycle, LALInferenceProposal *prop, INT4 weight)
void LALInferenceZeroProposalStats(LALInferenceProposalCycle *cycle)
const char *const ensembleWalkFullName
void LALInferenceRandomizeProposalCycle(LALInferenceProposalCycle *cycle, gsl_rng *rng)
LALInferenceProposal * LALInferenceInitProposal(LALInferenceProposalFunction func, const char *name)
REAL8 LALInferenceDifferentialEvolutionFull(LALInferenceThreadState *thread, LALInferenceVariables *currentParams, LALInferenceVariables *proposedParams)
REAL8 LALInferenceDrawFlatPrior(LALInferenceThreadState *thread, LALInferenceVariables *currentParams, LALInferenceVariables *proposedParams)
LALInferenceProposalCycle * LALInferenceInitProposalCycle(void)
REAL8 LALInferenceCyclicProposal(LALInferenceThreadState *thread, LALInferenceVariables *currentParams, LALInferenceVariables *proposedParams)
const char *const ensembleStretchFullName
REAL8 LALInferenceFrequencyBinJump(LALInferenceThreadState *thread, LALInferenceVariables *currentParams, LALInferenceVariables *proposedParams)
const char *const differentialEvolutionFullName
void * XLALCalloc(size_t m, size_t n)
void XLALFree(void *p)
char char * XLALStringDuplicate(const char *s)
int XLALStringCaseCompare(const char *s1, const char *s2)
int char * XLALStringAppend(char *s, const char *append)
void XLALDestroyStringVector(LALStringVector *vect)
LALStringVector * XLALAppendString2Vector(LALStringVector *vect, const CHAR *string)
void XLALDestroyUINT4Vector(UINT4Vector *vector)
REAL8Vector * XLALCreateREAL8Vector(UINT4 length)
COMPLEX16Vector * XLALCreateCOMPLEX16Vector(UINT4 length)
void XLALDestroyREAL8Vector(REAL8Vector *vector)
UINT4Vector * XLALCreateUINT4Vector(UINT4 length)
#define XLAL_ERROR_VOID(...)
#define XLAL_PRINT_WARNING(...)
XLAL_SUCCESS
XLAL_EFUNC
XLAL_EIO
XLAL_EINVAL
XLAL_EFAILED
REAL8 XLALGPSGetREAL8(const LIGOTimeGPS *epoch)
long long count
Definition: hwinject.c:371
float data[BLOCKSIZE]
Definition: hwinject.c:360
list mu
T
CHAR * get_bracketed_string(CHAR *dest, const CHAR *bstr, int openbracket, int closebracket)
Definition: ppe_init.c:1700
void initialise_proposal(LALInferenceRunState *runState)
Initialise the MCMC proposal distribution for sampling new points.
Definition: ppe_init.c:918
void add_correlation_matrix(LALInferenceVariables *ini, LALInferenceVariables *priors, REAL8Array *corMat, LALStringVector *parMat)
Adds a correlation matrix for a multi-variate Gaussian prior.
Definition: ppe_init.c:1012
static const CHAR a2A[256]
Array for conversion from lowercase to uppercase.
Definition: ppe_init.c:28
static void strtoupper(CHAR *s)
Convert string to uppercase.
Definition: ppe_init.c:37
void initialise_prior(LALInferenceRunState *runState)
Sets up the parameters to be searched over and their prior ranges.
Definition: ppe_init.c:620
void sum_data(LALInferenceRunState *runState)
Calculates the sum of the square of the data and model terms.
Definition: ppe_init.c:1110
void add_initial_variables(LALInferenceVariables *ini, PulsarParameters *pars)
Set up all the allowed variables for a known pulsar search This functions sets up all possible variab...
Definition: ppe_init.c:386
void setup_lookup_tables(LALInferenceRunState *runState, LALSource *source)
Sets the time angle antenna response lookup table.
Definition: ppe_init.c:296
void initialise_threads(LALInferenceRunState *state, INT4 nthreads)
Definition: ppe_init.c:1719
void initialise_algorithm(LALInferenceRunState *runState)
Initialises the nested sampling algorithm control.
Definition: ppe_init.c:138
gsl_matrix ** parse_gmm_covs(CHAR *covstr, UINT4 npars, UINT4 nmodes)
Parse data from a prior file containing Gaussian Mixture Model covariance matrix values.
Definition: ppe_init.c:1608
void setup_live_points_array_wrapper(LALInferenceRunState *runState)
A wrapper around LALInferenceSetupLivePointsArray.
Definition: ppe_init.c:100
void nested_sampling_algorithm_wrapper(LALInferenceRunState *runState)
A wrapper around LALInferenceNestedSamplingAlgorithm.
Definition: ppe_init.c:57
REAL8Vector ** parse_gmm_means(CHAR *meanstr, UINT4 npars, UINT4 nmodes)
Parse data from a prior file containing Gaussian Mixture Model mean values.
Definition: ppe_init.c:1537
Header file for the initialisation functions for the parameter estimation code for known pulsar searc...
void add_variable_parameter(PulsarParameters *params, LALInferenceVariables *var, const CHAR *parname, LALInferenceParamVaryType vary)
Add a REAL8 parameter from a PulsarParameters variable into a LALInferenceVariable.
Definition: ppe_models.c:376
void response_lookup_table(REAL8 t0, LALDetAndSource detNSource, INT4 timeSteps, REAL8 avedt, REAL8Vector *aT, REAL8Vector *bT, REAL8Vector *aV, REAL8Vector *bV, REAL8Vector *aS, REAL8Vector *bS)
Creates a lookup table of the detector antenna pattern.
Definition: ppe_models.c:1285
#define IFO_XTRA_DATA(ifo)
Definition: ppe_models.h:35
void samples_prior(LALInferenceRunState *runState)
Read in an ascii text file of nested samples, convert to posterior samples and create k-d tree.
void check_and_add_fixed_variable(LALInferenceVariables *vars, const char *name, void *value, LALInferenceVariableType type)
Add a variable, checking first if it already exists and is of type LALINFERENCE_PARAM_FIXED and if so...
Definition: ppe_utils.c:778
LALStringVector * corlist
#define NUMGLITCHPARS
The total number of glitch parameters that can define a signal.
#define NUMSKYPARS
The total number of sky position parameters that can define a signal e.g.
static const CHAR binpars[NUMBINPARS][VARNAME_MAX]
A list of the binary system parameters.
#define TIMEBINS
Default number of bins in time (over one sidereal day) for the time vs.
#define NUMAMPPARS
The total number of 'amplitude' parameters that can define a signal e.g.
static const CHAR amppars[NUMAMPPARS][VARNAME_MAX]
A list of the amplitude parameters.
static const CHAR glitchpars[NUMGLITCHPARS][VARNAME_MAX]
A list of the glitch parameters.
#define NUMBINPARS
The total number of binary system parameters that can define a signal e.g.
static const CHAR skypars[NUMSKYPARS][VARNAME_MAX]
A list of the sky position parameters.
double t0
COMPLEX16 * data
LALSource * pSource
const LALDetector * pDetector
struct tagLALInferenceIFOData * next
struct tagLALInferenceIFOModel * next
LALInferenceVariables * params
LALInferenceIFOModel * ifo
ProcessParamsTable * commandLine
LALInferenceVariables * proposalArgs
LALInferenceVariables * algorithmParams
struct tagLALInferenceIFOData * data
LALInferenceVariables * priorArgs
LALInferenceThreadState * threads
LALInferenceLogFunction logsample
LALInferenceVariables * currentParams
LALInferenceVariables * priorArgs
LALInferenceModel * model
LALInferenceProposalFunction proposal
LALInferenceProposalCycle * cycle
LALInferenceVariables * proposalArgs
LALInferenceVariables ** differentialPoints
char name[VARNAME_MAX]
struct tagVariableItem * next
LALInferenceVariableItem * head
The PulsarParameters structure to contain a set of pulsar parameters.
UINT4Vector * dimLength
REAL8 * data
REAL8 * data
CHAR ** tokens
UINT4 nTokens
UINT4 * data