queso-0.53.0
LogNormalJointPdf.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/LogNormalJointPdf.h>
26 #include <queso/GslVector.h>
27 #include <queso/GslMatrix.h>
28 
29 namespace QUESO {
30 
31 // Constructor -------------------------------------
32 template<class V,class M>
34  const char* prefix,
35  const VectorSet<V,M>& domainSet,
36  const V& lawExpVector,
37  const V& lawVarVector)
38  :
39  BaseJointPdf<V,M>(((std::string)(prefix)+"gau").c_str(),domainSet),
40  m_lawExpVector (new V(lawExpVector)),
41  m_lawVarVector (new V(lawVarVector)),
42  m_diagonalCovMatrix(true)
43 {
44  if ((m_env.subDisplayFile()) && (m_env.displayVerbosity() >= 54)) {
45  *m_env.subDisplayFile() << "Entering LogNormalJointPdf<V,M>::constructor() [1]"
46  << ": prefix = " << m_prefix
47  << std::endl;
48  }
49 
50  if ((m_env.subDisplayFile()) && (m_env.displayVerbosity() >= 55)) {
51  *m_env.subDisplayFile() << "In LogNormalJointPdf<V,M>::constructor()"
52  //<< ", prefix = " << m_prefix
53  << ": meanVector = " << this->lawExpVector()
54  << ", Variances = " << this->lawVarVector()
55  << std::endl;
56  }
57 
58  if ((m_env.subDisplayFile()) && (m_env.displayVerbosity() >= 54)) {
59  *m_env.subDisplayFile() << "Leaving LogNormalJointPdf<V,M>::constructor() [1]"
60  << ": prefix = " << m_prefix
61  << std::endl;
62  }
63 }
64 // Destructor --------------------------------------
65 template<class V,class M>
67 {
68  delete m_lawVarVector;
69  delete m_lawExpVector;
70 }
71 // Math methods-------------------------------------
72 template <class V, class M>
73 const V&
75 {
76  return *m_lawExpVector;
77 }
78 //--------------------------------------------------
79 template <class V, class M>
80 const V&
82 {
83  return *m_lawVarVector;
84 }
85 //--------------------------------------------------
86 template<class V, class M>
87 double
89  const V& domainVector,
90  const V* domainDirection,
91  V* gradVector,
92  M* hessianMatrix,
93  V* hessianEffect) const
94 {
95  if ((m_env.subDisplayFile()) && (m_env.displayVerbosity() >= 55)) {
96  *m_env.subDisplayFile() << "Entering LogNormalJointPdf<V,M>::actualValue()"
97  << ", meanVector = " << *m_lawExpVector
98  << ": domainVector = " << domainVector
99  << ", domainVector.sizeLocal() = " << domainVector.sizeLocal()
100  << ", this->m_domainSet.vectorSpace().dimLocal() = " << this->m_domainSet.vectorSpace().dimLocal()
101  << std::endl;
102  }
103 
104  queso_require_equal_to_msg(domainVector.sizeLocal(), this->m_domainSet.vectorSpace().dimLocal(), "invalid input");
105 
106  queso_require_msg(!(gradVector || hessianMatrix || hessianEffect), "incomplete code for gradVector, hessianMatrix and hessianEffect calculations");
107 
108  double returnValue = 0.;
109 
110  V zeroVector(domainVector);
111  zeroVector.cwSet(0.);
112  if (domainVector.atLeastOneComponentSmallerOrEqualThan(zeroVector)) {
113  returnValue = 0.;
114  }
115  else if (this->m_domainSet.contains(domainVector) == false) { // prudenci 2011-Oct-04
116  returnValue = 0.;
117  }
118  else {
119  returnValue = std::exp(this->lnValue(domainVector,domainDirection,gradVector,hessianMatrix,hessianEffect));
120  }
121  //returnValue *= exp(m_logOfNormalizationFactor); // No need, because 'lnValue()' is called right above // [PDF-10]
122 
123  if ((m_env.subDisplayFile()) && (m_env.displayVerbosity() >= 55)) {
124  *m_env.subDisplayFile() << "Leaving LogNormalJointPdf<V,M>::actualValue()"
125  << ", meanVector = " << *m_lawExpVector
126  << ": domainVector = " << domainVector
127  << ", returnValue = " << returnValue
128  << std::endl;
129  }
130 
131  return returnValue;
132 }
133 //--------------------------------------------------
134 template<class V, class M>
135 double
137  const V& domainVector,
138  const V* domainDirection,
139  V* gradVector,
140  M* hessianMatrix,
141  V* hessianEffect) const
142 {
143  if ((m_env.subDisplayFile()) && (m_env.displayVerbosity() >= 55)) {
144  *m_env.subDisplayFile() << "Entering LogNormalJointPdf<V,M>::lnValue()"
145  << ", meanVector = " << *m_lawExpVector
146  << ": domainVector = " << domainVector
147  << std::endl;
148  }
149 
150  queso_require_msg(!(gradVector || hessianMatrix || hessianEffect), "incomplete code for gradVector, hessianMatrix and hessianEffect calculations");
151 
152  if (domainDirection) {}; // just to remove compiler warning
153 
154  double returnValue = 0.;
155 
156  V zeroVector(domainVector);
157  zeroVector.cwSet(0.);
158  if (domainVector.atLeastOneComponentSmallerOrEqualThan(zeroVector)) {
159  returnValue = -INFINITY;
160  }
161  else if (this->m_domainSet.contains(domainVector) == false) { // prudenci 2011-Oct-04
162  returnValue = -INFINITY;
163  }
164  else {
165  if (m_diagonalCovMatrix) {
166  V diffVec(zeroVector);
167  for (unsigned int i = 0; i < domainVector.sizeLocal(); ++i) {
168  diffVec[i] = std::log(domainVector[i]) - this->lawExpVector()[i];
169  }
170  returnValue = ((diffVec*diffVec)/this->lawVarVector()).sumOfComponents();
171  returnValue *= -0.5;
172  if (m_normalizationStyle == 0) {
173  for (unsigned int i = 0; i < domainVector.sizeLocal(); ++i) {
174  returnValue -= std::log(domainVector[i] * std::sqrt(2. * M_PI * this->lawVarVector()[i])); // Contribution of 1/(x\sqrt{2\pi\sigma^2})
175  }
176  }
177  }
178  else {
179  queso_error_msg("situation with a non-diagonal covariance matrix makes no sense");
180  }
181  returnValue += m_logOfNormalizationFactor; // [PDF-10]
182  }
183 
184  if ((m_env.subDisplayFile()) && (m_env.displayVerbosity() >= 55)) {
185  *m_env.subDisplayFile() << "Leaving LogNormalJointPdf<V,M>::lnValue()"
186  << ", meanVector = " << *m_lawExpVector
187  << ": domainVector = " << domainVector
188  << ", returnValue = " << returnValue
189  << std::endl;
190  }
191 
192  return returnValue;
193 }
194 //--------------------------------------------------
195 template<class V, class M>
196 double
197 LogNormalJointPdf<V,M>::computeLogOfNormalizationFactor(unsigned int numSamples, bool updateFactorInternally) const
198 {
199  double value = 0.;
200 
201  if ((m_env.subDisplayFile()) && (m_env.displayVerbosity() >= 2)) {
202  *m_env.subDisplayFile() << "Entering LogNormalJointPdf<V,M>::computeLogOfNormalizationFactor()"
203  << std::endl;
204  }
205  value = BaseJointPdf<V,M>::commonComputeLogOfNormalizationFactor(numSamples, updateFactorInternally);
206  if ((m_env.subDisplayFile()) && (m_env.displayVerbosity() >= 2)) {
207  *m_env.subDisplayFile() << "Leaving LogNormalJointPdf<V,M>::computeLogOfNormalizationFactor()"
208  << ", m_logOfNormalizationFactor = " << m_logOfNormalizationFactor
209  << std::endl;
210  }
211 
212  return value;
213 }
214 
215 } // End namespace QUESO
216 
unsigned int displayVerbosity() const
Definition: Environment.C:396
A templated class for handling sets.
Definition: VectorSet.h:52
~LogNormalJointPdf()
Destructor.
A templated (base) class for handling joint PDFs.
Definition: JointPdf.h:56
const V & lawVarVector() const
Access to the vector of variance values and private attribute: m_lawVarVector.
double commonComputeLogOfNormalizationFactor(unsigned int numSamples, bool updateFactorInternally) const
Common method (to the derived classes) to compute the logarithm of the normalization factor...
Definition: JointPdf.C:77
#define queso_error_msg(msg)
Definition: asserts.h:47
double actualValue(const V &domainVector, const V *domainDirection, V *gradVector, M *hessianMatrix, V *hessianEffect) const
Actual value of the Log-Normal PDF (scalar function).
double lnValue(const V &domainVector, const V *domainDirection, V *gradVector, M *hessianMatrix, V *hessianEffect) const
Logarithm of the value of the Log-Normal PDF (scalar function).
LogNormalJointPdf(const char *prefix, const VectorSet< V, M > &domainSet, const V &lawExpVector, const V &lawVarVector)
Constructor.
#define queso_require_equal_to_msg(expr1, expr2, msg)
Definition: asserts.h:85
const BaseEnvironment & m_env
#define queso_require_msg(asserted, msg)
Definition: asserts.h:69
std::ofstream * subDisplayFile() const
Access function for m_subDisplayFile (displays file on stream).
Definition: Environment.C:274
const V & lawExpVector() const
Access to the vector of mean values and private attribute: m_lawExpVector.
A class for handling Log-Normal joint PDFs.
double computeLogOfNormalizationFactor(unsigned int numSamples, bool updateFactorInternally) const
Computes the logarithm of the normalization factor.

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