Skip to content

Commit edf1bdb

Browse files
Merge pull request #33 from plusk01/mixfactor_normalization
DCMixtureFactor normalization
2 parents 8acd301 + 9ece6dc commit edf1bdb

2 files changed

Lines changed: 14 additions & 48 deletions

File tree

include/dcsam/DCMixtureFactor.h

Lines changed: 0 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -112,54 +112,6 @@ class DCMixtureFactor : public DCFactor {
112112
// error.
113113
return factors_[assignment].linearize(continuousVals);
114114
}
115-
116-
/**
117-
* If the component factors are not already normalized, we want to compute
118-
* their normalizing constants so that the resulting joint distribution is
119-
* appropriately computed. Remember, this is the _negative_ normalizing
120-
* constant for the measurement likelihood (since we are minimizing the
121-
* _negative_ log-likelihood).
122-
*/
123-
double nonlinearFactorLogNormalizingConstant(
124-
const NonlinearFactorType& factor, const gtsam::Values& values) const {
125-
// Information matrix (inverse covariance matrix) for the factor.
126-
gtsam::Matrix infoMat;
127-
128-
// NOTE: This is sloppy, is there a cleaner way?
129-
boost::shared_ptr<NonlinearFactorType> fPtr =
130-
boost::make_shared<NonlinearFactorType>(factor);
131-
boost::shared_ptr<gtsam::NonlinearFactor> factorPtr(fPtr);
132-
133-
// If this is a NoiseModelFactor, we'll use its noiseModel to
134-
// otherwise noiseModelFactor will be nullptr
135-
boost::shared_ptr<gtsam::NoiseModelFactor> noiseModelFactor =
136-
boost::dynamic_pointer_cast<gtsam::NoiseModelFactor>(factorPtr);
137-
if (noiseModelFactor) {
138-
// If dynamic cast to NoiseModelFactor succeeded, see if the noise model
139-
// is Gaussian
140-
gtsam::noiseModel::Base::shared_ptr noiseModel =
141-
noiseModelFactor->noiseModel();
142-
143-
boost::shared_ptr<gtsam::noiseModel::Gaussian> gaussianNoiseModel =
144-
boost::dynamic_pointer_cast<gtsam::noiseModel::Gaussian>(noiseModel);
145-
if (gaussianNoiseModel) {
146-
// If the noise model is Gaussian, retrieve the information matrix
147-
infoMat = gaussianNoiseModel->information();
148-
} else {
149-
// If the factor is not a Gaussian factor, we'll linearize it to get
150-
// something with a normalized noise model
151-
// TODO(kevin): does this make sense to do? I think maybe not in
152-
// general? Should we just yell at the user?
153-
boost::shared_ptr<gtsam::GaussianFactor> gaussianFactor =
154-
factor.linearize(values);
155-
infoMat = gaussianFactor->information();
156-
}
157-
}
158-
159-
// Compute the (negative) log of the normalizing constant
160-
return -(factor.dim() * log(2.0 * M_PI) / 2.0) -
161-
(log(infoMat.determinant()) / 2.0);
162-
}
163115
};
164116

165117
} // namespace dcsam

tests/testDCSAM.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,20 @@ TEST(TestSuite, dccontinuous_mixture) {
309309
std::cout << "DK 1st: " << dkTest.first << std::endl;
310310
std::cout << "DK 2nd: " << dkTest.second << std::endl;
311311

312+
// Test calculation of negative log probability
313+
dcsam::DiscreteValues dv1, dvNH;
314+
dv1[dk.first] = 0;
315+
dvNH[dk.first] = 1;
316+
gtsam::Values xvals;
317+
xvals.insert(x1, 0.0);
318+
const double negLogProb_1 = dcMixture.error(xvals, dv1);
319+
const double negLogProb_NH = dcMixture.error(xvals, dvNH);
320+
xvals.clear();
321+
322+
// As calculated in MATLAB using -log(normpdf(0,0,1)), -log(normpdf(0,0,8))
323+
EXPECT_NEAR(negLogProb_1, 0.9189, 1e-3);
324+
EXPECT_NEAR(negLogProb_NH, 2.9984, 1e-3);
325+
312326
// Plot the cost functions for each hypothesis
313327
#ifdef ENABLE_PLOTTING
314328
// Query cost function

0 commit comments

Comments
 (0)