contrib/mul/pdf1d/tools/plot_pdf_estimate.cxx
Go to the documentation of this file.
00001 // This is mul/pdf1d/tools/plot_pdf_estimate.cxx
00002 
00003 //:
00004 // \file
00005 // \brief Plot kernel estimate to sampled data
00006 // Sample from one distribution, generate kernel estimate, generate text file for plotting.
00007 // \author Tim Cootes
00008 
00009 #include <vcl_iostream.h>
00010 #include <vnl/vnl_vector.h>
00011 #include <vnl/vnl_random.h>
00012 #include <pdf1d/pdf1d_flat.h>
00013 #include <pdf1d/pdf1d_exponential.h>
00014 #include <pdf1d/pdf1d_gaussian.h>
00015 #include <pdf1d/pdf1d_gaussian_builder.h>
00016 #include <pdf1d/pdf1d_epanech_kernel_pdf_builder.h>
00017 #include <pdf1d/pdf1d_gaussian_kernel_pdf_builder.h>
00018 
00019 vnl_random mz_random;
00020 
00021 //: Generate n samples from pdf, build a pdf and generate text file of resulting pdf
00022 void plot_estimate(const vcl_string& true_pdf_file, const vcl_string& new_pdf_file,
00023                    const pdf1d_pdf& pdf,
00024                    const pdf1d_builder& builder,
00025                    int n_samples, double min_x, double max_x, int nx)
00026 {
00027   vnl_vector<double> d(n_samples),b;
00028 
00029   // Generate n random samples from the pdf
00030   pdf.get_samples(d);
00031 
00032   vcl_cout<<"Range of values: ["<<d.min_value()<<','<<d.max_value()<<']'<<vcl_endl;
00033 
00034   pdf1d_pdf *new_pdf = builder.new_model();
00035 
00036   builder.build_from_array(*new_pdf,d.data_block(),n_samples);
00037 
00038   if (pdf.write_plot_file(true_pdf_file,min_x,max_x,nx))
00039     vcl_cout<<"Wrote original distribution points to "<<true_pdf_file<<vcl_endl;
00040   else
00041     vcl_cout<<"Failed to write distribution points to "<<true_pdf_file<<vcl_endl;
00042 
00043   if (new_pdf->write_plot_file(new_pdf_file,min_x,max_x,nx))
00044     vcl_cout<<"Wrote distribution points to "<<new_pdf_file<<vcl_endl;
00045   else
00046     vcl_cout<<"Failed to write distribution points to "<<new_pdf_file<<vcl_endl;
00047 
00048   delete new_pdf;
00049 }
00050 
00051 int main()
00052 {
00053   pdf1d_exponential exp_pdf(1);
00054   pdf1d_gaussian gaussian(0,1);
00055   pdf1d_gaussian gaussian2(0,2);
00056   pdf1d_flat flat(0,1);
00057   pdf1d_gaussian_builder g_builder;
00058   pdf1d_gaussian_kernel_pdf_builder gk_builder;
00059   gk_builder.set_use_width_from_separation();
00060   pdf1d_epanech_kernel_pdf_builder ek_builder;
00061 //  ek_builder.set_use_width_from_separation();
00062   ek_builder.set_use_adaptive();
00063 
00064   int n_samples = 100;
00065 
00066   plot_estimate("true_pdf.txt","kernel.txt",flat,ek_builder,n_samples,-3,3,200);
00067 
00068   return 0;
00069 }