queso-0.53.0
InvLogitGaussianJointPdf.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/InvLogitGaussianJointPdf.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 BoxSubset<V,M>& domainBoxSubset,
36  const V& lawExpVector,
37  const V& lawVarVector)
38  :
39  BaseJointPdf<V,M>(((std::string)(prefix)+"invlogit_gau").c_str(),
40  domainBoxSubset),
41  m_lawExpVector(new V(lawExpVector)),
42  m_lawVarVector(new V(lawVarVector)),
43  m_diagonalCovMatrix(true),
44  m_lawCovMatrix(m_domainSet.vectorSpace().newDiagMatrix(lawVarVector)),
45  m_domainBoxSubset(domainBoxSubset)
46 {
47 }
48 
49 
50 template<class V,class M>
52  const char* prefix,
53  const BoxSubset<V,M>& domainBoxSubset,
54  const V& lawExpVector,
55  const M& lawCovMatrix)
56  :
57  BaseJointPdf<V,M>(((std::string)(prefix)+"invlogit_gau").c_str(),
58  domainBoxSubset),
59  m_lawExpVector(new V(lawExpVector)),
60  m_lawVarVector(domainBoxSubset.vectorSpace().newVector(INFINITY)), // FIX ME
61  m_diagonalCovMatrix(false),
62  m_lawCovMatrix(new M(lawCovMatrix)),
63  m_domainBoxSubset(domainBoxSubset)
64 {
65 }
66 
67 template<class V,class M>
69 {
70  delete m_lawCovMatrix;
71  delete m_lawVarVector;
72  delete m_lawExpVector;
73 }
74 
75 template <class V, class M>
76 const V&
78 {
79  return *m_lawExpVector;
80 }
81 
82 template <class V, class M>
83 const V&
85 {
86  return *m_lawVarVector;
87 }
88 
89 template<class V, class M>
90 double
92  const V& domainVector,
93  const V* domainDirection,
94  V* gradVector,
95  M* hessianMatrix,
96  V* hessianEffect) const
97 {
98  double returnValue;
99 
100  returnValue = std::exp(this->lnValue(domainVector,domainDirection,gradVector,hessianMatrix,hessianEffect));
101 
102  return returnValue;
103 }
104 
105 template<class V, class M>
106 double
108  const V& domainVector,
109  const V* domainDirection,
110  V* gradVector,
111  M* hessianMatrix,
112  V* hessianEffect) const
113 {
114  double returnValue;
115  double lnDeterminant = 0.0;
116  V transformedDomainVector(domainVector);
117 
118  V min_domain_bounds(this->m_domainBoxSubset.minValues());
119  V max_domain_bounds(this->m_domainBoxSubset.maxValues());
120 
121  double lnjacobian = 0.0;
122  for (unsigned int i = 0; i < domainVector.sizeLocal(); i++) {
123  double min_val = min_domain_bounds[i];
124  double max_val = max_domain_bounds[i];
125 
126  if (boost::math::isfinite(min_val) &&
127  boost::math::isfinite(max_val)) {
128 
129  if (domainVector[i] == min_val || domainVector[i] == max_val) {
130  // Exit early if we can
131  return -INFINITY;
132  }
133 
134  // Left- and right-hand sides are finite. Do full transform.
135  transformedDomainVector[i] = std::log(domainVector[i] - min_val) -
136  std::log(max_val - domainVector[i]);
137 
138  lnjacobian += std::log(max_val - min_val) -
139  std::log(domainVector[i] - min_val) -
140  std::log(max_val - domainVector[i]);
141  }
142  else if (boost::math::isfinite(min_val) &&
143  !boost::math::isfinite(max_val)) {
144 
145  if (domainVector[i] == min_val) {
146  // Exit early if we can
147  return -INFINITY;
148  }
149 
150  // Left-hand side finite, but right-hand side is not.
151  // Do only left-hand transform.
152  transformedDomainVector[i] = std::log(domainVector[i] - min_val);
153 
154  lnjacobian += -std::log(domainVector[i] - min_val);
155  }
156  else if (!boost::math::isfinite(min_val) &&
157  boost::math::isfinite(max_val)) {
158 
159  if (domainVector[i] == max_val) {
160  // Exit early if we can
161  return -INFINITY;
162  }
163 
164  // Right-hand side is finite, but left-hand side is not.
165  // Do only right-hand transform.
166  transformedDomainVector[i] = -std::log(max_val - domainVector[i]);
167 
168  lnjacobian += -std::log(max_val - domainVector[i]);
169  }
170  else {
171  // No transform.
172  transformedDomainVector[i] = domainVector[i];
173  }
174  }
175 
176  V diffVec(transformedDomainVector - this->lawExpVector());
177  if (m_diagonalCovMatrix) {
178  returnValue = ((diffVec * diffVec) /
179  this->lawVarVector()).sumOfComponents();
180  if (m_normalizationStyle == 0) {
181  unsigned int iMax = this->lawVarVector().sizeLocal();
182  for (unsigned int i = 0; i < iMax; ++i) {
183  lnDeterminant += log(this->lawVarVector()[i]);
184  }
185  }
186  }
187  else {
188  V tmpVec = this->m_lawCovMatrix->invertMultiply(diffVec);
189  returnValue = (diffVec * tmpVec).sumOfComponents();
190  if (m_normalizationStyle == 0) {
191  lnDeterminant = this->m_lawCovMatrix->lnDeterminant();
192  }
193  }
194  if (m_normalizationStyle == 0) {
195  returnValue += ((double) this->lawVarVector().sizeLocal()) * log(2 * M_PI);
196  returnValue += lnDeterminant;
197  }
198  returnValue *= -0.5;
199  returnValue += m_logOfNormalizationFactor;
200  returnValue += lnjacobian;
201 
202  return returnValue;
203 }
204 
205 template<class V, class M>
206 double
207 InvLogitGaussianJointPdf<V,M>::computeLogOfNormalizationFactor(unsigned int numSamples, bool updateFactorInternally) const
208 {
209  double value = 0.;
210 
211  if ((m_env.subDisplayFile()) && (m_env.displayVerbosity() >= 2)) {
212  *m_env.subDisplayFile() << "Entering GaussianJointPdf<V,M>::computeLogOfNormalizationFactor()"
213  << std::endl;
214  }
215  value = BaseJointPdf<V,M>::commonComputeLogOfNormalizationFactor(numSamples, updateFactorInternally);
216  if ((m_env.subDisplayFile()) && (m_env.displayVerbosity() >= 2)) {
217  *m_env.subDisplayFile() << "Leaving GaussianJointPdf<V,M>::computeLogOfNormalizationFactor()"
218  << ", m_logOfNormalizationFactor = " << m_logOfNormalizationFactor
219  << std::endl;
220  }
221 
222  return value;
223 }
224 
225 template<class V, class M>
226 void
228 {
229  // delete old expected values (allocated at construction or last call to this function)
230  delete m_lawExpVector;
231  m_lawExpVector = new V(newLawExpVector);
232 }
233 
234 template<class V, class M>
235 void
237 {
238  // delete old expected values (allocated at construction or last call to this function)
239  delete m_lawCovMatrix;
240  m_lawCovMatrix = new M(newLawCovMatrix);
241 }
242 
243 template<class V, class M>
244 const M&
246 {
247  return *m_lawCovMatrix;
248 }
249 
250 } // End namespace QUESO
251 
const V & lawExpVector() const
Access to the vector of mean values of the Gaussian (not transformed) and private attribute: m_lawExp...
void updateLawCovMatrix(const M &newLawCovMatrix)
Updates the lower triangular matrix from Cholesky decomposition of the covariance matrix to the new v...
A templated (base) class for handling joint PDFs.
Definition: JointPdf.h:56
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
const V & lawVarVector() const
Access to the vector of variance values and private attribute: m_lawVarVector.
double lnValue(const V &domainVector, const V *domainDirection, V *gradVector, M *hessianMatrix, V *hessianEffect) const
Logarithm of the value of the (transformed) Gaussian PDF (scalar function).
A class for handling hybrid (transformed) Gaussians with bounds.
double actualValue(const V &domainVector, const V *domainDirection, V *gradVector, M *hessianMatrix, V *hessianEffect) const
Actual value of the (transformed) Gaussian PDF.
InvLogitGaussianJointPdf(const char *prefix, const BoxSubset< V, M > &domainBoxSubset, const V &lawExpVector, const V &lawVarVector)
Constructor.
const M & lawCovMatrix() const
Returns the covariance matrix; access to protected attribute m_lawCovMatrix.
Class representing a subset of a vector space shaped like a hypercube.
Definition: BoxSubset.h:44
void updateLawExpVector(const V &newLawExpVector)
Updates the mean of the Gaussian (not transformed) with the new value newLawExpVector.
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