28 const std::vector<double>* parameters,
const std::vector<double>* fixed) {
51 if(!adapt_manager[
"AdaptionOptions"][
"Covariance"][matrix_name_str]) {
52 MACH3LOG_WARN(
"Adaptive Settings not found for {}, this is fine if you don't want adaptive MCMC", matrix_name_str);
57 if(!GetFromManager<bool>(adapt_manager[
"AdaptionOptions"][
"Covariance"][matrix_name_str][
"DoAdaption"],
false)) {
62 if(!
CheckNodeExists(adapt_manager,
"AdaptionOptions",
"Settings",
"OutputFileName")) {
63 MACH3LOG_ERROR(
"No OutputFileName specified in AdaptionOptions::Settings into your config file");
68 start_adaptive_throw = GetFromManager<int>(adapt_manager[
"AdaptionOptions"][
"Settings"][
"StartThrow"], 10);
69 start_adaptive_update = GetFromManager<int>(adapt_manager[
"AdaptionOptions"][
"Settings"][
"StartUpdate"], 0);
70 end_adaptive_update = GetFromManager<int>(adapt_manager[
"AdaptionOptions"][
"Settings"][
"EndUpdate"], 10000);
71 adaptive_update_step = GetFromManager<int>(adapt_manager[
"AdaptionOptions"][
"Settings"][
"UpdateStep"], 100);
73 output_file_name = GetFromManager<std::string>(adapt_manager[
"AdaptionOptions"][
"Settings"][
"OutputFileName"],
"");
84 auto matrix_blocks = GetFromManager<std::vector<std::vector<int>>>(adapt_manager[
"AdaptionOptions"][
"Covariance"][matrix_name_str][
"MatrixBlocks"], {{}});
114 if(block_indices.size()==0 || block_indices[0].size()==0)
return;
116 int block_size =
static_cast<int>(block_indices.size());
118 for(
int iblock=0; iblock < block_size; iblock++){
120 int sub_block_size =
static_cast<int>(block_indices.size()-1);
121 for(
int isubblock=0; isubblock < sub_block_size ; isubblock+=2){
122 int block_lb = block_indices[iblock][isubblock];
123 int block_ub = block_indices[iblock][isubblock+1];
126 MACH3LOG_ERROR(
"Cannot set matrix block with edges {}, {} for matrix of size {}",
130 for(
int ipar = block_lb; ipar < block_ub; ipar++){
142 const std::string &systematicName,
bool is_final) {
151 TFile *outFile =
new TFile(outFileName.c_str(),
"UPDATE");
152 if (outFile->IsZombie()) {
157 TVectorD *outMeanVec =
new TVectorD(
int(
par_means.size()));
158 for (
int i = 0; i < int(
par_means.size()); i++) {
162 std::string adaptive_cov_name = systematicName +
"_posfit_matrix";
163 std::string mean_vec_name = systematicName +
"_mean_vec";
166 std::string total_steps_str =
168 std::string syst_name_str = systematicName;
170 total_steps_str +
'_' + syst_name_str + std::string(
"_throw_matrix");
172 total_steps_str +
'_' + syst_name_str + std::string(
"_mean_vec");
177 outMeanVec->Write(mean_vec_name.c_str());
188 const std::string& matrix_name,
189 const std::string& means_name,
190 bool& use_adaptive) {
194 auto matrix_file = std::make_unique<TFile>(matrix_file_name.c_str());
197 if(matrix_file->IsZombie()){
205 MACH3LOG_ERROR(
"Couldn't find {} in {}", matrix_name, matrix_file_name);
210 TVectorD* means_vector =
static_cast<TVectorD*
>(matrix_file->Get(means_name.c_str()));
216 if(means_vector->GetNrows()){
225 MACH3LOG_INFO(
"Found Means in External File, Will be able to adapt");
230 MACH3LOG_WARN(
"Cannot find means vector in {}, therefore I will not be able to adapt!", matrix_file_name);
231 use_adaptive =
false;
234 matrix_file->Close();
241 std::vector<double> par_means_prev =
par_means;
248 par_means[i] = (
CurrVal(i) + par_means_prev[i]*steps_post_burn)/(steps_post_burn+1);
254 #pragma omp parallel for
258 (*adaptive_covariance)(i, i) = 1.0;
264 for (
int j = 0; j <= i; ++j) {
266 (*adaptive_covariance)(i, j) = 0.0;
267 (*adaptive_covariance)(j, i) = 0.0;
271 double cov_prev = (*adaptive_covariance)(i, j);
272 double cov_updated = 0.0;
274 if (steps_post_burn > 0) {
276 double cov_t = cov_prev * (steps_post_burn - 1) / steps_post_burn;
277 double prev_means_t = steps_post_burn * par_means_prev[i] * par_means_prev[j];
281 cov_updated = cov_t +
adaption_scale * (prev_means_t - curr_means_t + curr_step_t) / steps_post_burn;
284 (*adaptive_covariance)(i, j) = cov_updated;
285 (*adaptive_covariance)(j, i) = cov_updated;
bool CheckNodeExists(const YAML::Node &node, Args... args)
KS: Wrapper function to call the recursive helper.
Custom exception class for MaCh3 errors.
std::vector< double > par_means
Mean values for all parameters.
bool IsFixed(const int ipar) const
Check if a parameter is fixed.
bool UpdateMatrixAdapt()
Tell whether matrix should be updated.
std::vector< int > adapt_block_matrix_indices
Indices for block-matrix adaption.
bool InitFromConfig(const YAML::Node &adapt_manager, const std::string &matrix_name_str, const std::vector< double > *parameters, const std::vector< double > *fixed)
Read initial values from config file.
std::string output_file_name
Name of the file to save the adaptive matrices into.
double adaption_scale
Scaling factor.
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.
std::vector< int > adapt_block_sizes
Size of blocks for adaption.
virtual ~AdaptiveMCMCHandler()
Destructor.
int adaptive_save_n_iterations
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.
AdaptiveMCMCHandler()
Constructor.
void CreateNewAdaptiveCovariance()
If we don't have a covariance matrix to start from for adaptive tune we need to make one!
bool SkipAdaption() const
Tell if we are Skipping Adaption.
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...
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
TMatrixDSym * adaptive_covariance
Full adaptive covariance matrix.
int GetNumParams() const
Get the current values of the parameters.