contrib/mul/pdf1d/tools/select_pdf.cxx
Go to the documentation of this file.
00001 // This is mul/pdf1d/tools/select_pdf.cxx
00002 
00003 //:
00004 // \file
00005 // \author Tim Cootes
00006 // \brief Test how often a particular comparator chooses correct pdf
00007 // Generate samples from one pdf.  Use comparator to decide which
00008 // of several pdfs samples belong to.  Generate graph of
00009 // %correct vs N.samples
00010 
00011 #include <vcl_iostream.h>
00012 #include <vnl/vnl_vector.h>
00013 #include <pdf1d/pdf1d_compare_to_pdf_ks.h>
00014 #include <pdf1d/pdf1d_compare_to_pdf_bhat.h>
00015 #include <pdf1d/pdf1d_flat.h>
00016 #include <pdf1d/pdf1d_sampler.h>
00017 #include <pdf1d/pdf1d_gaussian.h>
00018 #include <pdf1d/pdf1d_exponential.h>
00019 #include <pdf1d/pdf1d_exponential_builder.h>
00020 #include <pdf1d/pdf1d_gaussian_builder.h>
00021 #include <pdf1d/pdf1d_gaussian_kernel_pdf_builder.h>
00022 #include <pdf1d/pdf1d_select_pdf.h>
00023 
00024 
00025 //: Generate samples from pdf[0].  See how often pdf[0] is selected.
00026 double pdf1d_test_pdf_selection(vcl_vector<const pdf1d_pdf*>& pdf,
00027                                 int n_samples,
00028                                 pdf1d_compare_to_pdf& comparator, int n_tests)
00029 {
00030   int n_correct = 0;
00031   pdf1d_sampler *sampler = pdf[0]->new_sampler();
00032 
00033   vnl_vector<double> x(n_samples);
00034   for (int i=0;i<n_tests;++i)
00035   {
00036     sampler->get_samples(x);
00037     // Note: Using bootstrap gives more reliable results with more samples, but is much slower
00038     if (pdf1d_select_pdf(x.data_block(),n_samples,pdf,comparator)==0)
00039       n_correct++;
00040   }
00041 
00042   delete sampler;
00043 
00044   return double(n_correct)/n_tests;
00045 }
00046 
00047 //: Generate samples from pdf[0].  See how often pdf[0] is selected.
00048 double pdf1d_test_bhat_pdf_selection(vcl_vector<const pdf1d_pdf*>& pdf,
00049                           int n_samples,
00050                           vcl_vector<pdf1d_builder*>& pdf_builder, int n_tests)
00051 {
00052   pdf1d_compare_to_pdf_bhat comparator;
00053   int n_correct = 0;
00054   pdf1d_sampler *sampler = pdf[0]->new_sampler();
00055 
00056   vnl_vector<double> x(n_samples);
00057   for (int i=0;i<n_tests;++i)
00058   {
00059     sampler->get_samples(x);
00060     // Note: Using bootstrap gives more reliable results with more samples, but is much slower
00061     if (pdf1d_select_pdf(x.data_block(),n_samples,pdf,pdf_builder,comparator)==0)
00062       n_correct++;
00063   }
00064 
00065   delete sampler;
00066 
00067   return double(n_correct)/n_tests;
00068 }
00069 
00070 void graph_selection_results(const vcl_string& path,
00071                              vcl_vector<const pdf1d_pdf*>& pdf,
00072                              pdf1d_compare_to_pdf& comparator, int n_tests)
00073 {
00074   vcl_ofstream ofs(path.c_str(),vcl_ios::out);
00075   for (int n_samples=10;n_samples<=200;n_samples+=10)
00076   {
00077     double prop_correct = pdf1d_test_pdf_selection(pdf,n_samples,comparator,n_tests);
00078     ofs<<n_samples<<' '<<prop_correct<<vcl_endl;
00079   }
00080 
00081   ofs.close();
00082 
00083   vcl_cout<<"Results saved to "<<path<<vcl_endl;
00084 }
00085 
00086 void graph_bhat_selection_results(const vcl_string& path,
00087                              vcl_vector<const pdf1d_pdf*>& pdf,
00088                              vcl_vector<pdf1d_builder*>& pdf_builder,  int n_tests)
00089 {
00090   vcl_ofstream ofs(path.c_str(),vcl_ios::out);
00091   for (int n_samples=10;n_samples<=200;n_samples+=10)
00092   {
00093     double prop_correct = pdf1d_test_bhat_pdf_selection(pdf,n_samples,pdf_builder,n_tests);
00094     ofs<<n_samples<<' '<<prop_correct<<vcl_endl;
00095   }
00096 
00097   ofs.close();
00098 
00099   vcl_cout<<"Results saved to "<<path<<vcl_endl;
00100 }
00101 
00102 void test_flat_vs_gauss()
00103 {
00104   pdf1d_flat flat(-1,1);
00105   pdf1d_gaussian Gaussian(flat.mean(),flat.variance());
00106 
00107   vcl_vector<const pdf1d_pdf*> pdf;
00108   pdf.push_back(&flat);
00109   pdf.push_back(&Gaussian);
00110 
00111   pdf1d_compare_to_pdf_bhat bhat_comparator_NN;
00112   graph_selection_results("N_OK_flat_v_gauss_BNN.txt",
00113                           pdf,bhat_comparator_NN,100);
00114 
00115   pdf1d_compare_to_pdf_bhat bhat_comparator_fixed;
00116   bhat_comparator_fixed.set_builder(pdf1d_gaussian_kernel_pdf_builder());
00117   graph_selection_results("N_OK_flat_v_gauss_Bfixed.txt",
00118                           pdf,bhat_comparator_fixed,100);
00119 
00120   pdf1d_compare_to_pdf_ks ks_comparator;
00121   graph_selection_results("N_OK_flat_v_gauss_KS.txt",
00122                           pdf,ks_comparator,100);
00123 }
00124 
00125 // Test Gaussians with different variances
00126 void test_g1_v_g2()
00127 {
00128   pdf1d_gaussian g1(0,1);
00129   pdf1d_gaussian g2(0,2);
00130 
00131   vcl_vector<const pdf1d_pdf*> pdf;
00132   pdf.push_back(&g1);
00133   pdf.push_back(&g2);
00134 
00135   pdf1d_compare_to_pdf_bhat bhat_comparator_gauss;
00136   bhat_comparator_gauss.set_builder(pdf1d_gaussian_builder());
00137   graph_selection_results("N_OK_g1_v_g2_Bgauss.txt",
00138                           pdf,bhat_comparator_gauss,100);
00139 
00140   pdf1d_compare_to_pdf_bhat bhat_comparator_NN;
00141   graph_selection_results("N_OK_g1_v_g2_BNN.txt",
00142                           pdf,bhat_comparator_NN,100);
00143 
00144   pdf1d_compare_to_pdf_bhat bhat_comparator_fixed;
00145   bhat_comparator_fixed.set_builder(pdf1d_gaussian_kernel_pdf_builder());
00146   graph_selection_results("N_OK_g1_v_g2_Bfixed.txt",
00147                           pdf,bhat_comparator_fixed,100);
00148 
00149   pdf1d_compare_to_pdf_ks ks_comparator;
00150   graph_selection_results("N_OK_g1_v_g2_KS.txt",
00151                           pdf,ks_comparator,100);
00152 }
00153 
00154 // Test Gaussians with different variances
00155 void test_g2_v_g3()
00156 {
00157   pdf1d_gaussian g1(0,2);
00158   pdf1d_gaussian g2(0,3);
00159 
00160   vcl_vector<const pdf1d_pdf*> pdf;
00161   pdf.push_back(&g1);
00162   pdf.push_back(&g2);
00163 
00164   pdf1d_compare_to_pdf_bhat bhat_comparator_gauss;
00165   bhat_comparator_gauss.set_builder(pdf1d_gaussian_builder());
00166   graph_selection_results("N_OK_g2_v_g3_Bgauss.txt",
00167                           pdf,bhat_comparator_gauss,100);
00168 
00169   pdf1d_compare_to_pdf_bhat bhat_comparator_NN;
00170   graph_selection_results("N_OK_g2_v_g3_BNN.txt",
00171                           pdf,bhat_comparator_NN,100);
00172 
00173 //   pdf1d_epanech_kernel_pdf_builder ek_builder;
00174 //   ek_builder.set_use_width_from_separation();
00175 //   pdf1d_compare_to_pdf_bhat bhat_comparator_eNN;
00176 //   bhat_comparator_eNN.set_builder(ek_builder);
00177 //   graph_selection_results("N_OK_g2_v_g3_BeNN.txt",
00178 //                           pdf,bhat_comparator_eNN,100);
00179 
00180   pdf1d_compare_to_pdf_bhat bhat_comparator_fixed;
00181   bhat_comparator_fixed.set_builder(pdf1d_gaussian_kernel_pdf_builder());
00182   graph_selection_results("N_OK_g2_v_g3_Bfixed.txt",
00183                           pdf,bhat_comparator_fixed,100);
00184 
00185   pdf1d_compare_to_pdf_ks ks_comparator;
00186   graph_selection_results("N_OK_g2_v_g3_KS.txt",
00187                           pdf,ks_comparator,100);
00188 }
00189 
00190 // Test exponential vs Gaussian
00191 void test_e_v_g()
00192 {
00193   pdf1d_exponential e;
00194   pdf1d_gaussian g(e.mean(),e.variance());
00195 
00196   vcl_vector<const pdf1d_pdf*> pdf;
00197   pdf.push_back(&e);
00198   pdf.push_back(&g);
00199 
00200   pdf1d_exponential_builder e_builder;
00201   pdf1d_gaussian_builder g_builder;
00202   vcl_vector<pdf1d_builder*> pdf_builder;
00203   pdf_builder.push_back(&e_builder);
00204   pdf_builder.push_back(&g_builder);
00205 
00206   graph_bhat_selection_results("N_OK_e_v_g_Beg.txt",
00207                           pdf,pdf_builder,100);
00208 
00209 
00210   pdf1d_compare_to_pdf_bhat bhat_comparator_NN;
00211   graph_selection_results("N_OK_e_v_g_BNN.txt",
00212                           pdf,bhat_comparator_NN,100);
00213 
00214   pdf1d_compare_to_pdf_bhat bhat_comparator_fixed;
00215   bhat_comparator_fixed.set_builder(pdf1d_gaussian_kernel_pdf_builder());
00216   graph_selection_results("N_OK_e_v_g_Bfixed.txt",
00217                           pdf,bhat_comparator_fixed,100);
00218 
00219   pdf1d_compare_to_pdf_ks ks_comparator;
00220   graph_selection_results("N_OK_e_v_g_KS.txt",
00221                           pdf,ks_comparator,100);
00222 }
00223 
00224 int main()
00225 {
00226 //  test_flat_vs_gauss();
00227   test_g1_v_g2();
00228 //  test_e_v_g();
00229 //  test_g2_v_g3();
00230 
00231   return 0;
00232 }