queso-0.52.0
TransformedScaledCovMatrixTKGroup.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/TransformedScaledCovMatrixTKGroup.h>
26 #include <queso/InvLogitGaussianJointPdf.h>
27 #include <queso/GslVector.h>
28 #include <queso/GslMatrix.h>
29 
30 namespace QUESO {
31 
32 template<class V, class M>
34  const char * prefix,
35  const BoxSubset<V,M> & boxSubset,
36  const std::vector<double> & scales,
37  const M & covMatrix)
38  : BaseTKGroup<V, M>(prefix, boxSubset.vectorSpace(), scales),
39  m_boxSubset(boxSubset),
40  m_originalCovMatrix(covMatrix)
41 {
42  if ((m_env.subDisplayFile()) && (m_env.displayVerbosity() >= 5)) {
43  *m_env.subDisplayFile() << "Entering TransformedScaledCovMatrixTKGroup<V,M>::constructor()"
44  << std::endl;
45  }
46 
47  if ((m_env.subDisplayFile()) && (m_env.displayVerbosity() >= 5)) {
48  *m_env.subDisplayFile() << "In TransformedScaledCovMatrixTKGroup<V,M>::constructor()"
49  << ": m_scales.size() = " << m_scales.size()
50  << ", m_preComputingPositions.size() = " << m_preComputingPositions.size()
51  << ", m_rvs.size() = " << m_rvs.size()
52  << ", m_originalCovMatrix = " << m_originalCovMatrix
53  << std::endl;
54  }
55 
56  // Set RVs to have zero mean in the Gaussian space
58 
59  if ((m_env.subDisplayFile()) && (m_env.displayVerbosity() >= 5)) {
60  *m_env.subDisplayFile() << "Leaving TransformedScaledCovMatrixTKGroup<V,M>::constructor()"
61  << std::endl;
62  }
63 }
64 // Destructor ---------------------------------------
65 template<class V, class M>
67 {
68 }
69 // Math/Stats methods--------------------------------
70 template<class V, class M>
71 bool
73 {
74  return false;
75 }
76 //---------------------------------------------------
77 template<class V, class M>
79 TransformedScaledCovMatrixTKGroup<V,M>::rv(unsigned int stageId) const
80 {
81  UQ_FATAL_TEST_MACRO(m_rvs.size() == 0,
82  m_env.worldRank(),
83  "TransformedScaledCovMatrixTKGroup<V,M>::rv1()",
84  "m_rvs.size() = 0");
85 
86  UQ_FATAL_TEST_MACRO(m_rvs[0] == NULL, // Yes, '0', because that is the id used below
87  m_env.worldRank(),
88  "TransformedScaledCovMatrixTKGroup<V,M>::rv1()",
89  "m_rvs[0] == NULL");
90 
91  UQ_FATAL_TEST_MACRO(m_preComputingPositions.size() <= stageId,
92  m_env.worldRank(),
93  "TransformedScaledCovMatrixTKGroup<V,M>::rv1()",
94  "m_preComputingPositions.size() <= stageId");
95 
96  UQ_FATAL_TEST_MACRO(m_preComputingPositions[stageId] == NULL,
97  m_env.worldRank(),
98  "TransformedScaledCovMatrixTKGroup<V,M>::rv1()",
99  "m_preComputingPositions[stageId] == NULL");
100 
101  if ((m_env.subDisplayFile() ) &&
102  (m_env.displayVerbosity() >= 10)) {
103  *m_env.subDisplayFile() << "In TransformedScaledCovMatrixTKGroup<V,M>::rv1()"
104  << ", stageId = " << stageId
105  << ": about to call m_rvs[0]->updateLawExpVector()"
106  << ", vector = " << *m_preComputingPositions[stageId] // FIX ME: might demand parallelism
107  << std::endl;
108  }
109 
110  InvLogitGaussianVectorRV<V, M> * invlogit_gaussian =
111  dynamic_cast<InvLogitGaussianVectorRV<V, M> * >(m_rvs[0]);
112 
113  V transformedPreComputingPositions(*m_preComputingPositions[stageId]);
114  transformToGaussianSpace(*m_preComputingPositions[stageId],
115  transformedPreComputingPositions);
116 
117  invlogit_gaussian->updateLawExpVector(transformedPreComputingPositions);
118 
119  return (*invlogit_gaussian);
120 }
121 //---------------------------------------------------
122 template<class V, class M>
124 TransformedScaledCovMatrixTKGroup<V,M>::rv(const std::vector<unsigned int>& stageIds)
125 {
126  UQ_FATAL_TEST_MACRO(m_rvs.size() < stageIds.size(),
127  m_env.worldRank(),
128  "TransformedScaledCovMatrixTKGroup<V,M>::rv2()",
129  "m_rvs.size() < stageIds.size()");
130 
131  UQ_FATAL_TEST_MACRO(m_rvs[stageIds.size()-1] == NULL,
132  m_env.worldRank(),
133  "TransformedScaledCovMatrixTKGroup<V,M>::rv2()",
134  "m_rvs[stageIds.size()-1] == NULL");
135 
136  UQ_FATAL_TEST_MACRO(m_preComputingPositions.size() <= stageIds[0],
137  m_env.worldRank(),
138  "TransformedScaledCovMatrixTKGroup<V,M>::rv2()",
139  "m_preComputingPositions.size() <= stageIds[0]");
140 
141  UQ_FATAL_TEST_MACRO(m_preComputingPositions[stageIds[0]] == NULL,
142  m_env.worldRank(),
143  "TransformedScaledCovMatrixTKGroup<V,M>::rv2()",
144  "m_preComputingPositions[stageIds[0]] == NULL");
145 
146  if ((m_env.subDisplayFile() ) &&
147  (m_env.displayVerbosity() >= 10)) {
148  *m_env.subDisplayFile() << "In TransformedScaledCovMatrixTKGroup<V,M>::rv2()"
149  << ", stageIds.size() = " << stageIds.size()
150  << ", stageIds[0] = " << stageIds[0]
151  << ": about to call m_rvs[stageIds.size()-1]->updateLawExpVector()"
152  << ", vector = " << *m_preComputingPositions[stageIds[0]] // FIX ME: might demand parallelism
153  << std::endl;
154  }
155 
156  InvLogitGaussianVectorRV<V, M> * invlogit_gaussian =
157  dynamic_cast<InvLogitGaussianVectorRV<V, M> * >(m_rvs[stageIds.size()-1]);
158 
159  V transformedPreComputingPositions(*m_preComputingPositions[stageIds[0]]);
160  transformToGaussianSpace(*m_preComputingPositions[stageIds[0]],
161  transformedPreComputingPositions);
162 
163  invlogit_gaussian->updateLawExpVector(transformedPreComputingPositions);
164 
165  return (*invlogit_gaussian);
166 }
167 //---------------------------------------------------
168 template<class V, class M>
169 void
171 {
172  for (unsigned int i = 0; i < m_scales.size(); ++i) {
173  double factor = 1./m_scales[i]/m_scales[i];
174  if ((m_env.subDisplayFile() ) &&
175  (m_env.displayVerbosity() >= 10)) {
176  *m_env.subDisplayFile() << "In TransformedScaledCovMatrixTKGroup<V,M>::updateLawCovMatrix()"
177  << ", m_scales.size() = " << m_scales.size()
178  << ", i = " << i
179  << ", m_scales[i] = " << m_scales[i]
180  << ", factor = " << factor
181  << ": about to call m_rvs[i]->updateLawCovMatrix()"
182  << ", covMatrix = \n" << factor*covMatrix // FIX ME: might demand parallelism
183  << std::endl;
184  }
185 
186  InvLogitGaussianVectorRV<V, M> * invlogit_gaussian =
187  dynamic_cast<InvLogitGaussianVectorRV<V, M> * >(m_rvs[i]);
188 
189  invlogit_gaussian->updateLawCovMatrix(factor*covMatrix);
190  }
191 
192  return;
193 }
194 
195 // Misc methods -------------------------------------
196 template<class V, class M>
197 bool
199 {
200  if ((m_env.subDisplayFile()) && (m_env.displayVerbosity() >= 5)) {
201  *m_env.subDisplayFile() << "Entering TransformedScaledCovMatrixTKGroup<V,M>::setPreComputingPosition()"
202  << ": position = " << position
203  << ", stageId = " << stageId
204  << std::endl;
205  }
206 
208  //setRVsWithZeroMean();
209 
210  if ((m_env.subDisplayFile()) && (m_env.displayVerbosity() >= 5)) {
211  *m_env.subDisplayFile() << "In TransformedScaledCovMatrixTKGroup<V,M>::setPreComputingPosition()"
212  << ", position = " << position
213  << ", stageId = " << stageId
214  << ": preComputingPos = " << *m_preComputingPositions[stageId];
215  if (stageId < m_scales.size()) {
216  *m_env.subDisplayFile() << ", factor = " << 1./m_scales[stageId]/m_scales[stageId];
217  }
218  if (stageId < m_rvs.size()) {
219  const InvLogitGaussianJointPdf<V,M>* pdfPtr = dynamic_cast< const InvLogitGaussianJointPdf<V,M>* >(&(m_rvs[stageId]->pdf()));
220  *m_env.subDisplayFile() << ", rvCov = " << pdfPtr->lawCovMatrix(); // FIX ME: might demand parallelism
221  }
222  *m_env.subDisplayFile() << std::endl;
223  }
224 
225  if ((m_env.subDisplayFile()) && (m_env.displayVerbosity() >= 5)) {
226  *m_env.subDisplayFile() << "Leaving TransformedScaledCovMatrixTKGroup<V,M>::setPreComputingPosition()"
227  << ": position = " << position
228  << ", stageId = " << stageId
229  << std::endl;
230  }
231 
232  return true;
233 }
234 //---------------------------------------------------
235 template<class V, class M>
236 void
238 {
240  return;
241 }
242 
243 
244 // Private methods------------------------------------
245 template<class V, class M>
246 void
248 {
249  UQ_FATAL_TEST_MACRO(m_rvs.size() == 0,
250  m_env.worldRank(),
251  "TransformedScaledCovMatrixTKGroup<V,M>::setRVsWithZeroMean()",
252  "m_rvs.size() = 0");
253 
254  UQ_FATAL_TEST_MACRO(m_rvs.size() != m_scales.size(),
255  m_env.worldRank(),
256  "TransformedScaledCovMatrixTKGroup<V,M>::setRVsWithZeroMean()",
257  "m_rvs.size() != m_scales.size()");
258 
259  for (unsigned int i = 0; i < m_scales.size(); ++i) {
260  double factor = 1./m_scales[i]/m_scales[i];
261  UQ_FATAL_TEST_MACRO(m_rvs[i] != NULL,
262  m_env.worldRank(),
263  "TransformedScaledCovMatrixTKGroup<V,M>::setRVsWithZeroMean()",
264  "m_rvs[i] != NULL");
265  m_rvs[i] = new InvLogitGaussianVectorRV<V,M>(m_prefix.c_str(),
266  m_boxSubset, m_vectorSpace->zeroVector(),
267  factor*m_originalCovMatrix);
268  }
269 
270  return;
271 }
272 
273 template<class V, class M>
274 void
276 {
278  return;
279 }
280 
281 template<class V, class M>
282 void
284  const V & physicalPoint, V & transformedPoint) const
285 {
286  V min_domain_bounds(this->m_boxSubset.minValues());
287  V max_domain_bounds(this->m_boxSubset.maxValues());
288 
289  for (unsigned int i = 0; i < transformedPoint.sizeLocal(); i++) {
290  double min_val = min_domain_bounds[i];
291  double max_val = max_domain_bounds[i];
292 
293  if (boost::math::isfinite(min_val) &&
294  boost::math::isfinite(max_val)) {
295  // Left- and right-hand sides are finite. Do full transform.
296  transformedPoint[i] = std::log(physicalPoint[i] - min_val) -
297  std::log(max_val - physicalPoint[i]);
298  }
299  else if (boost::math::isfinite(min_val) &&
300  !boost::math::isfinite(max_val)) {
301  // Left-hand side finite, but right-hand side is not.
302  // Do only left-hand transform.
303  transformedPoint[i] = std::log(physicalPoint[i] - min_val);
304  }
305  else if (!boost::math::isfinite(min_val) &&
306  boost::math::isfinite(max_val)) {
307  // Right-hand side is finite, but left-hand side is not.
308  // Do only right-hand transform.
309  transformedPoint[i] = -std::log(max_val - physicalPoint[i]);
310  }
311  else {
312  // No transform.
313  transformedPoint[i] = physicalPoint[i];
314  }
315  }
316 }
317 
318 } // End namespace QUESO
319 
bool setPreComputingPosition(const V &position, unsigned int stageId)
Sets the pre-computing positions m_preComputingPositions[stageId] with a new vector of size position...
std::vector< double > m_scales
Definition: TKGroup.h:102
This base class allows the representation of a transition kernel.
Definition: TKGroup.h:49
const BaseEnvironment & m_env
Definition: TKGroup.h:99
std::ofstream * subDisplayFile() const
Access function for m_subDisplayFile (displays file on stream).
Definition: Environment.C:305
TransformedScaledCovMatrixTKGroup(const char *prefix, const BoxSubset< V, M > &boxSubset, const std::vector< double > &scales, const M &covMatrix)
Default constructor.
void updateLawCovMatrix(const M &newLawCovMatrix)
Updates the covariance matrix.
virtual void clearPreComputingPositions()
Clears the pre-computing positions m_preComputingPositions[stageId].
Definition: TKGroup.C:121
virtual void print(std::ostream &os) const
TODO: Prints the transition kernel.
Definition: TKGroup.C:135
const M & lawCovMatrix() const
Returns the covariance matrix; access to protected attribute m_lawCovMatrix.
Class representing a subset of a vector space shaped like a hypercube.
Definition: BoxSubset.h:41
void setRVsWithZeroMean()
Sets the mean of the underlying Gaussian RVs to zero.
void clearPreComputingPositions()
Clears the pre-computing positions m_preComputingPositions[stageId].
#define UQ_FATAL_TEST_MACRO(test, givenRank, where, what)
Definition: Defines.h:223
This class represents a transition kernel with a scaled covariance matrix on hybrid bounded/unbounded...
void updateLawCovMatrix(const M &covMatrix)
Scales the covariance matrix of the underlying Gaussian distribution.
A class for handling hybrid (transformed) Gaussians with bounds.
bool symmetric() const
Whether or not the matrix is symmetric. Always &#39;false&#39;.
void updateLawExpVector(const V &newLawExpVector)
Updates the vector that contains the mean values for the underlying Gaussian.
void print(std::ostream &os) const
TODO: Prints the transition kernel.
void transformToGaussianSpace(const V &physicalPoint, V &transformedPoint) const
unsigned int displayVerbosity() const
Definition: Environment.C:436
std::vector< const V * > m_preComputingPositions
Definition: TKGroup.h:103
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:102
const InvLogitGaussianVectorRV< V, M > & rv(unsigned int stageId) const
InvLogitGaussian increment property to construct a transition kernel.
std::vector< BaseVectorRV< V, M > * > m_rvs
Definition: TKGroup.h:104
A class representing a (transformed) Gaussian vector RV with bounds.

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