queso-0.53.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  queso_require_not_equal_to_msg(m_rvs.size(), 0, "m_rvs.size() = 0");
82 
83  queso_require_msg(m_rvs[0], "m_rvs[0] == NULL");
84 
85  queso_require_greater_msg(m_preComputingPositions.size(), stageId, "m_preComputingPositions.size() <= stageId");
86 
87  queso_require_msg(m_preComputingPositions[stageId], "m_preComputingPositions[stageId] == NULL");
88 
89  if ((m_env.subDisplayFile() ) &&
90  (m_env.displayVerbosity() >= 10)) {
91  *m_env.subDisplayFile() << "In TransformedScaledCovMatrixTKGroup<V,M>::rv1()"
92  << ", stageId = " << stageId
93  << ": about to call m_rvs[0]->updateLawExpVector()"
94  << ", vector = " << *m_preComputingPositions[stageId] // FIX ME: might demand parallelism
95  << std::endl;
96  }
97 
98  InvLogitGaussianVectorRV<V, M> * invlogit_gaussian =
99  dynamic_cast<InvLogitGaussianVectorRV<V, M> * >(m_rvs[0]);
100 
101  V transformedPreComputingPositions(*m_preComputingPositions[stageId]);
102  transformToGaussianSpace(*m_preComputingPositions[stageId],
103  transformedPreComputingPositions);
104 
105  invlogit_gaussian->updateLawExpVector(transformedPreComputingPositions);
106 
107  return (*invlogit_gaussian);
108 }
109 //---------------------------------------------------
110 template<class V, class M>
112 TransformedScaledCovMatrixTKGroup<V,M>::rv(const std::vector<unsigned int>& stageIds)
113 {
114  queso_require_greater_equal_msg(m_rvs.size(), stageIds.size(), "m_rvs.size() < stageIds.size()");
115 
116  queso_require_msg(m_rvs[stageIds.size()-1], "m_rvs[stageIds.size()-1] == 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  if ((m_env.subDisplayFile() ) &&
123  (m_env.displayVerbosity() >= 10)) {
124  *m_env.subDisplayFile() << "In TransformedScaledCovMatrixTKGroup<V,M>::rv2()"
125  << ", stageIds.size() = " << stageIds.size()
126  << ", stageIds[0] = " << stageIds[0]
127  << ": about to call m_rvs[stageIds.size()-1]->updateLawExpVector()"
128  << ", vector = " << *m_preComputingPositions[stageIds[0]] // FIX ME: might demand parallelism
129  << std::endl;
130  }
131 
132  InvLogitGaussianVectorRV<V, M> * invlogit_gaussian =
133  dynamic_cast<InvLogitGaussianVectorRV<V, M> * >(m_rvs[stageIds.size()-1]);
134 
135  V transformedPreComputingPositions(*m_preComputingPositions[stageIds[0]]);
136  transformToGaussianSpace(*m_preComputingPositions[stageIds[0]],
137  transformedPreComputingPositions);
138 
139  invlogit_gaussian->updateLawExpVector(transformedPreComputingPositions);
140 
141  return (*invlogit_gaussian);
142 }
143 //---------------------------------------------------
144 template<class V, class M>
145 void
147 {
148  for (unsigned int i = 0; i < m_scales.size(); ++i) {
149  double factor = 1./m_scales[i]/m_scales[i];
150  if ((m_env.subDisplayFile() ) &&
151  (m_env.displayVerbosity() >= 10)) {
152  *m_env.subDisplayFile() << "In TransformedScaledCovMatrixTKGroup<V,M>::updateLawCovMatrix()"
153  << ", m_scales.size() = " << m_scales.size()
154  << ", i = " << i
155  << ", m_scales[i] = " << m_scales[i]
156  << ", factor = " << factor
157  << ": about to call m_rvs[i]->updateLawCovMatrix()"
158  << ", covMatrix = \n" << factor*covMatrix // FIX ME: might demand parallelism
159  << std::endl;
160  }
161 
162  InvLogitGaussianVectorRV<V, M> * invlogit_gaussian =
163  dynamic_cast<InvLogitGaussianVectorRV<V, M> * >(m_rvs[i]);
164 
165  invlogit_gaussian->updateLawCovMatrix(factor*covMatrix);
166  }
167 
168  return;
169 }
170 
171 // Misc methods -------------------------------------
172 template<class V, class M>
173 bool
175 {
176  if ((m_env.subDisplayFile()) && (m_env.displayVerbosity() >= 5)) {
177  *m_env.subDisplayFile() << "Entering TransformedScaledCovMatrixTKGroup<V,M>::setPreComputingPosition()"
178  << ": position = " << position
179  << ", stageId = " << stageId
180  << std::endl;
181  }
182 
184  //setRVsWithZeroMean();
185 
186  if ((m_env.subDisplayFile()) && (m_env.displayVerbosity() >= 5)) {
187  *m_env.subDisplayFile() << "In TransformedScaledCovMatrixTKGroup<V,M>::setPreComputingPosition()"
188  << ", position = " << position
189  << ", stageId = " << stageId
190  << ": preComputingPos = " << *m_preComputingPositions[stageId];
191  if (stageId < m_scales.size()) {
192  *m_env.subDisplayFile() << ", factor = " << 1./m_scales[stageId]/m_scales[stageId];
193  }
194  if (stageId < m_rvs.size()) {
195  const InvLogitGaussianJointPdf<V,M>* pdfPtr = dynamic_cast< const InvLogitGaussianJointPdf<V,M>* >(&(m_rvs[stageId]->pdf()));
196  *m_env.subDisplayFile() << ", rvCov = " << pdfPtr->lawCovMatrix(); // FIX ME: might demand parallelism
197  }
198  *m_env.subDisplayFile() << std::endl;
199  }
200 
201  if ((m_env.subDisplayFile()) && (m_env.displayVerbosity() >= 5)) {
202  *m_env.subDisplayFile() << "Leaving TransformedScaledCovMatrixTKGroup<V,M>::setPreComputingPosition()"
203  << ": position = " << position
204  << ", stageId = " << stageId
205  << std::endl;
206  }
207 
208  return true;
209 }
210 //---------------------------------------------------
211 template<class V, class M>
212 void
214 {
216  return;
217 }
218 
219 
220 // Private methods------------------------------------
221 template<class V, class M>
222 void
224 {
225  queso_require_not_equal_to_msg(m_rvs.size(), 0, "m_rvs.size() = 0");
226 
227  queso_require_equal_to_msg(m_rvs.size(), m_scales.size(), "m_rvs.size() != m_scales.size()");
228 
229  for (unsigned int i = 0; i < m_scales.size(); ++i) {
230  double factor = 1./m_scales[i]/m_scales[i];
231  queso_require_msg(!(m_rvs[i]), "m_rvs[i] != NULL");
232  m_rvs[i] = new InvLogitGaussianVectorRV<V,M>(m_prefix.c_str(),
233  m_boxSubset, m_vectorSpace->zeroVector(),
234  factor*m_originalCovMatrix);
235  }
236 
237  return;
238 }
239 
240 template<class V, class M>
241 void
243 {
245  return;
246 }
247 
248 template<class V, class M>
249 void
251  const V & physicalPoint, V & transformedPoint) const
252 {
253  V min_domain_bounds(this->m_boxSubset.minValues());
254  V max_domain_bounds(this->m_boxSubset.maxValues());
255 
256  for (unsigned int i = 0; i < transformedPoint.sizeLocal(); i++) {
257  double min_val = min_domain_bounds[i];
258  double max_val = max_domain_bounds[i];
259 
260  if (boost::math::isfinite(min_val) &&
261  boost::math::isfinite(max_val)) {
262  // Left- and right-hand sides are finite. Do full transform.
263  transformedPoint[i] = std::log(physicalPoint[i] - min_val) -
264  std::log(max_val - physicalPoint[i]);
265  }
266  else if (boost::math::isfinite(min_val) &&
267  !boost::math::isfinite(max_val)) {
268  // Left-hand side finite, but right-hand side is not.
269  // Do only left-hand transform.
270  transformedPoint[i] = std::log(physicalPoint[i] - min_val);
271  }
272  else if (!boost::math::isfinite(min_val) &&
273  boost::math::isfinite(max_val)) {
274  // Right-hand side is finite, but left-hand side is not.
275  // Do only right-hand transform.
276  transformedPoint[i] = -std::log(max_val - physicalPoint[i]);
277  }
278  else {
279  // No transform.
280  transformedPoint[i] = physicalPoint[i];
281  }
282  }
283 }
284 
285 } // End namespace QUESO
286 
unsigned int displayVerbosity() const
Definition: Environment.C:396
virtual void print(std::ostream &os) const
TODO: Prints the transition kernel.
Definition: TKGroup.C:123
bool symmetric() const
Whether or not the matrix is symmetric. Always &#39;false&#39;.
This base class allows the representation of a transition kernel.
Definition: TKGroup.h:50
bool setPreComputingPosition(const V &position, unsigned int stageId)
Sets the pre-computing positions m_preComputingPositions[stageId] with a new vector of size position...
#define queso_require_not_equal_to_msg(expr1, expr2, msg)
Definition: asserts.h:86
void print(std::ostream &os) const
TODO: Prints the transition kernel.
void setRVsWithZeroMean()
Sets the mean of the underlying Gaussian RVs to zero.
TransformedScaledCovMatrixTKGroup(const char *prefix, const BoxSubset< V, M > &boxSubset, const std::vector< double > &scales, const M &covMatrix)
Default constructor.
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
void updateLawExpVector(const V &newLawExpVector)
Updates the vector that contains the mean values for the underlying Gaussian.
#define queso_require_equal_to_msg(expr1, expr2, msg)
Definition: asserts.h:85
This class represents a transition kernel with a scaled covariance matrix on hybrid bounded/unbounded...
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
std::ofstream * subDisplayFile() const
Access function for m_subDisplayFile (displays file on stream).
Definition: Environment.C:274
A class for handling hybrid (transformed) Gaussians with bounds.
std::vector< const V * > m_preComputingPositions
Definition: TKGroup.h:104
void updateLawCovMatrix(const M &newLawCovMatrix)
Updates the covariance matrix.
void updateLawCovMatrix(const M &covMatrix)
Scales the covariance matrix of the underlying Gaussian distribution.
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:44
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:105
#define queso_require_greater_equal_msg(expr1, expr2, msg)
Definition: asserts.h:90
void clearPreComputingPositions()
Clears the pre-computing positions m_preComputingPositions[stageId].
std::vector< double > m_scales
Definition: TKGroup.h:103
void transformToGaussianSpace(const V &physicalPoint, V &transformedPoint) const
#define queso_require_greater_msg(expr1, expr2, msg)
Definition: asserts.h:88
A class representing a (transformed) Gaussian vector RV with bounds.

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