25 #include <queso/StatisticalInverseProblem.h>
26 #include <queso/GslVector.h>
27 #include <queso/GslMatrix.h>
28 #include <queso/GPMSA.h>
29 #include <queso/GslOptimizer.h>
30 #include <queso/OptimizerMonitor.h>
31 #include <queso/BayesianJointPdf.h>
36 template <
class P_V,
class P_M>
44 m_env (priorRv.env()),
46 m_likelihoodFunction (likelihoodFunction),
52 m_solutionRealizer (),
56 m_logLikelihoodValues (),
59 m_seedWithMAPEstimator (
false)
61 #ifdef QUESO_MEMORY_DEBUGGING
62 std::cout <<
"Entering Sip" << std::endl;
64 if (m_env.subDisplayFile()) {
65 *m_env.subDisplayFile() <<
"Entering StatisticalInverseProblem<P_V,P_M>::constructor()"
66 <<
": prefix = " << prefix
67 <<
", alternativeOptionsValues = " << alternativeOptionsValues
68 <<
", m_env.optionsInputFileName() = " << m_env.optionsInputFileName()
73 if (m_optionsObj == NULL) {
82 if (m_optionsObj->m_help !=
"") {
83 if (m_env.subDisplayFile()) {
84 *m_env.subDisplayFile() << (*m_optionsObj) << std::endl;
88 #ifdef QUESO_MEMORY_DEBUGGING
89 std::cout <<
"In Sip, finished scanning options" << std::endl;
92 queso_require_equal_to_msg(priorRv.imageSet().vectorSpace().dimLocal(), likelihoodFunction.domainSet().vectorSpace().dimLocal(),
"'priorRv' and 'likelihoodFunction' are related to vector spaces of different dimensions");
94 queso_require_equal_to_msg(priorRv.imageSet().vectorSpace().dimLocal(), postRv.imageSet().vectorSpace().dimLocal(),
"'priorRv' and 'postRv' are related to vector spaces of different dimensions");
96 if (m_env.subDisplayFile()) {
97 *m_env.
subDisplayFile() <<
"Leaving StatisticalInverseProblem<P_V,P_M>::constructor()"
98 <<
": prefix = " << m_optionsObj->m_prefix
105 template <
class P_V,
class P_M>
112 m_env (gpmsaFactory.m_totalPrior->env()),
113 m_priorRv (*(gpmsaFactory.m_totalPrior)),
114 m_likelihoodFunction (gpmsaFactory.getGPMSAEmulator()),
120 m_solutionRealizer (),
124 m_logLikelihoodValues (),
125 m_logTargetValues (),
127 m_seedWithMAPEstimator (false)
131 <<
": prefix = " << prefix
132 <<
", alternativeOptionsValues = " << alternativeOptionsValues
168 template <
class P_V,
class P_M>
173 template <
class P_V,
class P_M>
177 const P_V& initialValues,
178 const P_M* initialProposalCovMatrix)
182 m_env.fullComm().Barrier();
184 m_env.fullComm().syncPrintDebugMsg(
"Entering StatisticalInverseProblem<P_V,P_M>::solveWithBayesMetropolisHastings()",1,3000000);
186 if (m_optionsObj->m_computeSolution ==
false) {
187 if ((m_env.subDisplayFile())) {
188 *m_env.subDisplayFile() <<
"In StatisticalInverseProblem<P_V,P_M>::solveWithBayesMetropolisHastings()"
189 <<
": avoiding solution, as requested by user"
194 if ((m_env.subDisplayFile())) {
195 *m_env.subDisplayFile() <<
"In StatisticalInverseProblem<P_V,P_M>::solveWithBayesMetropolisHastings()"
196 <<
": computing solution, as requested by user"
200 queso_require_equal_to_msg(m_priorRv.imageSet().vectorSpace().dimLocal(), initialValues.sizeLocal(),
"'m_priorRv' and 'initialValues' should have equal dimensions");
202 if (initialProposalCovMatrix) {
203 queso_require_equal_to_msg(m_priorRv.imageSet().vectorSpace().dimLocal(), initialProposalCovMatrix->numRowsLocal(),
"'m_priorRv' and 'initialProposalCovMatrix' should have equal dimensions");
204 queso_require_equal_to_msg(initialProposalCovMatrix->numCols(), initialProposalCovMatrix->numRowsGlobal(),
"'initialProposalCovMatrix' should be a square matrix");
207 P_V numEvaluationPointsVec(m_priorRv.imageSet().vectorSpace().zeroVector());
208 numEvaluationPointsVec.cwSet(250.);
215 m_likelihoodFunction,
219 m_postRv.setPdf(*m_solutionPdf);
226 if (this->m_seedWithMAPEstimator ||
227 m_optionsObj->m_seedWithMAPEstimator) {
235 GslOptimizer optimizer(optimizer_options, *m_solutionPdf);
236 optimizer.
setInitialPoint(dynamic_cast<const GslVector &>(initialValues));
242 if (m_optionsObj->m_useOptimizerMonitor) {
251 m_optionsObj->m_prefix.c_str(), alternativeOptionsValues,
252 m_postRv, optimizer.
minimizer(), initialProposalCovMatrix));
257 m_optionsObj->m_prefix.c_str(), alternativeOptionsValues, m_postRv,
258 initialValues, initialProposalCovMatrix));
263 m_optionsObj->m_prefix +
267 m_optionsObj->m_prefix +
271 m_mhSeqGenerator->generateSequence(*m_chain, m_logLikelihoodValues.get(),
272 m_logTargetValues.get());
277 m_postRv.setRealizer(*m_solutionRealizer);
279 m_env.fullComm().syncPrintDebugMsg(
"In StatisticalInverseProblem<P_V,P_M>::solveWithBayesMetropolisHastings(), code place 1",3,3000000);
283 #ifdef UQ_ALSO_COMPUTE_MDFS_WITHOUT_KDE
284 m_subMdfGrids.reset(
new ArrayOfOneDGrids <P_V,P_M>((m_optionsObj->m_prefix+
"Mdf_").c_str(),m_postRv.imageSet().vectorSpace()));
285 m_subMdfValues.reset(
new ArrayOfOneDTables<P_V,P_M>((m_optionsObj->m_prefix+
"Mdf_").c_str(),m_postRv.imageSet().vectorSpace()));
286 m_chain->subUniformlySampledMdf(numEvaluationPointsVec,
292 m_postRv.setMdf(*m_subSolutionMdf);
294 if ((m_optionsObj->m_dataOutputFileName != UQ_SIP_FILENAME_FOR_NO_FILE ) &&
295 (m_optionsObj->m_dataOutputAllowedSet.find(m_env.subId()) != m_optionsObj->m_dataOutputAllowedSet.end())) {
296 if (m_env.subRank() == 0) {
298 if (m_env.subDisplayFile()) {
299 *m_env.subDisplayFile() <<
"Opening data output file '" << m_optionsObj->m_dataOutputFileName
300 <<
"' for calibration problem with problem with prefix = " << m_optionsObj->m_prefix
306 std::ofstream* ofsvar =
new std::ofstream((m_optionsObj->m_dataOutputFileName+
"_sub"+m_env.subIdString()+
".m").c_str(), std::ofstream::out | std::ofstream::in | std::ofstream::ate);
307 if ((ofsvar == NULL ) ||
308 (ofsvar->is_open() ==
false)) {
310 ofsvar =
new std::ofstream((m_optionsObj->m_dataOutputFileName+
"_sub"+m_env.subIdString()+
".m").c_str(), std::ofstream::out | std::ofstream::trunc);
312 queso_require_msg((ofsvar && ofsvar->is_open()),
"failed to open file");
314 m_postRv.mdf().print(*ofsvar);
319 if (m_env.subDisplayFile()) {
320 *m_env.subDisplayFile() <<
"Closed data output file '" << m_optionsObj->m_dataOutputFileName
321 <<
"' for calibration problem with problem with prefix = " << m_optionsObj->m_prefix
327 if (m_env.subDisplayFile()) {
328 *m_env.subDisplayFile() << std::endl;
331 m_env.fullComm().syncPrintDebugMsg(
"Leaving StatisticalInverseProblem<P_V,P_M>::solveWithBayesMetropolisHastings()",1,3000000);
332 m_env.fullComm().Barrier();
336 template <
class P_V,
class P_M>
340 const P_V & initialValues)
342 P_M proposalCovMatrix(m_priorRv.imageSet().vectorSpace().zeroVector());
344 m_priorRv.pdf().distributionVariance(proposalCovMatrix);
346 this->solveWithBayesMetropolisHastings(alternativeOptionsValues,
351 template <
class P_V,
class P_M>
356 P_V initialValues(m_priorRv.imageSet().vectorSpace().zeroVector());
358 m_priorRv.pdf().distributionMean(initialValues);
360 this->solveWithBayesMetropolisHastings(alternativeOptionsValues,
364 template <
class P_V,
class P_M>
368 this->solveWithBayesMetropolisHastings(NULL);
371 template <
class P_V,
class P_M>
375 this->m_seedWithMAPEstimator =
true;
378 template <
class P_V,
class P_M>
382 m_env.fullComm().Barrier();
383 m_env.fullComm().syncPrintDebugMsg(
"Entering StatisticalInverseProblem<P_V,P_M>::solveWithBayesMLSampling()",1,3000000);
385 if (m_optionsObj->m_computeSolution ==
false) {
386 if ((m_env.subDisplayFile())) {
387 *m_env.subDisplayFile() <<
"In StatisticalInverseProblem<P_V,P_M>::solveWithBayesMLSampling()"
388 <<
": avoiding solution, as requested by user"
393 if ((m_env.subDisplayFile())) {
394 *m_env.subDisplayFile() <<
"In StatisticalInverseProblem<P_V,P_M>::solveWithBayesMLSampling()"
395 <<
": computing solution, as requested by user"
399 P_V numEvaluationPointsVec(m_priorRv.imageSet().vectorSpace().zeroVector());
400 numEvaluationPointsVec.cwSet(250.);
407 m_likelihoodFunction,
411 m_postRv.setPdf(*m_solutionPdf);
418 m_likelihoodFunction));
422 m_mlSampler->generateSequence(*m_chain,
429 m_postRv.setRealizer(*m_solutionRealizer);
431 if (m_env.subDisplayFile()) {
432 *m_env.subDisplayFile() << std::endl;
435 m_env.fullComm().syncPrintDebugMsg(
"Leaving StatisticalInverseProblem<P_V,P_M>::solveWithBayesMLSampling()",1,3000000);
436 m_env.fullComm().Barrier();
441 template <
class P_V,
class P_M>
445 return *m_mhSeqGenerator;
449 template <
class P_V,
class P_M>
456 template <
class P_V,
class P_M>
463 template <
class P_V,
class P_M>
467 queso_require_msg(m_chain,
"m_chain is NULL");
471 template <
class P_V,
class P_M>
475 queso_require_msg(m_logLikelihoodValues,
"m_logLikelihoodValues is NULL");
476 return *m_logLikelihoodValues;
479 template <
class P_V,
class P_M>
483 queso_require_msg(m_logTargetValues,
"m_logTargetValues is NULL");
484 return *m_logTargetValues;
487 template <
class P_V,
class P_M>
490 queso_require_msg(m_mlSampler,
"m_mlSampler is NULL");
491 return m_mlSampler->logEvidence();
494 template <
class P_V,
class P_M>
497 queso_require_msg(m_mlSampler,
"m_mlSampler is NULL");
498 return m_mlSampler->meanLogLikelihood();
501 template <
class P_V,
class P_M>
504 queso_require_msg(m_mlSampler,
"m_mlSampler is NULL");
505 return m_mlSampler->eig();
508 template <
class P_V,
class P_M>
void set_display_output(bool enable_output, bool print_xmin)
Enabling output to std::cout everytime append is called.
Class to accommodate arrays of one-dimensional grid.
const GslVector & minimizer() const
Return the point that minimizes the objective function.
StatisticalInverseProblem(const char *prefix, const SipOptionsValues *alternativeOptionsValues, const BaseVectorRV< P_V, P_M > &priorRv, const BaseScalarFunction< P_V, P_M > &likelihoodFunction, GenericVectorRV< P_V, P_M > &postRv)
Constructor.
virtual void minimize(OptimizerMonitor *monitor=NULL)
Minimize the objective function, starting at m_initialPoint.
double logEvidence() const
Returns the logarithm value of the evidence. Related to ML.
const ScalarSequence< double > & logLikelihoodValues() const
Returns log likelihood values; access to private attribute m_logLikelihoodValues. ...
const BaseVectorRV< P_V, P_M > & m_priorRv
Class to accommodate arrays of one-dimensional tables.
ScopedPtr< const SipOptionsValues >::Type m_optionsObj
const BaseEnvironment & m_env
const BaseVectorRV< P_V, P_M > & priorRv() const
Returns the Prior RV; access to private attribute m_priorRv.
const VectorSet< V, M > & imageSet() const
Image set of the vector RV; access to private attribute m_imageSet.
std::string optionsInputFileName() const
Access to the attribute m_optionsInputFileName, which stores the name of the input file passed by the...
This class provides options for a Statistical Inverse Problem if no input file is available...
virtual const VectorSpace< V, M > & vectorSpace() const =0
Vector space to which this set belongs to. See template specialization.
void print(std::ostream &os) const
TODO: Prints the sequence.
This class provides options for the Metropolis-Hastings generator of samples if no input file is avai...
const BaseVectorSequence< P_V, P_M > & chain() const
Returns the MCMC chain; access to private attribute m_chain.
const VectorSet< V, M > & domainSet() const
Access to the protected attribute m_domainSet: domain set of the scalar function. ...
A templated class that represents a Metropolis-Hastings generator of samples.
void setInitialPoint(const GslVector &intialPoint)
Set the point at which the optimization starts.
Class for handling vector samples (sequence of vectors).
const GenericVectorRV< P_V, P_M > & postRv() const
Returns the Posterior RV; access to private attribute m_postrRv.
void solveWithBayesMLSampling()
Solves with Bayes Multi-Level (ML) sampling.
double meanLogLikelihood() const
Returns the mean of the logarithm value of the likelihood. Related to ML.
Base class for handling vector and array samples (sequence of vectors or arrays). ...
const ScalarSequence< double > & logTargetValues() const
Returns log target values; access to private attribute m_logTargetValues.
const BaseScalarFunction< P_V, P_M > & m_likelihoodFunction
const MetropolisHastingsSG< P_V, P_M > & sequenceGenerator() const
Return the underlying MetropolisHastingSG object.
This class provides options for a Optimizer.
unsigned int dimLocal() const
This templated class represents a Statistical Inverse Problem.
MonteCarloSGOptions::MonteCarloSGOptions(const BaseEnvironment &env, const char *prefix, const McOptionsValues &alternativeOptionsValues queso_require_equal_to_msg)(m_env.optionsInputFileName(), std::string(""), std::string("this constructor is incompatible with the existence of an options input file"))
void seedWithMAPEstimator()
Seeds the chain with the result of a deterministic optimisation.
Object to monitor convergence of optimizers.
A templated class that represents a Multilevel generator of samples.
A class for handling sampled vector MDFs.
A class for handling Bayesian joint PDFs.
~StatisticalInverseProblem()
Destructor.
A class for handling sequential draws (sampling) from probability density distributions.
VectorSet< V, M > * InstantiateIntersection(const VectorSet< V, M > &domain1, const VectorSet< V, M > &domain2)
This method calculates the intersection of domain1 and domain2.
A base class for handling optimisation of scalar functions.
void solveWithBayesMetropolisHastings()
Solves the problem via Bayes formula and a Metropolis-Hastings algorithm.
std::ofstream * subDisplayFile() const
Access function for m_subDisplayFile (displays file on stream).