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-2017 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 <cmath>
26 #include <limits>
27 #include <queso/InvLogitGaussianVectorRealizer.h>
28 #include <queso/GslVector.h>
29 #include <queso/GslMatrix.h>
30 #include <queso/math_macros.h>
31 
32 namespace QUESO {
33 
34 template<class V, class M>
InvLogitGaussianVectorRealizer(const char * prefix,const VectorSet<V,M> & unifiedImageSet,const V & lawExpVector,const M & lowerCholLawCovMatrix)35 InvLogitGaussianVectorRealizer<V, M>::InvLogitGaussianVectorRealizer(
36     const char * prefix,
37     const VectorSet<V, M> & unifiedImageSet,
38     const V & lawExpVector,
39     const M & lowerCholLawCovMatrix)
40   : BaseVectorRealizer<V, M>(((std::string)(prefix)+"invlogit_gau").c_str(),
41       unifiedImageSet, std::numeric_limits<unsigned int>::max()),
42     m_unifiedLawExpVector(new V(lawExpVector)),
43     m_unifiedLawVarVector(
44         unifiedImageSet.vectorSpace().newVector(INFINITY)),  // FIX ME
45     m_lowerCholLawCovMatrix(new M(lowerCholLawCovMatrix)),
46     m_matU(NULL),
47     m_vecSsqrt(NULL),
48     m_matVt(NULL)
49 {
50   *m_unifiedLawExpVector = lawExpVector;
51 }
52 
53 template<class V, class M>
InvLogitGaussianVectorRealizer(const char * prefix,const VectorSet<V,M> & unifiedImageSet,const V & lawExpVector,const M & matU,const V & vecSsqrt,const M & matVt)54 InvLogitGaussianVectorRealizer<V, M>::InvLogitGaussianVectorRealizer(
55     const char * prefix,
56     const VectorSet<V, M> & unifiedImageSet,
57     const V & lawExpVector,
58     const M & matU,
59     const V & vecSsqrt,
60     const M & matVt)
61   : BaseVectorRealizer<V, M>(((std::string)(prefix)+"invlogit_gau").c_str(),
62       unifiedImageSet, std::numeric_limits<unsigned int>::max()),
63     m_unifiedLawExpVector(new V(lawExpVector)),
64     m_unifiedLawVarVector(unifiedImageSet.vectorSpace().newVector( INFINITY)), // FIX ME
65     m_lowerCholLawCovMatrix(NULL),
66     m_matU(new M(matU)),
67     m_vecSsqrt(new V(vecSsqrt)),
68     m_matVt(new M(matVt))
69 {
70   *m_unifiedLawExpVector = lawExpVector; // ????
71 }
72 
73 template<class V, class M>
~InvLogitGaussianVectorRealizer()74 InvLogitGaussianVectorRealizer<V, M>::~InvLogitGaussianVectorRealizer()
75 {
76   delete m_matVt;
77   delete m_vecSsqrt;
78   delete m_matU;
79   delete m_lowerCholLawCovMatrix;
80   delete m_unifiedLawVarVector;
81   delete m_unifiedLawExpVector;
82 }
83 
84 template <class V, class M>
85 const V &
unifiedLawExpVector()86 InvLogitGaussianVectorRealizer<V, M>::unifiedLawExpVector() const
87 {
88   return *m_unifiedLawExpVector;
89 }
90 
91 template <class V, class M>
92 const V &
unifiedLawVarVector()93 InvLogitGaussianVectorRealizer<V, M>::unifiedLawVarVector() const
94 {
95   return *m_unifiedLawVarVector;
96 }
97 
98 template<class V, class M>
99 void
realization(V & nextValues)100 InvLogitGaussianVectorRealizer<V, M>::realization(V & nextValues) const
101 {
102   V iidGaussianVector(m_unifiedImageSet.vectorSpace().zeroVector());
103 
104   iidGaussianVector.cwSetGaussian(0.0, 1.0);
105 
106   if (m_lowerCholLawCovMatrix) {
107     nextValues = (*m_unifiedLawExpVector) +
108       (*m_lowerCholLawCovMatrix) * iidGaussianVector;
109   }
110   else if (m_matU && m_vecSsqrt && m_matVt) {
111     nextValues = (*m_unifiedLawExpVector) +
112       (*m_matU) * ((*m_vecSsqrt) * ((*m_matVt) * iidGaussianVector));
113   }
114   else {
115     queso_error_msg("GaussianVectorRealizer<V,M>::realization() inconsistent internal state");
116   }
117 
118   V min_domain_bounds(this->m_unifiedImageSet.minValues());
119   V max_domain_bounds(this->m_unifiedImageSet.maxValues());
120 
121   for (unsigned int i = 0; i < nextValues.sizeLocal(); i++) {
122     double temp = std::exp(nextValues[i]);
123     double min_val = min_domain_bounds[i];
124     double max_val = max_domain_bounds[i];
125 
126     if (queso_isfinite(min_val) &&
127         queso_isfinite(max_val)) {
128         // Left- and right-hand sides are finite.  Do full transform.
129         nextValues[i] = (max_val * temp + min_val) / (1.0 + temp);
130       }
131     else if (queso_isfinite(min_val) &&
132              !queso_isfinite(max_val)) {
133       // Left-hand side finite, but right-hand side is not.
134       // Do only left-hand transform.
135       nextValues[i] = temp + min_val;
136     }
137     else if (!queso_isfinite(min_val) &&
138              queso_isfinite(max_val)) {
139       // Right-hand side is finite, but left-hand side is not.
140       // Do only right-hand transform.
141       nextValues[i] = (max_val * temp - 1.0) / temp;
142     }
143   }
144 }
145 
146 template<class V, class M>
147 void
updateLawExpVector(const V & newLawExpVector)148 InvLogitGaussianVectorRealizer<V, M>::updateLawExpVector(
149     const V & newLawExpVector)
150 {
151   // delete old expected values (allocated at construction or last call to this function)
152   delete m_unifiedLawExpVector;
153 
154   m_unifiedLawExpVector = new V(newLawExpVector);
155 }
156 
157 template<class V, class M>
158 void
updateLowerCholLawCovMatrix(const M & newLowerCholLawCovMatrix)159 InvLogitGaussianVectorRealizer<V, M>::updateLowerCholLawCovMatrix(
160     const M & newLowerCholLawCovMatrix)
161 {
162   // delete old expected values (allocated at construction or last call to this function)
163   delete m_lowerCholLawCovMatrix;
164   delete m_matU;
165   delete m_vecSsqrt;
166   delete m_matVt;
167 
168   m_lowerCholLawCovMatrix = new M(newLowerCholLawCovMatrix);
169   m_matU                  = NULL;
170   m_vecSsqrt              = NULL;
171   m_matVt                 = NULL;
172 }
173 
174 template<class V, class M>
175 void
updateLowerCholLawCovMatrix(const M & matU,const V & vecSsqrt,const M & matVt)176 InvLogitGaussianVectorRealizer<V, M>::updateLowerCholLawCovMatrix(
177   const M & matU,
178   const V & vecSsqrt,
179   const M & matVt)
180 {
181   // delete old expected values (allocated at construction or last call to this function)
182   delete m_lowerCholLawCovMatrix;
183   delete m_matU;
184   delete m_vecSsqrt;
185   delete m_matVt;
186 
187   m_lowerCholLawCovMatrix = NULL;
188   m_matU                  = new M(matU);
189   m_vecSsqrt              = new V(vecSsqrt);
190   m_matVt                 = new M(matVt);
191 }
192 
193 }  // End namespace QUESO
194 
195 template class QUESO::InvLogitGaussianVectorRealizer<QUESO::GslVector, QUESO::GslMatrix>;
196