In Themis, we can use any number of sampling routines to sample the posterior probability distribution function in the parameter space associated with any model. Affine invariant parallel tempered MCMC (Markov Chain Monte Carlo) is one such routine. It runs an ensemble of MCMC chains that collectively sample the posterior distribution. It also has the ability to use parallel tempering to facilitate sampling multi-modal distributions. The sampler needs a Themis likelihood object in order to sample the posterior distribution. In the following example we use a five dimensional egg-box likelihood which is a multimodal distribution with 3125 peaks within the prior region.
#include <mpi.h>
#include <memory>
#include <string>
int main(int argc, char* argv[])
{
MPI_Init(&argc, &argv);
int world_rank;
MPI_Comm_rank(MPI_COMM_WORLD, &world_rank);
std::cout << "MPI Initiated - Processor Node: " << world_rank << " executing main." << std::endl;
std::vector<Themis::prior_base*> P;
std::vector<Themis::transform_base*> T;
std::vector<Themis::likelihood_base*> L;
std::vector<double> W(1, 1.0);
std::vector<double> means, ranges;
means.push_back(0.0);
means.push_back(0.0);
means.push_back(0.0);
means.push_back(0.0);
means.push_back(0.0);
ranges.push_back(1.0);
ranges.push_back(1.0);
ranges.push_back(1.0);
ranges.push_back(1.0);
ranges.push_back(1.0);
std::vector<std::string> var_names;
var_names.push_back("test_parameter1");
var_names.push_back("test_parameter2");
var_names.push_back("test_parameter3");
var_names.push_back("test_parameter4");
var_names.push_back("test_parameter5");
int Number_of_chains = 100;
int Number_of_temperatures = 5;
int cpu_per_likelihood = 1;
int Number_of_steps = 8000;
int temp_stride = 50;
int chi2_stride = 10000;
int ckpt_stride = 100;
MC_obj.set_cpu_distribution(Number_of_temperatures, Number_of_chains, cpu_per_likelihood);
MC_obj.set_checkpoint(ckpt_stride, "sampler.ckpt");
MC_obj.run_sampler(L_obj, Number_of_steps,
temp_stride, chi2_stride, "chain.dat", "lklhd.dat", "chi2.dat",
means, ranges, var_names, false);
MPI_Finalize();
return 0;
}