MaCh3  2.8.0
Reference Guide
Public Member Functions | Protected Member Functions | Protected Attributes | List of all members
MR2T2 Class Reference

MCMC algorithm implementing the Metropolis–Rosenbluth–Rosenbluth–Teller–Teller (MR \(^2\)T \(^2\)) method. More...

#include <Fitters/Algorithms/MR2T2.h>

Inheritance diagram for MR2T2:
[legend]
Collaboration diagram for MR2T2:
[legend]

Public Member Functions

 MR2T2 (Manager *const FitManager)
 Constructor. More...
 
virtual ~MR2T2 ()=default
 Destructor. More...
 
- Public Member Functions inherited from MCMCBase
 MCMCBase (Manager *const fitMan)
 Constructor. More...
 
virtual ~MCMCBase ()=default
 Destructor. More...
 
void RunMCMC () final
 Actual implementation of MCMC fitting algorithm. More...
 
void StartFromPreviousFit (const std::string &FitName) final
 Allow to start from previous fit/chain. More...
 
std::string GetFancyName () const final
 This name is to get include some special algorithm extensions. More...
 
void setChainLength (unsigned int L)
 Set how long chain should be. More...
 
- Public Member Functions inherited from FitterBase
 FitterBase (Manager *const fitMan)
 Constructor. More...
 
virtual ~FitterBase ()
 Destructor for the FitterBase class. More...
 
void AddSampleHandler (SampleHandlerInterface *sample)
 This function adds a sample PDF object to the analysis framework. The sample PDF object will be utilized in fitting procedures or likelihood scans. More...
 
void AddSystObj (ParameterHandlerBase *cov)
 This function adds a Covariance object to the analysis framework. The Covariance object will be utilized in fitting procedures or likelihood scans. More...
 
void DragRace (const int NLaps=100)
 Calculates the required time for each sample or covariance object in a drag race simulation. Inspired by Dan's feature. More...
 
void RunLLHScan ()
 Perform a 1D likelihood scan. More...
 
void RunLLHMap ()
 Perform a general multi-dimensional likelihood scan. More...
 
void GetStepScaleBasedOnLLHScan (const std::string &filename="")
 LLH scan is good first estimate of step scale. More...
 
void Run2DLLHScan ()
 Perform a 2D likelihood scan. More...
 
void RunSigmaVar ()
 Perform a 1D/2D sigma var for all samples. More...
 
std::string GetName () const
 Get name of class. More...
 

Protected Member Functions

void DoStep () override
 The MCMC step proposal and acceptance. More...
 
void ProposeStep () final
 Propose a step. More...
 
double AcceptanceProbability () override
 Step acceptance probability. More...
 
- Protected Member Functions inherited from MCMCBase
void DoMCMCStep ()
 The full StartStep->DoStep->EndStep chain. More...
 
void PreStepProcess ()
 Actions before step proposal [start stopwatch]. More...
 
void PostStepProcess ()
 Actions after step proposal [end stopwatch, fill tree]. More...
 
bool IsStepAccepted (const double acc_prob)
 Is step accepted? More...
 
void AcceptStep ()
 Accept a step. More...
 
void AdaptiveStep ()
 Adaptive MCMC step. More...
 
void PrintProgress (const bool StepsPrint=true)
 Print the progress. More...
 
void CheckAcceptanceRates ()
 Check the acceptance rates. More...
 
- Protected Member Functions inherited from FitterBase
void ProcessMCMC ()
 Process MCMC output. More...
 
void PrepareOutput ()
 Prepare the output file. More...
 
void SaveOutput ()
 Save output and close files. More...
 
void SanitiseInputs ()
 Remove obsolete memory and make other checks before fit starts. More...
 
void SaveSettings ()
 Save the settings that the MCMC was run with. More...
 
bool GetScanRange (std::map< std::string, std::vector< double >> &scanRanges) const
 YSP: Set up a mapping to store parameters with user-specified ranges, suggested by D. Barrow. More...
 
void GetParameterScanRange (const ParameterHandlerBase *cov, const int i, double &CentralValue, double &lower, double &upper, const int n_points, const std::string &suffix="") const
 Helper function to get parameter scan range, central value. More...
 
bool CheckSkipParameter (const std::vector< std::string > &SkipVector, const std::string &ParamName) const
 KS: Check whether we want to skip parameter using skip vector. More...
 
void CustomRange (const std::string &ParName, const double sigma, double &ParamShiftValue) const
 For comparison with other fitting frameworks (like P-Theta) we usually have to apply different parameter values then usual 1, 3 sigma. More...
 
std::vector< double > CalculateBinEdges (double lowerlimit, double upperlimit, int n_points) const
 

Protected Attributes

bool anneal
 simulated annealing More...
 
double AnnealTemp
 simulated annealing temperature More...
 
- Protected Attributes inherited from MCMCBase
std::unique_ptr< MulticanonicalMCMCHandler > multicanonicalHandler
 multicanonical handler for umbrella sampling
More...
 
bool out_of_bounds
 Do we reject based on hitting boundaries in systs. More...
 
bool accept
 Accept. More...
 
unsigned int chainLength
 number of steps in chain More...
 
bool multicanonical
 multi-canonical method toggle on/off More...
 
- Protected Attributes inherited from FitterBase
Manager * fitMan
 The manager for configuration handling. More...
 
unsigned int step
 current state More...
 
double logLCurr
 current likelihood More...
 
double logLProp
 proposed likelihood More...
 
double accProb
 current acceptance prob More...
 
int accCount
 counts accepted steps More...
 
unsigned int stepStart
 step start, by default 0 if we start from previous chain then it will be different More...
 
std::vector< double > sample_llh
 store the llh breakdowns More...
 
std::vector< double > syst_llh
 systematic llh breakdowns More...
 
std::vector< SampleHandlerInterface * > samples
 Sample holder. More...
 
unsigned int TotalNSamples
 Total number of samples used, single SampleHandler can store more than one analysis sample! More...
 
std::vector< ParameterHandlerBase * > systematics
 Systematic holder. More...
 
std::unique_ptr< TStopwatch > clock
 tells global time how long fit took More...
 
std::unique_ptr< TStopwatch > stepClock
 tells how long single step/fit iteration took More...
 
double stepTime
 Time of single step. More...
 
TFile * outputFile
 Output. More...
 
TDirectory * CovFolder
 Output cov folder. More...
 
TDirectory * SampleFolder
 Output sample folder. More...
 
TTree * outTree
 Output tree with posteriors. More...
 
int auto_save
 auto save every N steps More...
 
bool fTestLikelihood
 Necessary for some fitting algorithms like PSO. More...
 
bool FileSaved
 Checks if file saved not repeat some operations. More...
 
bool SettingsSaved
 Checks if setting saved not repeat some operations. More...
 
bool OutputPrepared
 Checks if output prepared not repeat some operations. More...
 
std::string AlgorithmName
 Name of fitting algorithm that is being used. More...
 

Detailed Description

MCMC algorithm implementing the Metropolis–Rosenbluth–Rosenbluth–Teller–Teller (MR \(^2\)T \(^2\)) method.

Note
MR \(^2\)T \(^2\) is also known as Metropolis-Hastings [21]

The acceptance probability for a proposed step is given by

\[ \alpha = \min \Biggl( 1, \exp\bigl( \log \mathcal{L}_\text{prop} - \log \mathcal{L}_\text{curr} \bigr) \Biggr), \]

where \(\log \mathcal{L}_\text{prop}\) is the log-likelihood of the proposed state and \(\log \mathcal{L}_\text{curr}\) is the log-likelihood of the current state.

If annealing is enabled, the acceptance probability is modified as

\[ \alpha_\text{anneal} = \min \Biggl( 1, \exp\Bigl( -\frac{\log \mathcal{L}_\text{prop} - \log \mathcal{L}_\text{curr}} {\exp(-\text{step}/T_\text{anneal})} \Bigr) \Biggr), \]

where \(T_\text{anneal}\) is the current annealing temperature and \(\text{step}\) is the iteration index.

Author
Asher Kaboth

Definition at line 27 of file MR2T2.h.

Constructor & Destructor Documentation

◆ MR2T2()

MR2T2::MR2T2 ( Manager *const  FitManager)

Constructor.

Parameters
FitManagerA pointer to a manager object, which will handle all settings.

Definition at line 4 of file MR2T2.cpp.

4  : MCMCBase(man) {
5 // ***************
6  AnnealTemp = GetFromManager<double>(fitMan->raw()["General"]["MCMC"]["AnnealTemp"], -999, __FILE__ , __LINE__);
7  if (AnnealTemp < 0) {
8  anneal = false;
9  AlgorithmName = "MR2T2";
10  } else {
11  MACH3LOG_INFO("Enabling simulated annealing with T = {}", AnnealTemp);
12  anneal = true;
13  AlgorithmName = "MR2T2_Annealed";
14  }
15 }
#define MACH3LOG_INFO
Definition: MaCh3Logger.h:35
std::string AlgorithmName
Name of fitting algorithm that is being used.
Definition: FitterBase.h:177
Manager * fitMan
The manager for configuration handling.
Definition: FitterBase.h:120
MCMCBase(Manager *const fitMan)
Constructor.
Definition: MCMCBase.cpp:6
double AnnealTemp
simulated annealing temperature
Definition: MR2T2.h:46
bool anneal
simulated annealing
Definition: MR2T2.h:44
YAML::Node const & raw() const
Return config.
Definition: Manager.h:48

◆ ~MR2T2()

virtual MR2T2::~MR2T2 ( )
virtualdefault

Destructor.

Member Function Documentation

◆ AcceptanceProbability()

double MR2T2::AcceptanceProbability ( )
overrideprotectedvirtual

Step acceptance probability.

Implements MCMCBase.

Definition at line 134 of file MR2T2.cpp.

134  {
135 // **********************
136  // Set the acceptance probability to zero
137  double acc_prob = 0.0;
138 
139  // Calculate acceptance probability
140  if (anneal) {
141  acc_prob = std::min(1., std::exp(-(logLProp - logLCurr) / (std::exp(-step / AnnealTemp))));
142  } else {
143  acc_prob = std::min(1., std::exp(logLCurr - logLProp));
144  }
145  return acc_prob;
146 }
double logLProp
proposed likelihood
Definition: FitterBase.h:127
unsigned int step
current state
Definition: FitterBase.h:123
double logLCurr
current likelihood
Definition: FitterBase.h:125

◆ DoStep()

void MR2T2::DoStep ( )
overrideprotectedvirtual

The MCMC step proposal and acceptance.

Implements MCMCBase.

Definition at line 18 of file MR2T2.cpp.

18  {
19 // *******************
20  ProposeStep();
21  // Does the MCMC accept this step?
23 
25  {
26  AcceptStep();
27  }
28 }
double accProb
current acceptance prob
Definition: FitterBase.h:129
void AcceptStep()
Accept a step.
Definition: MCMCBase.cpp:244
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:226
void ProposeStep() final
Propose a step.
Definition: MR2T2.cpp:32
double AcceptanceProbability() override
Step acceptance probability.
Definition: MR2T2.cpp:134

◆ ProposeStep()

void MR2T2::ProposeStep ( )
finalprotectedvirtual

Propose a step.

Implements MCMCBase.

Definition at line 32 of file MR2T2.cpp.

32  {
33 // *******************
34  // Initial likelihood
35  out_of_bounds = false;
36 
37  double llh = 0.0;
38 
39  // Loop over the systematics and propose the initial step
40  for (size_t s = 0; s < systematics.size(); ++s)
41  {
42  // Could throw the initial value here to do MCMC stability studies
43  // Propose the steps for the systematics
44  systematics[s]->ProposeStep();
45 
46  // Get the likelihood from the systematics
47  syst_llh[s] = systematics[s]->GetLikelihood();
48  llh += syst_llh[s];
49 
50 #ifdef MACH3_DEBUG
51  if (debug)
52  debugFile << "LLH after " << systematics[s]->GetName() << " " << llh << std::endl;
53 #endif
54  }
55 
56  // if we're using the multicanonical method, we need to add the penalty to the
57  // likelihood now prior to the Large LLH check
58  if (multicanonical) {
59  // get the proposed value of delta_cp and apply the multicanonical penalty,
60  // weighting it using the beta value to increase or decrease the strength of
61  // the penalty
62 
63  double multicanonical_penalty = multicanonicalHandler->GetMulticanonicalWeight();
64 
65 #ifdef MACH3_DEBUG
66  // Print the multicanonical penalty and the delta_cp and delm23 values to the debug file
67  if (debug) {
68  auto delta_cp_value = *multicanonicalHandler->multicanonicalVarValue;
69  auto delm23_value = *multicanonicalHandler->multicanonicalVar_dm23_value;
70  debugFile << " delta_cp: " << delta_cp_value << " delm23: " << delm23_value << " multicanonical_penalty: " << multicanonical_penalty << std::endl;
71  }
72 #endif
73 
74  llh += multicanonical_penalty;
75 
76  MACH3LOG_DEBUG("Delta CP value: {}", *multicanonicalHandler->multicanonicalVarValue);
77  MACH3LOG_DEBUG("Multicanonical penalty: {}", multicanonical_penalty);
78  MACH3LOG_DEBUG("LLH after multicanonical penalty: {}", llh);
79  }
80 
81  // Check if we've hit a boundary in the systematics
82  // In this case we can save time by not having to reconfigure the simulation
83  if (llh >= M3::_LARGE_LOGL_)
84  {
85  out_of_bounds = true;
86 #ifdef MACH3_DEBUG
87  if (debug)
88  debugFile << "Rejecting based on boundary" << std::endl;
89 #endif
90  }
91 
92  // Only reweight when we have a good parameter configuration
93  // This speeds things up considerably because for every bad parameter configuration we don't have to reweight the MC
94  if (!out_of_bounds)
95  {
96  // Could multi-thread this
97  // But since sample reweight is multi-threaded it's probably better to do that
98  for (size_t i = 0; i < samples.size(); ++i)
99  {
100  samples[i]->Reweight();
101  }
102 
103  // DB for atmospheric event by event sample migration, need to fully reweight all samples to allow event passing prior to likelihood evaluation
104  for (size_t i = 0; i < samples.size(); ++i)
105  {
106  // Get the sample likelihoods and add them
107  sample_llh[i] = samples[i]->GetLikelihood();
108  llh += sample_llh[i];
109 #ifdef MACH3_DEBUG
110  if (debug)
111  debugFile << "LLH after sample " << i << " " << llh << std::endl;
112 #endif
113  }
114  // For when we don't have to reweight, set sample to madness
115  }
116  else
117  {
118  for (size_t i = 0; i < samples.size(); ++i)
119  {
120  // Set the sample_llh[i] to be madly high also to signify a step out of bounds
122 #ifdef MACH3_DEBUG
123  if (debug)
124  debugFile << "LLH after REJECT sample " << i << " " << llh << std::endl;
125 #endif
126  }
127  }
128  // Save the proposed likelihood (class member)
129  logLProp = llh;
130 }
#define MACH3LOG_DEBUG
Definition: MaCh3Logger.h:34
std::vector< double > sample_llh
store the llh breakdowns
Definition: FitterBase.h:136
std::vector< SampleHandlerInterface * > samples
Sample holder.
Definition: FitterBase.h:141
std::vector< double > syst_llh
systematic llh breakdowns
Definition: FitterBase.h:138
std::vector< ParameterHandlerBase * > systematics
Systematic holder.
Definition: FitterBase.h:146
std::unique_ptr< MulticanonicalMCMCHandler > multicanonicalHandler
multicanonical handler for umbrella sampling
Definition: MCMCBase.h:67
bool multicanonical
multi-canonical method toggle on/off
Definition: MCMCBase.h:79
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

Member Data Documentation

◆ anneal

bool MR2T2::anneal
protected

simulated annealing

Definition at line 44 of file MR2T2.h.

◆ AnnealTemp

double MR2T2::AnnealTemp
protected

simulated annealing temperature

Definition at line 46 of file MR2T2.h.


The documentation for this class was generated from the following files: