25 #include <queso/StatisticalInverseProblem.h>
26 #include <queso/GslVector.h>
27 #include <queso/GslMatrix.h>
32 template <
class P_V,
class P_M>
40 m_env (priorRv.env()),
42 m_likelihoodFunction (likelihoodFunction),
44 m_solutionDomain (NULL),
46 m_subSolutionMdf (NULL),
47 m_subSolutionCdf (NULL),
48 m_solutionRealizer (NULL),
49 m_mhSeqGenerator (NULL),
52 m_logLikelihoodValues (NULL),
53 m_logTargetValues (NULL),
54 m_alternativeOptionsValues(),
57 #ifdef QUESO_MEMORY_DEBUGGING
58 std::cout <<
"Entering Sip" << std::endl;
60 if (m_env.subDisplayFile()) {
61 *m_env.subDisplayFile() <<
"Entering StatisticalInverseProblem<P_V,P_M>::constructor()"
62 <<
": prefix = " << prefix
63 <<
", alternativeOptionsValues = " << alternativeOptionsValues
64 <<
", m_env.optionsInputFileName() = " << m_env.optionsInputFileName()
68 if (alternativeOptionsValues) m_alternativeOptionsValues = *alternativeOptionsValues;
69 if (m_env.optionsInputFileName() ==
"") {
74 m_optionsObj->scanOptionsValues();
76 #ifdef QUESO_MEMORY_DEBUGGING
77 std::cout <<
"In Sip, finished scanning options" << std::endl;
80 UQ_FATAL_TEST_MACRO(priorRv.imageSet().vectorSpace().dimLocal() != likelihoodFunction.domainSet().vectorSpace().dimLocal(),
82 "StatisticalInverseProblem<P_V,P_M>::constructor()",
83 "'priorRv' and 'likelihoodFunction' are related to vector spaces of different dimensions");
85 UQ_FATAL_TEST_MACRO(priorRv.imageSet().vectorSpace().dimLocal() != postRv.imageSet().vectorSpace().dimLocal(),
87 "StatisticalInverseProblem<P_V,P_M>::constructor()",
88 "'priorRv' and 'postRv' are related to vector spaces of different dimensions");
90 if (m_env.subDisplayFile()) {
91 *m_env.subDisplayFile() <<
"Leaving StatisticalInverseProblem<P_V,P_M>::constructor()"
92 <<
": prefix = " << m_optionsObj->m_prefix
100 template <
class P_V,
class P_M>
107 if (m_logLikelihoodValues) {
108 m_logLikelihoodValues->clear();
109 delete m_logLikelihoodValues;
111 if (m_logTargetValues) {
112 m_logTargetValues->clear();
113 delete m_logTargetValues;
115 if (m_mlSampler )
delete m_mlSampler;
116 if (m_mhSeqGenerator )
delete m_mhSeqGenerator;
117 if (m_solutionRealizer)
delete m_solutionRealizer;
118 if (m_subSolutionCdf )
delete m_subSolutionCdf;
119 if (m_subSolutionMdf )
delete m_subSolutionMdf;
120 if (m_solutionPdf )
delete m_solutionPdf;
121 if (m_solutionDomain )
delete m_solutionDomain;
122 if (m_optionsObj )
delete m_optionsObj;
125 template <
class P_V,
class P_M>
129 const P_V& initialValues,
130 const P_M* initialProposalCovMatrix)
134 m_env.fullComm().Barrier();
136 m_env.fullComm().syncPrintDebugMsg(
"Entering StatisticalInverseProblem<P_V,P_M>::solveWithBayesMetropolisHastings()",1,3000000);
138 if (m_optionsObj->m_ov.m_computeSolution ==
false) {
139 if ((m_env.subDisplayFile())) {
140 *m_env.subDisplayFile() <<
"In StatisticalInverseProblem<P_V,P_M>::solveWithBayesMetropolisHastings()"
141 <<
": avoiding solution, as requested by user"
146 if ((m_env.subDisplayFile())) {
147 *m_env.subDisplayFile() <<
"In StatisticalInverseProblem<P_V,P_M>::solveWithBayesMetropolisHastings()"
148 <<
": computing solution, as requested by user"
152 UQ_FATAL_TEST_MACRO(m_priorRv.imageSet().vectorSpace().dimLocal() != initialValues.sizeLocal(),
154 "StatisticalInverseProblem<P_V,P_M>::solveWithBayesMetropolisHastings()",
155 "'m_priorRv' and 'initialValues' should have equal dimensions");
157 if (initialProposalCovMatrix) {
158 UQ_FATAL_TEST_MACRO(m_priorRv.imageSet().vectorSpace().dimLocal() != initialProposalCovMatrix->numRowsLocal(),
160 "StatisticalInverseProblem<P_V,P_M>::solveWithBayesMetropolisHastings()",
161 "'m_priorRv' and 'initialProposalCovMatrix' should have equal dimensions");
162 UQ_FATAL_TEST_MACRO(initialProposalCovMatrix->numCols() != initialProposalCovMatrix->numRowsGlobal(),
164 "StatisticalInverseProblem<P_V,P_M>::solveWithBayesMetropolisHastings()",
165 "'initialProposalCovMatrix' should be a square matrix");
168 if (m_mlSampler )
delete m_mlSampler;
169 if (m_mhSeqGenerator )
delete m_mhSeqGenerator;
170 if (m_solutionRealizer)
delete m_solutionRealizer;
171 if (m_subSolutionCdf )
delete m_subSolutionCdf;
172 if (m_subSolutionMdf )
delete m_subSolutionMdf;
173 if (m_solutionPdf )
delete m_solutionPdf;
174 if (m_solutionDomain )
delete m_solutionDomain;
176 P_V numEvaluationPointsVec(m_priorRv.imageSet().vectorSpace().zeroVector());
177 numEvaluationPointsVec.cwSet(250.);
184 m_likelihoodFunction,
188 m_postRv.setPdf(*m_solutionPdf);
195 alternativeOptionsValues,
198 initialProposalCovMatrix);
200 m_mhSeqGenerator->generateSequence(*m_chain,
207 m_postRv.setRealizer(*m_solutionRealizer);
209 m_env.fullComm().syncPrintDebugMsg(
"In StatisticalInverseProblem<P_V,P_M>::solveWithBayesMetropolisHastings(), code place 1",3,3000000);
213 #ifdef UQ_ALSO_COMPUTE_MDFS_WITHOUT_KDE
216 m_chain->subUniformlySampledMdf(numEvaluationPointsVec,
222 m_postRv.setMdf(*m_subSolutionMdf);
225 (m_optionsObj->m_ov.m_dataOutputAllowedSet.find(m_env.subId()) != m_optionsObj->m_ov.m_dataOutputAllowedSet.end())) {
226 if (m_env.subRank() == 0) {
228 if (m_env.subDisplayFile()) {
229 *m_env.subDisplayFile() <<
"Opening data output file '" << m_optionsObj->m_ov.m_dataOutputFileName
230 <<
"' for calibration problem with problem with prefix = " << m_optionsObj->m_prefix
236 std::ofstream* ofsvar =
new std::ofstream((m_optionsObj->m_ov.m_dataOutputFileName+
"_sub"+m_env.subIdString()+
".m").c_str(), std::ofstream::out | std::ofstream::in | std::ofstream::ate);
237 if ((ofsvar == NULL ) ||
238 (ofsvar->is_open() ==
false)) {
240 ofsvar =
new std::ofstream((m_optionsObj->m_ov.m_dataOutputFileName+
"_sub"+m_env.subIdString()+
".m").c_str(), std::ofstream::out | std::ofstream::trunc);
244 "StatisticalInverseProblem<P_V,P_M>::solveWithBayesMetropolisHastings()",
245 "failed to open file");
247 m_postRv.mdf().print(*ofsvar);
252 if (m_env.subDisplayFile()) {
253 *m_env.subDisplayFile() <<
"Closed data output file '" << m_optionsObj->m_ov.m_dataOutputFileName
254 <<
"' for calibration problem with problem with prefix = " << m_optionsObj->m_prefix
260 if (m_env.subDisplayFile()) {
261 *m_env.subDisplayFile() << std::endl;
264 m_env.fullComm().syncPrintDebugMsg(
"Leaving StatisticalInverseProblem<P_V,P_M>::solveWithBayesMetropolisHastings()",1,3000000);
265 m_env.fullComm().Barrier();
270 template <
class P_V,
class P_M>
274 m_env.fullComm().Barrier();
275 m_env.fullComm().syncPrintDebugMsg(
"Entering StatisticalInverseProblem<P_V,P_M>::solveWithBayesMLSampling()",1,3000000);
277 if (m_optionsObj->m_ov.m_computeSolution ==
false) {
278 if ((m_env.subDisplayFile())) {
279 *m_env.subDisplayFile() <<
"In StatisticalInverseProblem<P_V,P_M>::solveWithBayesMLSampling()"
280 <<
": avoiding solution, as requested by user"
285 if ((m_env.subDisplayFile())) {
286 *m_env.subDisplayFile() <<
"In StatisticalInverseProblem<P_V,P_M>::solveWithBayesMLSampling()"
287 <<
": computing solution, as requested by user"
291 if (m_mlSampler )
delete m_mlSampler;
292 if (m_mhSeqGenerator )
delete m_mhSeqGenerator;
293 if (m_solutionRealizer)
delete m_solutionRealizer;
294 if (m_subSolutionCdf )
delete m_subSolutionCdf;
295 if (m_subSolutionMdf )
delete m_subSolutionMdf;
296 if (m_solutionPdf )
delete m_solutionPdf;
297 if (m_solutionDomain )
delete m_solutionDomain;
299 P_V numEvaluationPointsVec(m_priorRv.imageSet().vectorSpace().zeroVector());
300 numEvaluationPointsVec.cwSet(250.);
307 m_likelihoodFunction,
311 m_postRv.setPdf(*m_solutionPdf);
318 m_likelihoodFunction);
322 m_mlSampler->generateSequence(*m_chain,
329 m_postRv.setRealizer(*m_solutionRealizer);
331 if (m_env.subDisplayFile()) {
332 *m_env.subDisplayFile() << std::endl;
335 m_env.fullComm().syncPrintDebugMsg(
"Leaving StatisticalInverseProblem<P_V,P_M>::solveWithBayesMLSampling()",1,3000000);
336 m_env.fullComm().Barrier();
341 template <
class P_V,
class P_M>
348 template <
class P_V,
class P_M>
355 template <
class P_V,
class P_M>
360 "StatisticalInverseProblem<P_V,P_M>::logEvidence()",
361 "m_mlSampler is NULL");
362 return m_mlSampler->logEvidence();
365 template <
class P_V,
class P_M>
370 "StatisticalInverseProblem<P_V,P_M>::meanLogLikelihood()",
371 "m_mlSampler is NULL");
372 return m_mlSampler->meanLogLikelihood();
375 template <
class P_V,
class P_M>
380 "StatisticalInverseProblem<P_V,P_M>::eig()",
381 "m_mlSampler is NULL");
382 return m_mlSampler->eig();
385 template <
class P_V,
class P_M>
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.
Class to accommodate arrays of one-dimensional grid.
A templated class that represents a Multilevel generator of samples.
This class provides options for a Statistical Inverse Problem if no input file is available...
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.
A class for handling sampled vector MDFs.
void print(std::ostream &os) const
TODO: Prints the sequence.
void solveWithBayesMLSampling()
Solves with Bayes Multi-Level (ML) sampling.
This class reads option values for a Statistical Inverse Problem from an input file.
A templated class that represents a Metropolis-Hastings generator of samples.
double logEvidence() const
Returns the logarithm value of the evidence. Related to ML.
#define UQ_FATAL_TEST_MACRO(test, givenRank, where, what)
double meanLogLikelihood() const
Returns the mean of the logarithm value of the likelihood. Related to ML.
A class for handling sequential draws (sampling) from probability density distributions.
Class to accommodate arrays of one-dimensional tables.
void solveWithBayesMetropolisHastings(const MhOptionsValues *alternativeOptionsValues, const P_V &initialValues, const P_M *initialProposalCovMatrix)
Solves the problem through Bayes formula and a Metropolis-Hastings algorithm.
This class provides options for the Metropolis-Hastings generator of samples if no input file is avai...
~StatisticalInverseProblem()
Destructor.
VectorSet< V, M > * InstantiateIntersection(const VectorSet< V, M > &domain1, const VectorSet< V, M > &domain2)
This method calculates the intersection of domain1 and domain2.
const GenericVectorRV< P_V, P_M > & postRv() const
Returns the Posterior RV; access to private attribute m_postrRv.
#define UQ_SIP_FILENAME_FOR_NO_FILE
Class for handling vector samples (sequence of vectors).