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>
34 template <
class P_V,
class P_M>
42 m_env (priorRv.env()),
44 m_likelihoodFunction (likelihoodFunction),
46 m_solutionDomain (NULL),
48 m_subSolutionMdf (NULL),
49 m_subSolutionCdf (NULL),
50 m_solutionRealizer (NULL),
51 m_mhSeqGenerator (NULL),
54 m_logLikelihoodValues (NULL),
55 m_logTargetValues (NULL),
56 m_optionsObj (alternativeOptionsValues),
57 m_seedWithMAPEstimator (
false),
58 m_userDidNotProvideOptions(
false)
60 #ifdef QUESO_MEMORY_DEBUGGING
61 std::cout <<
"Entering Sip" << std::endl;
63 if (m_env.subDisplayFile()) {
64 *m_env.subDisplayFile() <<
"Entering StatisticalInverseProblem<P_V,P_M>::constructor()"
65 <<
": prefix = " << prefix
66 <<
", alternativeOptionsValues = " << alternativeOptionsValues
67 <<
", m_env.optionsInputFileName() = " << m_env.optionsInputFileName()
72 if (m_optionsObj == NULL) {
77 m_optionsObj = tempOptions;
81 m_userDidNotProvideOptions =
true;
84 if (m_optionsObj->m_help !=
"") {
85 if (m_env.subDisplayFile()) {
86 *m_env.subDisplayFile() << (*m_optionsObj) << std::endl;
90 #ifdef QUESO_MEMORY_DEBUGGING
91 std::cout <<
"In Sip, finished scanning options" << std::endl;
94 queso_require_equal_to_msg(priorRv.imageSet().vectorSpace().dimLocal(), likelihoodFunction.domainSet().vectorSpace().dimLocal(),
"'priorRv' and 'likelihoodFunction' are related to vector spaces of different dimensions");
96 queso_require_equal_to_msg(priorRv.imageSet().vectorSpace().dimLocal(), postRv.imageSet().vectorSpace().dimLocal(),
"'priorRv' and 'postRv' are related to vector spaces of different dimensions");
98 if (m_env.subDisplayFile()) {
99 *m_env.subDisplayFile() <<
"Leaving StatisticalInverseProblem<P_V,P_M>::constructor()"
100 <<
": prefix = " << m_optionsObj->m_prefix
107 template <
class P_V,
class P_M>
114 m_env (gpmsaFactory.m_totalPrior->env()),
115 m_priorRv (*(gpmsaFactory.m_totalPrior)),
116 m_likelihoodFunction (gpmsaFactory.getGPMSAEmulator()),
118 m_solutionDomain (NULL),
119 m_solutionPdf (NULL),
120 m_subSolutionMdf (NULL),
121 m_subSolutionCdf (NULL),
122 m_solutionRealizer (NULL),
123 m_mhSeqGenerator (NULL),
126 m_logLikelihoodValues (NULL),
127 m_logTargetValues (NULL),
128 m_optionsObj (alternativeOptionsValues),
129 m_seedWithMAPEstimator (false),
130 m_userDidNotProvideOptions(false)
134 <<
": prefix = " << prefix
135 <<
", alternativeOptionsValues = " << alternativeOptionsValues
171 template <
class P_V,
class P_M>
178 if (m_logLikelihoodValues) {
179 m_logLikelihoodValues->clear();
180 delete m_logLikelihoodValues;
182 if (m_logTargetValues) {
183 m_logTargetValues->clear();
184 delete m_logTargetValues;
186 if (m_mlSampler )
delete m_mlSampler;
187 if (m_mhSeqGenerator )
delete m_mhSeqGenerator;
188 if (m_solutionRealizer)
delete m_solutionRealizer;
189 if (m_subSolutionCdf )
delete m_subSolutionCdf;
190 if (m_subSolutionMdf )
delete m_subSolutionMdf;
191 if (m_solutionPdf )
delete m_solutionPdf;
192 if (m_solutionDomain )
delete m_solutionDomain;
194 if (m_optionsObj && m_userDidNotProvideOptions) {
199 template <
class P_V,
class P_M>
203 const P_V& initialValues,
204 const P_M* initialProposalCovMatrix)
208 m_env.fullComm().Barrier();
210 m_env.fullComm().syncPrintDebugMsg(
"Entering StatisticalInverseProblem<P_V,P_M>::solveWithBayesMetropolisHastings()",1,3000000);
212 if (m_optionsObj->m_computeSolution ==
false) {
213 if ((m_env.subDisplayFile())) {
214 *m_env.subDisplayFile() <<
"In StatisticalInverseProblem<P_V,P_M>::solveWithBayesMetropolisHastings()"
215 <<
": avoiding solution, as requested by user"
220 if ((m_env.subDisplayFile())) {
221 *m_env.subDisplayFile() <<
"In StatisticalInverseProblem<P_V,P_M>::solveWithBayesMetropolisHastings()"
222 <<
": computing solution, as requested by user"
226 queso_require_equal_to_msg(m_priorRv.imageSet().vectorSpace().dimLocal(), initialValues.sizeLocal(),
"'m_priorRv' and 'initialValues' should have equal dimensions");
228 if (initialProposalCovMatrix) {
229 queso_require_equal_to_msg(m_priorRv.imageSet().vectorSpace().dimLocal(), initialProposalCovMatrix->numRowsLocal(),
"'m_priorRv' and 'initialProposalCovMatrix' should have equal dimensions");
230 queso_require_equal_to_msg(initialProposalCovMatrix->numCols(), initialProposalCovMatrix->numRowsGlobal(),
"'initialProposalCovMatrix' should be a square matrix");
233 if (m_mlSampler )
delete m_mlSampler;
234 if (m_mhSeqGenerator )
delete m_mhSeqGenerator;
235 if (m_solutionRealizer)
delete m_solutionRealizer;
236 if (m_subSolutionCdf )
delete m_subSolutionCdf;
237 if (m_subSolutionMdf )
delete m_subSolutionMdf;
238 if (m_solutionPdf )
delete m_solutionPdf;
239 if (m_solutionDomain )
delete m_solutionDomain;
241 P_V numEvaluationPointsVec(m_priorRv.imageSet().vectorSpace().zeroVector());
242 numEvaluationPointsVec.cwSet(250.);
249 m_likelihoodFunction,
253 m_postRv.setPdf(*m_solutionPdf);
260 if (this->m_seedWithMAPEstimator) {
263 optimizer.
setInitialPoint(dynamic_cast<const GslVector &>(initialValues));
268 m_optionsObj->m_prefix.c_str(), alternativeOptionsValues,
269 m_postRv, optimizer.
minimizer(), initialProposalCovMatrix);
274 m_optionsObj->m_prefix.c_str(), alternativeOptionsValues, m_postRv,
275 initialValues, initialProposalCovMatrix);
280 m_optionsObj->m_prefix +
284 m_optionsObj->m_prefix +
288 m_mhSeqGenerator->generateSequence(*m_chain, m_logLikelihoodValues,
294 m_postRv.setRealizer(*m_solutionRealizer);
296 m_env.fullComm().syncPrintDebugMsg(
"In StatisticalInverseProblem<P_V,P_M>::solveWithBayesMetropolisHastings(), code place 1",3,3000000);
300 #ifdef UQ_ALSO_COMPUTE_MDFS_WITHOUT_KDE
303 m_chain->subUniformlySampledMdf(numEvaluationPointsVec,
309 m_postRv.setMdf(*m_subSolutionMdf);
312 (m_optionsObj->m_dataOutputAllowedSet.find(m_env.subId()) != m_optionsObj->m_dataOutputAllowedSet.end())) {
313 if (m_env.subRank() == 0) {
315 if (m_env.subDisplayFile()) {
316 *m_env.subDisplayFile() <<
"Opening data output file '" << m_optionsObj->m_dataOutputFileName
317 <<
"' for calibration problem with problem with prefix = " << m_optionsObj->m_prefix
323 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);
324 if ((ofsvar == NULL ) ||
325 (ofsvar->is_open() ==
false)) {
327 ofsvar =
new std::ofstream((m_optionsObj->m_dataOutputFileName+
"_sub"+m_env.subIdString()+
".m").c_str(), std::ofstream::out | std::ofstream::trunc);
331 m_postRv.mdf().print(*ofsvar);
336 if (m_env.subDisplayFile()) {
337 *m_env.subDisplayFile() <<
"Closed data output file '" << m_optionsObj->m_dataOutputFileName
338 <<
"' for calibration problem with problem with prefix = " << m_optionsObj->m_prefix
344 if (m_env.subDisplayFile()) {
345 *m_env.subDisplayFile() << std::endl;
348 m_env.fullComm().syncPrintDebugMsg(
"Leaving StatisticalInverseProblem<P_V,P_M>::solveWithBayesMetropolisHastings()",1,3000000);
349 m_env.fullComm().Barrier();
354 template <
class P_V,
class P_M>
358 this->m_seedWithMAPEstimator =
true;
361 template <
class P_V,
class P_M>
365 m_env.fullComm().Barrier();
366 m_env.fullComm().syncPrintDebugMsg(
"Entering StatisticalInverseProblem<P_V,P_M>::solveWithBayesMLSampling()",1,3000000);
368 if (m_optionsObj->m_computeSolution ==
false) {
369 if ((m_env.subDisplayFile())) {
370 *m_env.subDisplayFile() <<
"In StatisticalInverseProblem<P_V,P_M>::solveWithBayesMLSampling()"
371 <<
": avoiding solution, as requested by user"
376 if ((m_env.subDisplayFile())) {
377 *m_env.subDisplayFile() <<
"In StatisticalInverseProblem<P_V,P_M>::solveWithBayesMLSampling()"
378 <<
": computing solution, as requested by user"
382 if (m_mlSampler )
delete m_mlSampler;
383 if (m_mhSeqGenerator )
delete m_mhSeqGenerator;
384 if (m_solutionRealizer)
delete m_solutionRealizer;
385 if (m_subSolutionCdf )
delete m_subSolutionCdf;
386 if (m_subSolutionMdf )
delete m_subSolutionMdf;
387 if (m_solutionPdf )
delete m_solutionPdf;
388 if (m_solutionDomain )
delete m_solutionDomain;
390 P_V numEvaluationPointsVec(m_priorRv.imageSet().vectorSpace().zeroVector());
391 numEvaluationPointsVec.cwSet(250.);
398 m_likelihoodFunction,
402 m_postRv.setPdf(*m_solutionPdf);
409 m_likelihoodFunction);
413 m_mlSampler->generateSequence(*m_chain,
420 m_postRv.setRealizer(*m_solutionRealizer);
422 if (m_env.subDisplayFile()) {
423 *m_env.subDisplayFile() << std::endl;
426 m_env.fullComm().syncPrintDebugMsg(
"Leaving StatisticalInverseProblem<P_V,P_M>::solveWithBayesMLSampling()",1,3000000);
427 m_env.fullComm().Barrier();
432 template <
class P_V,
class P_M>
436 return *m_mhSeqGenerator;
440 template <
class P_V,
class P_M>
447 template <
class P_V,
class P_M>
454 template <
class P_V,
class P_M>
462 template <
class P_V,
class P_M>
467 return *m_logLikelihoodValues;
470 template <
class P_V,
class P_M>
475 return *m_logTargetValues;
478 template <
class P_V,
class P_M>
482 return m_mlSampler->logEvidence();
485 template <
class P_V,
class P_M>
489 return m_mlSampler->meanLogLikelihood();
492 template <
class P_V,
class P_M>
496 return m_mlSampler->eig();
499 template <
class P_V,
class P_M>
Class to accommodate arrays of one-dimensional grid.
VectorSet< V, M > * InstantiateIntersection(const VectorSet< V, M > &domain1, const VectorSet< V, M > &domain2)
This method calculates the intersection of domain1 and domain2.
unsigned int dimLocal() const
void setInitialPoint(const GslVector &intialPoint)
Set the point at which the optimization starts.
const BaseEnvironment & m_env
A class for handling sampled vector MDFs.
const BaseScalarFunction< P_V, P_M > & m_likelihoodFunction
This class provides options for a Statistical Inverse Problem if no input file is available...
bool m_userDidNotProvideOptions
A templated class that represents a Metropolis-Hastings generator of samples.
const SipOptionsValues * m_optionsObj
void seedWithMAPEstimator()
Seeds the chain with the result of a deterministic optimisation.
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.
const GenericVectorRV< P_V, P_M > & postRv() const
Returns the Posterior RV; access to private attribute m_postrRv.
const BaseVectorRV< P_V, P_M > & m_priorRv
const MetropolisHastingsSG< P_V, P_M > & sequenceGenerator() const
Return the underlying MetropolisHastingSG object.
#define queso_require_equal_to_msg(expr1, expr2, msg)
double logEvidence() const
Returns the logarithm value of the evidence. Related to ML.
std::string optionsInputFileName() const
Access to the attribute m_optionsInputFileName, which stores the name of the input file passed by the...
virtual void minimize(OptimizerMonitor *monitor=NULL)
Minimize the objective function, starting at m_initialPoint.
#define queso_require_msg(asserted, msg)
void solveWithBayesMetropolisHastings(const MhOptionsValues *alternativeOptionsValues, const P_V &initialValues, const P_M *initialProposalCovMatrix)
Solves the problem via Bayes formula and a Metropolis-Hastings algorithm.
const VectorSet< V, M > & domainSet() const
Access to the protected attribute m_domainSet: domain set of the scalar function. ...
std::ofstream * subDisplayFile() const
Access function for m_subDisplayFile (displays file on stream).
void solveWithBayesMLSampling()
Solves with Bayes Multi-Level (ML) sampling.
const BaseVectorSequence< P_V, P_M > & chain() const
Returns the MCMC chain; access to private attribute m_chain.
This class provides options for the Metropolis-Hastings generator of samples if no input file is avai...
Class to accommodate arrays of one-dimensional tables.
const ScalarSequence< double > & logTargetValues() const
Returns log target values; access to private attribute m_logTargetValues.
A class for handling sequential draws (sampling) from probability density distributions.
A templated class that represents a Multilevel generator of samples.
~StatisticalInverseProblem()
Destructor.
void print(std::ostream &os) const
TODO: Prints the sequence.
std::string m_help
If this string is non-empty, options are print to the output file.
const VectorSet< V, M > & imageSet() const
Image set of the vector RV; access to private attribute m_imageSet.
double meanLogLikelihood() const
Returns the mean of the logarithm value of the likelihood. Related to ML.
A base class for handling optimisation of scalar functions.
const GslVector & minimizer() const
Return the point that minimizes the objective function.
const ScalarSequence< double > & logLikelihoodValues() const
Returns log likelihood values; access to private attribute m_logLikelihoodValues. ...
virtual const VectorSpace< V, M > & vectorSpace() const =0
Vector space to which this set belongs to. See template specialization.
#define UQ_SIP_FILENAME_FOR_NO_FILE
const BaseVectorRV< P_V, P_M > & priorRv() const
Returns the Prior RV; access to private attribute m_priorRv.
This templated class represents a Statistical Inverse Problem.