LALInference 4.1.9.1-eeff03c
LALInferenceKombineSampler.c
Go to the documentation of this file.
1/*
2 * LALInferenceKombineSampler.c: Bayesian Followup, ensemble-sampling algorithm.
3 *
4 * Copyright (C) 2014 Ben Farr
5 *
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with with program; see the file COPYING. If not, write to the
19 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
20 * MA 02110-1301 USA
21 */
22
23#include <mpi.h>
24#include <lal/Date.h>
25#include <lal/LALInference.h>
27#include <lal/LALInferenceProposal.h>
28#include <lal/LALInferenceClusteredKDE.h>
29
30#include <lal/LALInferenceVCSInfo.h>
31
32#define PROGRAM_NAME "LALInferenceKombineSampler.c"
33#define CVS_ID_STRING "$Id$"
34#define CVS_REVISION "$Revision$"
35#define CVS_SOURCE "$Source$"
36#define CVS_DATE "$Date$"
37#define CVS_NAME_STRING "$Name$"
38
39#ifndef _OPENMP
40#define omp ignore
41#endif
42
43
44void ensemble_sampler(struct tagLALInferenceRunState *run_state) {
45 INT4 mpi_rank, mpi_size;
46 INT4 walker, nwalkers_per_thread;
47 INT4 nsteps, skip, tracking_interval, update_interval, verbose;
48 INT4 *step, i;
49 INT4 *acceptance_buffer;
50 REAL8 *acceptance_rates;
51 REAL8 acceptance_rate, min_acceptance_rate, max_acceptance_rate;
52 REAL8 *prop_priors, *prop_likelihoods, *prop_densities;
53 INT4 update = 0;
54 FILE *output = NULL;
56
57 /* Initialize LIGO status */
59 memset(&status, 0, sizeof(status));
60
61 /* Determine number of MPI threads, and this thread's rank */
62 MPI_Comm_rank(MPI_COMM_WORLD, &mpi_rank);
63 MPI_Comm_size(MPI_COMM_WORLD, &mpi_size);
64
65 /* Parameters controlling output and ensemble update frequency */
66 LALInferenceVariables *algorithm_params = run_state->algorithmParams;
67 nsteps = LALInferenceGetINT4Variable(algorithm_params, "nsteps");
68 skip = LALInferenceGetINT4Variable(algorithm_params, "skip");
69 verbose = LALInferenceGetINT4Variable(algorithm_params, "verbose");
70
71 step = (INT4*) LALInferenceGetVariable(algorithm_params, "step");
72
73 nwalkers_per_thread = LALInferenceGetINT4Variable(algorithm_params, "nwalkers_per_thread");
74 update_interval = LALInferenceGetINT4Variable(algorithm_params, "update_interval");
75
76 /* Initialize walker acceptance rate tracking */
77 acceptance_buffer = XLALCalloc(nwalkers_per_thread * update_interval, sizeof(INT4));
78 acceptance_rates = XLALCalloc(nwalkers_per_thread, sizeof(REAL8));
79 acceptance_rate = 0.0;
80 min_acceptance_rate = 1.0;
81 max_acceptance_rate = 0.0;
82 if (update_interval > 10)
83 tracking_interval = (INT4) update_interval/10.;
84 else
85 tracking_interval = 1;
86
87 /* Create arrays for storing proposal stats that are used to calculate evidence */
88 prop_priors = XLALCalloc(nwalkers_per_thread, sizeof(REAL8));
89 prop_likelihoods = XLALCalloc(nwalkers_per_thread, sizeof(REAL8));
90 prop_densities = XLALCalloc(nwalkers_per_thread, sizeof(REAL8));
91
92 /* Open output and print header */
93 output = init_ensemble_output(run_state, verbose, mpi_rank);
94
95 /* Setup clustered-KDE proposal from the current state of the ensemble */
96 ensemble_update(run_state);
97
98 /* Main sampling loop */
99 while (*step < nsteps) {
100
101 /* Update step counters */
102 (*step)++;
103
104 /* Update the proposal from the current state of the ensemble */
105 if (update && ((*step % update_interval) == 0)) {
106 ensemble_update(run_state);
107
108 update = 0;
109 min_acceptance_rate = 1.0;
110 max_acceptance_rate = 0.0;
111 }
112
113 /* Update all walkers on this MPI-thread */
114 #pragma omp parallel for private(thread)
115 for (walker=0; walker<nwalkers_per_thread; walker++) {
116 thread = &run_state->threads[walker];
117
118 walker_step(run_state, thread, &(prop_priors[walker]),
119 &(prop_likelihoods[walker]), &(prop_densities[walker]));
120
121 /* Track acceptance rates */
122 acceptance_buffer[walker + (*step % tracking_interval)] = thread->accepted;
123 acceptance_rates[walker] = 0.0;
124 for (i = 0; i < tracking_interval; i++)
125 acceptance_rates[walker] += acceptance_buffer[walker+i];
126 acceptance_rates[walker] /= tracking_interval;
127
128 if (verbose)
129 print_proposed_sample(thread);
130 }
131
132 /* Update if acceptance rate has fallen by more than 10% */
133 if (!update && ((*step % tracking_interval) == 0)) {
134 acceptance_rate = get_acceptance_rate(run_state, acceptance_rates);
135
136 if (acceptance_rate < min_acceptance_rate)
137 min_acceptance_rate = acceptance_rate;
138
139 if (acceptance_rate > max_acceptance_rate)
140 max_acceptance_rate = acceptance_rate;
141
142 if (max_acceptance_rate - min_acceptance_rate > 0.1 ||
143 acceptance_rate < 0.01)
144 update = 1;
145 }
146
147
148 /* Output samples to file */
149 if ((*step % skip) == 0)
150 print_samples(run_state, output, prop_priors,
151 prop_likelihoods, prop_densities,
152 acceptance_rates, mpi_rank);
153 }
154
155 /* Sampling complete, so clean up and return */
156 for (walker=0; walker<nwalkers_per_thread; walker++)
157 run_state->threads[walker].currentPropDensity = -INFINITY;
158
159 fclose(output);
160 return;
161}
162
164 REAL8 *proposed_prior, REAL8 *proposed_likelihood, REAL8 *proposed_prop_density) {
165 REAL8 proposal_ratio, acceptance_probability;
166
167 thread->accepted = 0;
168
169 *proposed_prior = -INFINITY;
170 *proposed_likelihood = -INFINITY;
171
172 /* Get the probability of proposing the reverse jump */
173 *proposed_prop_density = thread->currentPropDensity;
174 proposal_ratio = LALInferenceStoredClusteredKDEProposal(thread,
175 thread->currentParams,
176 thread->proposedParams,
177 proposed_prop_density);
178
179 /* Only bother calculating likelihood if within prior boundaries */
180 *proposed_prior = run_state->prior(run_state, thread->proposedParams, thread->model);
181 if (isfinite(*proposed_prior))
182 *proposed_likelihood = run_state->likelihood(thread->proposedParams,
183 run_state->data, thread->model);
184
185 /* Find jump acceptance probability */
186 acceptance_probability = (*proposed_prior + *proposed_likelihood)
187 - (thread->currentPrior + thread->currentLikelihood)
188 + proposal_ratio;
189
190 /* Accept the jump with the calculated probability */
191 if (acceptance_probability > 0
192 || (log(gsl_rng_uniform(thread->GSLrandom)) < acceptance_probability)) {
194 thread->currentPrior = *proposed_prior;
195 thread->currentLikelihood = *proposed_likelihood;
196 thread->currentPropDensity = *proposed_prop_density;
197
198 thread->accepted = 1;
199 }
200}
201
202
203REAL8 get_acceptance_rate(LALInferenceRunState *run_state, REAL8 *local_acceptance_rates) {
204 INT4 nwalkers, nwalkers_per_thread;
205 INT4 mpi_rank;
206 REAL8 *acceptance_rates = NULL;
207 REAL8 acceptance_rate = 0;
208
209 MPI_Comm_rank(MPI_COMM_WORLD, &mpi_rank);
210 LALInferenceVariables *algorithm_params = run_state->algorithmParams;
211 nwalkers = LALInferenceGetINT4Variable(algorithm_params, "nwalkers");
212 nwalkers_per_thread = LALInferenceGetINT4Variable(algorithm_params, "nwalkers_per_thread");
213
214 if (mpi_rank == 0)
215 acceptance_rates = XLALCalloc(nwalkers, sizeof(REAL8));
216
217 /* Send all walker locations to all MPI threads */
218 MPI_Gather(local_acceptance_rates, nwalkers_per_thread, MPI_DOUBLE,
219 acceptance_rates, nwalkers_per_thread, MPI_DOUBLE,
220 0, MPI_COMM_WORLD);
221
222 if (mpi_rank == 0) {
223 acceptance_rate = gsl_stats_mean(acceptance_rates, 1, nwalkers);
224
225 XLALFree(acceptance_rates);
226 }
227
228 MPI_Bcast(&acceptance_rate, 1, MPI_DOUBLE, 0, MPI_COMM_WORLD);
229
230 return acceptance_rate;
231}
232
233
235 INT4 nwalkers, walker, ndim, cyclic_reflective;
236 REAL8 *parameters, *samples, *param_array;
237 LALInferenceThreadState *thread = &run_state->threads[0];
238
239 LALInferenceVariables *algorithm_params = run_state->algorithmParams;
240 nwalkers = LALInferenceGetINT4Variable(algorithm_params, "nwalkers");
241 cyclic_reflective = LALInferenceGetINT4Variable(algorithm_params, "cyclic_reflective");
242
243 /* Prepare array to contain samples */
245 samples = XLALCalloc(nwalkers * ndim, sizeof(REAL8));
246
247 /* Get this thread's walkers' locations */
248 param_array = XLALCalloc(run_state->nthreads * ndim, sizeof(REAL8));
249 for (walker = 0; walker < run_state->nthreads; walker++) {
250 thread = &run_state->threads[walker];
251 parameters = &(param_array[ndim*walker]);
253 }
254
255 /* Send all walker locations to all MPI threads */
256 MPI_Allgather(param_array, run_state->nthreads*ndim,
257 MPI_DOUBLE, samples, run_state->nthreads*ndim,
258 MPI_DOUBLE, MPI_COMM_WORLD);
259
260 /* Update the KDE proposal */
261 parallel_incremental_kmeans(run_state, samples, nwalkers, cyclic_reflective);
262
263 /* Clean up */
264 XLALFree(param_array);
266}
267
268
269/* This is a temporary, messy solution for now.
270TODO: When MPI is enables in lalinference, move this routine over and clean up */
272 REAL8 *samples,
273 INT4 nwalkers,
274 INT4 cyclic_reflective) {
275 INT4 i, ndim;
276 INT4 k = 0, kmax = 10;
277 INT4 mpi_rank, mpi_size, best_rank;
278 REAL8 bic = -INFINITY;
279 REAL8 best_bic = -INFINITY;
280 REAL8 *bics;
281
282 LALInferenceThreadState *thread = &run_state->threads[0];
283
284 LALInferenceKmeans *kmeans;
285 LALInferenceKmeans *best_clustering = NULL;
286
287 MPI_Comm_rank(MPI_COMM_WORLD, &mpi_rank);
288 MPI_Comm_size(MPI_COMM_WORLD, &mpi_size);
289
290 /* Try atleast as many clusterings as there are MPI threads,
291 * so everyone has something to do. */
292 if (mpi_size > kmax)
293 kmax = mpi_size;
294
295 bics = XLALCalloc(mpi_size, sizeof(REAL8));
296
298 gsl_matrix_view mview = gsl_matrix_view_array(samples, nwalkers, ndim);
299
300 /* Keep track of clustered parameter names */
301 LALInferenceVariables *backward_params = XLALCalloc(1, sizeof(LALInferenceVariables));
302 LALInferenceVariables *cluster_params = XLALCalloc(1, sizeof(LALInferenceVariables));
304 for (item = thread->currentParams->head; item; item = item->next)
306 LALInferenceAddVariable(backward_params, item->name,
307 item->value, item->type, item->vary);
308
309 for (item = backward_params->head; item; item = item->next)
310 LALInferenceAddVariable(cluster_params, item->name, item->value, item->type, item->vary);
311
312 /* Have each MPI thread handle a fixed-k clustering */
313 k = mpi_rank + 1;
314 while (k < kmax) {
315 kmeans = LALInferenceKmeansRunBestOf(k, &mview.matrix, 8, run_state->GSLrandom);
316 bic = -INFINITY;
317 if (kmeans)
318 bic = LALInferenceKmeansBIC(kmeans);
319 if (bic > best_bic) {
320 if (best_clustering)
321 LALInferenceKmeansDestroy(best_clustering);
322 best_clustering = kmeans;
323 best_bic = bic;
324 } else {
326 break;
327 }
328
329 k += mpi_size;
330 }
331
332 MPI_Gather(&best_bic, 1, MPI_DOUBLE, bics, 1, MPI_DOUBLE, 0, MPI_COMM_WORLD);
333
334 if (mpi_rank == 0) {
335 best_bic = -INFINITY;
336 for (i = 0; i < mpi_size; i++) {
337 if (bics[i] > best_bic) {
338 best_bic = bics[i];
339 best_rank = i;
340 }
341 }
342 }
343
344 /* Send the winning k-size to everyone */
345 if (best_clustering)
346 k = best_clustering->k;
347 MPI_Bcast(&best_rank, 1, MPI_INT, 0, MPI_COMM_WORLD);
348 MPI_Bcast(&k, 1, MPI_INT, best_rank, MPI_COMM_WORLD);
349
350 /* Create a kmeans instance with the winning k */
351 if (mpi_rank != best_rank) {
352 if (best_clustering)
353 LALInferenceKmeansDestroy(best_clustering);
354 best_clustering = LALInferenceKmeansRunBestOf(k, &mview.matrix, 8, run_state->GSLrandom);
355 }
356
357 /* Broadcast cluster assignments */
358 MPI_Bcast(best_clustering->assignments, nwalkers, MPI_INT, best_rank, MPI_COMM_WORLD);
359
360 /* Calculate centroids, the run to compute sizes and weights */
361 LALInferenceKmeansUpdate(best_clustering);
362 LALInferenceKmeansRun(best_clustering);
363
365
366 proposal->kmeans = best_clustering;
367
369 samples, nwalkers,
370 cluster_params, "ClusteredKDEProposal",
371 1.0, NULL, cyclic_reflective, 0);
372
374
375 LALInferenceClearVariables(backward_params);
376 XLALFree(backward_params);
377 XLALFree(bics);
378}
379
380
381//-----------------------------------------
382// file output routines:
383//-----------------------------------------
386 INT4 rank) {
387 FILE *output;
388
389 /* Print header */
390 output = print_ensemble_header(run_state, rank);
391
392 /* Extra outputs when running verbosely */
393 if (verbose)
394 print_proposal_header(run_state, rank);
395
396 return output;
397}
398
399
400char* ensemble_output_name(const char *out_type, INT4 rank) {
401 char *name = NULL;
402
403 name = (char*) XLALCalloc(255, sizeof(char*));
404 sprintf(name, "ensemble.%s.%i", out_type, rank);
405 return name;
406}
407
408
410 FILE *output,
411 REAL8* prop_priors,
412 REAL8* prop_likelihoods,
413 REAL8* prop_densities,
414 REAL8* acceptance_rates,
415 INT4 rank) {
416 REAL8 null_likelihood, timestamp_epoch;
417 REAL8 timestamp = 0.0;
418 REAL8 evidence_ratio;
419 INT4 nwalkers_per_thread;
420 INT4 walker, start_id, step;
421 INT4 benchmark;
422 struct timeval tv;
424
425 LALInferenceVariables *algorithm_params = run_state->algorithmParams;
426 nwalkers_per_thread = LALInferenceGetINT4Variable(algorithm_params, "nwalkers_per_thread");
427 start_id = rank * nwalkers_per_thread;
428
429 step = LALInferenceGetINT4Variable(run_state->algorithmParams, "step");
430
431 null_likelihood = LALInferenceGetREAL8Variable(run_state->proposalArgs, "nullLikelihood");
432
433 /* Keep track of wall time if benchmarking */
434 benchmark = LALInferenceGetINT4Variable(run_state->algorithmParams, "benchmark");
435 if (benchmark) {
436 timestamp_epoch = LALInferenceGetREAL8Variable(run_state->algorithmParams,
437 "timestamp_epoch");
438 gettimeofday(&tv, NULL);
439 timestamp = tv.tv_sec + tv.tv_usec/1E6 - timestamp_epoch;
440 }
441
442
443 for (walker = 0; walker < nwalkers_per_thread; walker++) {
444 thread = &run_state->threads[walker];
445
446 /* Print step number, log(posterior) */
447 fprintf(output, "%d\t", step);
448 fprintf(output, "%f\t",
449 (thread->currentLikelihood - null_likelihood) + thread->currentPrior);
450
451 /* Print the non-fixed parameter values */
452 LALInferencePrintSampleNonFixed(output, thread->currentParams);
453
454 /* Print log(prior) and log(likelihood) */
455 fprintf(output, "%f\t", thread->currentPrior);
456 fprintf(output, "%f\t", thread->currentLikelihood - null_likelihood);
457
458 if (benchmark)
459 fprintf(output, "%f\t", timestamp);
460
461 evidence_ratio = prop_priors[walker] + prop_likelihoods[walker] - prop_densities[walker];
462
463 fprintf(output, "%f\t", evidence_ratio);
464
465 fprintf(output, "%f\t", acceptance_rates[walker]);
466
467 fprintf(output, "%i\t", start_id + walker);
468
469 fprintf(output, "\n");
470 }
471}
472
473
475 FILE *output = NULL;
476 char *outname;
477
478 outname = ensemble_output_name("proposed", thread->id);
479 output = fopen(outname, "a");
480
482 fprintf(output, "%i\n", thread->accepted);
483
484 fclose(output);
485 XLALFree(outname);
486}
487
488
490 FILE *output,
491 REAL8* logprior,
492 REAL8* loglike,
493 REAL8* prop_density) {
494 INT4 walker, nwalkers_per_thread;
495 REAL8 *ratios;
496 REAL8 evidence, std = 0.0;
497
498 LALInferenceVariables *algorithm_params = run_state->algorithmParams;
499 nwalkers_per_thread = LALInferenceGetINT4Variable(algorithm_params, "nwalkers_per_thread");
500
501 ratios = XLALCalloc(nwalkers_per_thread, sizeof(REAL8));
502 for (walker = 0; walker < nwalkers_per_thread; walker++)
503 ratios[walker] = logprior[walker] + loglike[walker] - prop_density[walker];
504
505 evidence = log_add_exps(ratios, nwalkers_per_thread) - log((REAL8)nwalkers_per_thread);
506
507 for (walker = 0; walker < nwalkers_per_thread; walker++)
508 std += pow(logprior[walker] + loglike[walker] - prop_density[walker] - evidence, 2.0);
509
510 std = sqrt(std)/nwalkers_per_thread;
511
512 fprintf(output, "%g\t%g\t", evidence, std);
513
514 /* Close file before returning */
515 XLALFree(ratios);
516}
517
518
521 LALInferenceIFOData *ifo_data;
522 REAL8TimeSeries *time_data;
523 INT4 walker;
524 INT4 nwalkers_per_thread;
525 INT4 ndim, int_pn_order, waveform = 0;
526 INT4 nifo, randomseed, benchmark;
527 REAL8 null_likelihood, normed_logl, pn_order=-1.0;
528 REAL8 network_snr, sampling_rate;
529 REAL8 f_ref = 0.0;
530 char *outfile_name = NULL;
531 FILE *output = NULL;
532 char *cmd_str;
533 LALInferenceThreadState *thread = &run_state->threads[0];
534
535 /* Get ensemble size */
536 LALInferenceVariables *algorithm_params = run_state->algorithmParams;
537 nwalkers_per_thread = LALInferenceGetINT4Variable(algorithm_params, "nwalkers_per_thread");
538
539 /* Decide on file name(s) and open */
540 ppt = LALInferenceGetProcParamVal(run_state->commandLine, "--outfile");
541 if (ppt) {
542 outfile_name = (char*) XLALCalloc(255, sizeof(char*));
543 sprintf(outfile_name, "%s.%i", ppt->value, rank);
544 } else
545 outfile_name = ensemble_output_name("output", rank);
546
547 output = fopen(outfile_name, "w");
548 if (output == NULL){
550 XLALPrintError("Output file error in %s, line %d. %s.\n",
551 __FILE__, __LINE__, strerror(errno));
553 }
554
555 /* Save the command line for repoducability */
556 cmd_str = LALInferencePrintCommandLine(run_state->commandLine);
557
558 randomseed = LALInferenceGetINT4Variable(run_state->algorithmParams, "random_seed");
559 null_likelihood = LALInferenceGetREAL8Variable(run_state->proposalArgs, "nullLikelihood");
561
562 /* Reference frequency for evolving parameters */
563 if (LALInferenceCheckVariable(thread->currentParams, "f_ref"))
564 f_ref = LALInferenceGetREAL8Variable(thread->currentParams, "f_ref");
565
566 /* Count number of detectors */
567 ifo_data = run_state->data;
568 nifo = 0;
569 while (ifo_data){
570 nifo++;
571 ifo_data = ifo_data->next;
572 }
573
574 /* Integer (from an enum) identfying the waveform family used */
575 if (LALInferenceCheckVariable(thread->currentParams, "LAL_APPROXIMANT"))
576 waveform = (INT4) LALInferenceGetUINT4Variable(thread->currentParams, "LAL_APPROXIMANT");
577
578 /* Determine post-Newtonian (pN) order (half of the integer stored in currentParams) */
579 if (LALInferenceCheckVariable(thread->currentParams, "LAL_PNORDER")) {
580 int_pn_order = LALInferenceGetINT4Variable(thread->currentParams, "LAL_PNORDER");
581 pn_order = int_pn_order/2.0;
582 }
583
584 /* Calculated the network signal-to-noise ratio if an injection was done */
585 ifo_data = run_state->data;
586 network_snr = 0.0;
587 while (ifo_data) {
588 network_snr += ifo_data->SNR * ifo_data->SNR;
589 ifo_data = ifo_data->next;
590 }
591 network_snr = sqrt(network_snr);
592
593 /* Keep track of time if benchmarking */
594 benchmark = LALInferenceGetINT4Variable(run_state->algorithmParams, "benchmark");
595
596 /* Write the header information to file */
598 " LALInference version:%s,%s,%s,%s,%s\n",
601
602 fprintf(output, " %s\n", cmd_str);
603
604 fprintf(output, "%6s\t%20s\t%6s\t%12s\t%9s\t%9s\t%8s\t%8s\n",
605 "seed", "null_likelihood", "ndet", "network_snr", "waveform", "pn_order", "ndim", "f_ref");
606
607 fprintf(output, "%u\t%20.10lf\t%6d\t%14.6f\t%9i\t%9.1f\t%8i\t%12.1f\n",
608 randomseed, null_likelihood, nifo, network_snr, waveform, pn_order, ndim, f_ref);
609
610 /* Use time step in time-domain data to determine sampling rate */
611 fprintf(output, "\n%16s\t%16s\t%10s\t%10s\t%10s\t%10s\t%20s\n",
612 "Detector", "SNR", "f_low", "f_high", "start_time", "segment_length", "sampling_rate");
613 ifo_data=run_state->data;
614 while(ifo_data){
615 time_data = ifo_data->timeData;
616 sampling_rate = 1.0/time_data->deltaT;
618 "%16s\t%16.8lf\t%10.2lf\t%10.2lf\t%15.7lf\t%12d\t%10.2lf\n",
619 ifo_data->detector->frDetector.name,
620 ifo_data->SNR, ifo_data->fLow, ifo_data->fHigh,
621 XLALGPSGetREAL8(&(ifo_data->epoch)), time_data->data->length, sampling_rate);
622 ifo_data=ifo_data->next;
623 }
624 fprintf(output, "\n\n\n");
625
626 /* These are the actual column headers for the samples to be output */
627 fprintf(output, "cycle\tlogpost\t");
628
630
631 fprintf(output, "logprior\tlogl\t");
632
633 if (benchmark)
634 fprintf(output, "timestamp\t");
635
636 fprintf(output, "\tevidence_ratio\tacceptance_rate\twalker\t");
637 fprintf(output, "\n");
638
639 /* Print starting values as 0th iteration */
640 for (walker = 0; walker < nwalkers_per_thread; walker++) {
641 thread = &run_state->threads[walker];
642
643 normed_logl = thread->currentLikelihood-null_likelihood;
644 fprintf(output, "%d\t%f\t", 0, thread->currentPrior + normed_logl);
645
647
648 /* Starting prior and likelihood values */
649 fprintf(output, "%f\t%f\t", thread->currentPrior,
650 thread->currentLikelihood - null_likelihood);
651
652 /* Elapsed dime in seconds */
653 if (benchmark)
654 fprintf(output, "%f\t", 0.0);
655
656 /* Ratio used to calculate evidence */
657 fprintf(output, "%f\t", 0.0);
658
659 /* Jump proposal acceptance rate */
660 fprintf(output, "%f\t", 0.0);
661
662 /* Unique walker ID */
663 fprintf(output, "%i\t", thread->id);
664
665 fprintf(output,"\n");
666 }
667
668 XLALFree(outfile_name);
669 XLALFree(cmd_str);
670 return output;
671}
672
673
675 char *name = ensemble_output_name("proposal", rank);
676 FILE *output = fopen(name, "w");
677
679 fprintf(output, "accepted\n");
680
681 fclose(output);
682 XLALFree(name);
683}
REAL8 LALInferenceKmeansBIC(LALInferenceKmeans *kmeans)
Calculate the BIC using the KDEs of the cluster distributions.
void LALInferenceKmeansRun(LALInferenceKmeans *kmeans)
Run the kmeans algorithm until cluster assignments don't change.
void LALInferenceKmeansDestroy(LALInferenceKmeans *kmeans)
Destroy a kmeans instance.
void LALInferenceKmeansUpdate(LALInferenceKmeans *kmeans)
The update step of the kmeans algorithm.
LALInferenceKmeans * LALInferenceKmeansRunBestOf(INT4 k, gsl_matrix *samples, INT4 ntrials, gsl_rng *rng)
Run a kmeans several times and return the best.
REAL8 log_add_exps(REAL8 *vals, INT4 size)
Determine the log of the sum of an array of exponentials.
void parallel_incremental_kmeans(LALInferenceRunState *run_state, REAL8 *samples, INT4 nwalkers, INT4 cyclic_reflective)
char * ensemble_output_name(const char *out_type, INT4 rank)
void print_proposal_header(LALInferenceRunState *run_state, INT4 rank)
void print_proposed_sample(LALInferenceThreadState *thread)
void print_samples(LALInferenceRunState *run_state, FILE *output, REAL8 *prop_priors, REAL8 *prop_likelihoods, REAL8 *prop_densities, REAL8 *acceptance_rates, INT4 rank)
FILE * print_ensemble_header(LALInferenceRunState *run_state, INT4 rank)
void ensemble_update(LALInferenceRunState *run_state)
REAL8 get_acceptance_rate(LALInferenceRunState *run_state, REAL8 *local_acceptance_rates)
Update the ensemble proposal from the ensemble's current state.
void ensemble_sampler(struct tagLALInferenceRunState *run_state)
void print_evidence(LALInferenceRunState *run_state, FILE *output, REAL8 *logprior, REAL8 *loglike, REAL8 *prop_density)
FILE * init_ensemble_output(LALInferenceRunState *run_state, INT4 verbose, INT4 rank)
void walker_step(LALInferenceRunState *run_state, LALInferenceThreadState *thread, REAL8 *proposed_prior, REAL8 *proposed_likelihood, REAL8 *proposed_prop_density)
Evolve a walker a single step.
Ensemble Markov-Chain Monte Carlo sampler written for LALInference.
ProcessParamsTable * ppt
int k
const LALVCSInfo lalInferenceVCSInfo
VCS and build information for LALInference.
#define fprintf
const char *const name
double REAL8
int32_t INT4
void LALInferencePrintSampleNonFixed(FILE *fp, LALInferenceVariables *sample)
Output only non-fixed parameters.
Definition: LALInference.c:948
int LALInferenceFprintParameterNonFixedHeaders(FILE *out, LALInferenceVariables *params)
Print the parameters which do not vary to a file as a tab-separated ASCII line.
UINT4 LALInferenceGetUINT4Variable(LALInferenceVariables *vars, const char *name)
INT4 LALInferenceGetVariableDimensionNonFixed(LALInferenceVariables *vars)
Get number of dimensions in vars which are not fixed to a certain value.
Definition: LALInference.c:255
void LALInferenceAddVariable(LALInferenceVariables *vars, const char *name, const void *value, LALInferenceVariableType type, LALInferenceParamVaryType vary)
Add a variable named name to vars with initial value referenced by value.
Definition: LALInference.c:395
REAL8 LALInferenceGetREAL8Variable(LALInferenceVariables *vars, const char *name)
char * LALInferencePrintCommandLine(ProcessParamsTable *procparams)
Output the command line based on the ProcessParamsTable procparams.
void LALInferenceCopyVariables(LALInferenceVariables *origin, LALInferenceVariables *target)
Deep copy the variables from one to another LALInferenceVariables structure.
Definition: LALInference.c:558
void * LALInferenceGetVariable(const LALInferenceVariables *vars, const char *name)
Return a pointer to the memory the variable vars is stored in specified by name User must cast this p...
Definition: LALInference.c:238
INT4 LALInferenceGetINT4Variable(LALInferenceVariables *vars, const char *name)
ProcessParamsTable * LALInferenceGetProcParamVal(ProcessParamsTable *procparams, const char *name)
Returns the element of the process params table with "name".
void LALInferenceClearVariables(LALInferenceVariables *vars)
Delete the variables in this structure.
Definition: LALInference.c:532
void LALInferenceCopyVariablesToArray(LALInferenceVariables *origin, REAL8 *target)
LALInference variables to an array, and vica versa.
int LALInferenceCheckVariable(LALInferenceVariables *vars, const char *name)
Checks for name being present in vars returns 1(==true) or 0.
Definition: LALInference.c:525
int LALInferenceCheckVariableNonFixed(LALInferenceVariables *vars, const char *name)
Checks for name being present in vars and having type LINEAR or CIRCULAR.
Definition: LALInference.c:503
REAL8 LALInferenceStoredClusteredKDEProposal(LALInferenceThreadState *thread, LALInferenceVariables *currentParams, LALInferenceVariables *proposedParams, REAL8 *propDensity)
An interface to the KDE proposal that avoids a KDE evaluation if possible.
void LALInferenceInitClusteredKDEProposal(LALInferenceThreadState *thread, LALInferenceClusteredKDE *kde, REAL8 *array, INT4 nSamps, LALInferenceVariables *params, const char *name, REAL8 weight, LALInferenceKmeans *(*cluster_method)(gsl_matrix *, INT4, gsl_rng *), INT4 cyclic_reflective, INT4 ntrials)
Initialize a clustered-KDE proposal.
void LALInferenceAddClusteredKDEProposalToSet(LALInferenceVariables *propArgs, LALInferenceClusteredKDE *kde)
Add a KDE proposal to the KDE proposal set.
void * XLALCalloc(size_t m, size_t n)
void XLALFree(void *p)
#define XLAL_ERROR_NULL(...)
void XLALExitErrorHandler(const char *func, const char *file, int line, int errnum)
int XLALPrintError(const char *fmt,...) _LAL_GCC_PRINTF_FORMAT_(1
#define XLALErrorHandler
XLAL_EIO
REAL8 XLALGPSGetREAL8(const LIGOTimeGPS *epoch)
LALFrDetector frDetector
CHAR name[LALNameLength]
Struct to hold clustered-KDE estimates.
LALInferenceKmeans * kmeans
Structure to contain IFO data.
Definition: LALInference.h:625
REAL8TimeSeries * timeData
Detector name.
Definition: LALInference.h:627
LALDetector * detector
integration limits for overlap integral in F-domain
Definition: LALInference.h:651
REAL8 SNR
The epoch of this observation (the time of the first sample)
Definition: LALInference.h:653
struct tagLALInferenceIFOData * next
ROQ data.
Definition: LALInference.h:659
LIGOTimeGPS epoch
LALDetector structure for where this data came from.
Definition: LALInference.h:652
REAL8 fLow
FFT plan needed for time/time-and-phase marginalisation.
Definition: LALInference.h:650
Structure for performing the kmeans clustering algorithm on a set of samples.
INT4 * assignments
Cluster assignments.
INT4 k
Number of clusters.
Structure containing inference run state This includes pointers to the function types required to run...
Definition: LALInference.h:592
ProcessParamsTable * commandLine
Definition: LALInference.h:593
LALInferenceVariables * proposalArgs
The data from the interferometers.
Definition: LALInference.h:602
LALInferenceVariables * algorithmParams
Any special arguments for the prior function.
Definition: LALInference.h:604
INT4 nthreads
Array of live points for Nested Sampling.
Definition: LALInference.h:606
struct tagLALInferenceIFOData * data
Log sample, i.e.
Definition: LALInference.h:601
LALInferenceThreadState * threads
Definition: LALInference.h:612
LALInferencePriorFunction prior
The algorithm's single iteration function.
Definition: LALInference.h:597
LALInferenceLikelihoodFunction likelihood
MultiNest prior for the parameters.
Definition: LALInference.h:599
Structure containing chain-specific variables.
Definition: LALInference.h:541
LALInferenceVariables * proposedParams
Current location going into jump proposal.
Definition: LALInference.h:558
REAL8 currentPrior
This should be removed, can be given as an algorithmParams or proposalParams entry.
Definition: LALInference.h:573
LALInferenceVariables * currentParams
Prior boundaries, etc.
Definition: LALInference.h:556
REAL8 currentPropDensity
Stucture containing model buffers and parameters.
Definition: LALInference.h:549
LALInferenceModel * model
Cycle of proposals to call.
Definition: LALInference.h:548
INT4 accepted
This should be removed, can be given as an algorithmParams entry.
Definition: LALInference.h:574
The LALInferenceVariableItem list node structure This should only be accessed using the accessor func...
Definition: LALInference.h:154
char name[VARNAME_MAX]
Definition: LALInference.h:155
LALInferenceVariableType type
Definition: LALInference.h:157
struct tagVariableItem * next
Definition: LALInference.h:159
LALInferenceParamVaryType vary
Definition: LALInference.h:158
The LALInferenceVariables structure to contain a set of parameters Implemented as a linked list of LA...
Definition: LALInference.h:170
LALInferenceVariableItem * head
Definition: LALInference.h:171
const char *const vcsDate
const char *const vcsStatus
const char *const vcsAuthor
const char *const vcsId
const char *const vcsBranch
CHAR value[LIGOMETA_VALUE_MAX]
REAL8Sequence * data