MaCh3  2.8.0
Reference Guide
MR2T2.cpp
Go to the documentation of this file.
1 #include "Algorithms/MR2T2.h"
2 
3 // ***************
5 // ***************
6  AlgorithmName = "MR2T2";
7 }
8 
9 // *******************
10 void MR2T2::DoStep() {
11 // *******************
12  ProposeStep();
13  // Does the MCMC accept this step?
15 
17  {
18  AcceptStep();
19  }
20 }
21 
22 // *******************
23 // Do the initial reconfigure of the MCMC
25 // *******************
26  // Initial likelihood
27  out_of_bounds = false;
28 
29  double llh = 0.0;
30 
31  // Loop over the systematics and propose the initial step
32  for (size_t s = 0; s < systematics.size(); ++s)
33  {
34  // Could throw the initial value here to do MCMC stability studies
35  // Propose the steps for the systematics
36  systematics[s]->ProposeStep();
37 
38  // Get the likelihood from the systematics
39  syst_llh[s] = systematics[s]->GetLikelihood();
40  llh += syst_llh[s];
41 
42 #ifdef MACH3_DEBUG
43  if (debug)
44  debugFile << "LLH after " << systematics[s]->GetName() << " " << llh << std::endl;
45 #endif
46  }
47 
48  // if we're using the multicanonical method, we need to add the penalty to the
49  // likelihood now prior to the Large LLH check
50  if (multicanonical) {
51  // get the proposed value of delta_cp and apply the multicanonical penalty,
52  // weighting it using the beta value to increase or decrease the strength of
53  // the penalty
54 
55  double multicanonical_penalty = multicanonicalHandler->GetMulticanonicalWeight();
56 
57 #ifdef MACH3_DEBUG
58  // Print the multicanonical penalty and the delta_cp and delm23 values to the debug file
59  if (debug) {
60  auto delta_cp_value = *multicanonicalHandler->multicanonicalVarValue;
61  auto delm23_value = *multicanonicalHandler->multicanonicalVar_dm23_value;
62  debugFile << " delta_cp: " << delta_cp_value << " delm23: " << delm23_value << " multicanonical_penalty: " << multicanonical_penalty << std::endl;
63  }
64 #endif
65 
66  llh += multicanonical_penalty;
67 
68  MACH3LOG_DEBUG("Delta CP value: {}", *multicanonicalHandler->multicanonicalVarValue);
69  MACH3LOG_DEBUG("Multicanonical penalty: {}", multicanonical_penalty);
70  MACH3LOG_DEBUG("LLH after multicanonical penalty: {}", llh);
71  }
72 
73  // Check if we've hit a boundary in the systematics
74  // In this case we can save time by not having to reconfigure the simulation
75  if (llh >= M3::_LARGE_LOGL_)
76  {
77  out_of_bounds = true;
78 #ifdef MACH3_DEBUG
79  if (debug)
80  debugFile << "Rejecting based on boundary" << std::endl;
81 #endif
82  }
83 
84  // Only reweight when we have a good parameter configuration
85  // This speeds things up considerably because for every bad parameter configuration we don't have to reweight the MC
86  if (!out_of_bounds)
87  {
88  // Could multi-thread this
89  // But since sample reweight is multi-threaded it's probably better to do that
90  for (size_t i = 0; i < samples.size(); ++i)
91  {
92  samples[i]->Reweight();
93  }
94 
95  // DB for atmospheric event by event sample migration, need to fully reweight all samples to allow event passing prior to likelihood evaluation
96  for (size_t i = 0; i < samples.size(); ++i)
97  {
98  // Get the sample likelihoods and add them
99  sample_llh[i] = samples[i]->GetLikelihood();
100  llh += sample_llh[i];
101 #ifdef MACH3_DEBUG
102  if (debug)
103  debugFile << "LLH after sample " << i << " " << llh << std::endl;
104 #endif
105  }
106 
107  // For when we don't have to reweight, set sample to madness
108  }
109  else
110  {
111  for (size_t i = 0; i < samples.size(); ++i)
112  {
113  // Set the sample_llh[i] to be madly high also to signify a step out of bounds
115 #ifdef MACH3_DEBUG
116  if (debug)
117  debugFile << "LLH after REJECT sample " << i << " " << llh << std::endl;
118 #endif
119  }
120  }
121  // Save the proposed likelihood (class member)
122  logLProp = llh;
123 }
124 
125 // **********************
126 // Do we accept the proposed step for all the parameters?
128 // **********************
129  // Set the acceptance probability to zero
130  double acc_prob = 0.0;
131 
132  // Calculate acceptance probability
133  if (anneal)
134  acc_prob = std::min(1., std::exp(-(logLProp - logLCurr) / (std::exp(-step / AnnealTemp))));
135  else
136  acc_prob = std::min(1., std::exp(logLCurr - logLProp));
137 
138  return acc_prob;
139 }
#define MACH3LOG_DEBUG
Definition: MaCh3Logger.h:34
double logLProp
proposed likelihood
Definition: FitterBase.h:127
unsigned int step
current state
Definition: FitterBase.h:123
double accProb
current acceptance prob
Definition: FitterBase.h:129
std::string AlgorithmName
Name of fitting algorithm that is being used.
Definition: FitterBase.h:177
std::vector< double > sample_llh
store the llh breakdowns
Definition: FitterBase.h:136
std::vector< SampleHandlerInterface * > samples
Sample holder.
Definition: FitterBase.h:141
double logLCurr
current likelihood
Definition: FitterBase.h:125
std::vector< double > syst_llh
systematic llh breakdowns
Definition: FitterBase.h:138
std::vector< ParameterHandlerBase * > systematics
Systematic holder.
Definition: FitterBase.h:146
Base class for MCMC fitting algorithms.
Definition: MCMCBase.h:8
std::unique_ptr< MulticanonicalMCMCHandler > multicanonicalHandler
multicanonical handler for umbrella sampling
Definition: MCMCBase.h:67
bool anneal
simulated annealing
Definition: MCMCBase.h:79
void AcceptStep()
Accept a step.
Definition: MCMCBase.cpp:250
double AnnealTemp
simulated annealing temperature
Definition: MCMCBase.h:81
bool out_of_bounds
Do we reject based on hitting boundaries in systs.
Definition: MCMCBase.h:70
bool IsStepAccepted(const double acc_prob)
Is step accepted?
Definition: MCMCBase.cpp:232
bool multicanonical
multi-canonical method toggle on/off
Definition: MCMCBase.h:84
MR2T2(Manager *const FitManager)
Constructor.
Definition: MR2T2.cpp:4
void ProposeStep() final
Propose a step.
Definition: MR2T2.cpp:24
double AcceptanceProbability() override
Step acceptance probability.
Definition: MR2T2.cpp:127
void DoStep() override
The MCMC step proposal and acceptance.
Definition: MR2T2.cpp:10
The manager class is responsible for managing configurations and settings.
Definition: Manager.h:17
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:80