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