51 #include <visp3/core/vpColVector.h> 52 #include <visp3/core/vpDebug.h> 53 #include <visp3/core/vpMath.h> 54 #include <visp3/core/vpRobust.h> 60 : m_normres(), m_sorted_normres(), m_sorted_residues(), m_mad_min(0.0017), m_mad_prev(0),
61 #if defined(VISP_BUILD_DEPRECATED_FUNCTIONS)
78 m_normres = other.m_normres;
79 m_sorted_normres = other.m_sorted_normres;
80 m_sorted_residues = other.m_sorted_residues;
81 m_mad_min = other.m_mad_min;
83 m_mad_prev = other.m_mad_prev;
84 #ifdef VISP_BUILD_DEPRECATED_FUNCTIONS 85 m_iter = other.m_iter;
87 m_size = other.m_size;
91 #if (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11) 97 m_normres = std::move(other.m_normres);
98 m_sorted_normres = std::move(other.m_sorted_normres);
99 m_sorted_residues = std::move(other.m_sorted_residues);
100 m_mad_min = std::move(other.m_mad_min);
101 m_mad_prev = std::move(other.m_mad_prev);
102 #ifdef VISP_BUILD_DEPRECATED_FUNCTIONS 103 m_iter = std::move(other.m_iter);
105 m_size = std::move(other.m_size);
114 void vpRobust::resize(
unsigned int n_data)
116 if (n_data != m_size) {
118 m_sorted_normres.
resize(n_data);
119 m_sorted_residues.
resize(n_data);
140 double normmedian = 0;
143 unsigned int n_data = residues.
getRows();
144 weights.
resize(n_data,
false);
147 m_sorted_residues = residues;
149 unsigned int ind_med = (
unsigned int)(ceil(n_data / 2.0)) - 1;
152 med = select(m_sorted_residues, 0, n_data - 1, ind_med);
156 for (
unsigned int i = 0; i < n_data; i++) {
157 m_normres[i] = (fabs(residues[i] - med));
158 m_sorted_normres[i] = (fabs(m_sorted_residues[i] - med));
162 normmedian = select(m_sorted_normres, 0, n_data - 1, ind_med);
165 m_mad = 1.4826 * normmedian;
169 if (m_mad < m_mad_min) {
174 psiTukey(m_mad, m_normres, weights);
178 psiCauchy(m_mad, m_normres, weights);
182 psiHuber(m_mad, m_normres, weights);
198 unsigned int n_data = x.
getRows();
199 double C = sig * 4.6851;
202 for (
unsigned int i = 0; i < n_data; i++) {
203 double xi = x[i] / C;
225 double C = sig * 1.2107;
226 unsigned int n_data = x.
getRows();
228 for (
unsigned int i = 0; i < n_data; i++) {
229 double xi = x[i] / C;
231 weights[i] = std::fabs(1./xi);
247 unsigned int n_data = x.
getRows();
248 double C = sig * 2.3849;
251 for (
unsigned int i = 0; i < n_data; i++) {
262 int vpRobust::partition(
vpColVector &a,
unsigned int l,
unsigned int r)
264 unsigned int i = l - 1;
276 std::swap(a[i], a[j]);
278 std::swap(a[i], a[r]);
289 double vpRobust::select(
vpColVector &a,
unsigned int l,
unsigned int r,
unsigned int k)
292 unsigned int i = partition(a, l, r);
304 #if defined(VISP_BUILD_DEPRECATED_FUNCTIONS) 313 : m_normres(), m_sorted_normres(), m_sorted_residues(), m_mad_min(0.0017), m_mad_prev(0),
314 #if defined(VISP_BUILD_DEPRECATED_FUNCTIONS)
317 m_size(n_data), m_mad(0)
319 vpCDEBUG(2) <<
"vpRobust constructor reached" << std::endl;
322 m_sorted_normres.
resize(n_data);
323 m_sorted_residues.
resize(n_data);
330 double normmedian = 0;
332 unsigned int n_all_data = all_residues.
getRows();
337 normmedian = computeNormalizedMedian(all_normres, residues, all_residues, weights);
340 m_mad = 1.4826 * normmedian;
344 if (m_mad < m_mad_min) {
350 psiTukey(m_mad, all_normres, weights);
352 vpCDEBUG(2) <<
"Tukey's function computed" << std::endl;
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;
446 m_mad = simultscale(residues);
451 if (m_mad < m_mad_min) {
455 psiHuber(m_mad, norm_res, w);
467 double Expectation = 0;
470 for (
unsigned int i = 0; i < n; i++) {
472 double chiTmp = simult_chi_huber(x[i]);
473 #if defined(VISP_HAVE_FUNC_STD_ERFC) 474 Expectation += chiTmp * std::erfc(chiTmp);
475 #elif defined(VISP_HAVE_FUNC_ERFC) 476 Expectation += chiTmp * erfc(chiTmp);
478 Expectation += chiTmp * (1 - erf(chiTmp));
483 #if VP_DEBUG_MODE == 3 485 #if defined(VISP_HAVE_FUNC_STD_ERFC) 486 std::cout <<
"erf = " << std::erfc(chiTmp) << std::endl;
487 #elif defined(VISP_HAVE_FUNC_ERFC) 488 std::cout <<
"erf = " << erfc(chiTmp) << std::endl;
490 std::cout <<
"erf = " << (1 - erf(chiTmp)) << std::endl;
492 std::cout <<
"x[i] = " << x[i] << std::endl;
493 std::cout <<
"chi = " << chiTmp << std::endl;
494 std::cout <<
"Sum chi = " << chiTmp *
vpMath::sqr(m_mad_prev) << std::endl;
495 #if defined(VISP_HAVE_FUNC_STD_ERFC) 496 std::cout <<
"Expectation = " << chiTmp * std::erfc(chiTmp) << std::endl;
497 #elif defined(VISP_HAVE_FUNC_ERFC) 498 std::cout <<
"Expectation = " << chiTmp * erfc(chiTmp) << std::endl;
500 std::cout <<
"Expectation = " << chiTmp * (1 - erf(chiTmp)) << std::endl;
508 sigma2 = Sum_chi *
vpMath::sqr(m_mad_prev) / ((n - p) * Expectation);
511 #if VP_DEBUG_MODE == 3 513 std::cout <<
"Expectation = " << Expectation << std::endl;
514 std::cout <<
"Sum chi = " << Sum_chi << std::endl;
515 std::cout <<
"MAD prev" << m_mad_prev << std::endl;
516 std::cout <<
"sig_out" << sqrt(fabs(sigma2)) << std::endl;
521 return sqrt(fabs(sigma2));
528 return constrainedChiTukey(x);
530 return constrainedChiCauchy(x);
532 return constrainedChiHuber(x);
538 double vpRobust::constrainedChiTukey(
double x)
541 double s = m_mad_prev;
544 if (fabs(x) <= 4.7 * m_mad_prev) {
557 double vpRobust::constrainedChiCauchy(
double x)
561 double s = m_mad_prev;
569 double vpRobust::constrainedChiHuber(
double x)
572 double u = x / m_mad_prev;
583 double vpRobust::simult_chi_huber(
double x)
586 double u = x / m_mad_prev;
600 #if !defined(VISP_HAVE_FUNC_ERFC) && !defined(VISP_HAVE_FUNC_STD_ERFC) 601 double vpRobust::erf(
double x) {
return x < 0.0 ? -gammp(0.5, x * x) : gammp(0.5, x * x); }
603 double vpRobust::gammp(
double a,
double x)
605 double gamser = 0., gammcf = 0., gln;
607 if (x < 0.0 || a <= 0.0)
608 std::cout <<
"Invalid arguments in routine GAMMP";
610 gser(&gamser, a, x, &gln);
613 gcf(&gammcf, a, x, &gln);
618 void vpRobust::gser(
double *gamser,
double a,
double x,
double *gln)
623 std::cout <<
"x less than 0 in routine GSER";
628 double sum = 1.0 / a;
630 for (
int n = 1; n <= vpITMAX; n++) {
634 if (fabs(del) < fabs(sum) * vpEPS) {
635 *gamser = sum * exp(-x + a * log(x) - (*gln));
639 std::cout <<
"a too large, vpITMAX too small in routine GSER";
644 void vpRobust::gcf(
double *gammcf,
double a,
double x,
double *gln)
646 double gold = 0.0, g, fac = 1.0, b1 = 1.0;
647 double b0 = 0.0, a1, a0 = 1.0;
651 for (
int n = 1; n <= vpITMAX; n++) {
652 double an = (double)n;
654 a0 = (a1 + a0 * ana) * fac;
655 b0 = (b1 + b0 * ana) * fac;
656 double anf = an * fac;
657 a1 = x * a0 + anf * a1;
658 b1 = x * b0 + anf * b1;
660 if (std::fabs(a1) > std::numeric_limits<double>::epsilon()) {
663 if (fabs((g - gold) / g) < vpEPS) {
664 *gammcf = exp(-x + a * log(x) - (*gln)) * g;
670 std::cout <<
"a too large, vpITMAX too small in routine GCF";
673 double vpRobust::gammln(
double xx)
676 static double cof[6] = {76.18009173, -86.50532033, 24.01409822, -1.231739516, 0.120858003e-2, -0.536382e-5};
680 tmp -= (x + 0.5) * log(tmp);
682 for (
int j = 0; j <= 5; j++) {
686 return -tmp + log(2.50662827465 * ser);
void MEstimator(const vpRobustEstimatorType method, const vpColVector &residues, vpColVector &weights)
Type * data
Address of the first element of the data array.
Huber influence function.
vp_deprecated vpColVector simultMEstimator(vpColVector &residues)
static double sqr(double x)
vpRobust & operator=(const vpRobust &other)
unsigned int getRows() const
void resize(unsigned int i, bool flagNullify=true)
Implementation of column vector and the associated operations.
Contains an M-estimator and various influence function.
Tukey influence function.
Cauchy influence function.
vpRobustEstimatorType
Enumeration of influence functions.