contrib/mul/pdf1d/pdf1d_select_pdf.cxx
Go to the documentation of this file.
00001 // This is mul/pdf1d/pdf1d_select_pdf.cxx
00002 #include "pdf1d_select_pdf.h"
00003 //:
00004 // \file
00005 // \author Tim Cootes
00006 // \brief Functions to select a pdf or form of pdf for given data.
00007 
00008 #include <vcl_cassert.h>
00009 #include <vnl/vnl_vector.h>
00010 
00011 //: Use the comparator to decide which pdf the data is most like
00012 //  Returns index of the chosen pdf.
00013 int pdf1d_select_pdf(const double* data, int n, vcl_vector<const pdf1d_pdf*>& pdf,
00014                      pdf1d_compare_to_pdf& comparator)
00015 {
00016   assert(pdf.size()>0);
00017 
00018   int best_i = 0;
00019   double best_B = comparator.compare(data,n,*pdf[0]);
00020   for (unsigned int i=1;i<pdf.size();++i)
00021   {
00022     double B = comparator.compare(data,n,*pdf[i]);
00023     if (B>best_B)
00024     {
00025       best_B=B;
00026       best_i = i;
00027     }
00028   }
00029 
00030   return best_i;
00031 }
00032 
00033 //: Use the comparator to decide which pdf the data is most like
00034 //  Typically matches data with pdf_builder[i] and computes difference
00035 //  between result and pdf[i]
00036 //  Returns index of the chosen pdf.
00037 int pdf1d_select_pdf(const double* data, int n,
00038                      vcl_vector<const pdf1d_pdf*>& pdf,
00039                      vcl_vector<pdf1d_builder*>& pdf_builder,
00040                      pdf1d_compare_to_pdf_bhat& comparator)
00041 {
00042   assert(pdf.size()>0);
00043 
00044   int best_i = 0;
00045   double best_B = 0.0;
00046   for (unsigned int i=0;i<pdf.size();++i)
00047   {
00048     comparator.set_builder(*pdf_builder[i]);
00049     double B = comparator.compare(data,n,*pdf[i]);
00050     if (i==0 || B>best_B)
00051     {
00052       best_B=B;
00053       best_i = i;
00054     }
00055   }
00056 
00057   return best_i;
00058 }
00059 
00060 //: Use the comparator to decide which pdf the data is most like
00061 //  Returns index of the chosen pdf.
00062 int pdf1d_select_pdf_using_bootstrap(const double* data, int n, vcl_vector<const pdf1d_pdf*>& pdf,
00063                      pdf1d_compare_to_pdf& comparator)
00064 {
00065   assert(pdf.size()>0);
00066 
00067   int n_trials = 20;
00068   vnl_vector<double> B(n_trials);
00069 
00070   int best_i = 0;
00071   double best_B = comparator.bootstrap_compare(B,data,n,*pdf[0],n_trials);
00072   for (unsigned int i=1;i<pdf.size();++i)
00073   {
00074     double B_mean = comparator.bootstrap_compare(B,data,n,*pdf[i],n_trials);
00075     if (B_mean>best_B)
00076     {
00077       best_B=B_mean;
00078       best_i = i;
00079     }
00080   }
00081 
00082   return best_i;
00083 }
00084 
00085 //: Use the comparator to decide which pdf form the data is most like
00086 //  Returns index of the chosen pdf.
00087 int pdf1d_select_pdf_form(const double* data, int n,
00088                      vcl_vector<pdf1d_builder*>& pdf_builder,
00089                      pdf1d_compare_to_pdf& comparator)
00090 {
00091   assert(pdf_builder.size()>0);
00092 
00093   int best_i = 0;
00094   double best_B = comparator.compare_form(data,n,*pdf_builder[0]);
00095   for (unsigned int i=1;i<pdf_builder.size();++i)
00096   {
00097     double B = comparator.compare_form(data,n,*pdf_builder[i]);
00098     if (B>best_B)
00099     {
00100       best_B=B;
00101       best_i = i;
00102     }
00103   }
00104 
00105   return best_i;
00106 }