10 namespace pt = boost::property_tree;
14 SingleChainMCMC::SingleChainMCMC(pt::ptree pt,
15 std::shared_ptr<AbstractSamplingProblem>
const& problem) :
18 printLevel(pt.
get(
"PrintLevel",3))
23 SingleChainMCMC::SingleChainMCMC(boost::property_tree::ptree pt,
24 std::vector<std::shared_ptr<TransitionKernel> >
const& kernelsIn) :
27 printLevel(pt.
get(
"PrintLevel",3))
34 std::shared_ptr<AbstractSamplingProblem>
const& problem,
35 std::shared_ptr<parcer::Communicator>
const& comm) :
38 printLevel(pt.
get(
"PrintLevel",3)),
45 std::shared_ptr<parcer::Communicator>
const& comm,
46 std::vector<std::shared_ptr<TransitionKernel> >
const& kernelsIn) :
49 printLevel(pt.
get(
"PrintLevel",3)),
60 std::vector<std::shared_ptr<TransitionKernel>>
const& kernelsIn) {
62 assert(kernelsIn.size()>0);
64 numSamps = pt.get<
unsigned int>(
"NumSamples");
65 burnIn = pt.get(
"BurnIn",0);
69 scheduler = std::make_shared<ThinScheduler>(pt);
76 for(
int i=0; i<
kernels.size(); ++i)
85 std::string kernelString = pt.get<std::string>(
"KernelList");
88 std::vector<std::shared_ptr<TransitionKernel>> kernelVec;
89 unsigned int numBlocks = kernelNames.size();
91 kernelVec.resize(numBlocks);
94 for(
int i=0; i<numBlocks; ++i) {
95 boost::property_tree::ptree subTree = pt.get_child(kernelNames.at(i));
96 subTree.put(
"BlockIndex",i);
98 problem->AddOptions(subTree);
102 Setup(pt, kernelVec);
111 std::cout << prefix << int(std::floor(
double((currInd - 1) * 100) /
double(
numSamps))) <<
"% Complete" << std::endl;
113 for(
int blockInd=0; blockInd<
kernels.size(); ++blockInd){
114 std::cout << prefix <<
" Block " << blockInd <<
":\n";
115 kernels.at(blockInd)->PrintStatus(prefix +
" ");
124 const unsigned int printIncr = std::floor(
numSamps /
double(10));
125 unsigned int nextPrintInd = printIncr;
129 std::cout <<
"Starting single chain MCMC sampler..." << std::endl;
138 nextPrintInd += printIncr;
147 std::cout <<
"Completed in " <<
totalTime <<
" seconds." << std::endl;
155 std::stringstream msg;
156 msg <<
"\nERROR in SingleChainMCMC::Sample! Trying to sample chain but previous (or initial) state has not been set.\n";
157 throw std::runtime_error(msg.str());
160 auto startTime = std::chrono::high_resolution_clock::now();
162 std::vector<std::shared_ptr<SamplingState> > newStates;
165 for(
int blockInd=0; blockInd<
kernels.size(); ++blockInd){
178 const double now = std::chrono::duration<double>(std::chrono::high_resolution_clock::now()-startTime).count();
179 for(
auto it=newStates.begin(); it!=newStates.end(); ++it ) {
180 (*it)->meta[
"time"] = now;
190 auto endTime = std::chrono::high_resolution_clock::now();
191 totalTime += std::chrono::duration<double>(endTime - startTime).count();
195 std::shared_ptr<SamplingState>& lastSavedState,
196 unsigned int& sampNum)
const {
197 for(
auto it : newStates ) {
201 if (it->HasMeta(
"QOI")) {
202 std::shared_ptr<SamplingState> qoi =
AnyCast(it->meta[
"QOI"]);
211 return newStates.back();
217 SetState(std::make_shared<SamplingState>(x0));
A class for storing and working with the results of Markov chain Monte Carlo algorithms.
bool ShouldSave(unsigned int const sampNum) const
virtual std::shared_ptr< MarkovChain > GetSamples() const
std::shared_ptr< SamplingState > SaveSamples(std::vector< std::shared_ptr< SamplingState > > const &newStates, std::shared_ptr< SamplingState > &lastSavedState, unsigned int &sampNum) const
SingleChainMCMC(boost::property_tree::ptree pt, std::shared_ptr< parcer::Communicator > const &comm, std::vector< std::shared_ptr< TransitionKernel > > const &kernelsIn)
std::shared_ptr< parcer::Communicator > comm
std::shared_ptr< SamplingState > prevState
std::shared_ptr< SaveSchedulerBase > schedulerQOI
std::shared_ptr< MarkovChain > samples
void Setup(boost::property_tree::ptree pt, std::vector< std::shared_ptr< TransitionKernel >> const &kernelsIn)
std::vector< std::shared_ptr< TransitionKernel > > kernels
std::shared_ptr< MarkovChain > Run(Args const &... args)
virtual void SetState(std::shared_ptr< SamplingState > const &x0)
Set the state of the MCMC chain.
std::shared_ptr< SaveSchedulerBase > scheduler
std::shared_ptr< MarkovChain > QOIs
void PrintStatus(std::string prefix) const
std::shared_ptr< MarkovChain > GetQOIs() const
std::shared_ptr< SamplingState > lastSavedState
static std::shared_ptr< TransitionKernel > Construct(boost::property_tree::ptree const &pt, std::shared_ptr< AbstractSamplingProblem > problem)
Static constructor for the transition kernel.
Class for easily casting boost::any's in assignment operations.
std::vector< std::string > Split(std::string str, char delim=',')
Split a string into parts based on a particular character delimiter. Also Strips whitespace from part...
auto get(const nlohmann::detail::iteration_proxy_value< IteratorType > &i) -> decltype(i.key())