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

Generated on Thu Apr 23 2015 19:18:34 for queso-0.50.1 by  doxygen 1.8.5