contrib/mul/pdf1d/pdf1d_compare_to_pdf.cxx
Go to the documentation of this file.
00001 // This is mul/pdf1d/pdf1d_compare_to_pdf.cxx
00002 
00003 //:
00004 // \file
00005 // \author Tim Cootes
00006 // \brief Base for classes with test whether data could come from a given pdf.
00007 
00008 #include "pdf1d_compare_to_pdf.h"
00009 #include <pdf1d/pdf1d_pdf.h>
00010 #include <pdf1d/pdf1d_resample.h>
00011 #include <vsl/vsl_indent.h>
00012 #include <pdf1d/pdf1d_calc_mean_var.h>
00013 
00014 //=======================================================================
00015 // Dflt ctor
00016 //=======================================================================
00017 
00018 pdf1d_compare_to_pdf::pdf1d_compare_to_pdf()
00019 {
00020 }
00021 
00022 //=======================================================================
00023 // Destructor
00024 //=======================================================================
00025 
00026 pdf1d_compare_to_pdf::~pdf1d_compare_to_pdf()
00027 {
00028 }
00029 
00030 
00031 //: Test whether data came from the given distribution, using bootstrap
00032 //  Repeatedly resamples n values from data[0..n-1] and compares with
00033 //  the given pdf.  Individual comparisons are returned in B.
00034 //  \return Mean of B
00035 double pdf1d_compare_to_pdf::bootstrap_compare(vnl_vector<double>& B,
00036                                                const double* data, int n,
00037                                                const pdf1d_pdf& pdf, int n_trials)
00038 {
00039   vnl_vector<double> sample(n);
00040 
00041   B.set_size(n_trials);
00042 
00043   double sum = 0;
00044   for (int i=0;i<n_trials;++i)
00045   {
00046     // Check resampled data is not all identical
00047     double s_mean,s_var=0;
00048     while (s_var<1e-9)
00049     {
00050       pdf1d_resample(sample,data,n);
00051       pdf1d_calc_mean_var(s_mean,s_var,sample);
00052     }
00053 
00054     double c = compare(sample.data_block(),n,pdf);
00055     B[i] = c;
00056     sum+=c;
00057   }
00058 
00059   return sum/n_trials;
00060 }
00061 
00062 //: Test whether data has form of the given distribution
00063 //  Default behaviour is to build pdf from data and then compare data with pdf
00064 double pdf1d_compare_to_pdf::compare_form(const double* data, int n,
00065                               const pdf1d_builder& builder)
00066 {
00067   if (!pdf_.isDefined() || pdf_->is_a()!=builder.new_model_type())
00068     pdf_ = builder.new_model();
00069 
00070   builder.build_from_array(pdf_,data,n);
00071   return compare(data,n,*(pdf_.ptr()));
00072 }
00073 
00074 //: Test whether data has form of the given distribution
00075 double pdf1d_compare_to_pdf::bootstrap_compare_form(vnl_vector<double>& B,
00076                               const double* data, int n,
00077                               const pdf1d_builder& builder, int n_trials)
00078 {
00079   vnl_vector<double> sample(n);
00080 
00081   if (!pdf_.isDefined() || pdf_->is_a()!=builder.new_model_type())
00082     pdf_ = builder.new_model();
00083 
00084   B.set_size(n_trials);
00085 
00086   double sum = 0;
00087   for (int i=0;i<n_trials;++i)
00088   {
00089     // Build pdf from resampled data.
00090     // Check resampled data is not all identical
00091     double s_mean,s_var=0;
00092     while (s_var<1e-9)
00093     {
00094       pdf1d_resample(sample,data,n);
00095       pdf1d_calc_mean_var(s_mean,s_var,sample);
00096     }
00097 
00098     builder.build_from_array(pdf_,sample.data_block(),n);
00099 
00100     // Test overlap of pdf with original data
00101     double c = compare(data,n,*(pdf_.ptr()));
00102     B[i] = c;
00103     sum+=c;
00104   }
00105 
00106   return sum/n_trials;
00107 }
00108 
00109 //=======================================================================
00110 // Method: is_a
00111 //=======================================================================
00112 
00113 vcl_string pdf1d_compare_to_pdf::is_a() const
00114 {
00115   return vcl_string("pdf1d_compare_to_pdf");
00116 }
00117 
00118 //=======================================================================
00119 // Method: is_class
00120 //=======================================================================
00121 
00122 bool pdf1d_compare_to_pdf::is_class(vcl_string const& s) const
00123 {
00124   return s==pdf1d_compare_to_pdf::is_a();
00125 }
00126 
00127 
00128 void vsl_print_summary(vcl_ostream& os,const pdf1d_compare_to_pdf& b)
00129 {
00130   os << b.is_a() << ": ";
00131   vsl_indent_inc(os);
00132   b.print_summary(os);
00133   vsl_indent_dec(os);
00134 }
00135 
00136 //=======================================================================
00137 
00138 void vsl_print_summary(vcl_ostream& os,const pdf1d_compare_to_pdf* b)
00139 {
00140   if (b)
00141     vsl_print_summary(os, *b);
00142   else
00143     os << "No pdf1d_compare_to_pdf defined.";
00144 }
00145