queso-0.57.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Pages
MetropolisAdjustedLangevinTK.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-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 <queso/MetropolisAdjustedLangevinTK.h>
26 #include <queso/GslVector.h>
27 #include <queso/GslMatrix.h>
28 #include <queso/GaussianJointPdf.h>
29 #include <queso/BayesianJointPdf.h>
30 
31 namespace QUESO {
32 
33 template <class V, class M>
35  const char * prefix,
36  const BayesianJointPdf<V, M> & targetPdf,
37  const std::vector<double> & scales,
38  const M & covMatrix)
39  :
40  BaseTKGroup<V, M>(prefix, targetPdf.domainSet().vectorSpace(), scales),
41  m_originalCovMatrix(covMatrix),
42  m_targetPdf(targetPdf),
43  m_time_step(1.0)
44 {
45  if ((m_env.subDisplayFile()) && (m_env.displayVerbosity() >= 5)) {
46  *m_env.subDisplayFile() << "Entering MetropolisAdjustedLangevinTK<V, M>::constructor()"
47  << std::endl;
48  }
49 
50  if ((m_env.subDisplayFile()) && (m_env.displayVerbosity() >= 5)) {
51  *m_env.subDisplayFile() << "In MetropolisAdjustedLangevinTK<V, M>::constructor()"
52  << ": m_scales.size() = " << m_scales.size()
53  << ", m_preComputingPositions.size() = " << m_preComputingPositions.size()
54  << ", m_rvs.size() = " << m_rvs.size()
55  << ", m_originalCovMatrix = " << m_originalCovMatrix
56  << std::endl;
57  }
58 
60 
61  if ((m_env.subDisplayFile()) && (m_env.displayVerbosity() >= 5)) {
62  *m_env.subDisplayFile() << "Leaving MetropolisAdjustedLangevinTK<V, M>::constructor()"
63  << std::endl;
64  }
65 }
66 
67 template <class V, class M>
69 {
70 }
71 
72 template <class V, class M>
73 bool
75 {
76  return false;
77 }
78 
79 template <class V, class M>
81 MetropolisAdjustedLangevinTK<V, M>::rv(unsigned int stageId) const
82 {
83  queso_require_not_equal_to(m_rvs.size(), 0);
84  queso_require(m_rvs[0]);
85  queso_require_greater(m_preComputingPositions.size(), stageId);
86  queso_require(m_preComputingPositions[stageId]);
87 
88  if ((m_env.subDisplayFile() ) &&
89  (m_env.displayVerbosity() >= 10)) {
90  *m_env.subDisplayFile() << "In MetropolisAdjustedLangevinTK<V, M>::rv1()"
91  << ", stageId = " << stageId
92  << ": about to call m_rvs[0]->updateLawExpVector()"
93  << ", vector = " << *m_preComputingPositions[stageId] // FIX ME: might demand parallelism
94  << std::endl;
95  }
96 
97  GaussianVectorRV<V, M> * gaussian_rv = dynamic_cast<GaussianVectorRV<V, M> * >(m_rvs[0]);
98 
99  gaussian_rv->updateLawExpVector(*m_preComputingPositions[stageId]);
100 
101  return (*gaussian_rv);
102 }
103 
104 template <class V, class M>
106 MetropolisAdjustedLangevinTK<V, M>::rv(const std::vector<unsigned int> & stageIds)
107 {
108  queso_require_greater_equal(m_rvs.size(), stageIds.size());
109  queso_require(m_rvs[stageIds.size()-1]);
110  queso_require_greater(m_preComputingPositions.size(), stageIds[0]);
111  queso_require(m_preComputingPositions[stageIds[0]]);
112 
113  if ((m_env.subDisplayFile() ) &&
114  (m_env.displayVerbosity() >= 10)) {
115  *m_env.subDisplayFile() << "In MetropolisAdjustedLangevinTK<V, M>::rv2()"
116  << ", stageIds.size() = " << stageIds.size()
117  << ", stageIds[0] = " << stageIds[0]
118  << ": about to call m_rvs[stageIds.size()-1]->updateLawExpVector()"
119  << ", vector = " << *m_preComputingPositions[stageIds[0]] // FIX ME: might demand parallelism
120  << std::endl;
121  }
122 
123  GaussianVectorRV<V, M> * gaussian_rv = dynamic_cast<GaussianVectorRV<V, M> * >(m_rvs[stageIds.size()-1]);
124 
125  gaussian_rv->updateLawExpVector(*m_preComputingPositions[stageIds[0]]);
126 
127  return (*gaussian_rv);
128 }
129 
130 template <class V, class M>
132 MetropolisAdjustedLangevinTK<V, M>::rv(const V & position) const
133 {
134  queso_require_not_equal_to(m_rvs.size(), 0);
135  queso_require(m_rvs[0]);
136 
137  GaussianVectorRV<V, M> * gaussian_rv = dynamic_cast<GaussianVectorRV<V, M> * >(m_rvs[this->m_stageId]);
138 
139  // 'position' is a position in the chain? Hopefully? Anyway, assume it's a
140  // position in the chain and then that means we need to modify it slightly so
141  // to get the transition distribution for the next state in the chain.
142 
143  V grad(this->m_targetPdf.domainSet().vectorSpace().zeroVector());
144 
145  // Get the gradient of the log-posterior. This is so inefficient it's
146  // painful. We should be caching the gradient evaluations.
147  this->m_targetPdf.lnValue(position, grad);
148 
149  // Euler time-step
150  grad *= 0.5 * this->m_time_step;
151 
152  // Add on current position
153  grad += position;
154 
155  // Update the mean of the transition kernel. The vector gets copied, so
156  // we're ok.
157  gaussian_rv->updateLawExpVector(grad);
158 
159  return (*gaussian_rv);
160 }
161 
162 template <class V, class M>
163 void
165 {
166  for (unsigned int i = 0; i < m_scales.size(); ++i) {
167  double factor = 1./m_scales[i]/m_scales[i];
168  if ((m_env.subDisplayFile() ) &&
169  (m_env.displayVerbosity() >= 10)) {
170  *m_env.subDisplayFile() << "In MetropolisAdjustedLangevinTK<V, M>::updateLawCovMatrix()"
171  << ", m_scales.size() = " << m_scales.size()
172  << ", i = " << i
173  << ", m_scales[i] = " << m_scales[i]
174  << ", factor = " << factor
175  << ": about to call m_rvs[i]->updateLawCovMatrix()"
176  << ", covMatrix = \n" << factor*covMatrix // FIX ME: might demand parallelism
177  << std::endl;
178  }
179  dynamic_cast<GaussianVectorRV<V, M> * >(m_rvs[i])->updateLawCovMatrix(factor*m_time_step*covMatrix);
180  }
181 }
182 
183 template <class V, class M>
184 bool
185 MetropolisAdjustedLangevinTK<V, M>::setPreComputingPosition(const V & position, unsigned int stageId)
186 {
187  if ((m_env.subDisplayFile()) && (m_env.displayVerbosity() >= 5)) {
188  *m_env.subDisplayFile() << "Entering MetropolisAdjustedLangevinTK<V, M>::setPreComputingPosition()"
189  << ": position = " << position
190  << ", stageId = " << stageId
191  << std::endl;
192  }
193 
195 
196  if ((m_env.subDisplayFile()) && (m_env.displayVerbosity() >= 5)) {
197  *m_env.subDisplayFile() << "In MetropolisAdjustedLangevinTK<V, M>::setPreComputingPosition()"
198  << ", position = " << position
199  << ", stageId = " << stageId
200  << ": preComputingPos = " << *m_preComputingPositions[stageId];
201  if (stageId < m_scales.size()) {
202  *m_env.subDisplayFile() << ", factor = " << 1./m_scales[stageId]/m_scales[stageId];
203  }
204  if (stageId < m_rvs.size()) {
205  const GaussianJointPdf<V, M>* pdfPtr = dynamic_cast< const GaussianJointPdf<V, M>* >(&(m_rvs[stageId]->pdf()));
206  *m_env.subDisplayFile() << ", rvCov = " << pdfPtr->lawCovMatrix(); // FIX ME: might demand parallelism
207  }
208  *m_env.subDisplayFile() << std::endl;
209  }
210 
211  if ((m_env.subDisplayFile()) && (m_env.displayVerbosity() >= 5)) {
212  *m_env.subDisplayFile() << "Leaving MetropolisAdjustedLangevinTK<V, M>::setPreComputingPosition()"
213  << ": position = " << position
214  << ", stageId = " << stageId
215  << std::endl;
216  }
217 
218  return true;
219 }
220 
221 template <class V, class M>
222 void
224 {
226 }
227 
228 template <class V, class M>
229 void
231 {
232  queso_require_not_equal_to(m_rvs.size(), 0);
233  queso_require_equal_to(m_rvs.size(), m_scales.size());
234 
235  for (unsigned int i = 0; i < m_scales.size(); ++i) {
236  double factor = 1./m_scales[i]/m_scales[i];
237  queso_require(!(m_rvs[i]));
238  m_rvs[i] = new GaussianVectorRV<V, M>(m_prefix.c_str(),
239  *m_vectorSpace,
240  m_vectorSpace->zeroVector(),
241  factor*m_time_step*m_originalCovMatrix);
242  }
243 }
244 
245 template <class V, class M>
246 void
248 {
250 }
251 
253 
254 } // End namespace QUESO
A class for handling Gaussian joint PDFs.
std::vector< const V * > m_preComputingPositions
Definition: TKGroup.h:132
A class representing a Gaussian vector RV.
const GaussianVectorRV< V, M > & rv(unsigned int stageId) const
Gaussian increment property to construct a transition kernel.
std::vector< BaseVectorRV< V, M > * > m_rvs
Definition: TKGroup.h:133
const BaseEnvironment & m_env
Definition: TKGroup.h:128
virtual void print(std::ostream &os) const
TODO: Prints the transition kernel.
Definition: TKGroup.C:133
virtual void clearPreComputingPositions()
Clears the pre-computing positions m_preComputingPositions[stageId].
Definition: TKGroup.C:111
void setRVsWithZeroMean()
Sets the mean of the RVs to zero.
void updateLawCovMatrix(const M &covMatrix)
Scales the covariance matrix.
void updateLawExpVector(const V &newLawExpVector)
Updates the vector that contains the mean values.
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:131
const M & lawCovMatrix() const
Returns the covariance matrix; access to protected attribute m_lawCovMatrix.
void clearPreComputingPositions()
Clears the pre-computing positions m_preComputingPositions[stageId].
This base class allows the representation of a transition kernel.
Definition: Algorithm.h:32
MetropolisAdjustedLangevinTK(const char *prefix, const BayesianJointPdf< V, M > &targetPdf, const std::vector< double > &scales, const M &covMatrix)
Default constructor.
bool symmetric() const
Whether or not the matrix is symmetric. Always &#39;true&#39;.
void print(std::ostream &os) const
TODO: Prints the transition kernel.
This class allows the representation of the MALA transition kernel with a scaled covariance matrix fo...
unsigned int displayVerbosity() const
Definition: Environment.C:450
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:98
A class for handling Bayesian joint PDFs.
std::ofstream * subDisplayFile() const
Access function for m_subDisplayFile (displays file on stream).
Definition: Environment.C:320

Generated on Tue Jun 5 2018 19:48:55 for queso-0.57.1 by  doxygen 1.8.5