queso-0.51.1
GaussianVectorRV.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,2009,2010,2011,2012,2013 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/GaussianVectorRV.h>
26 #include <queso/GaussianVectorRealizer.h>
27 #include <queso/GaussianJointPdf.h>
28 #include <queso/GslVector.h>
29 #include <queso/GslMatrix.h>
30 
31 namespace QUESO {
32 
33 // Constructor---------------------------------------
34 template<class V, class M>
36  const char* prefix,
37  const VectorSet<V,M>& imageSet,
38  const V& lawExpVector,
39  const V& lawVarVector)
40  :
41  BaseVectorRV<V,M>(((std::string)(prefix)+"gau").c_str(),imageSet)
42 {
43  if ((m_env.subDisplayFile()) && (m_env.displayVerbosity() >= 54)) {
44  *m_env.subDisplayFile() << "Entering GaussianVectorRV<V,M>::constructor() [1]"
45  << ": prefix = " << m_prefix
46  << std::endl;
47  }
48 
49  UQ_FATAL_TEST_MACRO((lawVarVector.getMinValue() <= 0.0),
50  m_env.worldRank(),
51  "GaussianVectorRV<V,M>::constructor() [1]",
52  "Covariance matrix is not symmetric positive definite.");
53 
54  m_pdf = new GaussianJointPdf<V,M>(m_prefix.c_str(),
55  m_imageSet,
56  lawExpVector,
57  lawVarVector);
58 
59  V cholDiag(lawVarVector);
60  cholDiag.cwSqrt();
61  M lowerCholLawCovMatrix(cholDiag);
62  lowerCholLawCovMatrix.zeroUpper(false);
63 
65  m_imageSet,
66  lawExpVector,
67  lowerCholLawCovMatrix);
68 
69  m_subCdf = NULL; // FIX ME: complete code
70  m_unifiedCdf = NULL; // FIX ME: complete code
71  m_mdf = NULL; // FIX ME: complete code
72 
73  if ((m_env.subDisplayFile()) && (m_env.displayVerbosity() >= 54)) {
74  *m_env.subDisplayFile() << "Leaving GaussianVectorRV<V,M>::constructor() [1]"
75  << ": prefix = " << m_prefix
76  << std::endl;
77  }
78 }
79 // Constructor---------------------------------------
80 template<class V, class M>
82  const char* prefix,
83  const VectorSet<V,M>& imageSet,
84  const V& lawExpVector,
85  const M& lawCovMatrix)
86  :
87  BaseVectorRV<V,M>(((std::string)(prefix)+"gau").c_str(),imageSet)
88 {
89  if ((m_env.subDisplayFile()) && (m_env.displayVerbosity() >= 54)) {
90  *m_env.subDisplayFile() << "Entering GaussianVectorRV<V,M>::constructor() [2]"
91  << ": prefix = " << m_prefix
92  << std::endl;
93  }
94 
95  m_pdf = new GaussianJointPdf<V,M>(m_prefix.c_str(),
96  m_imageSet,
97  lawExpVector,
98  lawCovMatrix);
99 
100  M lowerCholLawCovMatrix(lawCovMatrix);
101  int iRC = lowerCholLawCovMatrix.chol();
102  lowerCholLawCovMatrix.zeroUpper(false);
103  if (iRC) {
104  std::cerr << "In GaussianVectorRV<V,M>::constructor() [2]: chol failed, will use svd\n";
105  if (m_env.subDisplayFile()) {
106  *m_env.subDisplayFile() << "In GaussianVectorRV<V,M>::constructor() [2]: chol failed; will use svd; lawCovMatrix contents are\n";
107  *m_env.subDisplayFile() << lawCovMatrix; // FIX ME: might demand parallelism
108  *m_env.subDisplayFile() << std::endl;
109  }
110  M matU (lawCovMatrix);
111  M matVt(m_imageSet.vectorSpace().zeroVector());
112  V vecS (m_imageSet.vectorSpace().zeroVector());
113  iRC = lawCovMatrix.svd(matU,vecS,matVt);
115  m_env.worldRank(),
116  "GaussianVectorRV<V,M>::constructor() [2]",
117  "Cholesky decomposition of covariance matrix failed.");
118 
119  vecS.cwSqrt();
121  m_imageSet,
122  lawExpVector,
123  matU,
124  vecS, // already square rooted
125  matVt);
126  }
127  else {
129  m_imageSet,
130  lawExpVector,
131  lowerCholLawCovMatrix);
132  }
133 
134  m_subCdf = NULL; // FIX ME: complete code
135  m_unifiedCdf = NULL; // FIX ME: complete code
136  m_mdf = NULL; // FIX ME: complete code
137 
138  if ((m_env.subDisplayFile()) && (m_env.displayVerbosity() >= 54)) {
139  *m_env.subDisplayFile() << "Leaving GaussianVectorRV<V,M>::constructor() [2]"
140  << ": prefix = " << m_prefix
141  << std::endl;
142  }
143 }
144 // Destructor ---------------------------------------
145 template<class V, class M>
147 {
148  delete m_mdf;
149  delete m_unifiedCdf;
150  delete m_subCdf;
151  delete m_realizer;
152  delete m_pdf;
153 }
154 // Statistical methods-------------------------------
155 template<class V, class M>
156 void
158 {
159  // We are sure that m_pdf (and m_realizer, etc) point to associated Gaussian classes, so all is well
160  ( dynamic_cast< GaussianJointPdf <V,M>* >(m_pdf ) )->updateLawExpVector(newLawExpVector);
161  ( dynamic_cast< GaussianVectorRealizer<V,M>* >(m_realizer) )->updateLawExpVector(newLawExpVector);
162  return;
163 }
164 //---------------------------------------------------
165 template<class V, class M>
166 void
168 {
169  // We are sure that m_pdf (and m_realizer, etc) point to associated Gaussian classes, so all is well
170  ( dynamic_cast< GaussianJointPdf<V,M>* >(m_pdf) )->updateLawCovMatrix(newLawCovMatrix);
171 
172  M newLowerCholLawCovMatrix(newLawCovMatrix);
173  int iRC = newLowerCholLawCovMatrix.chol();
174  newLowerCholLawCovMatrix.zeroUpper(false);
175  if (iRC) {
176  std::cerr << "In GaussianVectorRV<V,M>::updateLawCovMatrix(): chol failed, will use svd\n";
177  if (m_env.subDisplayFile()) {
178  *m_env.subDisplayFile() << "In GaussianVectorRV<V,M>::updateLawCovMatrix(): chol failed; will use svd; newLawCovMatrix contents are\n";
179  *m_env.subDisplayFile() << newLawCovMatrix; // FIX ME: might demand parallelism
180  *m_env.subDisplayFile() << std::endl;
181  }
182  M matU (newLawCovMatrix);
183  M matVt(m_imageSet.vectorSpace().zeroVector());
184  V vecS (m_imageSet.vectorSpace().zeroVector());
185  iRC = newLawCovMatrix.svd(matU,vecS,matVt);
187  m_env.worldRank(),
188  "GaussianVectorRV<V,M>::updateLawCovMatrix()",
189  "Cholesky decomposition of covariance matrix failed.");
190 
191  vecS.cwSqrt();
192  ( dynamic_cast< GaussianVectorRealizer<V,M>* >(m_realizer) )->updateLowerCholLawCovMatrix(matU,
193  vecS, // already square rooted
194  matVt);
195  }
196  else {
197  ( dynamic_cast< GaussianVectorRealizer<V,M>* >(m_realizer) )->updateLowerCholLawCovMatrix(newLowerCholLawCovMatrix);
198  }
199  return;
200 }
201 // I/O methods---------------------------------------
202 template <class V, class M>
203 void
204 GaussianVectorRV<V,M>::print(std::ostream& os) const
205 {
206  os << "GaussianVectorRV<V,M>::print() says, 'Please implement me.'" << std::endl;
207  return;
208 }
209 
210 
211 //---------------------------------------------------
212 // Method declared outside class definition ---------
213 //---------------------------------------------------
214 template<class V, class M>
215 void
217  const V& muVec1,
218  const V& muVec2,
219  const M& sigmaMat11,
220  const M& sigmaMat12,
221  const M& sigmaMat21,
222  const M& sigmaMat22,
223  const V& sampleVec2,
224  V& muVec1_cond_on_2,
225  M& sigmaMat11_cond_on_2)
226 {
227  const BaseEnvironment& env = muVec1.env();
228  unsigned int dim1 = muVec1.sizeLocal();
229  unsigned int dim2 = muVec2.sizeLocal();
230 
231  UQ_FATAL_TEST_MACRO((sigmaMat11.numRowsLocal() != dim1) || (sigmaMat11.numCols() != dim1),
232  env.worldRank(),
233  "ComputeConditionalGaussianVectorRV()",
234  "invalid sigmaMat11");
235 
236  UQ_FATAL_TEST_MACRO((sigmaMat12.numRowsLocal() != dim1) || (sigmaMat12.numCols() != dim2),
237  env.worldRank(),
238  "ComputeConditionalGaussianVectorRV()",
239  "invalid sigmaMat12");
240 
241  UQ_FATAL_TEST_MACRO((sigmaMat21.numRowsLocal() != dim2) || (sigmaMat21.numCols() != dim1),
242  env.worldRank(),
243  "ComputeConditionalGaussianVectorRV()",
244  "invalid sigmaMat21");
245 
246  UQ_FATAL_TEST_MACRO((sigmaMat22.numRowsLocal() != dim2) || (sigmaMat22.numCols() != dim2),
247  env.worldRank(),
248  "ComputeConditionalGaussianVectorRV()",
249  "invalid sigmaMat22");
250 
251  // Check transpose operation
252  M mat_tt(sigmaMat12);
253  mat_tt.cwSet(0.);
254  mat_tt.fillWithTranspose(0,0,sigmaMat21,true,true);
255  double auxNorm = (mat_tt - sigmaMat12).normFrob();
256  if (auxNorm >= 1.e-12) {
257  if (env.subDisplayFile()) {
258  *env.subDisplayFile() << "In ComputeConditionalGaussianVectorRV()"
259  << ": WARNING, ||sigmaMat21^T - sigmaMat12||_2 = " << auxNorm
260  << std::endl;
261  }
262  }
263  UQ_FATAL_TEST_MACRO(auxNorm >= 1.e-12,
264  env.worldRank(),
265  "ComputeConditionalGaussianVectorRV()",
266  "sigmaMat12 and sigmaMat21 are not transpose of each other");
267 
268  UQ_FATAL_TEST_MACRO((sampleVec2.sizeLocal() != dim2),
269  env.worldRank(),
270  "ComputeConditionalGaussianVectorRV()",
271  "invalid sampleVec2");
272 
273  UQ_FATAL_TEST_MACRO((muVec1_cond_on_2.sizeLocal() != dim1),
274  env.worldRank(),
275  "ComputeConditionalGaussianVectorRV()",
276  "invalid muVec1_cond_on_2");
277 
278  UQ_FATAL_TEST_MACRO((sigmaMat11_cond_on_2.numRowsLocal() != dim1) || (sigmaMat11_cond_on_2.numCols() != dim1),
279  env.worldRank(),
280  "ComputeConditionalGaussianVectorRV()",
281  "invalid sigmaMat11_cond_on_2");
282 
283  muVec1_cond_on_2 = muVec1 + sigmaMat12 * sigmaMat22.invertMultiply(sampleVec2 - muVec2);
284  sigmaMat11_cond_on_2 = sigmaMat11 - sigmaMat12 * sigmaMat22.invertMultiply(sigmaMat21);
285 
286  return;
287 }
288 
289 } // End namespace QUESO
290 
292 
293 template void QUESO::ComputeConditionalGaussianVectorRV<QUESO::GslVector, QUESO::GslMatrix>(QUESO::GslVector const&, QUESO::GslVector const&, QUESO::GslMatrix const&, QUESO::GslMatrix const&, QUESO::GslMatrix const&, QUESO::GslMatrix const&, QUESO::GslVector const&, QUESO::GslVector&, QUESO::GslMatrix&);
const BaseVectorCdf< V, M > * m_unifiedCdf
Definition: VectorRV.h:118
A templated class for handling sets.
Definition: VectorSet.h:49
void print(std::ostream &os) const
TODO: Prints the vector RV.
virtual ~GaussianVectorRV()
Virtual destructor.
int worldRank() const
Returns the process world rank.
Definition: Environment.C:235
std::ofstream * subDisplayFile() const
Access function for m_subDisplayFile (displays file on stream).
Definition: Environment.C:305
const VectorSet< V, M > & m_imageSet
Definition: VectorRV.h:114
void ComputeConditionalGaussianVectorRV(const V &muVec1, const V &muVec2, const M &sigmaMat11, const M &sigmaMat12, const M &sigmaMat21, const M &sigmaMat22, const V &sampleVec2, V &muVec1_cond_on_2, M &sigmaMat11_cond_on_2)
const BaseEnvironment & m_env
Definition: VectorRV.h:112
This (virtual) class sets up the environment underlying the use of the QUESO library by an executable...
Definition: Environment.h:187
Class for vector operations using GSL library.
Definition: GslVector.h:48
const BaseVectorCdf< V, M > * m_subCdf
Definition: VectorRV.h:117
BaseVectorRealizer< V, M > * m_realizer
Definition: VectorRV.h:116
void updateLawExpVector(const V &newLawExpVector)
Updates the vector that contains the mean values.
A class for handling Gaussian joint PDFs.
A class for handling sampling from Gaussian probability density distributions.
A templated base class for handling vector RV.
Definition: VectorRV.h:51
unsigned int displayVerbosity() const
Definition: Environment.C:436
const BaseVectorMdf< V, M > * m_mdf
Definition: VectorRV.h:119
#define UQ_FATAL_TEST_MACRO(test, givenRank, where, what)
Definition: Defines.h:223
GaussianVectorRV(const char *prefix, const VectorSet< V, M > &imageSet, const V &lawExpVector, const V &lawVarVector)
Constructor.
void updateLawCovMatrix(const M &newLawCovMatrix)
Updates the covariance matrix.
Class for matrix operations using GSL library.
Definition: GslMatrix.h:47
std::string m_prefix
Definition: VectorRV.h:113
A class representing a Gaussian vector RV.
BaseJointPdf< V, M > * m_pdf
Definition: VectorRV.h:115

Generated on Thu Apr 23 2015 19:26:15 for queso-0.51.1 by  doxygen 1.8.5