25 #include <queso/HessianCovMatricesTKGroup.h>
26 #include <queso/GslVector.h>
27 #include <queso/GslMatrix.h>
32 template<
class V,
class M>
36 const std::vector<double>& scales,
40 m_targetPdfSynchronizer(targetPdfSynchronizer),
41 m_originalNewtonSteps (scales.
size()+1,NULL),
42 m_originalCovMatrices (scales.
size()+1,NULL)
49 m_rvs.resize(scales.size()+1,NULL);
53 <<
": m_scales.size() = " <<
m_scales.size()
55 <<
", m_rvs.size() = " <<
m_rvs.size()
67 template<
class V,
class M>
72 template<
class V,
class M>
79 template<
class V,
class M>
83 queso_require_greater_msg(m_rvs.size(), stageId,
"m_rvs.size() <= stageId");
85 queso_require_msg(m_rvs[stageId],
"m_rvs[stageId] == NULL");
87 queso_require_greater_msg(m_preComputingPositions.size(), stageId,
"m_preComputingPositions.size() <= stageId");
89 queso_require_msg(m_preComputingPositions[stageId],
"m_preComputingPositions[stageId] == NULL");
94 gaussian_rv->
updateLawExpVector(*m_preComputingPositions[stageId] + *m_originalNewtonSteps[stageId]);
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]
110 template<
class V,
class M>
114 queso_require_greater_msg(m_rvs.size(), stageIds[0],
"m_rvs.size() <= stageIds[0]");
116 queso_require_msg(m_rvs[stageIds[0]],
"m_rvs[stageIds[0]] == NULL");
118 queso_require_greater_msg(m_preComputingPositions.size(), stageIds[0],
"m_preComputingPositions.size() <= stageIds[0]");
120 queso_require_msg(m_preComputingPositions[stageIds[0]],
"m_preComputingPositions[stageIds[0]] == NULL");
122 double factor = 1./m_scales[stageIds.size()-1]/m_scales[stageIds.size()-1];
127 gaussian_rv->
updateLawExpVector(*m_preComputingPositions[stageIds[0]] + factor*(*m_originalNewtonSteps[stageIds[0]]));
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]])
144 template <
class V,
class M>
148 queso_require_greater_msg(m_rvs.size(), this->m_stageId,
"m_rvs.size() <= stageId");
149 queso_require_msg(m_rvs[this->m_stageId],
"m_rvs[stageId] == NULL");
164 template<
class V,
class M>
168 if ((m_env.subDisplayFile()) && (m_env.displayVerbosity() >= 5)) {
169 *m_env.subDisplayFile() <<
"Entering HessianCovMatricesTKGroup<V,M>::setPreComputingPosition()"
170 <<
": position = " << position
171 <<
", stageId = " << stageId
175 bool validPreComputingPosition =
true;
178 queso_require_greater_msg(m_preComputingPositions.size(), stageId,
"m_preComputingPositions.size() <= stageId");
182 queso_require_equal_to_msg(m_preComputingPositions.size(), m_originalNewtonSteps.size(),
"m_preComputingPositions.size() != m_originalNewtonSteps.size()");
184 queso_require_equal_to_msg(m_preComputingPositions.size(), m_originalCovMatrices.size(),
"m_preComputingPositions.size() != m_originalCovMatrices.size()");
187 queso_require_msg(!(m_preComputingPositions[stageId]),
"m_preComputingPositions[stageId] != NULL");
189 queso_require_msg(!(m_rvs[stageId]),
"m_rvs[stageId] != NULL");
191 queso_require_msg(!(m_originalNewtonSteps[stageId]),
"m_originalNewtonSteps[stageId] != NULL");
193 queso_require_msg(!(m_originalCovMatrices[stageId]),
"m_originalCovMatrices[stageId] != NULL");
197 if ((m_env.subDisplayFile()) && (m_env.displayVerbosity() >= 5)) {
198 *m_env.subDisplayFile() <<
"In HessianCovMatricesTKGroup<V,M>::setPreComputingPosition()"
199 <<
", position = " << position
200 <<
", stageId = " << stageId
201 <<
": m_originalNewtonSteps.size() = " << m_originalNewtonSteps.size()
202 <<
", m_originalCovMatrices.size() = " << m_originalCovMatrices.size()
203 <<
", m_preComputingPositions.size() = " << m_preComputingPositions.size()
204 <<
", m_rvs.size() = " << m_rvs.size()
208 if (m_targetPdfSynchronizer.domainSet().contains(position)) {
209 M* tmpHessian = m_vectorSpace->newMatrix();
210 M* tmpCovMat = m_vectorSpace->newMatrix();
211 V* tmpGrad = m_vectorSpace->newVector();
213 double logPrior = 0.;
214 double logLikelihood = 0.;
215 double logTarget = 0.;
216 logTarget = m_targetPdfSynchronizer.callFunction(&position,
226 V unitVector(m_vectorSpace->zeroVector());
227 V multVector(m_vectorSpace->zeroVector());
228 for (
unsigned int j = 0; j < tmpHessian->numCols(); ++j) {
229 if (j > 0) unitVector[j-1] = 0.;
231 tmpHessian->invertMultiply(unitVector, multVector);
232 for (
unsigned int i = 0; i < tmpHessian->numRowsLocal(); ++i) {
233 (*tmpCovMat)(i,j) = multVector[i];
236 if ((m_env.subDisplayFile()) && (m_env.displayVerbosity() >= 5)) {
237 *m_env.subDisplayFile() <<
"In HessianCovMatricesTKGroup<V,M>::setPreComputingPosition()"
238 <<
", position = " << position
239 <<
", stageId = " << stageId
240 <<
":\n H = " << *tmpHessian
241 <<
"\n H^{-1} = " << *tmpCovMat
242 <<
"\n H*H^{-1} = " << (*tmpHessian)*(*tmpCovMat)
243 <<
"\n H^{-1}*H = " << (*tmpCovMat)*(*tmpHessian)
248 *tmpCovMat = .5*((*tmpCovMat) + tmpCovMat->transpose());
251 M lowerChol(*tmpCovMat);
252 if ((m_env.subDisplayFile() ) &&
253 (m_env.displayVerbosity() >= 10)) {
254 *m_env.subDisplayFile() <<
"In HessianCovMatricesTKGroup<V,M>::setPreComputingPosition()"
255 <<
", position = " << position
256 <<
", stageId = " << stageId
257 <<
": calling lowerChol.chol()"
258 <<
", lowerChol = " << lowerChol
261 int iRC = lowerChol.chol();
263 std::cerr <<
"In HessianCovMatricesTKGroup<V,M>::setPreComputingPosition(): chol failed\n";
265 if ((m_env.subDisplayFile() ) &&
266 (m_env.displayVerbosity() >= 10)) {
267 *m_env.subDisplayFile() <<
"In HessianCovMatricesTKGroup<V,M>::setPreComputingPosition()"
268 <<
", position = " << position
269 <<
", stageId = " << stageId
270 <<
": got lowerChol.chol() with iRC = " << iRC
274 bool covIsPositiveDefinite = !iRC;
276 if (covIsPositiveDefinite) {
284 m_originalNewtonSteps[stageId] =
new V(-1.*(*tmpCovMat)*(*tmpGrad));
285 m_originalCovMatrices[stageId] =
new M(*tmpCovMat);
287 if ((m_env.subDisplayFile()) && (m_env.displayVerbosity() >= 5)) {
288 *m_env.subDisplayFile() <<
"In HessianCovMatricesTKGroup<V,M>::setPreComputingPosition()"
289 <<
", position = " << position
290 <<
", stageId = " << stageId
291 <<
", about to instantiate a Gaussian RV"
292 <<
": tmpHessian = " << *tmpHessian
293 <<
", preComputingPos = " << *m_preComputingPositions[stageId]
294 <<
", tmpCovMat = " << *tmpCovMat
295 <<
", tmpGrad = " << *tmpGrad
296 <<
", preComputedPos = " << *m_preComputingPositions[stageId] + *m_originalNewtonSteps[stageId]
303 *m_preComputingPositions[stageId] + *m_originalNewtonSteps[stageId],
304 *m_originalCovMatrices[stageId]);
307 validPreComputingPosition =
false;
315 validPreComputingPosition =
false;
318 if (validPreComputingPosition ==
false) {
320 V tmpGrad (m_vectorSpace->zeroVector());
321 M tmpCovMat(tmpGrad,1.);
322 m_originalNewtonSteps[stageId] =
new V(-1.*tmpCovMat*tmpGrad);
323 m_originalCovMatrices[stageId] =
new M(tmpCovMat);
326 *m_preComputingPositions[stageId],
330 if ((m_env.subDisplayFile()) && (m_env.displayVerbosity() >= 5)) {
331 *m_env.subDisplayFile() <<
"Leaving HessianCovMatricesTKGroup<V,M>::setPreComputingPosition()"
332 <<
": position = " << position
333 <<
", stageId = " << stageId
337 return validPreComputingPosition;
340 template<
class V,
class M>
344 queso_require_equal_to_msg(m_preComputingPositions.size(), m_originalNewtonSteps.size(),
"m_preComputingPositions.size() != m_originalNewtonSteps.size()");
346 queso_require_equal_to_msg(m_preComputingPositions.size(), m_originalCovMatrices.size(),
"m_preComputingPositions.size() != m_originalCovMatrices.size()");
351 for (
unsigned int i = 0; i < m_rvs.size(); ++i) {
358 for (
unsigned int i = 0; i < m_originalNewtonSteps.size(); ++i) {
359 if (m_originalNewtonSteps[i]) {
360 delete m_originalNewtonSteps[i];
361 m_originalNewtonSteps[i] = NULL;
365 for (
unsigned int i = 0; i < m_originalCovMatrices.size(); ++i) {
366 if (m_originalCovMatrices[i]) {
367 delete m_originalCovMatrices[i];
368 m_originalCovMatrices[i] = NULL;
375 template <
class V,
class M>
379 unsigned int old_stageId = this->m_stageId;
380 this->m_stageId = stageId;
386 template<
class V,
class M>
A templated class for synchronizing the calls of scalar functions (BaseScalarFunction and derived cla...
std::vector< const V * > m_preComputingPositions
A class representing a vector space.
A class representing a Gaussian vector RV.
std::vector< BaseVectorRV< V, M > * > m_rvs
const BaseEnvironment & m_env
virtual void print(std::ostream &os) const
TODO: Prints the transition kernel.
This class allows the representation of a transition kernel with Hessians.
virtual void clearPreComputingPositions()
Clears the pre-computing positions m_preComputingPositions[stageId].
HessianCovMatricesTKGroup(const char *prefix, const VectorSpace< V, M > &vectorSpace, const std::vector< double > &scales, const ScalarFunctionSynchronizer< V, M > &targetPdfSynchronizer)
Default constructor.
void clearPreComputingPositions()
Clears the pre-computing positions m_preComputingPositions[stageId].
void updateLawExpVector(const V &newLawExpVector)
Updates the vector that contains the mean values.
std::vector< M * > m_originalCovMatrices
std::vector< V * > m_originalNewtonSteps
bool symmetric() const
Whether or not the matrix is symmetric. Always 'false'.
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
This base class allows the representation of a transition kernel.
MonteCarloSGOptions::MonteCarloSGOptions(const BaseEnvironment &env, const char *prefix, const McOptionsValues &alternativeOptionsValues queso_require_equal_to_msg)(m_env.optionsInputFileName(), std::string(""), std::string("this constructor is incompatible with the existence of an options input file"))
void print(std::ostream &os) const
TODO: Prints the transition kernel.
void updateLawCovMatrix(const M &newLawCovMatrix)
Updates the covariance matrix.
const GaussianVectorRV< V, M > & rv(unsigned int stageId) const
Gaussian increment property to construct a transition kernel.
unsigned int displayVerbosity() const
virtual bool setPreComputingPosition(const V &position, unsigned int stageId)
Sets the pre-computing positions m_preComputingPositions[stageId] with a new vector of size position...
virtual unsigned int set_dr_stage(unsigned int stageId)
Does nothing. Subclasses may re-implement. Returns the current stage id.
~HessianCovMatricesTKGroup()
Destructor.
std::ofstream * subDisplayFile() const
Access function for m_subDisplayFile (displays file on stream).