32 const std::vector<std::string>* parameter_names,
33 const std::vector<double>* parameters,
34 const std::vector<double>* fixed,
35 const std::vector<bool>* param_skip_adapt_flags,
36 const TMatrixDSym* throwMatrix,
37 const double initial_scale_) {
60 if(!adapt_manager[
"AdaptionOptions"][
"Covariance"][matrix_name_str]) {
61 MACH3LOG_WARN(
"Adaptive Settings not found for {}, this is fine if you don't want adaptive MCMC", matrix_name_str);
66 if(!GetFromManager<bool>(adapt_manager[
"AdaptionOptions"][
"Covariance"][matrix_name_str][
"DoAdaption"],
false)) {
71 if(!
CheckNodeExists(adapt_manager,
"AdaptionOptions",
"Settings",
"OutputFileName")) {
72 MACH3LOG_ERROR(
"No OutputFileName specified in AdaptionOptions::Settings into your config file");
77 start_adaptive_throw = GetFromManager<int>(adapt_manager[
"AdaptionOptions"][
"Settings"][
"StartThrow"], 10);
78 start_adaptive_update = GetFromManager<int>(adapt_manager[
"AdaptionOptions"][
"Settings"][
"StartUpdate"], 0);
79 end_adaptive_update = GetFromManager<int>(adapt_manager[
"AdaptionOptions"][
"Settings"][
"EndUpdate"], 10000);
80 adaptive_update_step = GetFromManager<int>(adapt_manager[
"AdaptionOptions"][
"Settings"][
"UpdateStep"], 100);
82 output_file_name = GetFromManager<std::string>(adapt_manager[
"AdaptionOptions"][
"Settings"][
"OutputFileName"],
"");
85 use_robbins_monro = GetFromManager<bool>(adapt_manager[
"AdaptionOptions"][
"Settings"][
"UseRobbinsMonro"],
false);
86 target_acceptance = GetFromManager<double>(adapt_manager[
"AdaptionOptions"][
"Settings"][
"TargetAcceptance"], 0.234);
87 if (target_acceptance <= 0 || target_acceptance >= 1) {
92 acceptance_rate_batch_size = GetFromManager<int>(adapt_manager[
"AdaptionOptions"][
"Settings"][
"AcceptanceRateBatchSize"], 10000);
93 total_rm_restarts = GetFromManager<int>(adapt_manager[
"AdaptionOptions"][
"Settings"][
"TotalRobbinsMonroRestarts"], 0);
120 bool matrix_blocks_by_name = GetFromManager<bool>(adapt_manager[
"AdaptionOptions"][
"Covariance"][matrix_name_str][
"MatrixBlocksByName"],
false);
121 std::vector<std::vector<int>> matrix_blocks;
123 if (matrix_blocks_by_name) {
124 auto matrix_blocks_input = GetFromManager<std::vector<std::vector<std::string>>>(adapt_manager[
"AdaptionOptions"][
"Covariance"][matrix_name_str][
"MatrixBlocks"], {{}});
126 for (
const auto& block : matrix_blocks_input) {
128 matrix_blocks.push_back(block_indices);
131 matrix_blocks = GetFromManager<std::vector<std::vector<int>>>(adapt_manager[
"AdaptionOptions"][
"Covariance"][matrix_name_str][
"MatrixBlocks"], {{}});
165 if(block_indices.size()==0 || block_indices[0].size()==0)
return;
167 int block_size =
static_cast<int>(block_indices.size());
169 for(
int iblock=0; iblock < block_size; iblock++){
171 int sub_block_size =
static_cast<int>(block_indices.size()-1);
172 for(
int isubblock=0; isubblock < sub_block_size ; isubblock+=2){
173 int block_lb = block_indices[iblock][isubblock];
174 int block_ub = block_indices[iblock][isubblock+1];
177 MACH3LOG_ERROR(
"Cannot set matrix block with edges {}, {} for matrix of size {}",
181 for(
int ipar = block_lb; ipar < block_ub; ipar++){
193 const std::string &systematicName,
bool is_final) {
201 TFile *outFile =
new TFile(outFileName.c_str(),
"UPDATE");
202 if (outFile->IsZombie()) {
207 TVectorD *outMeanVec =
new TVectorD(
int(
par_means.size()));
208 for (
int i = 0; i < int(
par_means.size()); i++) {
212 std::string adaptive_cov_name = systematicName +
"_posfit_matrix";
213 std::string mean_vec_name = systematicName +
"_mean_vec";
216 std::string total_steps_str =
218 std::string syst_name_str = systematicName;
220 total_steps_str +
'_' + syst_name_str + std::string(
"_throw_matrix");
222 total_steps_str +
'_' + syst_name_str + std::string(
"_mean_vec");
230 for (
int j = 0; j <= i; ++j) {
255 adaptive_to_save->Write(adaptive_cov_name.c_str());
256 outMeanVec->Write(mean_vec_name.c_str());
260 std::string initial_cov_name =
"0_" + systematicName + std::string(
"_initial_throw_matrix");
264 initial_throw_matrix_to_save->Write(initial_cov_name.c_str());
265 delete initial_throw_matrix_to_save;
269 delete adaptive_to_save;
279 const std::string& matrix_name,
280 const std::string& means_name,
281 bool& use_adaptive) {
285 auto matrix_file = std::make_unique<TFile>(matrix_file_name.c_str());
288 if(matrix_file->IsZombie()){
296 MACH3LOG_ERROR(
"Couldn't find {} in {}", matrix_name, matrix_file_name);
301 TVectorD* means_vector =
static_cast<TVectorD*
>(matrix_file->Get(means_name.c_str()));
316 MACH3LOG_INFO(
"Found Means in External File, Will be able to adapt");
321 MACH3LOG_WARN(
"Cannot find means vector in {}, therefore I will not be able to adapt!", matrix_file_name);
322 use_adaptive =
false;
325 matrix_file->Close();
332 std::vector<double> par_means_prev =
par_means;
341 par_means[i] = (
CurrVal(i) + par_means_prev[i]*steps_post_burn)/(steps_post_burn+1);
347 #pragma omp parallel for
351 (*adaptive_covariance)(i, i) = 1.0;
357 for (
int j = 0; j <= i; ++j) {
359 (*adaptive_covariance)(i, j) = 0.0;
360 (*adaptive_covariance)(j, i) = 0.0;
364 double cov_prev = (*adaptive_covariance)(i, j);
365 double cov_updated = 0.0;
382 if (steps_post_burn > 0) {
384 double cov_t = cov_prev * (steps_post_burn - 1) / steps_post_burn;
385 double prev_means_t = steps_post_burn * par_means_prev[i] * par_means_prev[j];
389 cov_updated = cov_t + (prev_means_t - curr_means_t + curr_step_t) / steps_post_burn;
392 (*adaptive_covariance)(i, j) = cov_updated;
393 (*adaptive_covariance)(j, i) = cov_updated;
450 int n = covMatrix->GetNrows();
451 std::vector<std::vector<double>> corrMatrix(n, std::vector<double>(n));
454 std::vector<double> stdDevs(n);
455 for (
int i = 0; i < n; ++i) {
456 stdDevs[i] = std::sqrt((*covMatrix)(i, i));
460 for (
int i = 0; i < n; ++i) {
461 for (
int j = 0; j < n; ++j) {
462 double cov = (*covMatrix)(i, j);
463 double corr = cov / (stdDevs[i] * stdDevs[j]);
464 corrMatrix[i][j] = corr;
469 constexpr
double NumberOfParametersThreshold = 5;
470 constexpr
double CorrelationThreshold = 0.5;
472 bool FlagMessage =
false;
474 for (
int i = 0; i < n; ++i) {
479 int correlatedCount = 0;
480 std::vector<int> correlatedWith;
482 for (
int j = 0; j < n; ++j) {
486 if (corrMatrix[i][j] > CorrelationThreshold) {
488 correlatedWith.push_back(j);
492 if (correlatedCount > NumberOfParametersThreshold) {
493 MACH3LOG_WARN(
"Parameter {} ({}) is highly correlated with {} other parameters (above threshold {}).",
_ParamNames->at(i), i, correlatedCount, CorrelationThreshold);
494 MACH3LOG_WARN(
"Correlated with parameters: {}.", fmt::join(correlatedWith,
", "));
499 MACH3LOG_WARN(
"Found highly correlated block, adaptive may not work as intended, proceed with caution");
500 MACH3LOG_WARN(
"You can consider defying so called Adaption Block to not update this part of matrix");
527 if(non_fixed_pars == 0){
528 MACH3LOG_ERROR(
"Number of non_fixed_pars is 0, have you fixed all parameters");
533 c_robbins_monro = (1- 1/non_fixed_pars)*std::sqrt(TMath::Pi()*2 * std::exp(alpha*alpha));
bool CheckNodeExists(const YAML::Node &node, Args... args)
KS: Wrapper function to call the recursive helper.
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 adaptive_save_n_iterations
int GetNumParams() const
Get the current values of the parameters.
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 > ¶m_names) const
Convert vector of parameter names to indices.
void SetFixed(const std::vector< double > *fix)
Set the fixed parameters.
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.
int total_steps
Total number of MCMC steps.
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.
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.
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.
int GetNFixed() const
Get the number of fixed parameters.
virtual ~AdaptiveMCMCHandler()
Destructor.
Custom exception class used throughout MaCh3.