queso-0.57.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Pages
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-2017 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(!(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  // What should the gradient be here?
114  returnValue = 0.;
115  }
116  else if (this->m_domainSet.contains(domainVector) == false) {
117  // What should the gradient be here?
118  returnValue = 0.;
119  }
120  else {
121  // Already normalised
122  returnValue = std::exp(this->lnValue(domainVector,domainDirection,gradVector,hessianMatrix,hessianEffect));
123 
124  if (gradVector) {
125  (*gradVector) *= returnValue;
126  }
127  }
128 
129  if ((m_env.subDisplayFile()) && (m_env.displayVerbosity() >= 55)) {
130  *m_env.subDisplayFile() << "Leaving LogNormalJointPdf<V,M>::actualValue()"
131  << ", meanVector = " << *m_lawExpVector
132  << ": domainVector = " << domainVector
133  << ", returnValue = " << returnValue
134  << std::endl;
135  }
136 
137  return returnValue;
138 }
139 //--------------------------------------------------
140 template<class V, class M>
141 double
143  const V& domainVector,
144  const V* domainDirection,
145  V* gradVector,
146  M* hessianMatrix,
147  V* hessianEffect) const
148 {
149  if ((m_env.subDisplayFile()) && (m_env.displayVerbosity() >= 55)) {
150  *m_env.subDisplayFile() << "Entering LogNormalJointPdf<V,M>::lnValue()"
151  << ", meanVector = " << *m_lawExpVector
152  << ": domainVector = " << domainVector
153  << std::endl;
154  }
155 
156  queso_require_msg(!(domainDirection || hessianMatrix || hessianEffect), "incomplete code for gradVector, hessianMatrix and hessianEffect calculations");
157 
158  double returnValue = 0.;
159 
160  V zeroVector(domainVector);
161  zeroVector.cwSet(0.);
162  if (domainVector.atLeastOneComponentSmallerOrEqualThan(zeroVector)) {
163  // What should the gradient be here?
164  returnValue = -INFINITY;
165  }
166  else if (this->m_domainSet.contains(domainVector) == false) {
167  // What should the gradient be here?
168  returnValue = -INFINITY;
169  }
170  else {
171  if (m_diagonalCovMatrix) {
172  V diffVec(zeroVector);
173  for (unsigned int i = 0; i < domainVector.sizeLocal(); ++i) {
174  diffVec[i] = std::log(domainVector[i]) - this->lawExpVector()[i];
175 
176  // Compute the gradient of log of the PDF
177  // The log of a log normal pdf is:
178  // f(x) = -log(x \sigma sqrt(2 \pi)) - ((log(x) - \mu)^2 / (2 \sigma^2))
179  // Therefore
180  // \frac{df}{dx}(x) = -1/x - (log(x) - \mu) / (x \sigma^2)
181  if (gradVector) {
182  (*gradVector)[i] = -(1.0 / domainVector[i]) -
183  diffVec[i] / (domainVector[i] * this->lawVarVector()[i]);
184  }
185  }
186  returnValue = ((diffVec*diffVec)/this->lawVarVector()).sumOfComponents();
187  returnValue *= -0.5;
188 
189  for (unsigned int i = 0; i < domainVector.sizeLocal(); ++i) {
190  returnValue -= std::log(domainVector[i]);
191  if (m_normalizationStyle == 0) {
192  returnValue -= std::log(std::sqrt(2. * M_PI * this->lawVarVector()[i])); // Contribution of 1/(x\sqrt{2\pi\sigma^2})
193  }
194  }
195  }
196  else {
197  queso_error_msg("situation with a non-diagonal covariance matrix makes no sense");
198  }
199  returnValue += m_logOfNormalizationFactor;
200  }
201 
202  if ((m_env.subDisplayFile()) && (m_env.displayVerbosity() >= 55)) {
203  *m_env.subDisplayFile() << "Leaving LogNormalJointPdf<V,M>::lnValue()"
204  << ", meanVector = " << *m_lawExpVector
205  << ": domainVector = " << domainVector
206  << ", returnValue = " << returnValue
207  << std::endl;
208  }
209 
210  return returnValue;
211 }
212 //--------------------------------------------------
213 template<class V, class M>
214 void
216 {
217  // FIXME - this is the mean of a non-truncated lognormal
218  // distribution, and doesn't take into account a limited domainSet.
219 
220  if (m_diagonalCovMatrix) {
221  unsigned int n_params = meanVector.sizeLocal();
222  queso_assert_equal_to (n_params, this->lawExpVector().sizeLocal());
223 
224  for (unsigned int i = 0; i < n_params; ++i) {
225  meanVector[i] = std::exp(this->lawExpVector()[i] + 0.5*this->lawVarVector()[i]);
226  }
227  }
228  else {
229  queso_error_msg("situation with a non-diagonal covariance matrix makes no sense");
230  }
231 }
232 //--------------------------------------------------
233 template<class V, class M>
234 void
236 {
237  // FIXME - this is the variance of a non-truncated lognormal
238  // distribution, and doesn't take into account a limited domainSet.
239 
240  if (m_diagonalCovMatrix) {
241  unsigned int n_params = this->lawExpVector().sizeLocal();
242  queso_assert_equal_to (n_params, this->lawVarVector().sizeLocal());
243  queso_assert_equal_to (n_params, covMatrix.numCols());
244  queso_assert_equal_to (covMatrix.numCols(), covMatrix.numRowsGlobal());
245 
246  covMatrix.zeroLower();
247  covMatrix.zeroUpper();
248 
249  for (unsigned int i = 0; i < n_params; ++i) {
250  covMatrix(i,i) = (std::exp(this->lawVarVector()[i]) - 1) *
251  std::exp(2*this->lawExpVector()[i] + this->lawVarVector()[i]);
252  }
253  }
254  else {
255  queso_error_msg("situation with a non-diagonal covariance matrix makes no sense");
256  }
257 }
258 //--------------------------------------------------
259 template<class V, class M>
260 double
261 LogNormalJointPdf<V,M>::computeLogOfNormalizationFactor(unsigned int numSamples, bool updateFactorInternally) const
262 {
263  double value = 0.;
264 
265  if ((m_env.subDisplayFile()) && (m_env.displayVerbosity() >= 2)) {
266  *m_env.subDisplayFile() << "Entering LogNormalJointPdf<V,M>::computeLogOfNormalizationFactor()"
267  << std::endl;
268  }
269  value = BaseJointPdf<V,M>::commonComputeLogOfNormalizationFactor(numSamples, updateFactorInternally);
270  if ((m_env.subDisplayFile()) && (m_env.displayVerbosity() >= 2)) {
271  *m_env.subDisplayFile() << "Leaving LogNormalJointPdf<V,M>::computeLogOfNormalizationFactor()"
272  << ", m_logOfNormalizationFactor = " << m_logOfNormalizationFactor
273  << std::endl;
274  }
275 
276  return value;
277 }
278 
279 } // End namespace QUESO
280 
double computeLogOfNormalizationFactor(unsigned int numSamples, bool updateFactorInternally) const
Computes the logarithm of the normalization factor.
virtual void distributionMean(V &meanVector) const
Mean value of the underlying random variable.
LogNormalJointPdf(const char *prefix, const VectorSet< V, M > &domainSet, const V &lawExpVector, const V &lawVarVector)
Constructor.
A templated class for handling sets.
Definition: VectorSet.h:52
~LogNormalJointPdf()
Destructor.
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:115
const BaseEnvironment & m_env
virtual void distributionVariance(M &covMatrix) const
Covariance matrix of the underlying random variable.
A templated (base) class for handling joint PDFs.
Definition: JointPdf.h:55
double actualValue(const V &domainVector, const V *domainDirection, V *gradVector, M *hessianMatrix, V *hessianEffect) const
Actual value of the Log-Normal PDF (scalar function).
A class for handling Log-Normal joint PDFs.
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).
MonteCarloSGOptions::MonteCarloSGOptions(const BaseEnvironment &env, const char *prefix, const McOptionsValues &alternativeOptionsValues queso_require_equal_to_msg)(m_env.optionsInputFileName(), std::string(""), std::string("this constructor is incompatible with the existence of an options input file"))
unsigned int displayVerbosity() const
Definition: Environment.C:450
const V & lawExpVector() const
Access to the vector of mean values and private attribute: m_lawExpVector.
const V & lawVarVector() const
Access to the vector of variance values and private attribute: m_lawVarVector.
std::ofstream * subDisplayFile() const
Access function for m_subDisplayFile (displays file on stream).
Definition: Environment.C:320

Generated on Tue Jun 5 2018 19:48:55 for queso-0.57.1 by  doxygen 1.8.5