MaCh3  2.5.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 
139  int GetNFixed() const
140  {
141  if (!_fFixedPars)
142  {
143  return 0;
144  }
145  int n_fixed = 0;
146  for (int i = 0; i < static_cast<int>(_fFixedPars->size()); i++)
147  {
148  if ((*_fFixedPars)[i] < 0)
149  {
150  n_fixed++;
151  }
152  }
153  return n_fixed;
154  }
155 
158  int GetNumParams() const
159  {
160  return static_cast<int>(_fCurrVal->size());
161  }
162 
164  bool IsFixed(const int ipar) const
165  {
166  if (!_fFixedPars)
167  {
168  return false;
169  }
170  return ((*_fFixedPars)[ipar] < 0);
171  }
172 
174  double CurrVal(const int par_index) const;
175 
178  int GetTotalSteps() const
179  {
180  return total_steps;
181  }
182 
184  void SetTotalSteps(const int nsteps)
185  {
186  total_steps = nsteps;
187  }
188 
191  {
192  total_steps++;
193  }
194 
196  {
197  prev_step_accepted = true;
198  }
199 
202  TMatrixDSym *GetAdaptiveCovariance() const
203  {
204  return adaptive_covariance;
205  }
206 
209  std::vector<double> GetParameterMeans() const
210  {
211  return par_means;
212  }
213 
216  std::string GetOutFileName() const
217  {
218  return output_file_name;
219  }
220 
223  {
224  return adaption_scale;
225  }
226 
228  bool GetUseRobbinsMonro() const
229  {
230  return use_robbins_monro;
231  }
232 
235 
236 private:
239 
243 
246 
249 
252 
256 
258  std::string output_file_name;
259 
261  std::vector<int> adapt_block_matrix_indices;
262 
264  std::vector<int> adapt_block_sizes;
265 
266  // Variables directedly linked to adaption
268  std::vector<double> par_means;
269 
271  TMatrixDSym *adaptive_covariance;
272 
275 
278 
281 
283  const std::vector<double> *_fFixedPars;
284 
286  const std::vector<double> *_fCurrVal;
287 
289  const std::vector<std::string> *_ParamNames;
290 
293 
296 
299 
302 
305 
308 
311 
313  const std::vector<bool>* _param_skip_adapt_flags;
314 
316  const TMatrixDSym* initial_throw_matrix;
317 
320 };
#define _MaCh3_Safe_Include_Start_
KS: Avoiding warning checking for headers.
Definition: Core.h:126
#define _MaCh3_Safe_Include_End_
KS: Restore warning checking after including external headers.
Definition: Core.h:141
#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.
double GetAdaptionScale()
Get the current adaption scale.
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.
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.
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 * GetAdaptiveCovariance() const
Increase by one number of total steps.
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