MaCh3  2.6.0
Reference Guide
AdaptiveMCMCHandler.h
Go to the documentation of this file.
1 #pragma once
2 
3 // MaCh3 Includes
4 #include "Manager/Manager.h"
6 
8 #include "Math/DistFunc.h"
10 
11 
26 {
27  public:
30 
32  virtual ~AdaptiveMCMCHandler();
33 
35  void Print() const;
36 
44  bool InitFromConfig(const YAML::Node &adapt_manager, const std::string &matrix_name_str,
45  const std::vector<std::string> *parameter_names,
46  const std::vector<double> *parameters,
47  const std::vector<double> *fixed,
48  const std::vector<bool> *param_skip_adapt_flags,
49  const TMatrixDSym* throwMatrix,
50  const double initial_scale_);
51 
54 
57  void SetAdaptiveBlocks(const std::vector<std::vector<int>> &block_indices);
58 
60  void SaveAdaptiveToFile(const std::string &outFileName, const std::string &systematicName, const bool is_final = false);
61 
66  void SetThrowMatrixFromFile(const std::string &matrix_file_name,
67  const std::string &matrix_name,
68  const std::string &means_name,
69  bool &use_adaptive);
70 
74  void CheckMatrixValidityForAdaption(const TMatrixDSym *covMatrix) const;
75 
80 
82  bool IndivStepScaleAdapt() const;
83 
85  bool UpdateMatrixAdapt();
86 
88  bool AdaptionUpdate() const;
89 
91  bool SkipAdaption() const;
92 
96  int GetParIndex(const std::string& name) const {
97  // HH: Adapted from ParameterHandlerBase.cpp
98  int index = M3::_BAD_INT_;
99  for (size_t i = 0; i < _ParamNames->size(); i++) {
100  if(name == _ParamNames->at(i)) {
101  index = static_cast<int>(i);
102  break;
103  }
104  }
105  return index;
106  }
107 
111  std::vector<int> GetParIndicesFromNames(const std::vector<std::string>& param_names) const {
112  std::vector<int> indices;
113  for (const auto& name : param_names) {
114  int index = GetParIndex(name);
115  if (index != M3::_BAD_INT_) {
116  indices.push_back(index);
117  } else {
118  MACH3LOG_ERROR("Parameter name {} not found in parameter list", name);
119  throw MaCh3Exception(__FILE__, __LINE__);
120  }
121  }
122  return indices;
123  }
124 
126  void SetParams(const std::vector<double> *params)
127  {
128  _fCurrVal = params;
129  }
130 
132  void SetFixed(const std::vector<double> *fix)
133  {
134  _fFixedPars = fix;
135  }
136 
138  int GetNFixed() const
139  {
140  if (!_fFixedPars)
141  {
142  return 0;
143  }
144  int n_fixed = 0;
145  for (int i = 0; i < static_cast<int>(_fFixedPars->size()); i++)
146  {
147  if ((*_fFixedPars)[i] < 0)
148  {
149  n_fixed++;
150  }
151  }
152  return n_fixed;
153  }
154 
156  int GetNumParams() const
157  {
158  return static_cast<int>(_fCurrVal->size());
159  }
160 
162  bool IsFixed(const int ipar) const
163  {
164  if (!_fFixedPars)
165  {
166  return false;
167  }
168  return ((*_fFixedPars)[ipar] < 0);
169  }
170 
172  double CurrVal(const int par_index) const;
173 
175  int GetTotalSteps() const
176  {
177  return total_steps;
178  }
179 
181  void SetTotalSteps(const int nsteps)
182  {
183  total_steps = nsteps;
184  }
185 
188  {
189  total_steps++;
190  }
191 
193  {
194  prev_step_accepted = true;
195  }
196 
198  const TMatrixDSym *GetAdaptiveCovariance() const
199  {
200  return adaptive_covariance;
201  }
202 
204  std::vector<double> GetParameterMeans() const
205  {
206  return par_means;
207  }
208 
210  std::string GetOutFileName() const
211  {
212  return output_file_name;
213  }
214 
216  double GetAdaptionScale() const
217  {
218  return adaption_scale;
219  }
220 
222  bool GetUseRobbinsMonro() const
223  {
224  return use_robbins_monro;
225  }
226 
229 
230 private:
233 
237 
240 
243 
246 
250 
252  std::string output_file_name;
253 
255  std::vector<int> adapt_block_matrix_indices;
256 
258  std::vector<int> adapt_block_sizes;
259 
260  // Variables directedly linked to adaption
262  std::vector<double> par_means;
263 
265  TMatrixDSym *adaptive_covariance;
266 
269 
272 
275 
277  const std::vector<double> *_fFixedPars;
278 
280  const std::vector<double> *_fCurrVal;
281 
283  const std::vector<std::string> *_ParamNames;
284 
287 
290 
293 
296 
299 
302 
305 
307  const std::vector<bool>* _param_skip_adapt_flags;
308 
310  const TMatrixDSym* initial_throw_matrix;
311 
314 };
#define _MaCh3_Safe_Include_Start_
KS: Avoiding warning checking for headers.
Definition: Core.h:126
#define _MaCh3_Safe_Include_End_
#define MACH3LOG_ERROR
Definition: MaCh3Logger.h:37
Contains information about adaptive covariance matrix .
const std::vector< bool > * _param_skip_adapt_flags
Parameters to skip during adaption.
bool IndivStepScaleAdapt() const
Tell whether we want reset step scale or not.
void SaveAdaptiveToFile(const std::string &outFileName, const std::string &systematicName, const bool is_final=false)
HW: Save adaptive throw matrix to file.
bool SkipAdaption() const
Tell if we are Skipping Adaption.
bool AdaptionUpdate() const
To be fair not a clue...
int GetNumParams() const
Get the current values of the parameters.
int GetParIndex(const std::string &name) const
Get the index of the parameter given its name.
void CheckMatrixValidityForAdaption(const TMatrixDSym *covMatrix) const
Check if there are structures in matrix that could result in failure of adaption fits....
const std::vector< std::string > * _ParamNames
Parameter names.
std::vector< int > GetParIndicesFromNames(const std::vector< std::string > &param_names) const
Convert vector of parameter names to indices.
void SetFixed(const std::vector< double > *fix)
Set the fixed parameters.
void IncrementNSteps()
Increase by one number of total steps.
double GetAdaptionScale() const
Get the current adaption scale.
int GetTotalSteps() const
Get Total Number of Steps.
bool UpdateMatrixAdapt()
Tell whether matrix should be updated.
int end_adaptive_update
Steps between changing throw matrix.
const TMatrixDSym * initial_throw_matrix
Initial throw matrix.
std::vector< double > GetParameterMeans() const
Get the parameter means used in the adaptive handler.
int total_steps
Total number of MCMC steps.
const std::vector< double > * _fFixedPars
Vector of fixed parameters.
const TMatrixDSym * GetAdaptiveCovariance() const
Return matrix used for adaption.
void UpdateAdaptiveCovariance()
Method to update adaptive MCMC .
void SetThrowMatrixFromFile(const std::string &matrix_file_name, const std::string &matrix_name, const std::string &means_name, bool &use_adaptive)
sets throw matrix from a file
double target_acceptance
Target acceptance rate for Robbins Monro.
int adaptive_update_step
Steps between changing throw matrix.
void UpdateRobbinsMonroScale()
Update the scale factor for Robbins-Monro adaption.
void SetParams(const std::vector< double > *params)
Set the current values of the parameters.
void CreateNewAdaptiveCovariance()
If we don't have a covariance matrix to start from for adaptive tune we need to make one!
bool IsFixed(const int ipar) const
Check if a parameter is fixed.
bool InitFromConfig(const YAML::Node &adapt_manager, const std::string &matrix_name_str, const std::vector< std::string > *parameter_names, const std::vector< double > *parameters, const std::vector< double > *fixed, const std::vector< bool > *param_skip_adapt_flags, const TMatrixDSym *throwMatrix, const double initial_scale_)
Read initial values from config file.
AdaptiveMCMCHandler()
Constructor.
int start_adaptive_update
When do we stop update the adaptive matrix.
TMatrixDSym * adaptive_covariance
Full adaptive covariance matrix.
double initial_scale
Initial scaling factor.
double adaption_scale
Scaling factor.
bool GetUseRobbinsMonro() const
Use Robbins-Monro approach?
std::string output_file_name
Name of the file to save the adaptive matrices into.
double CurrVal(const int par_index) const
Get Current value of parameter.
int acceptance_rate_batch_size
Acceptance rate in the current batch.
void CalculateRobbinsMonroStepLength()
Calculate the constant step length for Robbins-Monro adaption.
std::vector< int > adapt_block_sizes
Size of blocks for adaption.
std::string GetOutFileName() const
Get Name of Output File.
void Print() const
Print all class members.
std::vector< double > par_means
Mean values for all parameters.
int total_rm_restarts
Total number of restarts ALLOWED for Robbins Monro.
bool initial_throw_matrix_saved
Flag to check if initial throw matrix has been saved.
void SetAdaptiveBlocks(const std::vector< std::vector< int >> &block_indices)
HW: sets adaptive block matrix.
bool use_robbins_monro
Use Robbins Monro https://arxiv.org/pdf/1006.3690.
std::vector< int > adapt_block_matrix_indices
Indices for block-matrix adaption.
bool prev_step_accepted
Need to keep track of whether previous step was accepted for RM.
double c_robbins_monro
Constant "step scaling" factor for Robbins-Monro.
int n_rm_restarts
Number of restarts for Robbins Monro (so far)
const std::vector< double > * _fCurrVal
Current values of parameters.
void SetTotalSteps(const int nsteps)
Change Total Number of Steps to new value.
int GetNFixed() const
Get the number of fixed parameters.
virtual ~AdaptiveMCMCHandler()
Destructor.
Custom exception class used throughout MaCh3.
constexpr static const int _BAD_INT_
Default value used for int initialisation.
Definition: Core.h:55