MaCh3  2.2.3
Reference Guide
MCMCBase.cpp
Go to the documentation of this file.
1 #include "Fitters/MCMCBase.h"
2 
3 // *************************
4 // Initialise the manager and make it an object of mcmc class
5 // Now we can dump manager settings to the output file
7 // *************************
8  // Beginning step number
9  stepStart = 0;
10 
11  // Starting parameters should be thrown
12  out_of_bounds = false;
13  chainLength = Get<unsigned>(fitMan->raw()["General"]["MCMC"]["NSteps"], __FILE__, __LINE__);
14 
15  AnnealTemp = GetFromManager<double>(fitMan->raw()["General"]["MCMC"]["AnnealTemp"], -999);
16  if (AnnealTemp < 0)
17  anneal = false;
18  else
19  {
20  MACH3LOG_INFO("Enabling simulated annealing with T = {}", AnnealTemp);
21  anneal = true;
22  }
23 }
24 
25 
26 // *******************
27 // Run the Markov chain with all the systematic objects added
29 // *******************
30  // Save the settings into the output file
31  SaveSettings();
32 
33  // Prepare the output branches
34  PrepareOutput();
35 
36  // Remove obsolete memory and make other checks before fit starts
38 
39  // Reconfigure the samples, systematics and oscillation for first weight
40  // ProposeStep sets logLProp
41  ProposeStep();
42  // Set the current logL to the proposed logL for the 0th step
43  // Accept the first step to set logLCurr: this shouldn't affect the MCMC because we ignore the first N steps in burn-in
45 
46  // Begin MCMC
47  const auto StepEnd = stepStart + chainLength;
48  for (step = stepStart; step < StepEnd; ++step)
49  {
50  DoMCMCStep();
51  }
52  // Save all the MCMC output
53  SaveOutput();
54 
55  // Process MCMC
56  ProcessMCMC();
57 }
58 
59 // *******************
61 // *******************
65  DoStep();
68 }
69 
70 // *******************
72 // *******************
73  stepClock->Start();
74  out_of_bounds = false;
75 
76  // Print 10 steps in total
77  if ((step - stepStart) % (chainLength / 10) == 0)
78  {
79  PrintProgress();
80  }
81 }
82 
83 // *************************
85 // *************************
86  stepClock->Stop();
87  stepTime = stepClock->RealTime();
88 
89  // Write step to output tree
90  outTree->Fill();
91 
92  // Do Adaptive MCMC
93  AdaptiveStep();
94 
95  if (step % auto_save == 0){
96  outTree->AutoSave();
97  }
98 }
99 
100 // *******************
101 // Print the fit output progress
103 // *******************
104  MACH3LOG_INFO("Step:\t{}/{}, current: {:.2f}, proposed: {:.2f}", step - stepStart, chainLength, logLCurr, logLProp);
105  MACH3LOG_INFO("Accepted/Total steps: {}/{} = {:.2f}", accCount, step - stepStart, static_cast<double>(accCount) / static_cast<double>(step - stepStart));
106 
107  for (ParameterHandlerBase *cov : systematics)
108  {
109  cov->PrintNominalCurrProp();
110  }
111 #ifdef DEBUG
112  if (debug)
113  {
114  debugFile << "\n-------------------------------------------------------" << std::endl;
115  debugFile << "Step:\t" << step + 1 << "/" << chainLength << " | current: " << logLCurr << " proposed: " << logLProp << std::endl;
116  }
117 #endif
118 }
119 
120 // *******************
121 void MCMCBase::StartFromPreviousFit(const std::string &FitName) {
122 // *******************
123  // Use base class
125 
126  // For MCMC we also need to set stepStart
127  TFile *infile = new TFile(FitName.c_str(), "READ");
128  TTree *posts = infile->Get<TTree>("posteriors");
129  unsigned int step_val = 0;
130 
131  posts->SetBranchAddress("step", &step_val);
132  posts->GetEntry(posts->GetEntries() - 1);
133 
134  stepStart = step_val;
135  // KS: Also update number of steps if using adaption
136  for (unsigned int i = 0; i < systematics.size(); ++i)
137  {
138  if (systematics[i]->GetDoAdaption())
139  {
140  systematics[i]->SetNumberOfSteps(step_val);
141  }
142  }
143  infile->Close();
144  delete infile;
145 }
146 
147 // *************************
149 // *************************
150  // Save the Adaptive output
151  for (const auto &syst : systematics)
152  {
153  if (syst->GetDoAdaption()){
154  syst->UpdateAdaptiveCovariance();
155  }
156  }
157 }
158 
159 // *************************
160 bool MCMCBase::IsStepAccepted(const double acc_prob) {
161 // *************************
162  // Get the random number
163  const double fRandom = random->Rndm();
164  // Do the accept/reject
165  #ifdef DEBUG
166  debugFile << " logLProp: " << logLProp << " logLCurr: " << logLCurr << " acc_prob: " << acc_prob << " fRandom: " << fRandom << std::endl;
167  #endif
168 
169  if (fRandom > acc_prob)
170  {
171  // Reject
172  return false;
173  }
174  return true;
175 }
176 
177 // *************************
179 // *************************
180  ++accCount;
181  logLCurr = logLProp;
182 
183  // Loop over systematics and accept
184  for (size_t s = 0; s < systematics.size(); ++s)
185  {
186  systematics[s]->AcceptStep();
187  }
188 }
MaCh3Plotting::PlottingManager * man
#define MACH3LOG_INFO
Definition: MaCh3Logger.h:25
Base class for implementing fitting algorithms.
Definition: FitterBase.h:23
std::unique_ptr< TRandom3 > random
Random number.
Definition: FitterBase.h:146
double logLProp
proposed likelihood
Definition: FitterBase.h:117
void ProcessMCMC()
Process MCMC output.
Definition: FitterBase.cpp:371
int accCount
counts accepted steps
Definition: FitterBase.h:121
void SaveOutput()
Save output and close files.
Definition: FitterBase.cpp:230
void SaveSettings()
Save the settings that the MCMC was run with.
Definition: FitterBase.cpp:84
manager * fitMan
The manager.
Definition: FitterBase.h:110
unsigned int step
current state
Definition: FitterBase.h:113
void PrepareOutput()
Prepare the output file.
Definition: FitterBase.cpp:155
virtual void StartFromPreviousFit(const std::string &FitName)
Allow to start from previous fit/chain.
Definition: FitterBase.cpp:304
double stepTime
Time of single step.
Definition: FitterBase.h:143
unsigned int stepStart
step start, by default 0 if we start from previous chain then it will be different
Definition: FitterBase.h:123
std::unique_ptr< TStopwatch > stepClock
tells how long single step/fit iteration took
Definition: FitterBase.h:141
double logLCurr
current likelihood
Definition: FitterBase.h:115
int auto_save
auto save every N steps
Definition: FitterBase.h:157
TTree * outTree
Output tree with posteriors.
Definition: FitterBase.h:155
void SanitiseInputs()
Remove obsolete memory and make other checks before fit starts.
Definition: FitterBase.cpp:222
std::vector< ParameterHandlerBase * > systematics
Systematic holder.
Definition: FitterBase.h:136
void RunMCMC() override
Actual implementation of MCMC fitting algorithm.
Definition: MCMCBase.cpp:28
bool anneal
simulated annealing
Definition: MCMCBase.h:70
unsigned int chainLength
number of steps in chain
Definition: MCMCBase.h:67
void AcceptStep()
Accept a step.
Definition: MCMCBase.cpp:178
double AnnealTemp
simulated annealing temperature
Definition: MCMCBase.h:72
void StartFromPreviousFit(const std::string &FitName) override
Allow to start from previous fit/chain.
Definition: MCMCBase.cpp:121
void PrintProgress()
Print the progress.
Definition: MCMCBase.cpp:102
bool out_of_bounds
Do we reject based on hitting boundaries in systs.
Definition: MCMCBase.h:61
bool IsStepAccepted(const double acc_prob)
Is step accepted?
Definition: MCMCBase.cpp:160
void DoMCMCStep()
The full StartStep->DoStep->EndStep chain.
Definition: MCMCBase.cpp:60
void PreStepProcess()
Actions before step proposal [start stopwatch].
Definition: MCMCBase.cpp:71
MCMCBase(manager *const fitMan)
Constructor.
Definition: MCMCBase.cpp:6
virtual void ProposeStep()=0
Propose a step.
virtual void DoStep()=0
The MCMC step proposal and acceptance.
void PostStepProcess()
Actions after step proposal [end stopwatch, fill tree].
Definition: MCMCBase.cpp:84
void AdaptiveStep()
Adaptive MCMC step.
Definition: MCMCBase.cpp:148
Base class responsible for handling of systematic error parameters. Capable of using PCA or using ada...
The manager class is responsible for managing configurations and settings.
Definition: Manager.h:16
YAML::Node const & raw()
Return config.
Definition: Manager.h:41