MaCh3  2.4.2
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 namespace adaptive_mcmc
12 {
27  {
28  public:
31 
33  virtual ~AdaptiveMCMCHandler();
34 
36  void Print() const;
37 
45  bool InitFromConfig(const YAML::Node &adapt_manager, const std::string &matrix_name_str,
46  const std::vector<std::string> *parameter_names,
47  const std::vector<double> *parameters,
48  const std::vector<double> *fixed,
49  const std::vector<bool> *param_skip_adapt_flags,
50  const TMatrixDSym* throwMatrix,
51  const double initial_scale_);
52 
55 
58  void SetAdaptiveBlocks(const std::vector<std::vector<int>> &block_indices);
59 
61  void SaveAdaptiveToFile(const std::string &outFileName, const std::string &systematicName, const bool is_final = false);
62 
67  void SetThrowMatrixFromFile(const std::string &matrix_file_name,
68  const std::string &matrix_name,
69  const std::string &means_name,
70  bool &use_adaptive);
71 
75  void CheckMatrixValidityForAdaption(const TMatrixDSym *covMatrix) const;
76 
81 
83  bool IndivStepScaleAdapt() const;
84 
86  bool UpdateMatrixAdapt();
87 
89  bool AdaptionUpdate() const;
90 
92  bool SkipAdaption() const;
93 
97  int GetParIndex(const std::string& name) const {
98  // HH: Adapted from ParameterHandlerBase.cpp
99  int index = M3::_BAD_INT_;
100  for (size_t i = 0; i < _ParamNames->size(); i++) {
101  if(name == _ParamNames->at(i)) {
102  index = static_cast<int>(i);
103  break;
104  }
105  }
106  return index;
107  }
108 
112  std::vector<int> GetParIndicesFromNames(const std::vector<std::string>& param_names) const {
113  std::vector<int> indices;
114  for (const auto& name : param_names) {
115  int index = GetParIndex(name);
116  if (index != M3::_BAD_INT_) {
117  indices.push_back(index);
118  } else {
119  MACH3LOG_ERROR("Parameter name {} not found in parameter list", name);
120  throw MaCh3Exception(__FILE__, __LINE__);
121  }
122  }
123  return indices;
124  }
125 
127  void SetParams(const std::vector<double> *params)
128  {
129  _fCurrVal = params;
130  }
131 
133  void SetFixed(const std::vector<double> *fix)
134  {
135  _fFixedPars = fix;
136  }
137 
140  int GetNFixed() const
141  {
142  if (!_fFixedPars)
143  {
144  return 0;
145  }
146  int n_fixed = 0;
147  for (int i = 0; i < static_cast<int>(_fFixedPars->size()); i++)
148  {
149  if ((*_fFixedPars)[i] < 0)
150  {
151  n_fixed++;
152  }
153  }
154  return n_fixed;
155  }
156 
159  int GetNumParams() const
160  {
161  return static_cast<int>(_fCurrVal->size());
162  }
163 
165  bool IsFixed(const int ipar) const
166  {
167  if (!_fFixedPars)
168  {
169  return false;
170  }
171  return ((*_fFixedPars)[ipar] < 0);
172  }
173 
175  double CurrVal(const int par_index) const;
176 
179  int GetTotalSteps() const
180  {
181  return total_steps;
182  }
183 
185  void SetTotalSteps(const int nsteps)
186  {
187  total_steps = nsteps;
188  }
189 
192  {
193  total_steps++;
194  }
195 
197  {
198  prev_step_accepted = true;
199  }
200 
203  TMatrixDSym *GetAdaptiveCovariance() const
204  {
205  return adaptive_covariance;
206  }
207 
210  std::vector<double> GetParameterMeans() const
211  {
212  return par_means;
213  }
214 
217  std::string GetOutFileName() const
218  {
219  return output_file_name;
220  }
221 
224  {
225  return adaption_scale;
226  }
227 
229  bool GetUseRobbinsMonro() const
230  {
231  return use_robbins_monro;
232  }
233 
236 
237  private:
240 
244 
247 
250 
253 
257 
259  std::string output_file_name;
260 
262  std::vector<int> adapt_block_matrix_indices;
263 
265  std::vector<int> adapt_block_sizes;
266 
267  // Variables directedly linked to adaption
269  std::vector<double> par_means;
270 
272  TMatrixDSym *adaptive_covariance;
273 
276 
279 
282 
284  const std::vector<double> *_fFixedPars;
285 
287  const std::vector<double> *_fCurrVal;
288 
290  const std::vector<std::string> *_ParamNames;
291 
294 
297 
300 
303 
306 
309 
312 
314  const std::vector<bool>* _param_skip_adapt_flags;
315 
317  const TMatrixDSym* initial_throw_matrix;
318 
321  };
322 } // adaptive_mcmc namespace
#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:140
#define MACH3LOG_ERROR
Definition: MaCh3Logger.h:37
Custom exception class used throughout MaCh3.
Contains information about adaptive covariance matrix .
std::vector< double > par_means
Mean values for all parameters.
const std::vector< std::string > * _ParamNames
Parameter names.
bool IsFixed(const int ipar) const
Check if a parameter is fixed.
int GetNFixed() const
Get the number of fixed parameters.
double GetAdaptionScale()
Get the current adaption scale.
bool UpdateMatrixAdapt()
Tell whether matrix should be updated.
std::vector< int > adapt_block_matrix_indices
Indices for block-matrix adaption.
int GetTotalSteps() const
Get Total Number of Steps.
std::string output_file_name
Name of the file to save the adaptive matrices into.
void CheckMatrixValidityForAdaption(const TMatrixDSym *covMatrix) const
Check if there are structures in matrix that could result in failure of adaption fits....
std::vector< int > GetParIndicesFromNames(const std::vector< std::string > &param_names) const
Convert vector of parameter names to indices.
void SaveAdaptiveToFile(const std::string &outFileName, const std::string &systematicName, const bool is_final=false)
HW: Save adaptive throw matrix to file.
const std::vector< double > * _fCurrVal
Current values of parameters.
int start_adaptive_update
When do we stop update the adaptive matrix.
double target_acceptance
Target acceptance rate for Robbins Monro.
int acceptance_rate_batch_size
Acceptance rate in the current batch.
std::vector< int > adapt_block_sizes
Size of blocks for adaption.
double initial_scale
Initial scaling factor.
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.
const std::vector< double > * _fFixedPars
Vector of fixed parameters.
bool IndivStepScaleAdapt() const
Tell whether we want reset step scale or not.
void SetFixed(const std::vector< double > *fix)
Set the fixed parameters.
void SetParams(const std::vector< double > *params)
Set the current values of the parameters.
void CalculateRobbinsMonroStepLength()
Calculate the constant step length for Robbins-Monro adaption.
void SetTotalSteps(const int nsteps)
Change Total Number of Steps to new value.
bool GetUseRobbinsMonro() const
Use Robbins-Monro approach?
void CreateNewAdaptiveCovariance()
If we don't have a covariance matrix to start from for adaptive tune we need to make one!
TMatrixDSym * GetAdaptiveCovariance() const
Increase by one number of total steps.
bool SkipAdaption() const
Tell if we are Skipping Adaption.
const std::vector< bool > * _param_skip_adapt_flags
Parameters to skip during adaption.
int GetParIndex(const std::string &name) const
Get the index of the parameter given its name.
std::string GetOutFileName() const
Get Name of Output File.
int end_adaptive_update
Steps between changing throw matrix.
int total_steps
Total number of MCMC steps.
void Print() const
Print all class members.
void UpdateAdaptiveCovariance()
Method to update adaptive MCMC .
double CurrVal(const int par_index) const
Get Current value of parameter.
int adaptive_update_step
Steps between changing throw matrix.
bool AdaptionUpdate() const
To be fair not a clue...
int n_rm_restarts
Number of restarts for Robbins Monro (so far)
double c_robbins_monro
Constant "step scaling" factor for Robbins-Monro.
bool use_robbins_monro
Use Robbins Monro https://arxiv.org/pdf/1006.3690.
void SetAdaptiveBlocks(const std::vector< std::vector< int >> &block_indices)
HW: sets adaptive block matrix.
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
int GetNumParams() const
Get the current values of the parameters.
std::vector< double > GetParameterMeans() const
Get the parameter means used in the adaptive handler.
void UpdateRobbinsMonroScale()
Update the scale factor for Robbins-Monro adaption.
bool prev_step_accepted
Need to keep track of whether previous step was accepted for RM.
bool initial_throw_matrix_saved
Flag to check if initial throw matrix has been saved.
int total_rm_restarts
Total number of restarts ALLOWED for Robbins Monro.
const TMatrixDSym * initial_throw_matrix
Initial throw matrix.
TMatrixDSym * adaptive_covariance
Full adaptive covariance matrix.
void IncrementNSteps()
Increase by one number of total steps.
constexpr static const int _BAD_INT_
Default value used for int initialisation.
Definition: Core.h:55