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

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