25 #include <queso/BayesianJointPdf.h>
26 #include <queso/GslVector.h>
27 #include <queso/GslMatrix.h>
32 template<
class V,
class M>
37 double likelihoodExponent,
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())
52 template<
class V,
class M>
58 template<
class V,
class M>
62 m_priorDensity.setNormalizationStyle(value);
66 template<
class V,
class M>
70 return m_lastComputedLogPrior;
73 template<
class V,
class M>
77 return m_lastComputedLogLikelihood;
80 template<
class V,
class M>
83 const V& domainVector,
84 const V* domainDirection,
87 V* hessianEffect)
const
89 if ((m_env.subDisplayFile()) && (m_env.displayVerbosity() >= 54)) {
90 *m_env.subDisplayFile() <<
"Entering BayesianJointPdf<V,M>::actualValue()"
91 <<
": domainVector = " << domainVector
98 if (gradVector) gradVLike = &m_tmpVector1;
100 M* hessianMLike = NULL;
101 if (hessianMatrix) hessianMLike = m_tmpMatrix;
103 V* hessianELike = NULL;
104 if (hessianEffect) hessianELike = &m_tmpVector2;
106 double value1 = m_priorDensity.actualValue (domainVector,domainDirection,gradVector,hessianMatrix,hessianEffect);
108 if (m_likelihoodExponent != 0.) {
109 value2 = m_likelihoodFunction.actualValue(domainVector,domainDirection,gradVLike ,hessianMLike ,hessianELike );
112 queso_require_msg(!(gradVector || hessianMatrix || hessianEffect),
"incomplete code for gradVector, hessianMatrix and hessianEffect calculations");
114 double returnValue = value1;
115 if (m_likelihoodExponent == 0.) {
118 else if (m_likelihoodExponent == 1.) {
119 returnValue *= value2;
122 returnValue *= pow(value2,m_likelihoodExponent);
124 returnValue *= exp(m_logOfNormalizationFactor);
126 m_lastComputedLogPrior = log(value1);
127 m_lastComputedLogLikelihood = m_likelihoodExponent*log(value2);
129 if ((m_env.subDisplayFile()) && (m_env.displayVerbosity() >= 54)) {
130 *m_env.subDisplayFile() <<
"Leaving BayesianJointPdf<V,M>::actualValue()"
131 <<
": domainVector = " << domainVector
132 <<
", returnValue = " << returnValue
139 template<
class V,
class M>
142 const V& domainVector,
143 const V* domainDirection,
146 V* hessianEffect)
const
148 if ((m_env.subDisplayFile()) && (m_env.displayVerbosity() >= 54)) {
149 *m_env.subDisplayFile() <<
"Entering BayesianJointPdf<V,M>::lnValue()"
150 <<
": domainVector = " << domainVector
156 gradVLike = &m_tmpVector1;
161 for (
unsigned int i = 0; i < m_tmpVector1.sizeLocal(); i++) {
162 m_tmpVector1[i] = (*gradVector)[i];
166 M* hessianMLike = NULL;
167 if (hessianMatrix) hessianMLike = m_tmpMatrix;
169 V* hessianELike = NULL;
170 if (hessianEffect) hessianELike = &m_tmpVector2;
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()"
179 double value1 = m_priorDensity.lnValue(domainVector,domainDirection,gradVector,hessianMatrix,hessianEffect);
181 if ((m_env.subDisplayFile()) && (m_env.displayVerbosity() >= 54)) {
182 *m_env.subDisplayFile() <<
"In BayesianJointPdf<V,M>::lnValue()"
183 <<
", domainVector = " << domainVector
184 <<
": lnPrior = " << value1
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()"
196 if (m_likelihoodExponent != 0.) {
197 value2 = m_likelihoodFunction.lnValue(domainVector,domainDirection,gradVLike, hessianMLike, hessianELike );
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
207 *m_env.subDisplayFile() <<
"In BayesianJointPdf<V,M>::lnValue()"
208 <<
", domainVector = " << domainVector
209 <<
": gradVector = " << *gradVector
210 <<
", gradVLike = " << *gradVLike
214 *m_env.subDisplayFile() <<
"In BayesianJointPdf<V,M>::lnValue()"
215 <<
", domainVector = " << domainVector
216 <<
": hessianMatrix = " << *hessianMatrix
217 <<
", hessianMLike = " << *hessianMLike
221 *m_env.subDisplayFile() <<
"In BayesianJointPdf<V,M>::lnValue()"
222 <<
", domainVector = " << domainVector
223 <<
": hessianEffect = " << *hessianEffect
224 <<
", hessianELike = " << *hessianELike
229 if (gradVector ) *gradVector += *gradVLike;
230 if (hessianMatrix) *hessianMatrix += *hessianMLike;
231 if (hessianEffect) *hessianEffect += *hessianELike;
233 double returnValue = value1;
234 if (m_likelihoodExponent == 0.) {
237 else if (m_likelihoodExponent == 1.) {
238 returnValue += value2;
241 returnValue += value2*m_likelihoodExponent;
243 returnValue += m_logOfNormalizationFactor;
245 m_lastComputedLogPrior = value1;
246 m_lastComputedLogLikelihood = m_likelihoodExponent*value2;
248 if ((m_env.subDisplayFile()) && (m_env.displayVerbosity() >= 54)) {
249 *m_env.subDisplayFile() <<
"Leaving BayesianJointPdf<V,M>::lnValue()"
250 <<
": domainVector = " << domainVector
251 <<
", returnValue = " << returnValue
258 template<
class V,
class M>
264 double volume = m_domainSet.volume();
265 if (((boost::math::isnan)(volume)) ||
266 (volume == -INFINITY ) ||
267 (volume == INFINITY ) ||
272 queso_error_msg(
"incomplete code for computeLogOfNormalizationFactor()");
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.
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.
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)
A templated (base) class for handling scalar functions.
#define queso_require_equal_to_msg(expr1, expr2, msg)
~BayesianJointPdf()
Destructor.
#define queso_require_msg(asserted, msg)
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...