25#include <lal/LALInference.h>
27#include <lal/LALInferenceProposal.h>
28#include <lal/LALInferenceClusteredKDE.h>
30#include <lal/LALInferenceVCSInfo.h>
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$"
45 INT4 mpi_rank, mpi_size;
46 INT4 walker, nwalkers_per_thread;
47 INT4 nsteps, skip, tracking_interval, update_interval,
verbose;
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;
62 MPI_Comm_rank(MPI_COMM_WORLD, &mpi_rank);
63 MPI_Comm_size(MPI_COMM_WORLD, &mpi_size);
77 acceptance_buffer =
XLALCalloc(nwalkers_per_thread * update_interval,
sizeof(
INT4));
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.;
85 tracking_interval = 1;
99 while (*step < nsteps) {
105 if (update && ((*step % update_interval) == 0)) {
109 min_acceptance_rate = 1.0;
110 max_acceptance_rate = 0.0;
114 #pragma omp parallel for private(thread)
115 for (walker=0; walker<nwalkers_per_thread; walker++) {
116 thread = &run_state->threads[walker];
118 walker_step(run_state, thread, &(prop_priors[walker]),
119 &(prop_likelihoods[walker]), &(prop_densities[walker]));
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;
133 if (!update && ((*step % tracking_interval) == 0)) {
136 if (acceptance_rate < min_acceptance_rate)
137 min_acceptance_rate = acceptance_rate;
139 if (acceptance_rate > max_acceptance_rate)
140 max_acceptance_rate = acceptance_rate;
142 if (max_acceptance_rate - min_acceptance_rate > 0.1 ||
143 acceptance_rate < 0.01)
149 if ((*step % skip) == 0)
151 prop_likelihoods, prop_densities,
152 acceptance_rates, mpi_rank);
156 for (walker=0; walker<nwalkers_per_thread; walker++)
157 run_state->threads[walker].currentPropDensity = -INFINITY;
164 REAL8 *proposed_prior,
REAL8 *proposed_likelihood,
REAL8 *proposed_prop_density) {
165 REAL8 proposal_ratio, acceptance_probability;
169 *proposed_prior = -INFINITY;
170 *proposed_likelihood = -INFINITY;
177 proposed_prop_density);
181 if (isfinite(*proposed_prior))
186 acceptance_probability = (*proposed_prior + *proposed_likelihood)
191 if (acceptance_probability > 0
192 || (log(gsl_rng_uniform(thread->
GSLrandom)) < acceptance_probability)) {
204 INT4 nwalkers, nwalkers_per_thread;
206 REAL8 *acceptance_rates = NULL;
207 REAL8 acceptance_rate = 0;
209 MPI_Comm_rank(MPI_COMM_WORLD, &mpi_rank);
218 MPI_Gather(local_acceptance_rates, nwalkers_per_thread, MPI_DOUBLE,
219 acceptance_rates, nwalkers_per_thread, MPI_DOUBLE,
223 acceptance_rate = gsl_stats_mean(acceptance_rates, 1, nwalkers);
228 MPI_Bcast(&acceptance_rate, 1, MPI_DOUBLE, 0, MPI_COMM_WORLD);
230 return acceptance_rate;
235 INT4 nwalkers, walker, ndim, cyclic_reflective;
249 for (walker = 0; walker < run_state->
nthreads; walker++) {
250 thread = &run_state->
threads[walker];
256 MPI_Allgather(param_array, run_state->
nthreads*ndim,
258 MPI_DOUBLE, MPI_COMM_WORLD);
274 INT4 cyclic_reflective) {
276 INT4 k = 0, kmax = 10;
277 INT4 mpi_rank, mpi_size, best_rank;
278 REAL8 bic = -INFINITY;
279 REAL8 best_bic = -INFINITY;
287 MPI_Comm_rank(MPI_COMM_WORLD, &mpi_rank);
288 MPI_Comm_size(MPI_COMM_WORLD, &mpi_size);
298 gsl_matrix_view mview = gsl_matrix_view_array(
samples, nwalkers, ndim);
309 for (item = backward_params->
head; item; item = item->
next)
319 if (bic > best_bic) {
322 best_clustering = kmeans;
332 MPI_Gather(&best_bic, 1, MPI_DOUBLE, bics, 1, MPI_DOUBLE, 0, MPI_COMM_WORLD);
335 best_bic = -INFINITY;
336 for (
i = 0;
i < mpi_size;
i++) {
337 if (bics[
i] > best_bic) {
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);
351 if (mpi_rank != best_rank) {
358 MPI_Bcast(best_clustering->
assignments, nwalkers, MPI_INT, best_rank, MPI_COMM_WORLD);
366 proposal->
kmeans = best_clustering;
370 cluster_params,
"ClusteredKDEProposal",
371 1.0, NULL, cyclic_reflective, 0);
404 sprintf(
name,
"ensemble.%s.%i", out_type, rank);
412 REAL8* prop_likelihoods,
413 REAL8* prop_densities,
414 REAL8* acceptance_rates,
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;
427 start_id = rank * nwalkers_per_thread;
438 gettimeofday(&tv, NULL);
439 timestamp = tv.tv_sec + tv.tv_usec/1E6 - timestamp_epoch;
443 for (walker = 0; walker < nwalkers_per_thread; walker++) {
444 thread = &run_state->
threads[walker];
449 (thread->currentLikelihood - null_likelihood) + thread->currentPrior);
456 fprintf(
output,
"%f\t", thread->currentLikelihood - null_likelihood);
461 evidence_ratio = prop_priors[walker] + prop_likelihoods[walker] - prop_densities[walker];
479 output = fopen(outname,
"a");
493 REAL8* prop_density) {
494 INT4 walker, nwalkers_per_thread;
502 for (walker = 0; walker < nwalkers_per_thread; walker++)
503 ratios[walker] = logprior[walker] + loglike[walker] - prop_density[walker];
507 for (walker = 0; walker < nwalkers_per_thread; walker++)
508 std += pow(logprior[walker] + loglike[walker] - prop_density[walker] -
evidence, 2.0);
510 std = sqrt(std)/nwalkers_per_thread;
524 INT4 nwalkers_per_thread;
526 INT4 nifo, randomseed, benchmark;
527 REAL8 null_likelihood, normed_logl, pn_order=-1.0;
528 REAL8 network_snr, sampling_rate;
530 char *outfile_name = NULL;
542 outfile_name = (
char*)
XLALCalloc(255,
sizeof(
char*));
543 sprintf(outfile_name,
"%s.%i",
ppt->
value, rank);
547 output = fopen(outfile_name,
"w");
551 __FILE__, __LINE__, strerror(errno));
567 ifo_data = run_state->
data;
571 ifo_data = ifo_data->
next;
581 pn_order = int_pn_order/2.0;
585 ifo_data = run_state->
data;
588 network_snr += ifo_data->
SNR * ifo_data->
SNR;
589 ifo_data = ifo_data->
next;
591 network_snr = sqrt(network_snr);
598 " LALInference version:%s,%s,%s,%s,%s\n",
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");
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);
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;
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",
622 ifo_data=ifo_data->
next;
636 fprintf(
output,
"\tevidence_ratio\tacceptance_rate\twalker\t");
640 for (walker = 0; walker < nwalkers_per_thread; walker++) {
641 thread = &run_state->
threads[walker];
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.
const LALVCSInfo lalInferenceVCSInfo
VCS and build information for LALInference.
void LALInferencePrintSampleNonFixed(FILE *fp, LALInferenceVariables *sample)
Output only non-fixed parameters.
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.
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.
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.
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...
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.
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.
int LALInferenceCheckVariableNonFixed(LALInferenceVariables *vars, const char *name)
Checks for name being present in vars and having type LINEAR or CIRCULAR.
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)
#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
REAL8 XLALGPSGetREAL8(const LIGOTimeGPS *epoch)
Struct to hold clustered-KDE estimates.
LALInferenceKmeans * kmeans
Structure to contain IFO data.
REAL8TimeSeries * timeData
Detector name.
LALDetector * detector
integration limits for overlap integral in F-domain
REAL8 SNR
The epoch of this observation (the time of the first sample)
struct tagLALInferenceIFOData * next
ROQ data.
LIGOTimeGPS epoch
LALDetector structure for where this data came from.
REAL8 fLow
FFT plan needed for time/time-and-phase marginalisation.
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...
ProcessParamsTable * commandLine
LALInferenceVariables * proposalArgs
The data from the interferometers.
LALInferenceVariables * algorithmParams
Any special arguments for the prior function.
INT4 nthreads
Array of live points for Nested Sampling.
struct tagLALInferenceIFOData * data
Log sample, i.e.
LALInferenceThreadState * threads
LALInferencePriorFunction prior
The algorithm's single iteration function.
LALInferenceLikelihoodFunction likelihood
MultiNest prior for the parameters.
Structure containing chain-specific variables.
LALInferenceVariables * proposedParams
Current location going into jump proposal.
REAL8 currentPrior
This should be removed, can be given as an algorithmParams or proposalParams entry.
LALInferenceVariables * currentParams
Prior boundaries, etc.
REAL8 currentPropDensity
Stucture containing model buffers and parameters.
LALInferenceModel * model
Cycle of proposals to call.
INT4 accepted
This should be removed, can be given as an algorithmParams entry.
The LALInferenceVariableItem list node structure This should only be accessed using the accessor func...
LALInferenceVariableType type
struct tagVariableItem * next
LALInferenceParamVaryType vary
The LALInferenceVariables structure to contain a set of parameters Implemented as a linked list of LA...
LALInferenceVariableItem * head
const char *const vcsDate
const char *const vcsStatus
const char *const vcsAuthor
const char *const vcsBranch
CHAR value[LIGOMETA_VALUE_MAX]