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>
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>
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 if ((m_env.subDisplayFile()) && (m_env.displayVerbosity() >= 5)) {
149 *m_env.subDisplayFile() <<
"Entering HessianCovMatricesTKGroup<V,M>::setPreComputingPosition()"
150 <<
": position = " << position
151 <<
", stageId = " << stageId
155 bool validPreComputingPosition =
true;
162 queso_require_equal_to_msg(m_preComputingPositions.size(), m_originalNewtonSteps.size(),
"m_preComputingPositions.size() != m_originalNewtonSteps.size()");
164 queso_require_equal_to_msg(m_preComputingPositions.size(), m_originalCovMatrices.size(),
"m_preComputingPositions.size() != m_originalCovMatrices.size()");
167 queso_require_msg(!(m_preComputingPositions[stageId]),
"m_preComputingPositions[stageId] != NULL");
171 queso_require_msg(!(m_originalNewtonSteps[stageId]),
"m_originalNewtonSteps[stageId] != NULL");
173 queso_require_msg(!(m_originalCovMatrices[stageId]),
"m_originalCovMatrices[stageId] != NULL");
177 if ((m_env.subDisplayFile()) && (m_env.displayVerbosity() >= 5)) {
178 *m_env.subDisplayFile() <<
"In HessianCovMatricesTKGroup<V,M>::setPreComputingPosition()"
179 <<
", position = " << position
180 <<
", stageId = " << stageId
181 <<
": m_originalNewtonSteps.size() = " << m_originalNewtonSteps.size()
182 <<
", m_originalCovMatrices.size() = " << m_originalCovMatrices.size()
183 <<
", m_preComputingPositions.size() = " << m_preComputingPositions.size()
184 <<
", m_rvs.size() = " << m_rvs.size()
188 if (m_targetPdfSynchronizer.domainSet().contains(position)) {
189 M* tmpHessian = m_vectorSpace->newMatrix();
190 M* tmpCovMat = m_vectorSpace->newMatrix();
191 V* tmpGrad = m_vectorSpace->newVector();
193 double logPrior = 0.;
194 double logLikelihood = 0.;
195 double logTarget = 0.;
196 logTarget = m_targetPdfSynchronizer.callFunction(&position,
206 V unitVector(m_vectorSpace->zeroVector());
207 V multVector(m_vectorSpace->zeroVector());
208 for (
unsigned int j = 0; j < tmpHessian->numCols(); ++j) {
209 if (j > 0) unitVector[j-1] = 0.;
211 tmpHessian->invertMultiply(unitVector, multVector);
212 for (
unsigned int i = 0; i < tmpHessian->numRowsLocal(); ++i) {
213 (*tmpCovMat)(i,j) = multVector[i];
216 if ((m_env.subDisplayFile()) && (m_env.displayVerbosity() >= 5)) {
217 *m_env.subDisplayFile() <<
"In HessianCovMatricesTKGroup<V,M>::setPreComputingPosition()"
218 <<
", position = " << position
219 <<
", stageId = " << stageId
220 <<
":\n H = " << *tmpHessian
221 <<
"\n H^{-1} = " << *tmpCovMat
222 <<
"\n H*H^{-1} = " << (*tmpHessian)*(*tmpCovMat)
223 <<
"\n H^{-1}*H = " << (*tmpCovMat)*(*tmpHessian)
228 *tmpCovMat = .5*((*tmpCovMat) + tmpCovMat->transpose());
231 M lowerChol(*tmpCovMat);
232 if ((m_env.subDisplayFile() ) &&
233 (m_env.displayVerbosity() >= 10)) {
234 *m_env.subDisplayFile() <<
"In HessianCovMatricesTKGroup<V,M>::setPreComputingPosition()"
235 <<
", position = " << position
236 <<
", stageId = " << stageId
237 <<
": calling lowerChol.chol()"
238 <<
", lowerChol = " << lowerChol
241 int iRC = lowerChol.chol();
243 std::cerr <<
"In HessianCovMatricesTKGroup<V,M>::setPreComputingPosition(): chol failed\n";
245 if ((m_env.subDisplayFile() ) &&
246 (m_env.displayVerbosity() >= 10)) {
247 *m_env.subDisplayFile() <<
"In HessianCovMatricesTKGroup<V,M>::setPreComputingPosition()"
248 <<
", position = " << position
249 <<
", stageId = " << stageId
250 <<
": got lowerChol.chol() with iRC = " << iRC
254 bool covIsPositiveDefinite = !iRC;
256 if (covIsPositiveDefinite) {
264 m_originalNewtonSteps[stageId] =
new V(-1.*(*tmpCovMat)*(*tmpGrad));
265 m_originalCovMatrices[stageId] =
new M(*tmpCovMat);
267 if ((m_env.subDisplayFile()) && (m_env.displayVerbosity() >= 5)) {
268 *m_env.subDisplayFile() <<
"In HessianCovMatricesTKGroup<V,M>::setPreComputingPosition()"
269 <<
", position = " << position
270 <<
", stageId = " << stageId
271 <<
", about to instantiate a Gaussian RV"
272 <<
": tmpHessian = " << *tmpHessian
273 <<
", preComputingPos = " << *m_preComputingPositions[stageId]
274 <<
", tmpCovMat = " << *tmpCovMat
275 <<
", tmpGrad = " << *tmpGrad
276 <<
", preComputedPos = " << *m_preComputingPositions[stageId] + *m_originalNewtonSteps[stageId]
283 *m_preComputingPositions[stageId] + *m_originalNewtonSteps[stageId],
284 *m_originalCovMatrices[stageId]);
287 validPreComputingPosition =
false;
295 validPreComputingPosition =
false;
298 if (validPreComputingPosition ==
false) {
300 V tmpGrad (m_vectorSpace->zeroVector());
301 M tmpCovMat(tmpGrad,1.);
302 m_originalNewtonSteps[stageId] =
new V(-1.*tmpCovMat*tmpGrad);
303 m_originalCovMatrices[stageId] =
new M(tmpCovMat);
306 *m_preComputingPositions[stageId],
310 if ((m_env.subDisplayFile()) && (m_env.displayVerbosity() >= 5)) {
311 *m_env.subDisplayFile() <<
"Leaving HessianCovMatricesTKGroup<V,M>::setPreComputingPosition()"
312 <<
": position = " << position
313 <<
", stageId = " << stageId
317 return validPreComputingPosition;
320 template<
class V,
class M>
324 queso_require_equal_to_msg(m_preComputingPositions.size(), m_originalNewtonSteps.size(),
"m_preComputingPositions.size() != m_originalNewtonSteps.size()");
326 queso_require_equal_to_msg(m_preComputingPositions.size(), m_originalCovMatrices.size(),
"m_preComputingPositions.size() != m_originalCovMatrices.size()");
331 for (
unsigned int i = 0; i < m_rvs.size(); ++i) {
338 for (
unsigned int i = 0; i < m_originalNewtonSteps.size(); ++i) {
339 if (m_originalNewtonSteps[i]) {
340 delete m_originalNewtonSteps[i];
341 m_originalNewtonSteps[i] = NULL;
345 for (
unsigned int i = 0; i < m_originalCovMatrices.size(); ++i) {
346 if (m_originalCovMatrices[i]) {
347 delete m_originalCovMatrices[i];
348 m_originalCovMatrices[i] = NULL;
355 template<
class V,
class M>
unsigned int displayVerbosity() const
virtual void print(std::ostream &os) const
TODO: Prints the transition kernel.
This base class allows the representation of a transition kernel.
This class allows the representation of a transition kernel with Hessians.
void clearPreComputingPositions()
Clears the pre-computing positions m_preComputingPositions[stageId].
virtual bool setPreComputingPosition(const V &position, unsigned int stageId)
Sets the pre-computing positions m_preComputingPositions[stageId] with a new vector of size position...
const BaseEnvironment & m_env
#define queso_require_equal_to_msg(expr1, expr2, msg)
virtual void clearPreComputingPositions()
Clears the pre-computing positions m_preComputingPositions[stageId].
#define queso_require_msg(asserted, msg)
A class representing a Gaussian vector RV.
void updateLawCovMatrix(const M &newLawCovMatrix)
Updates the covariance matrix.
std::ofstream * subDisplayFile() const
Access function for m_subDisplayFile (displays file on stream).
std::vector< M * > m_originalCovMatrices
~HessianCovMatricesTKGroup()
Destructor.
void print(std::ostream &os) const
TODO: Prints the transition kernel.
bool symmetric() const
Whether or not the matrix is symmetric. Always 'false'.
std::vector< const V * > m_preComputingPositions
HessianCovMatricesTKGroup(const char *prefix, const VectorSpace< V, M > &vectorSpace, const std::vector< double > &scales, const ScalarFunctionSynchronizer< V, M > &targetPdfSynchronizer)
Default constructor.
std::vector< BaseVectorRV< V, M > * > m_rvs
void updateLawExpVector(const V &newLawExpVector)
Updates the vector that contains the mean values.
std::vector< double > m_scales
std::vector< V * > m_originalNewtonSteps
#define queso_require_greater_msg(expr1, expr2, msg)
A templated class for synchronizing the calls of scalar functions (BaseScalarFunction and derived cla...
A class representing a vector space.
const GaussianVectorRV< V, M > & rv(unsigned int stageId) const
Gaussian increment property to construct a transition kernel.
bool setPreComputingPosition(const V &position, unsigned int stageId)
Sets the pre-computing positions m_preComputingPositions[stageId] with a new vector of size position...