queso-0.50.1
StatisticalInverseProblem.C
Go to the documentation of this file.
1 //-----------------------------------------------------------------------bl-
2 //--------------------------------------------------------------------------
3 //
4 // QUESO - a library to support the Quantification of Uncertainty
5 // for Estimation, Simulation and Optimization
6 //
7 // Copyright (C) 2008,2009,2010,2011,2012,2013 The PECOS Development Team
8 //
9 // This library is free software; you can redistribute it and/or
10 // modify it under the terms of the Version 2.1 GNU Lesser General
11 // Public License as published by the Free Software Foundation.
12 //
13 // This library is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 // Lesser General Public License for more details.
17 //
18 // You should have received a copy of the GNU Lesser General Public
19 // License along with this library; if not, write to the Free Software
20 // Foundation, Inc. 51 Franklin Street, Fifth Floor,
21 // Boston, MA 02110-1301 USA
22 //
23 //-----------------------------------------------------------------------el-
24 
25 #include <queso/StatisticalInverseProblem.h>
26 #include <queso/GslVector.h>
27 #include <queso/GslMatrix.h>
28 
29 namespace QUESO {
30 
31 // Default constructor -----------------------------
32 template <class P_V,class P_M>
33 StatisticalInverseProblem<P_V,P_M>::StatisticalInverseProblem( const char* prefix, const SipOptionsValues* alternativeOptionsValues, // dakota const BaseVectorRV <P_V,P_M>& priorRv, const BaseScalarFunction<P_V,P_M>& likelihoodFunction, GenericVectorRV <P_V,P_M>& postRv)
39  :
40  m_env (priorRv.env()),
41  m_priorRv (priorRv),
42  m_likelihoodFunction (likelihoodFunction),
43  m_postRv (postRv),
44  m_solutionDomain (NULL),
45  m_solutionPdf (NULL),
46  m_subSolutionMdf (NULL),
47  m_subSolutionCdf (NULL),
48  m_solutionRealizer (NULL),
49  m_mhSeqGenerator (NULL),
50  m_mlSampler (NULL),
51  m_chain (NULL),
52  m_logLikelihoodValues (NULL),
53  m_logTargetValues (NULL),
54  m_alternativeOptionsValues(),
55  m_optionsObj (NULL)
56 {
57 #ifdef QUESO_MEMORY_DEBUGGING
58  std::cout << "Entering Sip" << std::endl;
59 #endif
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()
65  << std::endl;
66  }
67 
68  if (alternativeOptionsValues) m_alternativeOptionsValues = *alternativeOptionsValues;
69  if (m_env.optionsInputFileName() == "") {
70  m_optionsObj = new StatisticalInverseProblemOptions(m_env,prefix,m_alternativeOptionsValues);
71  }
72  else {
73  m_optionsObj = new StatisticalInverseProblemOptions(m_env,prefix);
74  m_optionsObj->scanOptionsValues();
75  }
76 #ifdef QUESO_MEMORY_DEBUGGING
77  std::cout << "In Sip, finished scanning options" << std::endl;
78 #endif
79 
80  UQ_FATAL_TEST_MACRO(priorRv.imageSet().vectorSpace().dimLocal() != likelihoodFunction.domainSet().vectorSpace().dimLocal(),
81  m_env.worldRank(),
82  "StatisticalInverseProblem<P_V,P_M>::constructor()",
83  "'priorRv' and 'likelihoodFunction' are related to vector spaces of different dimensions");
84 
85  UQ_FATAL_TEST_MACRO(priorRv.imageSet().vectorSpace().dimLocal() != postRv.imageSet().vectorSpace().dimLocal(),
86  m_env.worldRank(),
87  "StatisticalInverseProblem<P_V,P_M>::constructor()",
88  "'priorRv' and 'postRv' are related to vector spaces of different dimensions");
89 
90  if (m_env.subDisplayFile()) {
91  *m_env.subDisplayFile() << "Leaving StatisticalInverseProblem<P_V,P_M>::constructor()"
92  << ": prefix = " << m_optionsObj->m_prefix
93  << std::endl;
94  }
95 
96  return;
97 }
98 
99 // Destructor --------------------------------------
100 template <class P_V,class P_M>
102 {
103  if (m_chain) {
104  m_chain->clear();
105  delete m_chain;
106  }
107  if (m_logLikelihoodValues) {
108  m_logLikelihoodValues->clear();
109  delete m_logLikelihoodValues;
110  }
111  if (m_logTargetValues) {
112  m_logTargetValues->clear();
113  delete m_logTargetValues;
114  }
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;
123 }
124 // Statistical methods -----------------------------
125 template <class P_V,class P_M>
126 void
128  const MhOptionsValues* alternativeOptionsValues, // dakota
129  const P_V& initialValues,
130  const P_M* initialProposalCovMatrix)
131 {
132  //grvy_timer_begin("BayesMetropolisHastings"); TODO: revisit timing output
133  //std::cout << "proc " << m_env.fullRank() << ", HERE sip 000" << std::endl;
134  m_env.fullComm().Barrier();
135  //std::cout << "proc " << m_env.fullRank() << ", HERE sip 001" << std::endl;
136  m_env.fullComm().syncPrintDebugMsg("Entering StatisticalInverseProblem<P_V,P_M>::solveWithBayesMetropolisHastings()",1,3000000);
137 
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"
142  << std::endl;
143  }
144  return;
145  }
146  if ((m_env.subDisplayFile())) {
147  *m_env.subDisplayFile() << "In StatisticalInverseProblem<P_V,P_M>::solveWithBayesMetropolisHastings()"
148  << ": computing solution, as requested by user"
149  << std::endl;
150  }
151 
152  UQ_FATAL_TEST_MACRO(m_priorRv.imageSet().vectorSpace().dimLocal() != initialValues.sizeLocal(),
153  m_env.worldRank(),
154  "StatisticalInverseProblem<P_V,P_M>::solveWithBayesMetropolisHastings()",
155  "'m_priorRv' and 'initialValues' should have equal dimensions");
156 
157  if (initialProposalCovMatrix) {
158  UQ_FATAL_TEST_MACRO(m_priorRv.imageSet().vectorSpace().dimLocal() != initialProposalCovMatrix->numRowsLocal(),
159  m_env.worldRank(),
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(),
163  m_env.worldRank(),
164  "StatisticalInverseProblem<P_V,P_M>::solveWithBayesMetropolisHastings()",
165  "'initialProposalCovMatrix' should be a square matrix");
166  }
167 
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;
175 
176  P_V numEvaluationPointsVec(m_priorRv.imageSet().vectorSpace().zeroVector());
177  numEvaluationPointsVec.cwSet(250.);
178 
179  // Compute output pdf up to a multiplicative constant: Bayesian approach
180  m_solutionDomain = InstantiateIntersection(m_priorRv.pdf().domainSet(),m_likelihoodFunction.domainSet());
181 
182  m_solutionPdf = new BayesianJointPdf<P_V,P_M>(m_optionsObj->m_prefix.c_str(),
183  m_priorRv.pdf(),
184  m_likelihoodFunction,
185  1.,
186  *m_solutionDomain);
187 
188  m_postRv.setPdf(*m_solutionPdf);
189 
190  // Compute output realizer: Metropolis-Hastings approach
191  m_chain = new SequenceOfVectors<P_V,P_M>(m_postRv.imageSet().vectorSpace(),0,m_optionsObj->m_prefix+"chain");
192  m_logLikelihoodValues = new ScalarSequence<double> (m_env,0,m_optionsObj->m_prefix+"logLike" );
193  m_logTargetValues = new ScalarSequence<double> (m_env,0,m_optionsObj->m_prefix+"logTarget");
194  m_mhSeqGenerator = new MetropolisHastingsSG<P_V,P_M>(m_optionsObj->m_prefix.c_str(), // dakota
195  alternativeOptionsValues,
196  m_postRv,
197  initialValues,
198  initialProposalCovMatrix);
199 
200  m_mhSeqGenerator->generateSequence(*m_chain,
201  NULL, //m_logLikelihoodValues,
202  NULL);//m_logTargetValues);
203 
204  m_solutionRealizer = new SequentialVectorRealizer<P_V,P_M>(m_optionsObj->m_prefix.c_str(),
205  *m_chain);
206 
207  m_postRv.setRealizer(*m_solutionRealizer);
208 
209  m_env.fullComm().syncPrintDebugMsg("In StatisticalInverseProblem<P_V,P_M>::solveWithBayesMetropolisHastings(), code place 1",3,3000000);
210  //m_env.fullComm().Barrier();
211 
212  // Compute output mdf: uniform sampling approach
213 #ifdef UQ_ALSO_COMPUTE_MDFS_WITHOUT_KDE
214  m_subMdfGrids = new ArrayOfOneDGrids <P_V,P_M>((m_optionsObj->m_prefix+"Mdf_").c_str(),m_postRv.imageSet().vectorSpace());
215  m_subMdfValues = new ArrayOfOneDTables<P_V,P_M>((m_optionsObj->m_prefix+"Mdf_").c_str(),m_postRv.imageSet().vectorSpace());
216  m_chain->subUniformlySampledMdf(numEvaluationPointsVec, // input
217  *m_subMdfGrids, // output
218  *m_subMdfValues); // output
219  m_subSolutionMdf = new SampledVectorMdf<P_V,P_M>(m_optionsObj->m_prefix.c_str(),
220  *m_subMdfGrids,
221  *m_subMdfValues);
222  m_postRv.setMdf(*m_subSolutionMdf);
223 
224  if ((m_optionsObj->m_ov.m_dataOutputFileName != UQ_SIP_FILENAME_FOR_NO_FILE ) &&
225  (m_optionsObj->m_ov.m_dataOutputAllowedSet.find(m_env.subId()) != m_optionsObj->m_ov.m_dataOutputAllowedSet.end())) {
226  if (m_env.subRank() == 0) {
227  // Write data output file
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
231  << std::endl;
232  }
233 
234  // Open file
235  // Always write at the end of an eventual pre-existing file
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)) {
239  delete ofsvar;
240  ofsvar = new std::ofstream((m_optionsObj->m_ov.m_dataOutputFileName+"_sub"+m_env.subIdString()+".m").c_str(), std::ofstream::out | std::ofstream::trunc);
241  }
242  UQ_FATAL_TEST_MACRO((ofsvar && ofsvar->is_open()) == false,
243  m_env.worldRank(),
244  "StatisticalInverseProblem<P_V,P_M>::solveWithBayesMetropolisHastings()",
245  "failed to open file");
246 
247  m_postRv.mdf().print(*ofsvar);
248 
249  // Close file
250  //ofsvar->close();
251  delete 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
255  << std::endl;
256  }
257  }
258  }
259 #endif
260  if (m_env.subDisplayFile()) {
261  *m_env.subDisplayFile() << std::endl;
262  }
263 
264  m_env.fullComm().syncPrintDebugMsg("Leaving StatisticalInverseProblem<P_V,P_M>::solveWithBayesMetropolisHastings()",1,3000000);
265  m_env.fullComm().Barrier();
266  // grvy_timer_end("BayesMetropolisHastings"); TODO: revisit timers
267  return;
268 }
269 //--------------------------------------------------
270 template <class P_V,class P_M>
271 void
273 {
274  m_env.fullComm().Barrier();
275  m_env.fullComm().syncPrintDebugMsg("Entering StatisticalInverseProblem<P_V,P_M>::solveWithBayesMLSampling()",1,3000000);
276 
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"
281  << std::endl;
282  }
283  return;
284  }
285  if ((m_env.subDisplayFile())) {
286  *m_env.subDisplayFile() << "In StatisticalInverseProblem<P_V,P_M>::solveWithBayesMLSampling()"
287  << ": computing solution, as requested by user"
288  << std::endl;
289  }
290 
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;
298 
299  P_V numEvaluationPointsVec(m_priorRv.imageSet().vectorSpace().zeroVector());
300  numEvaluationPointsVec.cwSet(250.);
301 
302  // Compute output pdf up to a multiplicative constant: Bayesian approach
303  m_solutionDomain = InstantiateIntersection(m_priorRv.pdf().domainSet(),m_likelihoodFunction.domainSet());
304 
305  m_solutionPdf = new BayesianJointPdf<P_V,P_M>(m_optionsObj->m_prefix.c_str(),
306  m_priorRv.pdf(),
307  m_likelihoodFunction,
308  1.,
309  *m_solutionDomain);
310 
311  m_postRv.setPdf(*m_solutionPdf);
312 
313  // Compute output realizer: ML approach
314  m_chain = new SequenceOfVectors<P_V,P_M>(m_postRv.imageSet().vectorSpace(),0,m_optionsObj->m_prefix+"chain");
315  m_mlSampler = new MLSampling<P_V,P_M>(m_optionsObj->m_prefix.c_str(),
316  //m_postRv,
317  m_priorRv,
318  m_likelihoodFunction);
319  // initialValues,
320  // initialProposalCovMatrix);
321 
322  m_mlSampler->generateSequence(*m_chain,
323  NULL,
324  NULL);
325 
326  m_solutionRealizer = new SequentialVectorRealizer<P_V,P_M>(m_optionsObj->m_prefix.c_str(),
327  *m_chain);
328 
329  m_postRv.setRealizer(*m_solutionRealizer);
330 
331  if (m_env.subDisplayFile()) {
332  *m_env.subDisplayFile() << std::endl;
333  }
334 
335  m_env.fullComm().syncPrintDebugMsg("Leaving StatisticalInverseProblem<P_V,P_M>::solveWithBayesMLSampling()",1,3000000);
336  m_env.fullComm().Barrier();
337 
338  return;
339 }
340 //--------------------------------------------------
341 template <class P_V,class P_M>
342 const BaseVectorRV<P_V,P_M>&
344 {
345  return m_priorRv;
346 }
347 //--------------------------------------------------
348 template <class P_V,class P_M>
351 {
352  return m_postRv;
353 }
354 //--------------------------------------------------
355 template <class P_V,class P_M>
357 {
358  UQ_FATAL_TEST_MACRO(m_mlSampler == NULL,
359  m_env.worldRank(),
360  "StatisticalInverseProblem<P_V,P_M>::logEvidence()",
361  "m_mlSampler is NULL");
362  return m_mlSampler->logEvidence();
363 }
364 //--------------------------------------------------
365 template <class P_V,class P_M>
367 {
368  UQ_FATAL_TEST_MACRO(m_mlSampler == NULL,
369  m_env.worldRank(),
370  "StatisticalInverseProblem<P_V,P_M>::meanLogLikelihood()",
371  "m_mlSampler is NULL");
372  return m_mlSampler->meanLogLikelihood();
373 }
374 //--------------------------------------------------
375 template <class P_V,class P_M>
377 {
378  UQ_FATAL_TEST_MACRO(m_mlSampler == NULL,
379  m_env.worldRank(),
380  "StatisticalInverseProblem<P_V,P_M>::eig()",
381  "m_mlSampler is NULL");
382  return m_mlSampler->eig();
383 }
384 // I/O methods--------------------------------------
385 template <class P_V,class P_M>
386 void
388 {
389  return;
390 }
391 
392 } // End namespace QUESO
393 
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.
Definition: MLSampling.h:118
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)
Definition: Defines.h:222
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...
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).

Generated on Thu Apr 23 2015 19:18:34 for queso-0.50.1 by  doxygen 1.8.5