Go to the documentation of this file.00001
00002 #include "pdf1d_bhat_overlap.h"
00003
00004
00005
00006
00007
00008 #include <pdf1d/pdf1d_sampler.h>
00009 #include <vcl_cmath.h>
00010 #include <vnl/vnl_vector.h>
00011 #include <vnl/vnl_math.h>
00012
00013
00014
00015
00016 double pdf1d_bhat_overlap(const pdf1d_pdf& pdf1, const pdf1d_pdf& pdf2,
00017 int n_samples, bool use_analytic)
00018 {
00019 if (use_analytic)
00020 {
00021
00022 if (pdf1.is_class("pdf1d_gaussian") && pdf2.is_class("pdf1d_gaussian"))
00023 return pdf1d_bhat_overlap_gaussians(pdf1,pdf2);
00024 }
00025
00026
00027
00028 vnl_vector<double> x(n_samples),p(n_samples);
00029
00030 pdf1d_sampler* sampler = pdf1.new_sampler();
00031 sampler->regular_samples_and_prob(x,p);
00032 delete sampler;
00033
00034 return pdf1d_bhat_overlap(pdf2,x.data_block(),p.data_block(),n_samples);
00035 }
00036
00037
00038
00039
00040
00041 double pdf1d_bhat_overlap(const pdf1d_pdf& pdf,
00042 const double* x,
00043 const double* p, int n)
00044 {
00045
00046 if (pdf.is_class("pdf1d_gaussian"))
00047 return pdf1d_bhat_overlap_gaussian(pdf.mean(),pdf.variance(),x,p,n);
00048
00049
00050 double sum = 0;
00051 for (int i=0;i<n;++i)
00052 {
00053 sum += vcl_sqrt( pdf(x[i])/p[i] );
00054 }
00055
00056 return sum/n;
00057 }
00058
00059
00060 double pdf1d_bhat_overlap_gaussian(double m, double v,
00061 const double* x,
00062 const double* p, int n)
00063 {
00064 double k = vnl_math::one_over_sqrt2pi/vcl_sqrt(v);
00065
00066 double sum = 0;
00067 for (int i=0;i<n;++i)
00068 {
00069 double dx=x[i]-m;
00070 double pgi = k*vcl_exp(-0.5*dx*dx/v);
00071 sum += vcl_sqrt(pgi/p[i]);
00072 }
00073 return sum/n;
00074 }
00075
00076
00077 double pdf1d_bhat_overlap_gaussians(double m1, double v1,
00078 double m2, double v2)
00079 {
00080 double dm = m1-m2;
00081 double k = vcl_sqrt(2*vcl_sqrt(v1*v2))/vcl_sqrt(v1+v2);
00082 return k * vcl_exp(-0.25*dm*dm/(v1+v2));
00083 }
00084
00085
00086 double pdf1d_bhat_overlap_gaussians(const pdf1d_pdf& g1, const pdf1d_pdf& g2)
00087 {
00088 return pdf1d_bhat_overlap_gaussians(g1.mean(),g1.variance(),g2.mean(),g2.variance());
00089 }
00090
00091
00092 double pdf1d_bhat_overlap_gaussian_with_pdf(double m, double v, const pdf1d_pdf& pdf, int n)
00093 {
00094 if (pdf.is_class("pdf1d_gaussian"))
00095 return pdf1d_bhat_overlap_gaussians(m,v,pdf.mean(),pdf.variance());
00096
00097 double k = vnl_math::one_over_sqrt2pi/vcl_sqrt(v);
00098 double sd = vcl_sqrt(v);
00099
00100
00101 double dx = 6.0/(n-1);
00102 double x = -0.5*dx*(n-1);
00103 double sum = 0.0;
00104 for (int i=0;i<n;++i)
00105 {
00106 double pgi = k*vcl_exp(-0.5*x*x);
00107 sum += vcl_sqrt(pgi*pdf(m+sd*x));
00108 x += dx;
00109 }
00110
00111
00112
00113 return sum*dx*sd;
00114 }
00115
00116
00117 double pdf1d_bhat_overlap_gaussian_with_pdf(const pdf1d_pdf& g, const pdf1d_pdf& pdf, int n)
00118 {
00119 return pdf1d_bhat_overlap_gaussian_with_pdf(g.mean(),g.variance(),pdf,n);
00120 }
00121