8#ifndef META_OCEAN_GEOMETRY_ESTIMATOR_H
9#define META_OCEAN_GEOMETRY_ESTIMATOR_H
133 template <EstimatorType tEstimator>
134 static constexpr bool needSigma();
141 template <EstimatorType tEstimator>
142 static constexpr bool isStandardEstimator();
149 static inline bool isStandardEstimator(
const EstimatorType estimator);
166 template <EstimatorType tEstimator>
187 template <EstimatorType tEstimator>
188 static inline Scalar robustErrorSquare(
const Scalar sqrValue,
const Scalar sqrSigma = 0);
207 template <EstimatorType tEstimator>
226 template <EstimatorType tEstimator>
227 static inline Scalar robustWeightSquare(
const Scalar sqrValue,
const Scalar sqrSigma = 0);
246 template <EstimatorType tEstimator>
247 static inline Scalar determineSigma(
const Scalar* errors,
const size_t number,
const size_t modelParameters);
257 static inline Scalar determineSigma(
const Scalar* errors,
const size_t number,
const size_t modelParameters,
const EstimatorType estimator);
268 template <EstimatorType tEstimator>
269 static inline Scalar determineSigma(
const Scalar* errors,
const unsigned int* indices,
const size_t numberIndices,
const size_t modelParameters);
280 static inline Scalar determineSigma(
const Scalar* errors,
const unsigned int* indices,
const size_t numberIndices,
const size_t modelParameters,
const EstimatorType estimator);
290 template <EstimatorType tEstimator>
291 static inline Scalar determineSigmaSquare(
const Scalar* sqrErrors,
const size_t number,
const size_t modelParameters);
301 static inline Scalar determineSigmaSquare(
const Scalar* sqrErrors,
const size_t number,
const size_t modelParameters,
const EstimatorType estimator);
312 template <EstimatorType tEstimator>
313 static inline Scalar determineSigmaSquare(
const Scalar* sqrErrors,
const unsigned int* indices,
const size_t numberIndices,
const size_t modelParameters);
324 static inline Scalar determineSigmaSquare(
const Scalar* sqrErrors,
const unsigned int* indices,
const size_t numberIndices,
const size_t modelParameters,
const EstimatorType estimator);
334 template <Estimator::EstimatorType tEstimator>
335 static inline Scalar determineRobustError(
const Scalar* sqrErrors,
const size_t number,
const size_t modelParameters);
341 static constexpr Scalar maximalWeight();
347 static constexpr Scalar invMaximalWeight();
375 template <EstimatorType tEstimator>
376 static inline Scalar sigmaTuningConstant();
379template <Estimator::EstimatorType tEstimator>
390 return needSigma<ET_SQUARE>();
393 return needSigma<ET_LINEAR>();
396 return needSigma<ET_HUBER>();
399 return needSigma<ET_TUKEY>();
402 return needSigma<ET_CAUCHY>();
408 ocean_assert(
false &&
"Invalid estimator!");
412template <Estimator::EstimatorType tEstimator>
427template <Estimator::EstimatorType tEstimator>
430 ocean_assert(
false &&
"Invalid estimator type!");
438 ocean_assert_and_suppress_unused(sigma ==
Scalar(0), sigma);
442 return value * value *
Scalar(0.5);
448 ocean_assert_and_suppress_unused(sigma ==
Scalar(0), sigma);
461 ocean_assert(sigma > 0);
465 if (absValue <= sigma)
467 return value * value *
Scalar(0.5);
470 return sigma * (absValue - sigma *
Scalar(0.5));
479 ocean_assert(sigma > 0);
481 const Scalar sqrSigma6 = sigma * sigma *
Scalar(1.0 / 6.0);
487 return sqrSigma6 * (
Scalar(1) - tmp * tmp * tmp);
498 ocean_assert(sigma > 0);
508 return robustError<ET_SQUARE>(value);
511 return robustError<ET_LINEAR>(value);
514 return robustError<ET_HUBER>(value, sigma);
517 return robustError<ET_TUKEY>(value, sigma);
520 return robustError<ET_CAUCHY>(value, sigma);
526 ocean_assert(
false &&
"Invalid estimator!");
527 return robustError<ET_SQUARE>(value);
530template <Estimator::EstimatorType tEstimator>
533 ocean_assert(
false &&
"Invalid estimator type!");
539inline Scalar Estimator::robustErrorSquare<Estimator::ET_SQUARE>(
const Scalar sqrValue,
const Scalar sqrSigma)
541 ocean_assert_and_suppress_unused(sqrSigma ==
Scalar(0), sqrSigma);
545 return sqrValue *
Scalar(0.5);
549inline Scalar Estimator::robustErrorSquare<Estimator::ET_LINEAR>(
const Scalar sqrValue,
const Scalar sqrSigma)
551 ocean_assert_and_suppress_unused(sqrSigma ==
Scalar(0), sqrSigma);
559inline Scalar Estimator::robustErrorSquare<Estimator::ET_HUBER>(
const Scalar sqrValue,
const Scalar sqrSigma)
564 ocean_assert(sqrSigma > 0);
566 if (sqrValue <= sqrSigma)
568 return sqrValue *
Scalar(0.5);
575inline Scalar Estimator::robustErrorSquare<Estimator::ET_TUKEY>(
const Scalar sqrValue,
const Scalar sqrSigma)
580 ocean_assert(sqrSigma > 0);
582 if (sqrValue <= sqrSigma)
586 return sqrSigma *
Scalar(1.0 / 6.0) * (
Scalar(1) - tmp * tmp * tmp);
589 return sqrSigma *
Scalar(1.0 / 6.0);
593inline Scalar Estimator::robustErrorSquare<Estimator::ET_CAUCHY>(
const Scalar sqrValue,
const Scalar sqrSigma)
597 ocean_assert(sqrSigma > 0);
607 return robustErrorSquare<ET_SQUARE>(sqrValue);
610 return robustErrorSquare<ET_LINEAR>(sqrValue);
613 return robustErrorSquare<ET_HUBER>(sqrValue, sqrSigma);
616 return robustErrorSquare<ET_TUKEY>(sqrValue, sqrSigma);
619 return robustErrorSquare<ET_CAUCHY>(sqrValue, sqrSigma);
625 ocean_assert(
false &&
"Invalid estimator!");
626 return robustErrorSquare<ET_SQUARE>(sqrValue);
629template <Estimator::EstimatorType tEstimator>
632 ocean_assert(
false &&
"Invalid estimator!");
640 ocean_assert_and_suppress_unused(sigma ==
Scalar(0), sigma);
648 ocean_assert_and_suppress_unused(sigma ==
Scalar(0), sigma);
658 ocean_assert(sigma > 0);
662 if (absValue <= sigma)
667 return std::min(
Numeric::ratio(sigma, absValue, maximalWeight()), maximalWeight());
673 ocean_assert(sigma > 0);
677 if (absValue > sigma)
688 ocean_assert(sigma > 0);
698 return robustWeight<ET_SQUARE>(value);
701 return robustWeight<ET_LINEAR>(value);
704 return robustWeight<ET_HUBER>(value, sigma);
707 return robustWeight<ET_TUKEY>(value, sigma);
710 return robustWeight<ET_CAUCHY>(value, sigma);
716 ocean_assert(
false &&
"Invalid estimator!");
717 return robustWeight<ET_SQUARE>(value);
720template <Estimator::EstimatorType tEstimator>
723 ocean_assert(
false &&
"Invalid estimator!");
728inline Scalar Estimator::robustWeightSquare<Estimator::ET_SQUARE>(
const Scalar ,
const Scalar sqrSigma)
730 ocean_assert_and_suppress_unused(sqrSigma ==
Scalar(0), sqrSigma);
736inline Scalar Estimator::robustWeightSquare<Estimator::ET_LINEAR>(
const Scalar sqrValue,
const Scalar sqrSigma)
738 ocean_assert(sqrValue >= 0);
739 ocean_assert_and_suppress_unused(sqrSigma ==
Scalar(0), sqrSigma);
745inline Scalar Estimator::robustWeightSquare<Estimator::ET_HUBER>(
const Scalar sqrValue,
const Scalar sqrSigma)
747 ocean_assert(sqrValue >= 0 && sqrSigma > 0);
749 if (sqrValue <= sqrSigma)
758inline Scalar Estimator::robustWeightSquare<Estimator::ET_TUKEY>(
const Scalar sqrValue,
const Scalar sqrSigma)
760 ocean_assert(sqrSigma > 0);
762 if (sqrValue > sqrSigma)
771inline Scalar Estimator::robustWeightSquare<Estimator::ET_CAUCHY>(
const Scalar sqrValue,
const Scalar sqrSigma)
773 ocean_assert(sqrSigma > 0);
783 return robustWeightSquare<ET_SQUARE>(sqrValue);
786 return robustWeightSquare<ET_LINEAR>(sqrValue);
789 return robustWeightSquare<ET_HUBER>(sqrValue, sqrSigma);
792 return robustWeightSquare<ET_TUKEY>(sqrValue, sqrSigma);
795 return robustWeightSquare<ET_CAUCHY>(sqrValue, sqrSigma);
801 ocean_assert(
false &&
"Invalid estimator!");
802 return robustWeightSquare<ET_SQUARE>(sqrValue);
805template <Estimator::EstimatorType tEstimator>
808 ocean_assert(number > 0);
812 if (number <= modelParameters)
814 return max(
Numeric::eps(), sigmaTuningConstant<tEstimator>() *
Scalar(1.4826) * median);
829 return determineSigma<ET_HUBER>(errors, number, modelParameters);
832 return determineSigma<ET_TUKEY>(errors, number, modelParameters);
835 return determineSigma<ET_CAUCHY>(errors, number, modelParameters);
841 ocean_assert(
false &&
"Invalid estimator!");
845template <Estimator::EstimatorType tEstimator>
848 ocean_assert(errors !=
nullptr);
849 ocean_assert(indices !=
nullptr);
850 ocean_assert(numberIndices > 0);
851 ocean_assert(modelParameters >= 1);
854 Scalars subsetErrors(numberIndices);
855 for (
size_t n = 0; n < numberIndices; ++n)
857 subsetErrors[n] = errors[indices[n]];
862 if (numberIndices <= modelParameters)
864 return max(
Numeric::eps(), sigmaTuningConstant<tEstimator>() *
Scalar(1.4826) * median);
867 return max(
Numeric::eps(), sigmaTuningConstant<tEstimator>() *
Scalar(1.4826) * (1 +
Scalar(5) /
Scalar(numberIndices - modelParameters)) * median);
879 return determineSigma<ET_HUBER>(errors, indices, numberIndices, modelParameters);
882 return determineSigma<ET_TUKEY>(errors, indices, numberIndices, modelParameters);
885 return determineSigma<ET_CAUCHY>(errors, indices, numberIndices, modelParameters);
891 ocean_assert(
false &&
"Invalid estimator!");
895template <Estimator::EstimatorType tEstimator>
898 ocean_assert(sqrErrors !=
nullptr);
899 ocean_assert(number > 0);
903 if (number <= modelParameters)
920 return determineSigmaSquare<ET_HUBER>(sqrErrors, number, modelParameters);
923 return determineSigmaSquare<ET_TUKEY>(sqrErrors, number, modelParameters);
926 return determineSigmaSquare<ET_CAUCHY>(sqrErrors, number, modelParameters);
932 ocean_assert(
false &&
"Invalid estimator!");
936template <Estimator::EstimatorType tEstimator>
939 ocean_assert(sqrErrors !=
nullptr);
940 ocean_assert(indices !=
nullptr);
941 ocean_assert(numberIndices > 0);
944 Scalars subsetSqrErrors(numberIndices);
945 for (
size_t n = 0; n < numberIndices; ++n)
947 subsetSqrErrors[n] = sqrErrors[indices[n]];
952 if (numberIndices <= modelParameters)
969 return determineSigmaSquare<ET_HUBER>(sqrErrors, indices, numberIndices, modelParameters);
972 return determineSigmaSquare<ET_TUKEY>(sqrErrors, indices, numberIndices, modelParameters);
975 return determineSigmaSquare<ET_CAUCHY>(sqrErrors, indices, numberIndices, modelParameters);
981 ocean_assert(
false &&
"Invalid estimator!");
985template <Estimator::EstimatorType tEstimator>
988 ocean_assert(sqrErrors !=
nullptr && number >= 1);
989 ocean_assert(modelParameters >= 1);
992 const Scalar sqrSigma = needSigma<tEstimator>() ?
Numeric::sqr(determineSigmaSquare<tEstimator>(sqrErrors, number, modelParameters)) : 0;
996 for (
size_t n = 0; n < number; ++n)
998 robustError += sqrErrors[n] * robustWeightSquare<tEstimator>(sqrErrors[n], sqrSigma);
1015template <Estimator::EstimatorType tEstimator>
1018 ocean_assert(
false &&
"Invalid estimator type!");
1023inline Scalar Estimator::sigmaTuningConstant<Estimator::ET_HUBER>()
1029inline Scalar Estimator::sigmaTuningConstant<Estimator::ET_TUKEY>()
1035inline Scalar Estimator::sigmaTuningConstant<Estimator::ET_CAUCHY>()
This class implements robust estimator functions.
Definition Estimator.h:27
static Scalar robustWeight(const Scalar value, const Scalar sigma=0)
Returns the weight in relation to a error for a given residual error and a specified estimator.
Definition Estimator.h:630
static Scalar robustWeightSquare(const Scalar sqrValue, const Scalar sqrSigma=0)
Returns the weight in relation to a squared error for a given residual error and a specified estimato...
Definition Estimator.h:721
static std::string translateEstimatorType(const EstimatorType estimatorType)
Translates a given estimator type into a readable string.
static constexpr Scalar invMaximalWeight()
Returns the inverse maximal weight for any estimator which is used to clamp extremely high weights (f...
Definition Estimator.h:1010
static Scalar determineSigmaSquare(const Scalar *sqrErrors, const size_t number, const size_t modelParameters)
Determines the squared sigma for a specific set of squared residual errors and a specified estimator.
Definition Estimator.h:896
static Scalar determineRobustError(const Scalar *sqrErrors, const size_t number, const size_t modelParameters)
Determines the overall robust error for set of given squared errors, a specified estimator and the di...
Definition Estimator.h:986
static constexpr Scalar maximalWeight()
Returns the maximal weight for any estimator which is used to clamp extremely high weights (for tiny ...
Definition Estimator.h:1005
static EstimatorType translateEstimatorType(const std::string &estimatorType)
Translates a readable name of an estimator type to it's value.
static constexpr bool needSigma()
Returns whether an estimator needs a standard deviation for computation.
Definition Estimator.h:380
static const EstimatorTypes & estimatorTypes()
Returns all existing valid estimator types.
std::vector< EstimatorType > EstimatorTypes
Definition of a vector holding estimator types.
Definition Estimator.h:124
EstimatorType
Definition of individual robust estimator types.
Definition Estimator.h:34
@ ET_HUBER
The Huber estimator type.
Definition Estimator.h:84
@ ET_TUKEY
The Tukey estimator.
Definition Estimator.h:102
@ ET_SQUARE
The standard square error estimator (L2).
Definition Estimator.h:52
@ ET_LINEAR
The linear estimator (L1).
Definition Estimator.h:66
@ ET_INVALID
An invalid estimator type.
Definition Estimator.h:38
@ ET_CAUCHY
The Cauchy estimator.
Definition Estimator.h:118
static constexpr bool isStandardEstimator()
Returns whether an estimator is the standard square error estimator.
Definition Estimator.h:413
static Scalar robustError(const Scalar value, const Scalar sigma=0)
Returns the robust error of a residual error for a specified estimator.
Definition Estimator.h:428
static Scalar robustErrorSquare(const Scalar sqrValue, const Scalar sqrSigma=0)
Returns the robust error of a squared residual error for a specified estimator.
Definition Estimator.h:531
static Scalar sigmaTuningConstant()
Returns the tuning constant allowing to determine a 95 percent efficiency on the standard normal dist...
Definition Estimator.h:1016
static Scalar determineSigma(const Scalar *errors, const size_t number, const size_t modelParameters)
Determines the sigma for a specific set of residual errors and a specified estimator.
Definition Estimator.h:806
static constexpr T weakEps()
Returns a weak epsilon.
static T log(const T value)
Returns the natural logarithm of a given value (the logarithm to the base e).
Definition Numeric.h:1659
static T abs(const T value)
Returns the absolute value of a given value.
Definition Numeric.h:1220
static T sqrt(const T value)
Returns the square root of a given value.
Definition Numeric.h:1537
static constexpr T eps()
Returns a small epsilon.
static constexpr T ratio(const T nominator, const T denominator, const T fallback=T(1))
Returns the ratio between two values if the denominator is not equal a small epsilon.
Definition Numeric.h:2085
static constexpr T sqr(const T value)
Returns the square of a given value.
Definition Numeric.h:1499
float Scalar
Definition of a scalar type.
Definition Math.h:129
std::vector< Scalar > Scalars
Definition of a vector holding Scalar objects.
Definition Math.h:145
The namespace covering the entire Ocean framework.
Definition Accessor.h:15