Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Huber mEstimator #116

Merged
merged 3 commits into from
Sep 19, 2019
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions gtsam/linear/NoiseModel.h
Original file line number Diff line number Diff line change
Expand Up @@ -629,7 +629,7 @@ namespace gtsam {
/**
* The mEstimator name space contains all robust error functions.
* It mirrors the exposition at
* http://research.microsoft.com/en-us/um/people/zhang/INRIA/Publis/Tutorial-Estim/node24.html
* https://members.loria.fr/MOBerger/Enseignement/Master2/Documents/ZhangIVC-97-01.pdf
* which talks about minimizing \sum \rho(r_i), where \rho is a residual function of choice.
*
* To illustrate, let's consider the least-squares (L2), L1, and Huber estimators as examples:
Expand Down Expand Up @@ -681,7 +681,7 @@ namespace gtsam {
/*
* This method is responsible for returning the weight function for a given amount of error.
* The weight function is related to the analytic derivative of the residual function. See
* http://research.microsoft.com/en-us/um/people/zhang/INRIA/Publis/Tutorial-Estim/node24.html
* https://members.loria.fr/MOBerger/Enseignement/Master2/Documents/ZhangIVC-97-01.pdf
* for details. This method is required when optimizing cost functions with robust penalties
* using iteratively re-weighted least squares.
*/
Expand Down Expand Up @@ -776,7 +776,7 @@ namespace gtsam {

Huber(double k = 1.345, const ReweightScheme reweight = Block);
double weight(double error) const {
return (error < k_) ? (1.0) : (k_ / fabs(error));
return (std::abs(error) < k_) ? (1.0) : (k_ / fabs(error));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I propose

const double absError = std::abs(error);
return (absError < k_) ? (1.0) : (k_ / absError);

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated both.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This reminded me again the beauty of functional programming :)

}
void print(const std::string &s) const;
bool equals(const Base& expected, double tol=1e-8) const;
Expand Down
11 changes: 8 additions & 3 deletions gtsam/linear/tests/testNoiseModel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -452,19 +452,24 @@ TEST(NoiseModel, WhitenInPlace)
/*
* These tests are responsible for testing the weight functions for the m-estimators in GTSAM.
* The weight function is related to the analytic derivative of the residual function. See
* http://research.microsoft.com/en-us/um/people/zhang/INRIA/Publis/Tutorial-Estim/node24.html
* https://members.loria.fr/MOBerger/Enseignement/Master2/Documents/ZhangIVC-97-01.pdf
* for details. This weight function is required when optimizing cost functions with robust
* penalties using iteratively re-weighted least squares.
*/

TEST(NoiseModel, robustFunctionHuber)
{
const double k = 5.0, error1 = 1.0, error2 = 10.0;
const double k = 5.0, error1 = 1.0, error2 = 10.0, error3 = -10.0, error4 = -1.0;
const mEstimator::Huber::shared_ptr huber = mEstimator::Huber::Create(k);
const double weight1 = huber->weight(error1),
weight2 = huber->weight(error2);
weight2 = huber->weight(error2),
weight3 = huber->weight(error3),
weight4 = huber->weight(error4);
DOUBLES_EQUAL(1.0, weight1, 1e-8);
DOUBLES_EQUAL(0.5, weight2, 1e-8);
// Test negative value to ensure we take absolute value of error.
DOUBLES_EQUAL(0.5, weight3, 1e-8);
varunagrawal marked this conversation as resolved.
Show resolved Hide resolved
DOUBLES_EQUAL(1.0, weight4, 1e-8);
}

TEST(NoiseModel, robustFunctionGemanMcClure)
Expand Down