queso-0.52.0
ConcatenatedJointPdf.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/ConcatenatedJointPdf.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 BaseJointPdf<V,M>& density1,
36  const BaseJointPdf<V,M>& density2,
37  const VectorSet <V,M>& concatenatedDomain)
38  :
39  BaseJointPdf<V,M>(((std::string)(prefix)+"concat").c_str(),concatenatedDomain),
40  m_densities (2,(const BaseJointPdf<V,M>*) NULL)
41 {
42  m_densities[0] = &density1;
43  m_densities[1] = &density2;
44 
45  unsigned int size1 = m_densities[0]->domainSet().vectorSpace().dimLocal();
46  unsigned int size2 = m_densities[1]->domainSet().vectorSpace().dimLocal();
47  unsigned int size = concatenatedDomain.vectorSpace().dimLocal();
48 
49  UQ_FATAL_TEST_MACRO((size1+size2) != size,
50  m_env.worldRank(),
51  "ConcatenatedJointPdf<V,M>::constructor(1)",
52  "incompatible dimensions");
53 }
54 // Constructor -------------------------------------
55 template<class V,class M>
57  const char* prefix,
58  const std::vector<const BaseJointPdf<V,M>* >& densities,
59  const VectorSet<V,M>& concatenatedDomain)
60  :
61  BaseJointPdf<V,M>(((std::string)(prefix)+"concat").c_str(),concatenatedDomain),
62  m_densities (densities.size(),(const BaseJointPdf<V,M>*) NULL)
63 {
64  unsigned int sumSizes = 0;
65  for (unsigned i = 0; i < m_densities.size(); ++i) {
66  m_densities[i] = densities[i];
67  sumSizes += m_densities[i]->domainSet().vectorSpace().dimLocal();
68  }
69 
70  unsigned int size = concatenatedDomain.vectorSpace().dimLocal();
71 
72  UQ_FATAL_TEST_MACRO(sumSizes != size,
73  m_env.worldRank(),
74  "ConcatenatedJointPdf<V,M>::constructor(2)",
75  "incompatible dimensions");
76 }
77 // Destructor --------------------------------------
78 template<class V,class M>
80 {
81 }
82 // Math methods-------------------------------------
83 template<class V,class M>
84 void
86 {
87  for (unsigned i = 0; i < m_densities.size(); ++i) {
88  m_densities[i]->setNormalizationStyle(value);
89  }
90  return;
91 }
92 //--------------------------------------------------
93 template<class V, class M>
94 double
96  const V& domainVector,
97  const V* domainDirection,
98  V* gradVector,
99  M* hessianMatrix,
100  V* hessianEffect) const
101 {
102  if ((m_env.subDisplayFile()) && (m_env.displayVerbosity() >= 54)) {
103  *m_env.subDisplayFile() << "Entering ConcatenatedJointPdf<V,M>::actualValue()"
104  << ": domainVector = " << domainVector
105  << std::endl;
106  }
107 
108  UQ_FATAL_TEST_MACRO(domainVector.sizeLocal() != this->m_domainSet.vectorSpace().dimLocal(),
109  m_env.worldRank(),
110  "ConcatenatedJointPdf<V,M>::actualValue()",
111  "invalid input");
112 
113  UQ_FATAL_TEST_MACRO((domainDirection || gradVector || hessianMatrix || hessianEffect),
114  m_env.worldRank(),
115  "ConcatenatedJointPdf<V,M>::actualValue()",
116  "incomplete code for gradVector, hessianMatrix and hessianEffect calculations");
117 
118  std::vector<V*> vecs(m_densities.size(),(V*) NULL);
119  std::vector<double> values(m_densities.size(),0.);
120  double returnValue = 1.;
121  unsigned int cummulativeSize = 0;
122  for (unsigned int i = 0; i < vecs.size(); ++i) {
123  vecs[i] = new V(m_densities[i]->domainSet().vectorSpace().zeroVector());
124  domainVector.cwExtract(cummulativeSize,*(vecs[i]));
125  values[i] = m_densities[i]->actualValue(*(vecs[i]),NULL,NULL,NULL,NULL);
126  returnValue *= values[i];
127  if ((m_env.subDisplayFile()) && (m_env.displayVerbosity() >= 99)) {
128  *m_env.subDisplayFile() << "In ConcatenatedJointPdf<V,M>::actualValue()"
129  << ", *(vecs[" << i << "]) = " << *(vecs[i])
130  << ": values[" << i << "] = " << values[i]
131  << ", temporary cumulative value = " << returnValue
132  << std::endl;
133  }
134  cummulativeSize += vecs[i]->sizeLocal();
135  delete vecs[i];
136  }
137  //returnValue *= exp(m_logOfNormalizationFactor); // No need, because each PDF should be already normalized [PDF-11]
138 
139  if ((m_env.subDisplayFile()) && (m_env.displayVerbosity() >= 54)) {
140  *m_env.subDisplayFile() << "Leaving ConcatenatedJointPdf<V,M>::actualValue()"
141  << ": domainVector = " << domainVector
142  << ", returnValue = " << returnValue
143  << std::endl;
144  }
145 
146  return returnValue;
147 }
148 //--------------------------------------------------
149 template<class V, class M>
150 double
152  const V& domainVector,
153  const V* domainDirection,
154  V* gradVector,
155  M* hessianMatrix,
156  V* hessianEffect) const
157 {
158  if ((m_env.subDisplayFile()) && (m_env.displayVerbosity() >= 54)) {
159  *m_env.subDisplayFile() << "Entering ConcatenatedJointPdf<V,M>::lnValue()"
160  << ": domainVector = " << domainVector
161  << std::endl;
162  }
163 
164  UQ_FATAL_TEST_MACRO((domainDirection || gradVector || hessianMatrix || hessianEffect),
165  m_env.worldRank(),
166  "ConcatenatedJointPdf<V,M>::lnValue()",
167  "incomplete code for gradVector, hessianMatrix and hessianEffect calculations");
168 
169  std::vector<V*> vecs(m_densities.size(),(V*) NULL);
170  std::vector<double> values(m_densities.size(),0.);
171  double returnValue = 0.;
172  unsigned int cummulativeSize = 0;
173  for (unsigned int i = 0; i < vecs.size(); ++i) {
174  vecs[i] = new V(m_densities[i]->domainSet().vectorSpace().zeroVector());
175  domainVector.cwExtract(cummulativeSize,*(vecs[i]));
176  values[i] = m_densities[i]->lnValue(*(vecs[i]),NULL,NULL,NULL,NULL);
177  returnValue += values[i];
178  if ((m_env.subDisplayFile()) && (m_env.displayVerbosity() >= 99)) { // gpmsa
179  *m_env.subDisplayFile() << "In ConcatenatedJointPdf<V,M>::lnValue()"
180  << ", *(vecs[" << i << "]) = " << *(vecs[i])
181  << ": values[" << i << "] = " << values[i]
182  << ", temporary cumulative value = " << returnValue
183  << std::endl;
184  }
185  cummulativeSize += vecs[i]->sizeLocal();
186  delete vecs[i];
187  }
188  //returnValue += m_logOfNormalizationFactor; // No need, because each PDF should be already normalized [PDF-11]
189 
190  if ((m_env.subDisplayFile()) && (m_env.displayVerbosity() >= 54)) {
191  *m_env.subDisplayFile() << "Leaving ConcatenatedJointPdf<V,M>::lnValue()"
192  << ": domainVector = " << domainVector
193  << ", returnValue = " << returnValue
194  << std::endl;
195  }
196 
197  return returnValue;
198 }
199 //--------------------------------------------------
200 template<class V, class M>
201 double
202 ConcatenatedJointPdf<V,M>::computeLogOfNormalizationFactor(unsigned int numSamples, bool updateFactorInternally) const
203 {
204  double value = 0.;
205 
206  if ((m_env.subDisplayFile()) && (m_env.displayVerbosity() >= 2)) {
207  *m_env.subDisplayFile() << "Entering ConcatenatedJointPdf<V,M>::computeLogOfNormalizationFactor()"
208  << std::endl;
209  }
210  double volume = m_domainSet.volume();
211  if (((boost::math::isnan)(volume)) ||
212  (volume == -INFINITY ) ||
213  (volume == INFINITY ) ||
214  (volume <= 0. )) {
215  // Do nothing
216  }
217  else {
218  for (unsigned int i = 0; i < m_densities.size(); ++i) {
219  m_densities[i]->computeLogOfNormalizationFactor(numSamples, updateFactorInternally);
220  }
221  }
222  if ((m_env.subDisplayFile()) && (m_env.displayVerbosity() >= 2)) {
223  *m_env.subDisplayFile() << "Leaving ConcatenatedJointPdf<V,M>::computeLogOfNormalizationFactor()"
224  << ", m_logOfNormalizationFactor = " << m_logOfNormalizationFactor
225  << std::endl;
226  }
227 
228  return value;
229 }
230 
231 } // End namespace QUESO
232 
double computeLogOfNormalizationFactor(unsigned int numSamples, bool updateFactorInternally) const
Computes the logarithm of the normalization factor.
double lnValue(const V &domainVector, const V *domainDirection, V *gradVector, M *hessianMatrix, V *hessianEffect) const
Calculates the logarithm of the values of each density.
A class for handling concatenated PDFs.
int worldRank() const
Returns the process world rank.
Definition: Environment.C:235
ConcatenatedJointPdf(const char *prefix, const BaseJointPdf< V, M > &density1, const BaseJointPdf< V, M > &density2, const VectorSet< V, M > &concatenatedDomain)
Constructor.
A templated class for handling sets.
Definition: VectorSet.h:49
double actualValue(const V &domainVector, const V *domainDirection, V *gradVector, M *hessianMatrix, V *hessianEffect) const
Calculates the actual values of each density.
#define UQ_FATAL_TEST_MACRO(test, givenRank, where, what)
Definition: Defines.h:223
std::vector< const BaseJointPdf< V, M > * > m_densities
A templated (base) class for handling joint PDFs.
Definition: JointPdf.h:60
void setNormalizationStyle(unsigned int value) const
Sets the normalization style of all densities to value.
virtual const VectorSpace< V, M > & vectorSpace() const =0
Vector space to which this set belongs to. See template specialization.
const BaseEnvironment & m_env

Generated on Thu Apr 23 2015 19:30:54 for queso-0.52.0 by  doxygen 1.8.5