queso-0.53.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  queso_require_equal_to_msg(domainVector.sizeLocal(), this->m_domainSet.vectorSpace().dimLocal(), "invalid input");
96 
97  V* gradVLike = NULL;
98  if (gradVector) gradVLike = &m_tmpVector1;
99 
100  M* hessianMLike = NULL;
101  if (hessianMatrix) hessianMLike = m_tmpMatrix;
102 
103  V* hessianELike = NULL;
104  if (hessianEffect) hessianELike = &m_tmpVector2;
105 
106  double value1 = m_priorDensity.actualValue (domainVector,domainDirection,gradVector,hessianMatrix,hessianEffect);
107  double value2 = 1.;
108  if (m_likelihoodExponent != 0.) {
109  value2 = m_likelihoodFunction.actualValue(domainVector,domainDirection,gradVLike ,hessianMLike ,hessianELike );
110  }
111 
112  queso_require_msg(!(gradVector || hessianMatrix || hessianEffect), "incomplete code for gradVector, hessianMatrix and hessianEffect calculations");
113 
114  double returnValue = value1;
115  if (m_likelihoodExponent == 0.) {
116  // Do nothing
117  }
118  else if (m_likelihoodExponent == 1.) {
119  returnValue *= value2;
120  }
121  else {
122  returnValue *= pow(value2,m_likelihoodExponent);
123  }
124  returnValue *= exp(m_logOfNormalizationFactor); // [PDF-02] ???
125 
126  m_lastComputedLogPrior = log(value1);
127  m_lastComputedLogLikelihood = m_likelihoodExponent*log(value2);
128 
129  if ((m_env.subDisplayFile()) && (m_env.displayVerbosity() >= 54)) {
130  *m_env.subDisplayFile() << "Leaving BayesianJointPdf<V,M>::actualValue()"
131  << ": domainVector = " << domainVector
132  << ", returnValue = " << returnValue
133  << std::endl;
134  }
135 
136  return returnValue;
137 }
138 // --------------------------------------------------
139 template<class V, class M>
140 double
142  const V& domainVector,
143  const V* domainDirection,
144  V* gradVector,
145  M* hessianMatrix,
146  V* hessianEffect) const
147 {
148  if ((m_env.subDisplayFile()) && (m_env.displayVerbosity() >= 54)) {
149  *m_env.subDisplayFile() << "Entering BayesianJointPdf<V,M>::lnValue()"
150  << ": domainVector = " << domainVector
151  << std::endl;
152  }
153 
154  V* gradVLike = NULL;
155  if (gradVector) {
156  gradVLike = &m_tmpVector1;
157 
158  // DM: We do this because we want to be able to test whether the user
159  // actually filled gradVector, rather than passing a different
160  // instance of V to the likelihood code
161  for (unsigned int i = 0; i < m_tmpVector1.sizeLocal(); i++) {
162  m_tmpVector1[i] = (*gradVector)[i];
163  }
164  }
165 
166  M* hessianMLike = NULL;
167  if (hessianMatrix) hessianMLike = m_tmpMatrix;
168 
169  V* hessianELike = NULL;
170  if (hessianEffect) hessianELike = &m_tmpVector2;
171 
172  if ((m_env.subDisplayFile()) && (m_env.displayVerbosity() >= 54)) {
173  *m_env.subDisplayFile() << "In BayesianJointPdf<V,M>::lnValue()"
174  << ", domainVector = " << domainVector
175  << ": about to call prior()"
176  << std::endl;
177  }
178 
179  double value1 = m_priorDensity.lnValue(domainVector,domainDirection,gradVector,hessianMatrix,hessianEffect);
180 
181  if ((m_env.subDisplayFile()) && (m_env.displayVerbosity() >= 54)) {
182  *m_env.subDisplayFile() << "In BayesianJointPdf<V,M>::lnValue()"
183  << ", domainVector = " << domainVector
184  << ": lnPrior = " << value1
185  << std::endl;
186  }
187 
188  if ((m_env.subDisplayFile()) && (m_env.displayVerbosity() >= 54)) {
189  *m_env.subDisplayFile() << "In BayesianJointPdf<V,M>::lnValue()"
190  << ", domainVector = " << domainVector
191  << ": about to call likelihood()"
192  << std::endl;
193  }
194 
195  double value2 = 0.;
196  if (m_likelihoodExponent != 0.) {
197  value2 = m_likelihoodFunction.lnValue(domainVector,domainDirection,gradVLike, hessianMLike, hessianELike );
198  }
199 
200  if ((m_env.subDisplayFile()) && (m_env.displayVerbosity() >= 54)) {
201  *m_env.subDisplayFile() << "In BayesianJointPdf<V,M>::lnValue()"
202  << ", domainVector = " << domainVector
203  << ": value1 = " << value1
204  << ", value2 = " << value2
205  << std::endl;
206  if (gradVector) {
207  *m_env.subDisplayFile() << "In BayesianJointPdf<V,M>::lnValue()"
208  << ", domainVector = " << domainVector
209  << ": gradVector = " << *gradVector
210  << ", gradVLike = " << *gradVLike
211  << std::endl;
212  }
213  if (hessianMatrix) {
214  *m_env.subDisplayFile() << "In BayesianJointPdf<V,M>::lnValue()"
215  << ", domainVector = " << domainVector
216  << ": hessianMatrix = " << *hessianMatrix
217  << ", hessianMLike = " << *hessianMLike
218  << std::endl;
219  }
220  if (hessianEffect) {
221  *m_env.subDisplayFile() << "In BayesianJointPdf<V,M>::lnValue()"
222  << ", domainVector = " << domainVector
223  << ": hessianEffect = " << *hessianEffect
224  << ", hessianELike = " << *hessianELike
225  << std::endl;
226  }
227  }
228 
229  if (gradVector ) *gradVector += *gradVLike;
230  if (hessianMatrix) *hessianMatrix += *hessianMLike;
231  if (hessianEffect) *hessianEffect += *hessianELike;
232 
233  double returnValue = value1;
234  if (m_likelihoodExponent == 0.) {
235  // Do nothing
236  }
237  else if (m_likelihoodExponent == 1.) {
238  returnValue += value2;
239  }
240  else {
241  returnValue += value2*m_likelihoodExponent;
242  } // prudenci 2010/03/05
243  returnValue += m_logOfNormalizationFactor; // [PDF-02] ???
244 
245  m_lastComputedLogPrior = value1;
246  m_lastComputedLogLikelihood = m_likelihoodExponent*value2;
247 
248  if ((m_env.subDisplayFile()) && (m_env.displayVerbosity() >= 54)) {
249  *m_env.subDisplayFile() << "Leaving BayesianJointPdf<V,M>::lnValue()"
250  << ": domainVector = " << domainVector
251  << ", returnValue = " << returnValue
252  << std::endl;
253  }
254 
255  return returnValue;
256 }
257 // --------------------------------------------------
258 template<class V, class M>
259 double
260 BayesianJointPdf<V,M>::computeLogOfNormalizationFactor(unsigned int numSamples, bool updateFactorInternally) const
261 {
262  double value = 0.;
263 
264  double volume = m_domainSet.volume();
265  if (((boost::math::isnan)(volume)) ||
266  (volume == -INFINITY ) ||
267  (volume == INFINITY ) ||
268  (volume <= 0. )) {
269  // Do nothing
270  }
271  else {
272  queso_error_msg("incomplete code for computeLogOfNormalizationFactor()");
273  }
274 
275  return value;
276 }
277 
278 } // End namespace QUESO
279 
double actualValue(const V &domainVector, const V *domainDirection, V *gradVector, M *hessianMatrix, V *hessianEffect) const
Actual value of the PDF (scalar function).
A templated class for handling sets.
Definition: VectorSet.h:52
BayesianJointPdf(const char *prefix, const BaseJointPdf< V, M > &priorDensity, const BaseScalarFunction< V, M > &likelihoodFunction, double likelihoodExponent, const VectorSet< V, M > &intersectionDomain)
Default constructor.
void setNormalizationStyle(unsigned int value) const
Sets a value to be used in the normalization style of the prior density PDF (ie, protected attribute ...
A templated (base) class for handling joint PDFs.
Definition: JointPdf.h:56
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.
#define queso_error_msg(msg)
Definition: asserts.h:47
A templated (base) class for handling scalar functions.
#define queso_require_equal_to_msg(expr1, expr2, msg)
Definition: asserts.h:85
~BayesianJointPdf()
Destructor.
#define queso_require_msg(asserted, msg)
Definition: asserts.h:69
A class for handling Bayesian joint PDFs.
double lastComputedLogLikelihood() const
Returns the logarithm of the last computed likelihood value. Access to protected attribute m_lastComp...
double lastComputedLogPrior() const
Returns the logarithm of the last computed Prior value. Access to protected attribute m_lastComputedL...

Generated on Thu Jun 11 2015 13:52:32 for queso-0.53.0 by  doxygen 1.8.5