queso-0.52.0
BayesianJointPdf.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-2015 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/BayesianJointPdf.h>
26 #include <queso/GslVector.h>
27 #include <queso/GslMatrix.h>
28 
29 namespace QUESO {
30 
31 // Default constructor -----------------------------
32 template<class V,class M>
34  const char* prefix,
35  const BaseJointPdf <V,M>& priorDensity,
36  const BaseScalarFunction<V,M>& likelihoodFunction,
37  double likelihoodExponent,
38  const VectorSet <V,M>& intersectionDomain)
39  :
40  BaseJointPdf<V,M>(((std::string)(prefix)+"bay").c_str(),intersectionDomain),
41  m_priorDensity (priorDensity),
42  m_likelihoodFunction (likelihoodFunction),
43  m_likelihoodExponent (likelihoodExponent),
44  m_lastComputedLogPrior (0.),
45  m_lastComputedLogLikelihood(0.),
46  m_tmpVector1 (m_domainSet.vectorSpace().zeroVector()),
47  m_tmpVector2 (m_domainSet.vectorSpace().zeroVector()),
48  m_tmpMatrix (m_domainSet.vectorSpace().newMatrix())
49 {
50 }
51 // Destructor ---------------------------------------
52 template<class V,class M>
54 {
55  delete m_tmpMatrix;
56 }
57 // Math methods -------------------------------------
58 template<class V,class M>
59 void
61 {
62  m_priorDensity.setNormalizationStyle(value);
63  return;
64 }
65 // --------------------------------------------------
66 template<class V,class M>
67 double
69 {
70  return m_lastComputedLogPrior;
71 }
72 // --------------------------------------------------
73 template<class V,class M>
74 double
76 {
77  return m_lastComputedLogLikelihood;
78 }
79 // --------------------------------------------------
80 template<class V, class M>
81 double
83  const V& domainVector,
84  const V* domainDirection,
85  V* gradVector,
86  M* hessianMatrix,
87  V* hessianEffect) const
88 {
89  if ((m_env.subDisplayFile()) && (m_env.displayVerbosity() >= 54)) {
90  *m_env.subDisplayFile() << "Entering BayesianJointPdf<V,M>::actualValue()"
91  << ": domainVector = " << domainVector
92  << std::endl;
93  }
94 
95  UQ_FATAL_TEST_MACRO(domainVector.sizeLocal() != this->m_domainSet.vectorSpace().dimLocal(),
96  m_env.worldRank(),
97  "BayesianJointPdf<V,M>::actualValue()",
98  "invalid input");
99 
100  V* gradVLike = NULL;
101  if (gradVector) gradVLike = &m_tmpVector1;
102 
103  M* hessianMLike = NULL;
104  if (hessianMatrix) hessianMLike = m_tmpMatrix;
105 
106  V* hessianELike = NULL;
107  if (hessianEffect) hessianELike = &m_tmpVector2;
108 
109  double value1 = m_priorDensity.actualValue (domainVector,domainDirection,gradVector,hessianMatrix,hessianEffect);
110  double value2 = 1.;
111  if (m_likelihoodExponent != 0.) {
112  value2 = m_likelihoodFunction.actualValue(domainVector,domainDirection,gradVLike ,hessianMLike ,hessianELike );
113  }
114 
115  UQ_FATAL_TEST_MACRO((gradVector || hessianMatrix || hessianEffect),
116  m_env.worldRank(),
117  "BayesianJointPdf<V,M>::actualValue()",
118  "incomplete code for gradVector, hessianMatrix and hessianEffect calculations");
119 
120  double returnValue = value1;
121  if (m_likelihoodExponent == 0.) {
122  // Do nothing
123  }
124  else if (m_likelihoodExponent == 1.) {
125  returnValue *= value2;
126  }
127  else {
128  returnValue *= pow(value2,m_likelihoodExponent);
129  }
130  returnValue *= exp(m_logOfNormalizationFactor); // [PDF-02] ???
131 
132  m_lastComputedLogPrior = log(value1);
133  m_lastComputedLogLikelihood = m_likelihoodExponent*log(value2);
134 
135  if ((m_env.subDisplayFile()) && (m_env.displayVerbosity() >= 54)) {
136  *m_env.subDisplayFile() << "Leaving BayesianJointPdf<V,M>::actualValue()"
137  << ": domainVector = " << domainVector
138  << ", returnValue = " << returnValue
139  << std::endl;
140  }
141 
142  return returnValue;
143 }
144 // --------------------------------------------------
145 template<class V, class M>
146 double
148  const V& domainVector,
149  const V* domainDirection,
150  V* gradVector,
151  M* hessianMatrix,
152  V* hessianEffect) const
153 {
154  if ((m_env.subDisplayFile()) && (m_env.displayVerbosity() >= 54)) {
155  *m_env.subDisplayFile() << "Entering BayesianJointPdf<V,M>::lnValue()"
156  << ": domainVector = " << domainVector
157  << std::endl;
158  }
159 
160  V* gradVLike = NULL;
161  if (gradVector) {
162  gradVLike = &m_tmpVector1;
163 
164  // DM: We do this because we want to be able to test whether the user
165  // actually filled gradVector, rather than passing a different
166  // instance of V to the likelihood code
167  for (unsigned int i = 0; i < m_tmpVector1.sizeLocal(); i++) {
168  m_tmpVector1[i] = (*gradVector)[i];
169  }
170  }
171 
172  M* hessianMLike = NULL;
173  if (hessianMatrix) hessianMLike = m_tmpMatrix;
174 
175  V* hessianELike = NULL;
176  if (hessianEffect) hessianELike = &m_tmpVector2;
177 
178  if ((m_env.subDisplayFile()) && (m_env.displayVerbosity() >= 54)) {
179  *m_env.subDisplayFile() << "In BayesianJointPdf<V,M>::lnValue()"
180  << ", domainVector = " << domainVector
181  << ": about to call prior()"
182  << std::endl;
183  }
184 
185  double value1 = m_priorDensity.lnValue(domainVector,domainDirection,gradVector,hessianMatrix,hessianEffect);
186 
187  if ((m_env.subDisplayFile()) && (m_env.displayVerbosity() >= 54)) {
188  *m_env.subDisplayFile() << "In BayesianJointPdf<V,M>::lnValue()"
189  << ", domainVector = " << domainVector
190  << ": lnPrior = " << value1
191  << std::endl;
192  }
193 
194  if ((m_env.subDisplayFile()) && (m_env.displayVerbosity() >= 54)) {
195  *m_env.subDisplayFile() << "In BayesianJointPdf<V,M>::lnValue()"
196  << ", domainVector = " << domainVector
197  << ": about to call likelihood()"
198  << std::endl;
199  }
200 
201  double value2 = 0.;
202  if (m_likelihoodExponent != 0.) {
203  value2 = m_likelihoodFunction.lnValue(domainVector,domainDirection,gradVLike, hessianMLike, hessianELike );
204  }
205 
206  if ((m_env.subDisplayFile()) && (m_env.displayVerbosity() >= 54)) {
207  *m_env.subDisplayFile() << "In BayesianJointPdf<V,M>::lnValue()"
208  << ", domainVector = " << domainVector
209  << ": value1 = " << value1
210  << ", value2 = " << value2
211  << std::endl;
212  if (gradVector) {
213  *m_env.subDisplayFile() << "In BayesianJointPdf<V,M>::lnValue()"
214  << ", domainVector = " << domainVector
215  << ": gradVector = " << *gradVector
216  << ", gradVLike = " << *gradVLike
217  << std::endl;
218  }
219  if (hessianMatrix) {
220  *m_env.subDisplayFile() << "In BayesianJointPdf<V,M>::lnValue()"
221  << ", domainVector = " << domainVector
222  << ": hessianMatrix = " << *hessianMatrix
223  << ", hessianMLike = " << *hessianMLike
224  << std::endl;
225  }
226  if (hessianEffect) {
227  *m_env.subDisplayFile() << "In BayesianJointPdf<V,M>::lnValue()"
228  << ", domainVector = " << domainVector
229  << ": hessianEffect = " << *hessianEffect
230  << ", hessianELike = " << *hessianELike
231  << std::endl;
232  }
233  }
234 
235  if (gradVector ) *gradVector += *gradVLike;
236  if (hessianMatrix) *hessianMatrix += *hessianMLike;
237  if (hessianEffect) *hessianEffect += *hessianELike;
238 
239  double returnValue = value1;
240  if (m_likelihoodExponent == 0.) {
241  // Do nothing
242  }
243  else if (m_likelihoodExponent == 1.) {
244  returnValue += value2;
245  }
246  else {
247  returnValue += value2*m_likelihoodExponent;
248  } // prudenci 2010/03/05
249  returnValue += m_logOfNormalizationFactor; // [PDF-02] ???
250 
251  m_lastComputedLogPrior = value1;
252  m_lastComputedLogLikelihood = m_likelihoodExponent*value2;
253 
254  if ((m_env.subDisplayFile()) && (m_env.displayVerbosity() >= 54)) {
255  *m_env.subDisplayFile() << "Leaving BayesianJointPdf<V,M>::lnValue()"
256  << ": domainVector = " << domainVector
257  << ", returnValue = " << returnValue
258  << std::endl;
259  }
260 
261  return returnValue;
262 }
263 // --------------------------------------------------
264 template<class V, class M>
265 double
266 BayesianJointPdf<V,M>::computeLogOfNormalizationFactor(unsigned int numSamples, bool updateFactorInternally) const
267 {
268  double value = 0.;
269 
270  double volume = m_domainSet.volume();
271  if (((boost::math::isnan)(volume)) ||
272  (volume == -INFINITY ) ||
273  (volume == INFINITY ) ||
274  (volume <= 0. )) {
275  // Do nothing
276  }
277  else {
278  UQ_FATAL_TEST_MACRO(true,
279  m_env.worldRank(),
280  "BayesianJointPdf<V,M>::lnValue()",
281  "incomplete code for computeLogOfNormalizationFactor()");
282  }
283 
284  return value;
285 }
286 
287 } // End namespace QUESO
288 
double lastComputedLogLikelihood() const
Returns the logarithm of the last computed likelihood value. Access to protected attribute m_lastComp...
A templated class for handling sets.
Definition: VectorSet.h:49
double lnValue(const V &domainVector, const V *domainDirection, V *gradVector, M *hessianMatrix, V *hessianEffect) const
Computes the logarithm of the value of the function.
double computeLogOfNormalizationFactor(unsigned int numSamples, bool updateFactorInternally) const
TODO: Computes the logarithm of the normalization factor.
~BayesianJointPdf()
Destructor.
void setNormalizationStyle(unsigned int value) const
Sets a value to be used in the normalization style of the prior density PDF (ie, protected attribute ...
#define UQ_FATAL_TEST_MACRO(test, givenRank, where, what)
Definition: Defines.h:223
A templated (base) class for handling scalar functions.
Definition: GslOptimizer.h:48
A templated (base) class for handling joint PDFs.
Definition: JointPdf.h:60
A class for handling Bayesian joint PDFs.
BayesianJointPdf(const char *prefix, const BaseJointPdf< V, M > &priorDensity, const BaseScalarFunction< V, M > &likelihoodFunction, double likelihoodExponent, const VectorSet< V, M > &intersectionDomain)
Default constructor.
double lastComputedLogPrior() const
Returns the logarithm of the last computed Prior value. Access to protected attribute m_lastComputedL...
double actualValue(const V &domainVector, const V *domainDirection, V *gradVector, M *hessianMatrix, V *hessianEffect) const
Actual value of the PDF (scalar function).

Generated on Thu Apr 23 2015 19:30:54 for queso-0.52.0 by  doxygen 1.8.5