queso-0.53.0
HessianCovMatricesTKGroup.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/HessianCovMatricesTKGroup.h>
26 #include <queso/GslVector.h>
27 #include <queso/GslMatrix.h>
28 
29 namespace QUESO {
30 
31 // Default constructor ------------------------------
32 template<class V, class M>
34  const char* prefix,
35  const VectorSpace<V,M>& vectorSpace,
36  const std::vector<double>& scales,
37  const ScalarFunctionSynchronizer<V,M>& targetPdfSynchronizer)
38  :
39  BaseTKGroup<V,M>(prefix,vectorSpace,scales),
40  m_targetPdfSynchronizer(targetPdfSynchronizer),
41  m_originalNewtonSteps (scales.size()+1,NULL), // Yes, +1
42  m_originalCovMatrices (scales.size()+1,NULL) // Yes, +1
43 {
44  if ((m_env.subDisplayFile()) && (m_env.displayVerbosity() >= 5)) {
45  *m_env.subDisplayFile() << "Entering HessianCovMatricesTKGroup<V,M>::constructor()"
46  << std::endl;
47  }
48 
49  m_rvs.resize(scales.size()+1,NULL); // Yes, +1 (IMPORTANT: overwrite initialization done by BaseTKGroup<V,M>)
50 
51  if ((m_env.subDisplayFile()) && (m_env.displayVerbosity() >= 5)) {
52  *m_env.subDisplayFile() << "In HessianCovMatricesTKGroup<V,M>::constructor()"
53  << ": m_scales.size() = " << m_scales.size()
54  << ", m_preComputingPositions.size() = " << m_preComputingPositions.size()
55  << ", m_rvs.size() = " << m_rvs.size()
56  << ", m_originalNewtonSteps.size() = " << m_originalNewtonSteps.size()
57  << ", m_originalCovMatrices.size() = " << m_originalCovMatrices.size()
58  << std::endl;
59  }
60 
61  if ((m_env.subDisplayFile()) && (m_env.displayVerbosity() >= 5)) {
62  *m_env.subDisplayFile() << "Leaving HessianCovMatricesTKGroup<V,M>::constructor()"
63  << std::endl;
64  }
65 }
66 // Destructor ---------------------------------------
67 template<class V, class M>
69 {
70 }
71 // Math/Stats methods--------------------------------
72 template<class V, class M>
73 bool
75 {
76  return false;
77 }
78 //---------------------------------------------------
79 template<class V, class M>
81 HessianCovMatricesTKGroup<V,M>::rv(unsigned int stageId) const
82 {
83  queso_require_greater_msg(m_rvs.size(), stageId, "m_rvs.size() <= stageId");
84 
85  queso_require_msg(m_rvs[stageId], "m_rvs[stageId] == NULL");
86 
87  queso_require_greater_msg(m_preComputingPositions.size(), stageId, "m_preComputingPositions.size() <= stageId");
88 
89  queso_require_msg(m_preComputingPositions[stageId], "m_preComputingPositions[stageId] == NULL");
90 
91  GaussianVectorRV<V, M> * gaussian_rv =
92  dynamic_cast<GaussianVectorRV<V, M> * >(m_rvs[stageId]);
93 
94  gaussian_rv->updateLawExpVector(*m_preComputingPositions[stageId] + *m_originalNewtonSteps[stageId]);
95 
96  if ((m_env.subDisplayFile() ) &&
97  (m_env.displayVerbosity() >= 10)) {
98  *m_env.subDisplayFile() << "In HessianCovMatrixTKGroup<V,M>::rv1()"
99  << ", stageId = " << stageId
100  << ": about to call m_rvs[stageId]->updateLawCovMatrix()"
101  << ", covMatrix = \n" << *m_originalCovMatrices[stageId] // FIX ME: might demand parallelism
102  << std::endl;
103  }
104 
105  gaussian_rv->updateLawCovMatrix(*m_originalCovMatrices[stageId]);
106 
107  return *gaussian_rv;
108 }
109 //---------------------------------------------------
110 template<class V, class M>
112 HessianCovMatricesTKGroup<V,M>::rv(const std::vector<unsigned int>& stageIds)
113 {
114  queso_require_greater_msg(m_rvs.size(), stageIds[0], "m_rvs.size() <= stageIds[0]");
115 
116  queso_require_msg(m_rvs[stageIds[0]], "m_rvs[stageIds[0]] == NULL");
117 
118  queso_require_greater_msg(m_preComputingPositions.size(), stageIds[0], "m_preComputingPositions.size() <= stageIds[0]");
119 
120  queso_require_msg(m_preComputingPositions[stageIds[0]], "m_preComputingPositions[stageIds[0]] == NULL");
121 
122  double factor = 1./m_scales[stageIds.size()-1]/m_scales[stageIds.size()-1];
123 
124  GaussianVectorRV<V, M> * gaussian_rv =
125  dynamic_cast<GaussianVectorRV<V, M> * >(m_rvs[stageIds[0]]);
126 
127  gaussian_rv->updateLawExpVector(*m_preComputingPositions[stageIds[0]] + factor*(*m_originalNewtonSteps[stageIds[0]]));
128 
129  if ((m_env.subDisplayFile() ) &&
130  (m_env.displayVerbosity() >= 10)) {
131  *m_env.subDisplayFile() << "In HessianCovMatrixTKGroup<V,M>::rv2()"
132  << ", stageIds.size() = " << stageIds.size()
133  << ", stageIds[0] = " << stageIds[0]
134  << ", factor = " << factor
135  << ": about to call m_rvs[stageIds[0]]->updateLawCovVector()"
136  << ", covMatrix = \n" << factor*(*m_originalCovMatrices[stageIds[0]]) // FIX ME: might demand parallelism
137  << std::endl;
138  }
139  gaussian_rv->updateLawCovMatrix(factor*(*m_originalCovMatrices[stageIds[0]]));
140 
141  return *gaussian_rv;
142 }
143 // Misc methods--------------------------------------
144 template<class V, class M>
145 bool
146 HessianCovMatricesTKGroup<V,M>::setPreComputingPosition(const V& position, unsigned int stageId)
147 {
148  if ((m_env.subDisplayFile()) && (m_env.displayVerbosity() >= 5)) {
149  *m_env.subDisplayFile() << "Entering HessianCovMatricesTKGroup<V,M>::setPreComputingPosition()"
150  << ": position = " << position
151  << ", stageId = " << stageId
152  << std::endl;
153  }
154 
155  bool validPreComputingPosition = true;
156 
157  // Verify consistency of sizes
158  queso_require_greater_msg(m_preComputingPositions.size(), stageId, "m_preComputingPositions.size() <= stageId");
159 
160  queso_require_equal_to_msg(m_preComputingPositions.size(), m_rvs.size(), "m_preComputingPositions.size() != m_rvs.size()");
161 
162  queso_require_equal_to_msg(m_preComputingPositions.size(), m_originalNewtonSteps.size(), "m_preComputingPositions.size() != m_originalNewtonSteps.size()");
163 
164  queso_require_equal_to_msg(m_preComputingPositions.size(), m_originalCovMatrices.size(), "m_preComputingPositions.size() != m_originalCovMatrices.size()");
165 
166  // Verify data is not null
167  queso_require_msg(!(m_preComputingPositions[stageId]), "m_preComputingPositions[stageId] != NULL");
168 
169  queso_require_msg(!(m_rvs[stageId]), "m_rvs[stageId] != NULL");
170 
171  queso_require_msg(!(m_originalNewtonSteps[stageId]), "m_originalNewtonSteps[stageId] != NULL");
172 
173  queso_require_msg(!(m_originalCovMatrices[stageId]), "m_originalCovMatrices[stageId] != NULL");
174 
176 
177  if ((m_env.subDisplayFile()) && (m_env.displayVerbosity() >= 5)) {
178  *m_env.subDisplayFile() << "In HessianCovMatricesTKGroup<V,M>::setPreComputingPosition()"
179  << ", position = " << position
180  << ", stageId = " << stageId
181  << ": m_originalNewtonSteps.size() = " << m_originalNewtonSteps.size()
182  << ", m_originalCovMatrices.size() = " << m_originalCovMatrices.size()
183  << ", m_preComputingPositions.size() = " << m_preComputingPositions.size()
184  << ", m_rvs.size() = " << m_rvs.size()
185  << std::endl;
186  }
187 
188  if (m_targetPdfSynchronizer.domainSet().contains(position)) {
189  M* tmpHessian = m_vectorSpace->newMatrix();
190  M* tmpCovMat = m_vectorSpace->newMatrix();
191  V* tmpGrad = m_vectorSpace->newVector();
192 
193  double logPrior = 0.;
194  double logLikelihood = 0.;
195  double logTarget = 0.;
196  logTarget = m_targetPdfSynchronizer.callFunction(&position, // Might demand parallel environment
197  NULL,
198  tmpGrad,
199  tmpHessian,
200  NULL,
201  &logPrior,
202  &logLikelihood);
203  if (logTarget) {}; // just to remove compiler warning
204 
205  // IMPORTANT: covariance matrix = (Hessian)^{-1} !!!
206  V unitVector(m_vectorSpace->zeroVector());
207  V multVector(m_vectorSpace->zeroVector());
208  for (unsigned int j = 0; j < tmpHessian->numCols(); ++j) {
209  if (j > 0) unitVector[j-1] = 0.;
210  unitVector[j] = 1.;
211  tmpHessian->invertMultiply(unitVector, multVector);
212  for (unsigned int i = 0; i < tmpHessian->numRowsLocal(); ++i) {
213  (*tmpCovMat)(i,j) = multVector[i];
214  }
215  }
216  if ((m_env.subDisplayFile()) && (m_env.displayVerbosity() >= 5)) {
217  *m_env.subDisplayFile() << "In HessianCovMatricesTKGroup<V,M>::setPreComputingPosition()"
218  << ", position = " << position
219  << ", stageId = " << stageId
220  << ":\n H = " << *tmpHessian
221  << "\n H^{-1} = " << *tmpCovMat
222  << "\n H*H^{-1} = " << (*tmpHessian)*(*tmpCovMat)
223  << "\n H^{-1}*H = " << (*tmpCovMat)*(*tmpHessian)
224  << std::endl;
225  }
226 
227  // Force covariance matrix to be symmetric, as the Hessian (supposedly) is
228  *tmpCovMat = .5*((*tmpCovMat) + tmpCovMat->transpose());
229 
230  // Test if covariance matrix is positive definite
231  M lowerChol(*tmpCovMat);
232  if ((m_env.subDisplayFile() ) &&
233  (m_env.displayVerbosity() >= 10)) {
234  *m_env.subDisplayFile() << "In HessianCovMatricesTKGroup<V,M>::setPreComputingPosition()"
235  << ", position = " << position
236  << ", stageId = " << stageId
237  << ": calling lowerChol.chol()"
238  << ", lowerChol = " << lowerChol
239  << std::endl;
240  }
241  int iRC = lowerChol.chol();
242  if (iRC) {
243  std::cerr << "In HessianCovMatricesTKGroup<V,M>::setPreComputingPosition(): chol failed\n";
244  }
245  if ((m_env.subDisplayFile() ) &&
246  (m_env.displayVerbosity() >= 10)) {
247  *m_env.subDisplayFile() << "In HessianCovMatricesTKGroup<V,M>::setPreComputingPosition()"
248  << ", position = " << position
249  << ", stageId = " << stageId
250  << ": got lowerChol.chol() with iRC = " << iRC
251  << std::endl;
252  }
253 
254  bool covIsPositiveDefinite = !iRC;
255 
256  if (covIsPositiveDefinite) {
257 
258  // m_env.worldRank(),
259  // "HessianCovMatricesTKGroup<V,M>::setPreComputingPosition()",
260  // "stageId is too large for m_scales");
261  //double factor = 1./m_scales[stageId]/m_scales[stageId];
262  //*tmpCovMat *= factor;
263 
264  m_originalNewtonSteps[stageId] = new V(-1.*(*tmpCovMat)*(*tmpGrad));
265  m_originalCovMatrices[stageId] = new M(*tmpCovMat);
266 
267  if ((m_env.subDisplayFile()) && (m_env.displayVerbosity() >= 5)) {
268  *m_env.subDisplayFile() << "In HessianCovMatricesTKGroup<V,M>::setPreComputingPosition()"
269  << ", position = " << position
270  << ", stageId = " << stageId
271  << ", about to instantiate a Gaussian RV"
272  << ": tmpHessian = " << *tmpHessian
273  << ", preComputingPos = " << *m_preComputingPositions[stageId]
274  << ", tmpCovMat = " << *tmpCovMat
275  << ", tmpGrad = " << *tmpGrad
276  << ", preComputedPos = " << *m_preComputingPositions[stageId] + *m_originalNewtonSteps[stageId]
277  //<< ", factor = " << factor
278  //<< ", rvCov = " << factor*(*tmpCovMat)
279  << std::endl;
280  }
281  m_rvs[stageId] = new GaussianVectorRV<V,M>(m_prefix.c_str(),
282  *m_vectorSpace,
283  *m_preComputingPositions[stageId] + *m_originalNewtonSteps[stageId],
284  *m_originalCovMatrices[stageId]);
285  }
286  else {
287  validPreComputingPosition = false;
288  }
289 
290  delete tmpGrad;
291  delete tmpCovMat;
292  delete tmpHessian;
293  }
294  else {
295  validPreComputingPosition = false;
296  }
297 
298  if (validPreComputingPosition == false) {
299  // Put "default" values on variables
300  V tmpGrad (m_vectorSpace->zeroVector());
301  M tmpCovMat(tmpGrad,1.); // = identity matrix
302  m_originalNewtonSteps[stageId] = new V(-1.*tmpCovMat*tmpGrad);
303  m_originalCovMatrices[stageId] = new M(tmpCovMat);
304  m_rvs[stageId] = new GaussianVectorRV<V,M>(m_prefix.c_str(),
305  *m_vectorSpace,
306  *m_preComputingPositions[stageId],
307  tmpCovMat);
308  }
309 
310  if ((m_env.subDisplayFile()) && (m_env.displayVerbosity() >= 5)) {
311  *m_env.subDisplayFile() << "Leaving HessianCovMatricesTKGroup<V,M>::setPreComputingPosition()"
312  << ": position = " << position
313  << ", stageId = " << stageId
314  << std::endl;
315  }
316 
317  return validPreComputingPosition;
318 }
319 //---------------------------------------------------
320 template<class V, class M>
321 void
323 {
324  queso_require_equal_to_msg(m_preComputingPositions.size(), m_originalNewtonSteps.size(), "m_preComputingPositions.size() != m_originalNewtonSteps.size()");
325 
326  queso_require_equal_to_msg(m_preComputingPositions.size(), m_originalCovMatrices.size(), "m_preComputingPositions.size() != m_originalCovMatrices.size()");
327 
329 
330  // RVs are not deleted in base class because the cov matrices are constant in the case of scaledTK class
331  for (unsigned int i = 0; i < m_rvs.size(); ++i) {
332  if (m_rvs[i]) {
333  delete m_rvs[i];
334  m_rvs[i] = NULL;
335  }
336  }
337 
338  for (unsigned int i = 0; i < m_originalNewtonSteps.size(); ++i) {
339  if (m_originalNewtonSteps[i]) {
340  delete m_originalNewtonSteps[i];
341  m_originalNewtonSteps[i] = NULL;
342  }
343  }
344 
345  for (unsigned int i = 0; i < m_originalCovMatrices.size(); ++i) {
346  if (m_originalCovMatrices[i]) {
347  delete m_originalCovMatrices[i];
348  m_originalCovMatrices[i] = NULL;
349  }
350  }
351 
352  return;
353 }
354 // I/O methods---------------------------------------
355 template<class V, class M>
356 void
358 {
360  return;
361 }
362 
363 } // End namespace QUESO
364 
unsigned int displayVerbosity() const
Definition: Environment.C:396
virtual void print(std::ostream &os) const
TODO: Prints the transition kernel.
Definition: TKGroup.C:123
This base class allows the representation of a transition kernel.
Definition: TKGroup.h:50
This class allows the representation of a transition kernel with Hessians.
void clearPreComputingPositions()
Clears the pre-computing positions m_preComputingPositions[stageId].
virtual bool setPreComputingPosition(const V &position, unsigned int stageId)
Sets the pre-computing positions m_preComputingPositions[stageId] with a new vector of size position...
Definition: TKGroup.C:96
const BaseEnvironment & m_env
Definition: TKGroup.h:100
#define queso_require_equal_to_msg(expr1, expr2, msg)
Definition: asserts.h:85
virtual void clearPreComputingPositions()
Clears the pre-computing positions m_preComputingPositions[stageId].
Definition: TKGroup.C:109
#define queso_require_msg(asserted, msg)
Definition: asserts.h:69
A class representing a Gaussian vector RV.
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:274
void print(std::ostream &os) const
TODO: Prints the transition kernel.
bool symmetric() const
Whether or not the matrix is symmetric. Always &#39;false&#39;.
std::vector< const V * > m_preComputingPositions
Definition: TKGroup.h:104
HessianCovMatricesTKGroup(const char *prefix, const VectorSpace< V, M > &vectorSpace, const std::vector< double > &scales, const ScalarFunctionSynchronizer< V, M > &targetPdfSynchronizer)
Default constructor.
std::vector< BaseVectorRV< V, M > * > m_rvs
Definition: TKGroup.h:105
void updateLawExpVector(const V &newLawExpVector)
Updates the vector that contains the mean values.
std::vector< double > m_scales
Definition: TKGroup.h:103
#define queso_require_greater_msg(expr1, expr2, msg)
Definition: asserts.h:88
A templated class for synchronizing the calls of scalar functions (BaseScalarFunction and derived cla...
A class representing a vector space.
Definition: VectorSet.h:49
const GaussianVectorRV< V, M > & rv(unsigned int stageId) const
Gaussian increment property to construct a transition kernel.
bool setPreComputingPosition(const V &position, unsigned int stageId)
Sets the pre-computing positions m_preComputingPositions[stageId] with a new vector of size position...

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