Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adaptive MCMC in the refactor! #79

Merged
merged 11 commits into from
Jul 19, 2024
Merged

Conversation

henry-wallace-phys
Copy link
Contributor

Pull request description:

Ports adaptive MCMC to the MaCh3 refactor.

Changes or fixes:

  • Easier to use interface, now takes in a YAML node of the form
AdaptionOptions:
  Settings:
    # When do we start throwing from our adaptive matrix?
    AdaptionStartThrow : 2000
    # When do we start putting steps into our adaptive covariance?
    AdaptionStartUpdate : 0
    # When do we end updating our covariance?
    AdaptionEndUpdate : 50000
    # How often do we change our matrix throws?
    AdaptionUpdateStep : 1000
  Covariance:
    # So now we list individual matrices, let's just do xsec
    xsec_cov:
      # Do we want to adapt this matrix?
      DoAdaption: True
      # So we can separate out flux and xsec
      MatrixBlocks: [ [45, 145] ]
      # External Settings
      UseExternalMatrix: false
      ExternalMatrixFileName: ""
      ExternalMatrixName: ""
      ExternalMeansName: ""
  • Updated adaptive MCMC calculation to use a block matrix approach.
  • Code is slightly more readable [...hopefully]

Examples:

Fit performed on JUST parameter priors

test_t2k_outputfile_not_delayed_fixdcp_python.pdf_posteriors.pdf

Copy link
Member

@KSkwarczynski KSkwarczynski left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks a lot for this

Comment on lines 1421 to 1425
// HW : Here be adaption
void covarianceBase::initialiseAdaption(manager* fitMan){
/*
HW: Idea is that adaption can simply read the YAML config
Options :
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you pass const YAML::Node& Adapt isntead of fitMan.
Seem cleaner

Comment on lines 1476 to 1482
}

// Finally, we accept that we want to read the matrix from a file!

setThrowMatrixFromFile(fitMan->raw()["AdaptionOptions"]["Covariance"][matrixName_str]["ExternalMatrixFileName"].as<std::string>(),
fitMan->raw()["AdaptionOptions"]["Covariance"][matrixName_str]["ExternalMatrixName"].as<std::string>(),
fitMan->raw()["AdaptionOptions"]["Covariance"][matrixName_str]["ExternalMeansName"].as<std::string>());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add some prinout wiht basic info about adaptive settings? Additional Verbose is always helpfull

Comment on lines 1458 to 1461
start_adaptive_throw = fitMan->raw()["AdaptionOptions"]["Settings"]["AdaptionStartThrow"].as<int>();
start_adaptive_update = fitMan->raw()["AdaptionOptions"]["Settings"]["AdaptionStartUpdate"].as<int>();
end_adaptive_update = fitMan->raw()["AdaptionOptions"]["Settings"]["AdaptionEndUpdate"].as<int>();
adaptive_update_step = fitMan->raw()["AdaptionOptions"]["Settings"]["AdaptionUpdateStep"].as<int>();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you use GetFromManager function to set default values and avoid crushes due to bad yaml?

Comment on lines 530 to 533
// Indices for block-matrix adaption
std::vector<int> adapt_block_matrix_indices;
// Size of blocks for adaption
std::vector<int> adapt_block_sizes;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nitpicky but if you replace // with /// above variable then this is recongised by Doxygen. Would be nice to have this in doxy docueamtnion

Comment on lines 1590 to 1592
adaptiveCovariance = new TMatrixDSym(size);
adaptiveCovariance->Zero();
par_means = std::vector<double>(size, 0);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I just noticed this but adaptiveCovariance is not being destroyed in destructor

if(block_lb>getNpars() || block_ub>getNpars()){
MACH3LOG_ERROR("Cannot set matrix block with edges {}, {} for matrix of size {}",
block_lb, block_ub, getNpars());
throw;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you replace throw with
throw MaCh3Exception(__FILE__ , __LINE__ );

outFile->cd();
adaptiveCovariance->Write(systematicName+"_postfit_matrix");
outMeanVec->Write(systematicName+"_mean_vec");
outFile->Close();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would delete outMeanVec just in case. i don't trust ROOT

void covarianceBase::setThrowMatrixFromFile(std::string matrix_file_name, std::string matrix_name, std::string means_name){
// Lets you set the throw matrix externally
// Open file
std::unique_ptr<TFile>matrix_file(new TFile(matrix_file_name.c_str()));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OMG more smart poiinters

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

SMART POINTERS 4 LIFE

Comment on lines 520 to 527
// When do we start throwing?
int start_adaptive_throw;
//Thresholds for when to turn on/off updating adaptive MCMC
int start_adaptive_update;
// When do we stop update the adaptive matrix?
int end_adaptive_update;
// Steps between changing throw matrix
int adaptive_update_step;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder wheter it make sesne to make small adaptation struct. Might be cleaner way to separate adaptive way.
Also if most of adaptive things are in struct then it is easier to move stuff from cov to Actual MCMC algorithm. Not that it will happen soon.

Up to you

@KSkwarczynski KSkwarczynski merged commit f44d96d into develop Jul 19, 2024
3 checks passed
@KSkwarczynski KSkwarczynski deleted the feature_adaption_update branch July 19, 2024 10:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants