contrib/mul/pdf1d/tools/test_overlap_pdf.cxx
Go to the documentation of this file.
00001 //:
00002 // \file
00003 #include <vcl_iostream.h>
00004 #include <vcl_cmath.h>
00005 #include <mbl/mbl_stats_1d.h>
00006 #include <vnl/vnl_vector.h>
00007 #include <vnl/vnl_random.h>
00008 #include <pdf1d/pdf1d_compare_to_pdf_ks.h>
00009 #include <pdf1d/pdf1d_compare_to_pdf_bhat.h>
00010 #include <pdf1d/pdf1d_calc_mean_var.h>
00011 #include <pdf1d/pdf1d_flat.h>
00012 #include <pdf1d/pdf1d_gaussian.h>
00013 #include <pdf1d/pdf1d_gaussian_builder.h>
00014 #include <pdf1d/pdf1d_epanech_kernel_pdf_builder.h>
00015 
00016 // Generate samples with one pdf and test if it matches a particular form
00017 // Output sent to B_vs_Nsamples.txt file.
00018 
00019 vnl_random mz_random;
00020 
00021 //: Generate n samples from pdf and compare with test_pdf
00022 void run_experiment(const pdf1d_pdf& pdf,
00023                     const pdf1d_builder& test_builder,
00024                     double& B_mean, double& B_var, int n)
00025 {
00026   vnl_vector<double> d(n),b;
00027 
00028   // Generate n random samples from the pdf
00029   pdf.get_samples(d);
00030 
00031   int n_trials = 20;
00032 
00033   // Strictly should use kernel builder, when one is available
00034   pdf1d_compare_to_pdf_bhat bhat_comparator;
00035   //  bhat_comparator.set_builder(pdf1d_gaussian_builder());
00036   pdf1d_epanech_kernel_pdf_builder ek_builder;
00037   ek_builder.set_use_width_from_separation();
00038   bhat_comparator.set_builder(ek_builder);
00039 
00040   pdf1d_compare_to_pdf_ks ks_comparator;
00041 
00042   bhat_comparator.bootstrap_compare_form(b,d.data_block(),n,test_builder,n_trials);
00043 
00044   pdf1d_calc_mean_var(B_mean,B_var,b);
00045 #if 0
00046   vcl_cout<<B_mean<<" sd:"<<vcl_sqrt(B_var)<<vcl_endl;
00047 
00048   // Test if distribution of B is gaussian or log gaussian:
00049   vcl_cout<<"Compare B pdf to gaussian: "<<pdf1d_compare_to_gaussian_1d(b,n_reps)<<vcl_endl;
00050 
00051   vnl_vector<double> log_b = b;
00052   for (int i=0;i<b.size();++i) log_b[i]=vcl_log(b[i]);
00053   vcl_cout<<"Compare log(B) pdf to gaussian: "<<pdf1d_compare_to_gaussian_1d(log_b,n_reps)<<vcl_endl;
00054 
00055   vnl_vector<double> exp_b = b;
00056   for (int i=0;i<b.size();++i) exp_b[i]=vcl_exp(b[i]);
00057   vcl_cout<<"Compare exp(B) pdf to gaussian: "<<pdf1d_compare_to_gaussian_1d(exp_b,n_reps)<<vcl_endl;
00058 #endif
00059 }
00060 
00061 #if 0
00062 void run_experiment(const pdf1d_pdf& pdf,int n_samples, int n_repeats)
00063 {
00064   mbl_stats_1d B_mean_stats,B_var_stats;
00065   for (int i=0;i<n_repeats;++i)
00066   {
00067     double B_mean,B_var;
00068     run_experiment(pdf,B_mean,B_var,n_samples);
00069     B_mean_stats.obs(B_mean);
00070     B_var_stats.obs(B_var);
00071   }
00072 
00073   vcl_cout<<"Overall statistics of B(stochastic):"<<vcl_endl;
00074   B_mean_stats.print_summary(vcl_cout);
00075   vcl_cout<<"\nAverage SD: "<<vcl_sqrt(B_var_stats.mean())<<vcl_endl;
00076 }
00077 #endif
00078 
00079 void run_multi_experiments(const pdf1d_pdf& pdf,
00080                            const pdf1d_builder& test_builder,
00081                            double& B_mean, double& B_var,
00082                            int n_samples, int n_repeats)
00083 {
00084   mbl_stats_1d B_mean_stats,B_var_stats;
00085   for (int i=0;i<n_repeats;++i)
00086   {
00087     double m,v;
00088     run_experiment(pdf,test_builder,m,v,n_samples);
00089     B_mean_stats.obs(m);
00090     B_var_stats.obs(v);
00091   }
00092 
00093   B_mean = B_mean_stats.mean();
00094   B_var  = B_var_stats.mean();
00095 }
00096 
00097 
00098 // Generate graph of Mean/SD B for increasing numbers of samples
00099 void graph_results(const pdf1d_pdf& pdf,
00100                    const pdf1d_builder& test_builder,
00101                    const vcl_string& path)
00102 {
00103   vcl_ofstream ofs(path.c_str(),vcl_ios::out);
00104   double B_mean,B_var;
00105 
00106   int n_repeats = 50;
00107 
00108   for (int i=1;i<20;++i)
00109   {
00110     int ns = i*10;
00111     run_multi_experiments(pdf,test_builder,B_mean,B_var,ns,n_repeats);
00112     ofs<<ns<<' '<<B_mean<<' '<<vcl_sqrt(B_var)<<vcl_endl;
00113   }
00114 
00115   ofs.close();
00116 
00117   vcl_cout<<"Results saved to "<<path<<vcl_endl;
00118 }
00119 
00120 int main()
00121 {
00122   pdf1d_gaussian gaussian(0,1);
00123   pdf1d_gaussian gaussian2(0,2);
00124   pdf1d_flat flat(0,1);
00125   pdf1d_gaussian_builder g_builder;
00126 
00127   // Generate samples with one pdf and test if it matches form defined by the builder
00128   graph_results(flat,g_builder,"B_vs_Nsamples.txt");
00129 #if 0
00130   vcl_cout<<"Testing distribution of Bhat. overlaps."<<vcl_endl;
00131 
00132   int n_samples = 10;
00133   int n_repeats = 20;
00134   run_experiment(n_samples,n_repeats);
00135 #endif
00136   return 0;
00137 }