73 const double prior_inlier,
const double prior_outlier,
74 const bool flag_bump_up_near_zero_probs =
false) :
75 Base(cref_list_of<2>(key1)(key2)), key1_(key1), key2_(key2), measured_(
76 measured), model_inlier_(model_inlier), model_outlier_(model_outlier), prior_inlier_(
77 prior_inlier), prior_outlier_(prior_outlier), flag_bump_up_near_zero_probs_(
78 flag_bump_up_near_zero_probs) {
89 std::cout << s <<
"BetweenFactorEM(" << keyFormatter(key1_) <<
"," 90 << keyFormatter(key2_) <<
")\n";
91 measured_.print(
" measured: ");
92 model_inlier_->print(
" noise model inlier: ");
93 model_outlier_->print(
" noise model outlier: ");
94 std::cout <<
"(prior_inlier, prior_outlier_) = (" << prior_inlier_ <<
"," 95 << prior_outlier_ <<
")\n";
101 const This *
t =
dynamic_cast<const This*
>(&
f);
104 return key1_ == t->key1_ && key2_ == t->key2_
108 prior_outlier_ == t->prior_outlier_
109 && prior_inlier_ == t->prior_inlier_ && measured_.
equals(t->measured_);
131 return boost::shared_ptr<JacobianFactor>();
135 std::vector<Matrix>
A(this->
size());
147 boost::optional<std::vector<Matrix>&>
H = boost::none)
const {
156 T hx = p1.between(p2, H1, H2);
159 Vector err = measured_.localCoordinates(hx);
163 double p_inlier = p_inlier_outlier[0];
164 double p_outlier = p_inlier_outlier[1];
166 Vector err_wh_inlier = model_inlier_->whiten(err);
167 Vector err_wh_outlier = model_outlier_->whiten(err);
169 Matrix invCov_inlier = model_inlier_->R().transpose() * model_inlier_->R();
170 Matrix invCov_outlier = model_outlier_->R().transpose()
171 * model_outlier_->R();
174 err_wh_eq.resize(err_wh_inlier.rows() * 2);
175 err_wh_eq <<
sqrt(p_inlier) * err_wh_inlier.array(),
sqrt(p_outlier)
176 * err_wh_outlier.array();
181 Matrix H1_inlier =
sqrt(p_inlier) * model_inlier_->Whiten(H1);
182 Matrix H1_outlier =
sqrt(p_outlier) * model_outlier_->Whiten(H1);
185 Matrix H2_inlier =
sqrt(p_inlier) * model_inlier_->Whiten(H2);
186 Matrix H2_outlier =
sqrt(p_outlier) * model_outlier_->Whiten(H2);
189 (*H)[0].resize(H1_aug.rows(), H1_aug.cols());
190 (*H)[1].resize(H2_aug.rows(), H2_aug.cols());
238 Vector err_wh_inlier = model_inlier_->whiten(err);
239 Vector err_wh_outlier = model_outlier_->whiten(err);
241 Matrix invCov_inlier = model_inlier_->R().transpose() * model_inlier_->R();
242 Matrix invCov_outlier = model_outlier_->R().transpose()
243 * model_outlier_->R();
245 double p_inlier = prior_inlier_ *
std::sqrt(invCov_inlier.determinant())
246 *
exp(-0.5 * err_wh_inlier.dot(err_wh_inlier));
247 double p_outlier = prior_outlier_ *
std::sqrt(invCov_outlier.determinant())
248 *
exp(-0.5 * err_wh_outlier.dot(err_wh_outlier));
251 std::cout <<
"in calcIndicatorProb. err_unwh: " << err[0] <<
", " 252 << err[1] <<
", " << err[2] << std::endl;
253 std::cout <<
"in calcIndicatorProb. err_wh_inlier: " << err_wh_inlier[0]
254 <<
", " << err_wh_inlier[1] <<
", " << err_wh_inlier[2] << std::endl;
255 std::cout <<
"in calcIndicatorProb. err_wh_inlier.dot(err_wh_inlier): " 256 << err_wh_inlier.dot(err_wh_inlier) << std::endl;
257 std::cout <<
"in calcIndicatorProb. err_wh_outlier.dot(err_wh_outlier): " 258 << err_wh_outlier.dot(err_wh_outlier) << std::endl;
261 <<
"in calcIndicatorProb. p_inlier, p_outlier before normalization: " 262 << p_inlier <<
", " << p_outlier << std::endl;
265 double sumP = p_inlier + p_outlier;
269 if (flag_bump_up_near_zero_probs_) {
272 if (p_inlier < minP || p_outlier < minP) {
275 if (p_outlier < minP)
277 sumP = p_inlier + p_outlier;
283 return (
Vector(2) << p_inlier, p_outlier).finished();
294 T hx = p1.between(p2, H1, H2);
296 return measured_.localCoordinates(hx);
301 flag_bump_up_near_zero_probs_ = flag;
321 return (model_inlier_->R().transpose() * model_inlier_->R()).
inverse();
326 return (model_outlier_->R().transpose() * model_outlier_->R()).
inverse();
343 Keys.push_back(key1_);
344 Keys.push_back(key2_);
347 Matrix cov1 = joint_marginal12(key1_, key1_);
348 Matrix cov2 = joint_marginal12(key2_, key2_);
349 Matrix cov12 = joint_marginal12(key1_, key2_);
370 p1.between(p2, H1, H2);
373 H.resize(H1.rows(), H1.rows() + H2.rows());
377 joint_cov.resize(cov1.rows() + cov2.rows(), cov1.cols() + cov2.cols());
378 joint_cov << cov1, cov12, cov12.transpose(), cov2;
380 Matrix cov_state = H * joint_cov * H.transpose();
386 (model_inlier_->R().transpose() * model_inlier_->R()).
inverse();
388 covRinlier + cov_state);
391 (model_outlier_->R().transpose() * model_outlier_->R()).
inverse();
393 covRoutlier + cov_state);
405 size_t dim()
const override {
406 return model_inlier_->R().rows() + model_inlier_->R().cols();
413 template<
class ARCHIVE>
416 & boost::serialization::make_nvp(
"NonlinearFactor",
417 boost::serialization::base_object<Base>(*
this));
418 ar & BOOST_SERIALIZATION_NVP(measured_);
const VALUE & measured() const
bool equals(const This &other, double tol=1e-9) const
check equality
static shared_ptr Covariance(const Matrix &covariance, bool smart=true)
Concept check for values that can be used in unit tests.
void serialize(ARCHIVE &ar, const unsigned int)
EIGEN_DEVICE_FUNC const ExpReturnType exp() const
Vector calcIndicatorProb(const Values &x) const
A factor with a quadratic error function - a Gaussian.
SharedGaussian model_outlier_
EIGEN_DEVICE_FUNC const SqrtReturnType sqrt() const
JointMarginal jointMarginalCovariance(const KeyVector &variables) const
set noclip points set clip one set noclip two set bar set border lt lw set xdata set ydata set zdata set x2data set y2data set boxwidth set dummy y set format x g set format y g set format x2 g set format y2 g set format z g set angles radians set nogrid set key title set key left top Right noreverse box linetype linewidth samplen spacing width set nolabel set noarrow set nologscale set logscale x set set pointsize set encoding default set nopolar set noparametric set set set set surface set nocontour set clabel set mapping cartesian set nohidden3d set cntrparam order set cntrparam linear set cntrparam levels auto set cntrparam points set size set set xzeroaxis lt lw set x2zeroaxis lt lw set yzeroaxis lt lw set y2zeroaxis lt lw set tics in set ticslevel set tics set mxtics default set mytics default set mx2tics default set my2tics default set xtics border mirror norotate autofreq set ytics border mirror norotate autofreq set ztics border nomirror norotate autofreq set nox2tics set noy2tics set timestamp bottom norotate set rrange[*:*] noreverse nowriteback set trange[*:*] noreverse nowriteback set urange[*:*] noreverse nowriteback set vrange[*:*] noreverse nowriteback set xlabel matrix size set x2label set timefmt d m y n H
#define GTSAM_CONCEPT_TESTABLE_TYPE(T)
NonlinearFactorGraph graph
void set_flag_bump_up_near_zero_probs(bool flag)
static const KeyFormatter DefaultKeyFormatter
bool get_flag_bump_up_near_zero_probs() const
virtual bool active(const Values &) const
SharedGaussian get_model_outlier() const
static shared_ptr Create(size_t dim)
friend class boost::serialization::access
Matrix get_model_inlier_cov() const
Matrix get_model_outlier_cov() const
const Symbol key1('v', 1)
void updateNoiseModels_givenCovs(const Values &values, const Matrix &cov1, const Matrix &cov2, const Matrix &cov12)
SharedGaussian get_model_inlier() const
Vector whitenedError(const Values &x, boost::optional< std::vector< Matrix > & > H=boost::none) const
FastVector< Key > KeyVector
Define collection type once and for all - also used in wrappers.
const ValueType at(Key j) const
SharedGaussian model_inlier_
BetweenFactorEM(Key key1, Key key2, const VALUE &measured, const SharedGaussian &model_inlier, const SharedGaussian &model_outlier, const double prior_inlier, const double prior_outlier, const bool flag_bump_up_near_zero_probs=false)
std::function< std::string(Key)> KeyFormatter
Typedef for a function to format a key, i.e. to convert it to a string.
Point2(* f)(const Point3 &, OptionalJacobian< 2, 3 >)
Array< double, 1, 3 > e(1./3., 0.5, 2.)
~BetweenFactorEM() override
virtual bool equals(const NonlinearFactor &f, double tol=1e-9) const
bool flag_bump_up_near_zero_probs_
boost::shared_ptr< This > shared_ptr
shared_ptr to this class
Base class and basic functions for Lie types.
void updateNoiseModels(const Values &values, const NonlinearFactorGraph &graph)
#define GTSAM_CONCEPT_LIE_TYPE(T)
boost::shared_ptr< Factor > shared_ptr
A shared_ptr to this class.
Non-linear factor base classes.
Vector unwhitenedError(const Values &x) const
const Symbol key2('v', 2)
size_t dim() const override
Matrix stack(size_t nrMatrices,...)
bool equals(const NonlinearFactor &f, double tol=1e-9) const override
BetweenFactorEM< VALUE > This
A class for computing marginals in a NonlinearFactorGraph.
set noclip points set clip one set noclip two set bar set border lt lw set xdata set ydata set zdata set x2data set y2data set boxwidth set dummy x
EIGEN_DEVICE_FUNC const InverseReturnType inverse() const
std::uint64_t Key
Integer nonlinear key type.
boost::shared_ptr< GaussianFactor > linearize(const Values &x) const override
Marginals marginals(graph, result)
void print(const std::string &s, const KeyFormatter &keyFormatter=DefaultKeyFormatter) const override
noiseModel::Gaussian::shared_ptr SharedGaussian
double error(const Values &x) const override