Line data Source code
1 : //* This file is part of the MOOSE framework 2 : //* https://mooseframework.inl.gov 3 : //* 4 : //* All rights reserved, see COPYRIGHT for full restrictions 5 : //* https://github.com/idaholab/moose/blob/master/COPYRIGHT 6 : //* 7 : //* Licensed under LGPL 2.1, please see LICENSE for details 8 : //* https://www.gnu.org/licenses/lgpl-2.1.html 9 : 10 : #pragma once 11 : 12 : #include "GeneralReporter.h" 13 : #include "PMCMCBase.h" 14 : #include "LikelihoodFunctionBase.h" 15 : #include "LikelihoodInterface.h" 16 : #include "Distribution.h" 17 : 18 : /** 19 : * PMCMCDecision will help making sample accept/reject decisions in MCMC 20 : * schemes (for e.g., when performing Bayesian inference). 21 : */ 22 : class PMCMCDecision : public GeneralReporter, public LikelihoodInterface 23 : { 24 : public: 25 : static InputParameters validParams(); 26 : PMCMCDecision(const InputParameters & parameters); 27 1200 : virtual void initialize() override {} 28 1200 : virtual void finalize() override {} 29 : virtual void execute() override; 30 : 31 : protected: 32 : /** 33 : * Compute the evidence (aka, betterness of the proposed sample vs the previous) 34 : * @param evidence The evidence vector to be filled 35 : * @param input_matrix The matrix of proposed inputs that are provided 36 : */ 37 : virtual void computeEvidence(std::vector<Real> & evidence, 38 : const DenseMatrix<Real> & input_matrix); 39 : 40 : /** 41 : * Compute the transition probability vector (after the computation of evidence) 42 : * @param tv The transition probability vector to be filled 43 : * @param evidence The vector of evidences provided 44 : */ 45 : virtual void computeTransitionVector(std::vector<Real> & tv, const std::vector<Real> & evidence); 46 : 47 : /** 48 : * Resample inputs given the transition vector (after transition vector computed) 49 : * @param req_inputs The vector of accepted samples to be filled 50 : * @param input_matrix The matrix of proposed inputs provided 51 : * @param tv The vector of transition probabilities provided 52 : * @param parallel_index The current parallel proposal index provided 53 : */ 54 : virtual void nextSamples(std::vector<Real> & req_inputs, 55 : DenseMatrix<Real> & input_matrix, 56 : const std::vector<Real> & tv, 57 : const unsigned int & parallel_index); 58 : 59 : /** 60 : * Compute the next set of seeds to facilitate proposals. 61 : * Set to empty in base to permit flexibility for MCMC samplers 62 : */ 63 1000 : virtual void nextSeeds() {} 64 : 65 : /// Model output value from SubApp 66 : const std::vector<Real> & _output_value; 67 : 68 : /// Transfer the right outputs to the file 69 : std::vector<Real> & _outputs_required; 70 : 71 : /// Model input data that is uncertain 72 : std::vector<std::vector<Real>> & _inputs; 73 : 74 : /// Transition probability matrix 75 : std::vector<Real> & _tpm; 76 : 77 : /// Model variance term 78 : std::vector<Real> & _variance; 79 : 80 : /// Model noise term to pass to Likelihoods object 81 : Real & _noise; 82 : 83 : /// Storage for the likelihood objects to be utilized 84 : std::vector<const LikelihoodFunctionBase *> _likelihoods; 85 : 86 : /// The MCMC sampler 87 : Sampler & _sampler; 88 : 89 : /// MCMC sampler base 90 : const PMCMCBase * const _pmcmc; 91 : 92 : /// Storage for the number of parallel proposals 93 : dof_id_type _props; 94 : 95 : /// Storage for the random numbers for decision making 96 : const std::vector<Real> & _rnd_vec; 97 : 98 : /// Storage for new proposed variance samples 99 : const std::vector<Real> & _new_var_samples; 100 : 101 : /// Storage for the priors 102 : const std::vector<const Distribution *> _priors; 103 : 104 : /// Storage for the prior over the variance 105 : const Distribution * _var_prior; 106 : 107 : /// Storage for the number of experimental configuration values 108 : dof_id_type _num_confg_values; 109 : 110 : /// Storage for the number of experimental configuration parameters 111 : dof_id_type _num_confg_params; 112 : 113 : /// Storage for previous inputs 114 : DenseMatrix<Real> _data_prev; 115 : 116 : /// Storage for previous variances 117 : std::vector<Real> _var_prev; 118 : 119 : /// Storage for previous outputs 120 : std::vector<Real> _outputs_prev; 121 : 122 : private: 123 : /// Communicator that was split based on samples that have rows 124 : libMesh::Parallel::Communicator & _local_comm; 125 : 126 : /// Ensure that the MCMC algorithm proceeds in a sequential fashion 127 : int _check_step; 128 : };