22 template<
typename _HYP>
35 typename HYP::data_t*
data;
51 current(h0), data(d), maxval(-
infinity),
52 samples(0), proposals(0), acceptances(0), steps_since_improvement(0),
53 temperature(1.0), history(100) {
58 current(h0), data(d), maxval(-
infinity),
59 samples(0), proposals(0), acceptances(0), steps_since_improvement(0),
60 temperature(1.0), history(100) {
65 current(m.current), data(m.data), maxval(m.maxval),
66 samples(m.samples), proposals(m.proposals), acceptances(m.acceptances),
67 steps_since_improvement(m.steps_since_improvement) {
77 proposals = m.proposals;
78 acceptances = m.acceptances;
79 steps_since_improvement = m.steps_since_improvement;
81 temperature = m.temperature.load();
82 history = std::move(m.history);
92 void set_data(
typename HYP::data_t* d,
bool recompute_posterior=
true) {
94 if(recompute_posterior) {
95 current.compute_posterior(*data);
120 std::lock_guard guard(current_mutex);
121 current.compute_posterior(*data);
132 current = current.restart();
133 current.compute_posterior(*data);
135 steps_since_improvement = 0;
136 maxval = current.posterior;
155 assert(ctl.
nthreads == 1 &&
"*** You seem to have called MCMCChain with nthreads>1. This is not how you parallel. Check out ChainPool");
158 DEBUG(
"# Starting MCMC Chain on\t", current.posterior, current.prior, current.likelihood, current.string());
168 std::lock_guard guard(current_mutex);
170 if(current.posterior > maxval) {
171 maxval = current.posterior;
172 steps_since_improvement = 0;
184 else if (std::isnan(current.posterior) or std::isinf(current.posterior)) {
196 DEBUG(
"# Current", current.posterior, current.prior, current.likelihood, current.string());
200 auto p = current.propose();
201 if(not p) {
continue; }
203 auto [proposal, fb] = p.value();
209 if(proposal == current) {
220 DEBUG(
"# Proposed(eq)", current.posterior, current.prior, current.likelihood, current.string(),
"fb="+
str(fb));
233 if(not
check(proposal)) {
242 const double u = log(
uniform());
257 const auto breakoutpair = std::make_pair(u + current.at_temperature(temperature) + fb, (double)temperature);
259 proposal.compute_posterior(*data, breakoutpair);
266 print(
"#Proposed", proposal.posterior, proposal.prior, proposal.likelihood, proposal.string(),
"fb="+
str(fb));
269 const double ratio = proposal.at_temperature(temperature) - current.at_temperature(temperature) - fb;
275 if((not std::isnan(proposal.posterior)) and u < ratio) {
282 current = std::move(proposal);
327 return history.
mean();
336 return current.at_temperature(t);
Definition: OrderedLock.h:16
unsigned long samples
Definition: MCMCChain.h:41
std::atomic< uintmax_t > global_sample_count(0)
virtual ~MCMCChain()
Definition: MCMCChain.h:85
unsigned long proposals
Definition: MCMCChain.h:42
double acceptance_ratio()
Definition: MCMCChain.h:322
void run()
Definition: MCMCChain.h:315
HYP::data_t * data
Definition: MCMCChain.h:35
unsigned long steps_since_improvement
Definition: MCMCChain.h:44
void restart()
Definition: MCMCChain.h:130
FiniteHistory< bool > history
Definition: MCMCChain.h:48
double uniform()
Definition: Random.h:11
const HYP & getCurrent() const
Definition: MCMCChain.h:107
bool print_proposals
Definition: FleetArgs.h:36
_HYP HYP
Definition: MCMCChain.h:26
unsigned long acceptances
Definition: MCMCChain.h:43
MCMCChain(HYP &&h0, typename HYP::data_t *d)
Definition: MCMCChain.h:57
void runOnCurrent()
Definition: MCMCChain.h:115
Definition: MCMCChain.h:23
HYP current
Definition: MCMCChain.h:28
std::string str(BindingTree *t)
Definition: BindingTree.h:195
MCMCChain(HYP &h0, typename HYP::data_t *d)
Definition: MCMCChain.h:50
void start()
Definition: Control.h:54
void print(FIRST f, ARGS... args)
Lock output_lock and print to std:cout.
Definition: IO.h:53
double maxval
Definition: MCMCChain.h:39
A FiniteHistory stores the previous N examples of something of type T. This is used e...
virtual bool check(HYP &p)
This allows us to overwrite/enforce stuff about proposals in subclasses of MCMCChain.
Definition: MCMCChain.h:144
constexpr double infinity
Definition: Numerics.h:20
A FIFO mutex (from stackoverflow) https://stackoverflow.com/questions/14792016/creating-a-lock-that-p...
Definition: generator.hpp:21
HYP & getCurrent()
Definition: MCMCChain.h:99
double at_temperature(double t)
Definition: MCMCChain.h:330
size_t nthreads
Definition: Control.h:26
bool MCMCYieldOnlyChanges
Definition: FleetArgs.h:50
void DEBUG(FIRST f, ARGS... args)
Print to std:ccout with debugging info.
Definition: IO.h:73
bool running()
Definition: Control.h:63
std::atomic< double > temperature
Definition: MCMCChain.h:46
OrderedLock current_mutex
Definition: MCMCChain.h:33
void set_data(typename HYP::data_t *d, bool recompute_posterior=true)
Set this data.
Definition: MCMCChain.h:92
const HYP & getMax()
Definition: MCMCChain.h:126
generator< HYP & > run(Control ctl)
Run MCMC according to the control parameters passed in. NOTE: ctl cannot be passed by reference...
Definition: MCMCChain.h:153
MCMCChain(const MCMCChain &m)
Definition: MCMCChain.h:64
MCMCChain(MCMCChain &&m)
Definition: MCMCChain.h:72
unsigned long restart
Definition: Control.h:27
double mean()
Compute the average.
Definition: FiniteHistory.h:94
This represents an MCMC hain on a hypothesis of type HYP. It uses HYP::propose and HYP::compute_poste...
This class has all the information for running MCMC or MCTS in a little package. It defaultly constru...