contrib/mul/pdf1d/pdf1d_compare_samples.cxx
Go to the documentation of this file.
00001 //:
00002 // \file
00003 // \author Tim Cootes
00004 // \brief Functions to compare sets of samples.
00005 
00006 #include "pdf1d_compare_samples.h"
00007 
00008 //: Return the average number of elements of x2 greater than each x1[i]
00009 double pdf1d_prop_larger(const double* x1, int n1, const double* x2, int n2)
00010 {
00011   int n_larger = 0;
00012   for (int i=0;i<n1;++i)
00013     for (int j=0;j<n2;++j)
00014       if (x1[i]>x2[j]) n_larger++;
00015 
00016   return double(n_larger)/(n1*n2);
00017 }
00018 
00019 //: Return true if x1[i] significantly larger than x2[j]
00020 //  Essentially tests if on average a sample from x1 is larger than
00021 //  one from x2
00022 bool pdf1d_larger_samples(const double* x1, int n1, const double* x2, int n2, double p)
00023 {
00024   double prop_larger = pdf1d_prop_larger(x1,n1,x2,n2);
00025   return prop_larger>p;
00026 }
00027 
00028 //: Return true if x1[i] significantly smaller than x2[j]
00029 //  Essentially tests if on average a sample from x1 is smaller than
00030 //  one from x2
00031 bool pdf1d_smaller_samples(const double* x1, int n1, const double* x2, int n2, double p)
00032 {
00033   double prop_larger = pdf1d_prop_larger(x1,n1,x2,n2);
00034   return prop_larger<(1.0-p);
00035 }
00036 
00037 //: Select the samples which are on average largest
00038 //  Where there is ambiguity return smallest index.
00039 int pdf1d_select_largest_samples(vcl_vector<const double*>& x,
00040                                  vcl_vector<int>& n, double p)
00041 {
00042   int t = n.size();
00043 
00044   int best_i = 0;
00045   int best_n_larger = 0;
00046 
00047   // Compare every distribution with every other distribution
00048   // Slightly inefficient implementation - effectively does
00049   // the comparison twice.  Fix this one day.
00050   for (int i=0;i<t;++i)
00051   {
00052     int n_larger=0;
00053     for (int j=0;j<t;++j)
00054     {
00055       if (i==j) continue;
00056       if (pdf1d_larger_samples(x[i],n[i],x[j],n[j],p))
00057         n_larger++;
00058     }
00059 
00060     // If this is better than all the others, then this is the one we want
00061     if (n_larger==(t-1)) return i;
00062 
00063     if (i==0 || n_larger>best_n_larger)
00064     {
00065       best_i = i;
00066       best_n_larger = n_larger;
00067     }
00068   }
00069 
00070   return best_i;
00071 }