45 #include <visp3/core/vpColVector.h>
46 #include <visp3/core/vpMath.h>
47 #include <visp3/core/vpRobust.h>
54 : m_normres(), m_sorted_normres(), m_sorted_residues(), m_mad_min(0.0017), m_mad_prev(0),
55 #if defined(VISP_BUILD_DEPRECATED_FUNCTIONS)
71 m_normres = other.m_normres;
72 m_sorted_normres = other.m_sorted_normres;
73 m_sorted_residues = other.m_sorted_residues;
74 m_mad_min = other.m_mad_min;
76 m_mad_prev = other.m_mad_prev;
77 #ifdef VISP_BUILD_DEPRECATED_FUNCTIONS
78 m_iter = other.m_iter;
80 m_size = other.m_size;
84 #if (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11)
90 m_normres = std::move(other.m_normres);
91 m_sorted_normres = std::move(other.m_sorted_normres);
92 m_sorted_residues = std::move(other.m_sorted_residues);
93 m_mad_min = std::move(other.m_mad_min);
94 m_mad_prev = std::move(other.m_mad_prev);
95 #ifdef VISP_BUILD_DEPRECATED_FUNCTIONS
96 m_iter = std::move(other.m_iter);
98 m_size = std::move(other.m_size);
107 void vpRobust::resize(
unsigned int n_data)
109 if (n_data != m_size) {
111 m_sorted_normres.
resize(n_data);
112 m_sorted_residues.
resize(n_data);
133 double normmedian = 0;
136 unsigned int n_data = residues.
getRows();
137 weights.
resize(n_data,
false);
140 m_sorted_residues = residues;
142 unsigned int ind_med =
static_cast<unsigned int>(ceil(n_data / 2.0)) - 1;
145 med = select(m_sorted_residues, 0, n_data - 1, ind_med);
149 for (
unsigned int i = 0; i < n_data; ++i) {
150 m_normres[i] = (fabs(residues[i] - med));
151 m_sorted_normres[i] = (fabs(m_sorted_residues[i] - med));
155 normmedian = select(m_sorted_normres, 0, n_data - 1, ind_med);
158 m_mad = 1.4826 * normmedian;
162 if (m_mad < m_mad_min) {
167 psiTukey(m_mad, m_normres, weights);
171 psiCauchy(m_mad, m_normres, weights);
175 psiHuber(m_mad, m_normres, weights);
180 std::cout <<
"MEstimator: method not recognised - id = " << method << std::endl;
194 unsigned int n_data = x.
getRows();
195 double C = sig * 4.6851;
198 for (
unsigned int i = 0; i < n_data; ++i) {
199 double xi = x[i] / C;
222 double C = sig * 1.2107;
223 unsigned int n_data = x.
getRows();
225 for (
unsigned int i = 0; i < n_data; ++i) {
226 double xi = x[i] / C;
228 weights[i] = std::fabs(1. / xi);
246 unsigned int n_data = x.
getRows();
247 double C = sig * 2.3849;
250 for (
unsigned int i = 0; i < n_data; ++i) {
261 int vpRobust::partition(
vpColVector &a,
int l,
int r)
268 while (a[++i] < v) { }
278 std::swap(a[i], a[j]);
280 std::swap(a[i], a[r]);
291 double vpRobust::select(
vpColVector &a,
int l,
int r,
int k)
294 int i = partition(a, l, r);
308 #if defined(VISP_BUILD_DEPRECATED_FUNCTIONS)
317 : m_normres(), m_sorted_normres(), m_sorted_residues(), m_mad_min(0.0017), m_mad_prev(0),
318 #if defined(VISP_BUILD_DEPRECATED_FUNCTIONS)
321 m_size(n_data), m_mad(0)
324 m_sorted_normres.
resize(n_data);
325 m_sorted_residues.
resize(n_data);
332 double normmedian = 0;
334 unsigned int n_all_data = all_residues.
getRows();
339 normmedian = computeNormalizedMedian(all_normres, residues, all_residues, weights);
342 m_mad = 1.4826 * normmedian;
346 if (m_mad < m_mad_min) {
352 psiTukey(m_mad, all_normres, weights);
356 psiCauchy(m_mad, all_normres, weights);
360 psiHuber(m_mad, all_normres, weights);
370 double normmedian = 0;
372 unsigned int n_all_data = all_residues.
getRows();
373 unsigned int n_data = residues.
getRows();
378 m_sorted_residues = residues;
380 no_null_weight_residues.
resize(n_data);
382 unsigned int index = 0;
383 for (
unsigned int j = 0; j < n_data; ++j) {
385 if (std::fabs(weights[j]) > std::numeric_limits<double>::epsilon()) {
386 no_null_weight_residues[index] = residues[j];
390 m_sorted_residues.
resize(index);
391 memcpy(m_sorted_residues.
data, no_null_weight_residues.
data, index *
sizeof(
double));
398 unsigned int ind_med = (
unsigned int)(ceil(n_data / 2.0)) - 1;
399 med = select(m_sorted_residues, 0, n_data - 1, ind_med);
402 for (
unsigned int i = 0; i < n_all_data; ++i) {
403 all_normres[i] = (fabs(all_residues[i] - med));
406 for (
unsigned int i = 0; i < n_data; ++i) {
407 m_sorted_normres[i] = (fabs(m_sorted_residues[i] - med));
410 normmedian = select(m_sorted_normres, 0, n_data - 1, ind_med);
425 unsigned int n_data = residues.
getRows();
430 unsigned int ind_med = (
unsigned int)(ceil(n_data / 2.0)) - 1;
431 med = select(residues, 0, n_data - 1, ind_med );
434 for (
unsigned int i = 0; i < n_data; ++i)
435 norm_res[i] = (fabs(residues[i] - med));
441 double normmedian = select(norm_res, 0, n_data - 1, ind_med);
443 m_mad = 1.4826 * normmedian;
447 m_mad = simultscale(residues);
452 if (m_mad < m_mad_min) {
456 psiHuber(m_mad, norm_res, w);
468 double Expectation = 0;
471 for (
unsigned int i = 0; i < n; ++i) {
473 double chiTmp = simult_chi_huber(x[i]);
474 #if defined(VISP_HAVE_FUNC_STD_ERFC)
475 Expectation += chiTmp * std::erfc(chiTmp);
476 #elif defined(VISP_HAVE_FUNC_ERFC)
477 Expectation += chiTmp * erfc(chiTmp);
479 Expectation += chiTmp * (1 - erf(chiTmp));
484 #if VP_DEBUG_MODE == 3
486 #if defined(VISP_HAVE_FUNC_STD_ERFC)
487 std::cout <<
"erf = " << std::erfc(chiTmp) << std::endl;
488 #elif defined(VISP_HAVE_FUNC_ERFC)
489 std::cout <<
"erf = " << erfc(chiTmp) << std::endl;
491 std::cout <<
"erf = " << (1 - erf(chiTmp)) << std::endl;
493 std::cout <<
"x[i] = " << x[i] << std::endl;
494 std::cout <<
"chi = " << chiTmp << std::endl;
495 std::cout <<
"Sum chi = " << chiTmp *
vpMath::sqr(m_mad_prev) << std::endl;
496 #if defined(VISP_HAVE_FUNC_STD_ERFC)
497 std::cout <<
"Expectation = " << chiTmp * std::erfc(chiTmp) << std::endl;
498 #elif defined(VISP_HAVE_FUNC_ERFC)
499 std::cout <<
"Expectation = " << chiTmp * erfc(chiTmp) << std::endl;
501 std::cout <<
"Expectation = " << chiTmp * (1 - erf(chiTmp)) << std::endl;
509 sigma2 = Sum_chi *
vpMath::sqr(m_mad_prev) / ((n - p) * Expectation);
512 #if VP_DEBUG_MODE == 3
514 std::cout <<
"Expectation = " << Expectation << std::endl;
515 std::cout <<
"Sum chi = " << Sum_chi << std::endl;
516 std::cout <<
"MAD prev" << m_mad_prev << std::endl;
517 std::cout <<
"sig_out" << sqrt(fabs(sigma2)) << std::endl;
522 return sqrt(fabs(sigma2));
525 double vpRobust::constrainedChi(vpRobustEstimatorType method,
double x)
529 return constrainedChiTukey(x);
531 return constrainedChiCauchy(x);
533 return constrainedChiHuber(x);
539 double vpRobust::constrainedChiTukey(
double x)
542 double s = m_mad_prev;
545 if (fabs(x) <= 4.7 * m_mad_prev) {
559 double vpRobust::constrainedChiCauchy(
double x)
563 double s = m_mad_prev;
571 double vpRobust::constrainedChiHuber(
double x)
574 double u = x / m_mad_prev;
585 double vpRobust::simult_chi_huber(
double x)
588 double u = x / m_mad_prev;
603 #if !defined(VISP_HAVE_FUNC_ERFC) && !defined(VISP_HAVE_FUNC_STD_ERFC)
604 double vpRobust::erf(
double x) {
return x < 0.0 ? -gammp(0.5, x * x) : gammp(0.5, x * x); }
606 double vpRobust::gammp(
double a,
double x)
608 double gamser = 0., gammcf = 0., gln;
610 if (x < 0.0 || a <= 0.0)
611 std::cout <<
"Invalid arguments in routine GAMMP";
613 gser(&gamser, a, x, &gln);
617 gcf(&gammcf, a, x, &gln);
622 void vpRobust::gser(
double *gamser,
double a,
double x,
double *gln)
627 std::cout <<
"x less than 0 in routine GSER";
633 double sum = 1.0 / a;
635 for (
int n = 1; n <= vpITMAX; ++n) {
639 if (fabs(del) < fabs(sum) * vpEPS) {
640 *gamser = sum * exp(-x + a * log(x) - (*gln));
644 std::cout <<
"a too large, vpITMAX too small in routine GSER";
649 void vpRobust::gcf(
double *gammcf,
double a,
double x,
double *gln)
651 double gold = 0.0, g, fac = 1.0, b1 = 1.0;
652 double b0 = 0.0, a1, a0 = 1.0;
656 for (
int n = 1; n <= vpITMAX; ++n) {
657 double an =
static_cast<double>(n);
659 a0 = (a1 + a0 * ana) * fac;
660 b0 = (b1 + b0 * ana) * fac;
661 double anf = an * fac;
662 a1 = x * a0 + anf * a1;
663 b1 = x * b0 + anf * b1;
665 if (std::fabs(a1) > std::numeric_limits<double>::epsilon()) {
668 if (fabs((g - gold) / g) < vpEPS) {
669 *gammcf = exp(-x + a * log(x) - (*gln)) * g;
675 std::cout <<
"a too large, vpITMAX too small in routine GCF";
678 double vpRobust::gammln(
double xx)
681 static double cof[6] = { 76.18009173, -86.50532033, 24.01409822, -1.231739516, 0.120858003e-2, -0.536382e-5 };
685 tmp -= (x + 0.5) * log(tmp);
687 for (
int j = 0; j <= 5; ++j) {
691 return -tmp + log(2.50662827465 * ser);
Type * data
Address of the first element of the data array.
unsigned int getRows() const
Implementation of column vector and the associated operations.
void resize(unsigned int i, bool flagNullify=true)
static double sqr(double x)
Contains an M-estimator and various influence function.
vpRobustEstimatorType
Enumeration of influence functions.
@ HUBER
Huber influence function.
@ TUKEY
Tukey influence function.
@ CAUCHY
Cauchy influence function.
void MEstimator(const vpRobustEstimatorType method, const vpColVector &residues, vpColVector &weights)
vpRobust & operator=(const vpRobust &other)