Main Page | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Class Members | File Members

vpdfl_prob_chi2.cxx

Go to the documentation of this file.
00001 // This is mul/vpdfl/vpdfl_prob_chi2.cxx
00002 #include "vpdfl_prob_chi2.h"
00003 //:
00004 // \file
00005 
00006 #include <vcl_iostream.h>
00007 #include <vcl_cstdlib.h> // for vcl_abort()
00008 #include <mbl/mbl_gamma.h>
00009 
00010 double vpdfl_chi2_for_cum_prob(double p, int n_dof, double tol)
00011 {
00012   if ((p<0) | (p>=1.0))
00013   {
00014     vcl_cerr<<"vpdfl_chi2_for_cum_prob : Illegal value for probability. (Outside range [0,1) )"<<vcl_endl;
00015     vcl_abort();
00016   }
00017 
00018   if (p==0) return 0;
00019 
00020   double d_step = n_dof; // prob = 50% ish
00021   double low_chi = 0;
00022   double high_chi = d_step;
00023 
00024   //double p_low = 0; // not used
00025   double p_high = vpdfl_cum_prob_chi2(n_dof,high_chi);
00026 
00027   // First step along till p_high >= p
00028   while (p_high<p)
00029   {
00030     low_chi = high_chi;
00031     // p_low = p_high; // not used
00032     high_chi += d_step;
00033     p_high = vpdfl_cum_prob_chi2(n_dof,high_chi);
00034   }
00035 
00036   // p_low and p_high now straddle answer
00037   double mid_chi = 0.5 * (low_chi+high_chi);
00038   double p_mid;
00039 
00040   while ((mid_chi-low_chi)>tol)
00041   {
00042     p_mid = vpdfl_cum_prob_chi2(n_dof,mid_chi);
00043     if (p_mid>p)
00044     {
00045       // Use low & mid as limits
00046       high_chi = mid_chi;
00047     }
00048     else
00049     {
00050       // Use mid and high as limits
00051       low_chi = mid_chi;
00052     }
00053 
00054     mid_chi = 0.5 * (low_chi+high_chi);
00055   }
00056 
00057   return mid_chi;
00058 }
00059 
00060 
00061 double vpdfl_cum_prob_chi2(int n_dof, double chi2)
00062 {
00063   return mbl_gamma_p((double) n_dof/2.0 , chi2/2.0 );
00064 }

Generated on Thu Jan 10 14:43:32 2008 for contrib/mul/vpdfl by  doxygen 1.4.4