MaCh3  2.2.3
Reference Guide
DelayedMR2T2.cpp
Go to the documentation of this file.
1 #include "Fitters/DelayedMR2T2.h"
2 
3 // *************************
5 // *************************
6  AlgorithmName = "DelayedMR2T2";
7  // Step scale for delayed step = step scale prev delay * decay_rate
8  decay_rate = GetFromManager<double>(fitMan->raw()["General"]["MCMC"]["DecayRate"], 0.1);
9  // Maximum number of rejections
10  max_rejections = GetFromManager<int>(fitMan->raw()["General"]["MCMC"]["MaxRejections"], 1);
11  // How big is the first step scale. This scales parameter step by initial_scale * <step scale in ParameterHandler>
12  initial_scale = GetFromManager<int>(fitMan->raw()["General"]["MCMC"]["InitialScale"], 1);
13 
14  // On delayed on out of bounds steps. This saves some compute time and possibly regains efficiency
15  delay_on_oob_only = GetFromManager<bool>(fitMan->raw()["General"]["MCMC"]["DelayOnlyOutBounds"], false);
16 
17  //
18  delay_probability = GetFromManager<double>(fitMan->raw()["General"]["MCMC"]["DelayProbability"], 1.0);
19 
20 
21  if(delay_probability > 1.0){
22  MACH3LOG_WARN("Probability of delaying steps > 1, setting to 1");
23  delay_probability = 1.0;
24  }
25  if(delay_probability<=0.0){
26  MACH3LOG_CRITICAL("Probability of delaying steps <= 0, setting to 0, delay will no longer have an impact!");
27  delay_probability = 0.0;
28  // Since we're not delaying at all set max_rejections = 0 to save time
29  max_rejections = 0;
30  }
31 
32  MACH3LOG_INFO("Using Delayed MCMC with decay rate: {} and {} allowed rejections", decay_rate, max_rejections);
33 }
34 
35 // *************************
36 void DelayedMR2T2::ScaleSystematics(const double scale) {
37 // *************************
38  for (auto &syst : systematics)
39  {
40  syst->SetStepScale(scale*syst->GetGlobalStepScale(), false);
41  }
42 }
43 
44 // *************************
46 // *************************
47  for (int i = 0; i < static_cast<int>(systematics.size()); ++i)
48  {
49  systematics[i]->SetStepScale(start_step_scale[i], false);
50  }
51 }
52 
53 // *************************
55 // *************************
57 
58  // Store delayed specific settings
59  outTree->Branch("DelayedStep", &accepted_delayed, "DelayedStep/B");
60 }
61 
62 //**********************************************
64 // *********************************************
65  /*
66  Stores values from step
67  */
68  // Ensure vector is correctly sized
69  current_step_vals.resize(systematics.size());
70  start_step_scale.resize(systematics.size());
71 
72  // Fill with old proposed step, need to copy to avoid using the same step twice
73  for(int i=0; i<static_cast<int>(systematics.size()); ++i){
74  current_step_vals[i] = std::vector<double>(systematics[i]->GetParCurrVec());
75  start_step_scale[i] = systematics[i]->GetGlobalStepScale();
76  }
77 }
78 
79 // *************************
81 // *************************
82  // From https://arxiv.org/pdf/2010.04190 and DRAM Matlab implementation
83  const double numerator = std::max(0.0, std::exp(MinLogLikelihood - logLProp) - 1.0);
84  const double denominator = std::exp(MinLogLikelihood - logLCurr) - 1.0;
85  if (denominator <= 0.0) return 1.0;
86 
87  // Large enough that the the minus 1 will make NO difference so the max term cancels
88  if(std::isinf(numerator) or std::isinf(denominator) ){
90  }
91 
92  return std::min(numerator / denominator, 1.0);
93 }
94 
95 // *************************
97 // *************************
98  // Set the initial rejection to false
100 
101  // Apply initial scale
103 
104  bool accepted = false;
105  bool delay = false;
106  accepted_delayed = false;
107 
109 
110  for(int i=0; i<=max_rejections; ++i)
111  {
112  ProposeStep();
113 
114  // Small hack to ensure we can "leapfrog"
115  for (auto &syst : systematics)
116  {
117  syst->AcceptStep();
118  }
119 
121  continue;
122  }
123 
124  if(i==0){
126  }
127  else{
129  delay = true;
130  }
131  if(IsStepAccepted(accProb)){
132  AcceptStep();
133  accepted = true;
134 
135  // Keep track of if step is delayed + accepted for storage
136  accepted_delayed = delay;
137 
138  // Update
139  break;
140  }
142  if(not out_of_bounds and delay_on_oob_only){
143  // If we are not out of bounds and only delaying on out of bounds steps
144  // We can skip the delay
145  break;
146  }
147 
149  if(not ProbabilisticDelay()){
150  break;
151  }
152 
153 
154  // Temporary, means we can "leapfrog"
157  }
158 
159  // If we reject we need to reset everything [this is probably a tad too slow...
160  // especially in PCA...
161  if(!accepted){
162  for(int i=0; i<static_cast<int>(systematics.size()); ++i){
163  for(int j=0; j<static_cast<int>(systematics[i]->GetParCurrVec().size()); ++j){
164  systematics[i]->SetParCurrProp(j, current_step_vals[i][j]);
165  }
166  }
167  }
168  ResetSystScale();
169 }
170 
171 // *************************
173 // *************************
174  // We can delay probabilistically
175  return (random->Rndm() > delay_probability);
176 }
int size
#define MACH3LOG_CRITICAL
Definition: MaCh3Logger.h:28
#define MACH3LOG_INFO
Definition: MaCh3Logger.h:25
#define MACH3LOG_WARN
Definition: MaCh3Logger.h:26
bool accepted_delayed
Was the step we just accepted delayed?
Definition: DelayedMR2T2.h:91
void PrepareOutput()
override to add in rejection information to output
int max_rejections
How many rejections allowed?
Definition: DelayedMR2T2.h:85
bool ProbabilisticDelay() const
if delay has a chance of occurring
void StoreCurrentStep()
Store information about the current proposed step.
double delay_probability
Can delay with probability instead.
Definition: DelayedMR2T2.h:96
DelayedMR2T2(manager *const fitMan)
Constructor.
Definition: DelayedMR2T2.cpp:4
std::vector< double > start_step_scale
Stores the initial scale for all parameters.
Definition: DelayedMR2T2.h:78
double AcceptanceProbability() override
Step acceptance probability.
bool delay_on_oob_only
Delay only if we go out of bounds.
Definition: DelayedMR2T2.h:94
double MinLogLikelihood
Minimum (negative) log-likelihood of proposed steps.
Definition: DelayedMR2T2.h:88
void ResetSystScale()
Reset scale after delay process.
double decay_rate
How much to decrease the step scale each step.
Definition: DelayedMR2T2.h:83
void DoStep() override
The MCMC step proposal.
void ScaleSystematics(const double scale)
Set parameter scale to be original_scale* scale.
double initial_scale
Scale for (non-delayed) step is initial_scale * start_step_scale.
Definition: DelayedMR2T2.h:81
std::vector< std::vector< double > > current_step_vals
Store information about the current proposed step.
Definition: DelayedMR2T2.h:75
std::unique_ptr< TRandom3 > random
Random number.
Definition: FitterBase.h:146
double logLProp
proposed likelihood
Definition: FitterBase.h:117
manager * fitMan
The manager.
Definition: FitterBase.h:110
void PrepareOutput()
Prepare the output file.
Definition: FitterBase.cpp:155
double accProb
current acceptance prob
Definition: FitterBase.h:119
std::string AlgorithmName
Name of fitting algorithm that is being used.
Definition: FitterBase.h:170
double logLCurr
current likelihood
Definition: FitterBase.h:115
TTree * outTree
Output tree with posteriors.
Definition: FitterBase.h:155
std::vector< ParameterHandlerBase * > systematics
Systematic holder.
Definition: FitterBase.h:136
void AcceptStep()
Accept a step.
Definition: MCMCBase.cpp:178
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
MCMC algorithm implementing the Metropolis–Rosenbluth–Rosenbluth–Teller–Teller (MR T ) method.
Definition: MR2T2.h:25
void ProposeStep() override
Propose a step.
Definition: MR2T2.cpp:25
double AcceptanceProbability() override
Step acceptance probability.
Definition: MR2T2.cpp:103
The manager class is responsible for managing configurations and settings.
Definition: Manager.h:16
YAML::Node const & raw()
Return config.
Definition: Manager.h:41
constexpr static const double _LARGE_LOGL_
Large Likelihood is used it parameter go out of physical boundary, this indicates in MCMC that such s...
Definition: Core.h:73