Boost logo

Boost-Commit :

Subject: [Boost-commit] svn:boost r50723 - in sandbox/math_toolkit: boost/math/distributions/detail libs/math/test
From: john_at_[hidden]
Date: 2009-01-22 05:01:55


Author: johnmaddock
Date: 2009-01-22 05:01:54 EST (Thu, 22 Jan 2009)
New Revision: 50723
URL: http://svn.boost.org/trac/boost/changeset/50723

Log:
Fix remaining quantile tests with the hypergeometric.
Text files modified:
   sandbox/math_toolkit/boost/math/distributions/detail/hypergeometric_quantile.hpp | 57 +++++++++---------
   sandbox/math_toolkit/libs/math/test/Jamfile.v2 | 37 +++++++++++
   sandbox/math_toolkit/libs/math/test/test_hypergeometric_dist.cpp | 121 ++++++++++++++++++++++++++++++++++++---
   3 files changed, 175 insertions(+), 40 deletions(-)

Modified: sandbox/math_toolkit/boost/math/distributions/detail/hypergeometric_quantile.hpp
==============================================================================
--- sandbox/math_toolkit/boost/math/distributions/detail/hypergeometric_quantile.hpp (original)
+++ sandbox/math_toolkit/boost/math/distributions/detail/hypergeometric_quantile.hpp 2009-01-22 05:01:54 EST (Thu, 22 Jan 2009)
@@ -14,77 +14,77 @@
 namespace boost{ namespace math{ namespace detail{
 
 template <class T>
-inline unsigned round_x_from_p(unsigned x, T p, T cum, T /*fudge_factor*/, const policies::discrete_quantile<policies::integer_round_down>&)
+inline unsigned round_x_from_p(unsigned x, T p, T cum, T fudge_factor, unsigned lbound, unsigned /*ubound*/, const policies::discrete_quantile<policies::integer_round_down>&)
 {
- if(p < cum)
+ if((p < cum * fudge_factor) && (x != lbound))
       return --x;
    return x;
 }
 
 template <class T>
-inline unsigned round_x_from_p(unsigned x, T p, T cum, T /*fudge_factor*/, const policies::discrete_quantile<policies::integer_round_up>&)
+inline unsigned round_x_from_p(unsigned x, T p, T cum, T fudge_factor, unsigned /*lbound*/, unsigned ubound, const policies::discrete_quantile<policies::integer_round_up>&)
 {
- if(cum < p)
+ if((cum < p * fudge_factor) && (x != ubound))
       return ++x;
    return x;
 }
 
 template <class T>
-inline unsigned round_x_from_p(unsigned x, T p, T cum, T fudge_factor, const policies::discrete_quantile<policies::integer_round_inwards>&)
+inline unsigned round_x_from_p(unsigned x, T p, T cum, T fudge_factor, unsigned lbound, unsigned ubound, const policies::discrete_quantile<policies::integer_round_inwards>&)
 {
    if(p >= 0.5)
- return round_x_from_p(x, p, cum, fudge_factor, policies::discrete_quantile<policies::integer_round_down>());
- return round_x_from_p(x, p, cum, fudge_factor, policies::discrete_quantile<policies::integer_round_up>());
+ return round_x_from_p(x, p, cum, fudge_factor, lbound, ubound, policies::discrete_quantile<policies::integer_round_down>());
+ return round_x_from_p(x, p, cum, fudge_factor, lbound, ubound, policies::discrete_quantile<policies::integer_round_up>());
 }
 
 template <class T>
-inline unsigned round_x_from_p(unsigned x, T p, T cum, T fudge_factor, const policies::discrete_quantile<policies::integer_round_outwards>&)
+inline unsigned round_x_from_p(unsigned x, T p, T cum, T fudge_factor, unsigned lbound, unsigned ubound, const policies::discrete_quantile<policies::integer_round_outwards>&)
 {
    if(p >= 0.5)
- return round_x_from_p(x, p, cum, fudge_factor, policies::discrete_quantile<policies::integer_round_up>());
- return round_x_from_p(x, p, cum, fudge_factor, policies::discrete_quantile<policies::integer_round_down>());
+ return round_x_from_p(x, p, cum, fudge_factor, lbound, ubound, policies::discrete_quantile<policies::integer_round_up>());
+ return round_x_from_p(x, p, cum, fudge_factor, lbound, ubound, policies::discrete_quantile<policies::integer_round_down>());
 }
 
 template <class T>
-inline unsigned round_x_from_p(unsigned x, T /*p*/, T /*cum*/, T /*fudge_factor*/, const policies::discrete_quantile<policies::integer_round_nearest>&)
+inline unsigned round_x_from_p(unsigned x, T /*p*/, T /*cum*/, T /*fudge_factor*/, unsigned /*lbound*/, unsigned /*ubound*/, const policies::discrete_quantile<policies::integer_round_nearest>&)
 {
    return x;
 }
 
 template <class T>
-inline unsigned round_x_from_q(unsigned x, T q, T cum, T /*fudge_factor*/, const policies::discrete_quantile<policies::integer_round_down>&)
+inline unsigned round_x_from_q(unsigned x, T q, T cum, T fudge_factor, unsigned lbound, unsigned /*ubound*/, const policies::discrete_quantile<policies::integer_round_down>&)
 {
- if(q > cum)
+ if((q * fudge_factor > cum) && (x != lbound))
       return --x;
    return x;
 }
 
 template <class T>
-inline unsigned round_x_from_q(unsigned x, T q, T cum, T /*fudge_factor*/, const policies::discrete_quantile<policies::integer_round_up>&)
+inline unsigned round_x_from_q(unsigned x, T q, T cum, T fudge_factor, unsigned /*lbound*/, unsigned ubound, const policies::discrete_quantile<policies::integer_round_up>&)
 {
- if(q < cum)
- return --x;
+ if((q < cum * fudge_factor) && (x != ubound))
+ return ++x;
    return x;
 }
 
 template <class T>
-inline unsigned round_x_from_q(unsigned x, T q, T cum, T fudge_factor, const policies::discrete_quantile<policies::integer_round_inwards>&)
+inline unsigned round_x_from_q(unsigned x, T q, T cum, T fudge_factor, unsigned lbound, unsigned ubound, const policies::discrete_quantile<policies::integer_round_inwards>&)
 {
    if(q < 0.5)
- return round_x_from_q(x, q, cum, fudge_factor, policies::discrete_quantile<policies::integer_round_down>());
- return round_x_from_q(x, q, cum, fudge_factor, policies::discrete_quantile<policies::integer_round_up>());
+ return round_x_from_q(x, q, cum, fudge_factor, lbound, ubound, policies::discrete_quantile<policies::integer_round_down>());
+ return round_x_from_q(x, q, cum, fudge_factor, lbound, ubound, policies::discrete_quantile<policies::integer_round_up>());
 }
 
 template <class T>
-inline unsigned round_x_from_q(unsigned x, T q, T cum, T fudge_factor, const policies::discrete_quantile<policies::integer_round_outwards>&)
+inline unsigned round_x_from_q(unsigned x, T q, T cum, T fudge_factor, unsigned lbound, unsigned ubound, const policies::discrete_quantile<policies::integer_round_outwards>&)
 {
    if(q >= 0.5)
- return round_x_from_q(x, q, cum, fudge_factor, policies::discrete_quantile<policies::integer_round_down>());
- return round_x_from_q(x, q, cum, fudge_factor, policies::discrete_quantile<policies::integer_round_up>());
+ return round_x_from_q(x, q, cum, fudge_factor, lbound, ubound, policies::discrete_quantile<policies::integer_round_down>());
+ return round_x_from_q(x, q, cum, fudge_factor, lbound, ubound, policies::discrete_quantile<policies::integer_round_up>());
 }
 
 template <class T>
-inline unsigned round_x_from_q(unsigned x, T /*q*/, T /*cum*/, T /*fudge_factor*/, const policies::discrete_quantile<policies::integer_round_nearest>&)
+inline unsigned round_x_from_q(unsigned x, T /*q*/, T /*cum*/, T /*fudge_factor*/, unsigned /*lbound*/, unsigned /*ubound*/, const policies::discrete_quantile<policies::integer_round_nearest>&)
 {
    return x;
 }
@@ -99,10 +99,12 @@
    typedef typename Policy::discrete_quantile_type discrete_quantile_type;
    BOOST_MATH_STD_USING
    T result;
- T fudge_factor = 1 + tools::epsilon<T>() * 50;
+ T fudge_factor = 1 + tools::epsilon<T>() * ((N <= boost::math::prime(boost::math::max_prime - 1)) ? 50 : N);
+ unsigned base = static_cast<unsigned>((std::max)(0, (int)(n + r) - (int)(N)));
+ unsigned lim = (std::min)(r, n);
+
    if(p <= 0.5)
    {
- unsigned base = static_cast<unsigned>((std::max)(0, (int)(n + r) - (int)(N)));
       unsigned x = base;
       result = hypergeometric_pdf<T>(x, r, n, N, pol);
       T diff = result;
@@ -116,11 +118,10 @@
          ++x;
          result += diff;
       }
- return x == base ? x : round_x_from_p(x, p, result, fudge_factor, discrete_quantile_type());
+ return round_x_from_p(x, p, result, fudge_factor, base, lim, discrete_quantile_type());
    }
    else
    {
- unsigned lim = (std::min)(r, n);
       unsigned x = lim;
       result = 0;
       T diff = hypergeometric_pdf<T>(x, r, n, N, pol);
@@ -132,7 +133,7 @@
             : hypergeometric_pdf<T>(x - 1, r, n, N, pol);
          --x;
       }
- return x == lim ? x : round_x_from_q(x, q, result, fudge_factor, discrete_quantile_type());
+ return round_x_from_q(x, q, result, fudge_factor, base, lim, discrete_quantile_type());
    }
 #ifdef BOOST_MSVC
 # pragma warning(pop)

Modified: sandbox/math_toolkit/libs/math/test/Jamfile.v2
==============================================================================
--- sandbox/math_toolkit/libs/math/test/Jamfile.v2 (original)
+++ sandbox/math_toolkit/libs/math/test/Jamfile.v2 2009-01-22 05:01:54 EST (Thu, 22 Jan 2009)
@@ -106,7 +106,42 @@
 run test_gamma.cpp pch ;
 run test_gamma_dist.cpp pch ;
 run test_hermite.cpp pch ;
-run test_hypergeometric_dist.cpp ;
+run test_hypergeometric_dist.cpp
+ : # command line
+ : # input files
+ : # requirements
+ <define>TEST_QUANT=0
+ : test_hypergeometric_dist0 ;
+run test_hypergeometric_dist.cpp
+ : # command line
+ : # input files
+ : # requirements
+ <define>TEST_QUANT=1
+ : test_hypergeometric_dist1 ;
+run test_hypergeometric_dist.cpp
+ : # command line
+ : # input files
+ : # requirements
+ <define>TEST_QUANT=2
+ : test_hypergeometric_dist2 ;
+run test_hypergeometric_dist.cpp
+ : # command line
+ : # input files
+ : # requirements
+ <define>TEST_QUANT=3
+ : test_hypergeometric_dist3 ;
+run test_hypergeometric_dist.cpp
+ : # command line
+ : # input files
+ : # requirements
+ <define>TEST_QUANT=4
+ : test_hypergeometric_dist4 ;
+run test_hypergeometric_dist.cpp
+ : # command line
+ : # input files
+ : # requirements
+ <define>TEST_QUANT=5
+ : test_hypergeometric_dist5 ;
 run test_ibeta.cpp pch
         : # command line
         : # input files

Modified: sandbox/math_toolkit/libs/math/test/test_hypergeometric_dist.cpp
==============================================================================
--- sandbox/math_toolkit/libs/math/test/test_hypergeometric_dist.cpp (original)
+++ sandbox/math_toolkit/libs/math/test/test_hypergeometric_dist.cpp 2009-01-22 05:01:54 EST (Thu, 22 Jan 2009)
@@ -36,7 +36,7 @@
       }\
    }
 
- void expected_results()
+void expected_results()
 {
    //
    // Define the max and mean errors expected for
@@ -135,6 +135,12 @@
 template <class T>
 void do_test_hypergeometric(const T& data, const char* type_name, const char* test_name)
 {
+ // warning suppression:
+ (void)data;
+ (void)type_name;
+ (void)test_name;
+
+#if !defined(TEST_QUANT) || (TEST_QUANT == 0)
    typedef typename T::value_type row_type;
    typedef typename row_type::value_type value_type;
 
@@ -189,6 +195,7 @@
       extract_result(6));
    handle_test_result(result, data[result.worst()], result.worst(), type_name, "hypergeometric CDF complement", test_name);
    std::cout << std::endl;
+#endif
 }
 
 template <class T>
@@ -212,18 +219,42 @@
          unsigned x = make_unsigned(data[i][3]);
          value_type cp = data[i][5];
          value_type ccp = data[i][6];
-#if 0
- boost::math::hypergeometric_distribution<RealType,
- policy<discrete_quantile<integer_round_up> > > du(r, n, N);
+ //
+ // A bit of warning supression:
+ //
+ (void)x;
+ (void)n;
+ (void)r;
+ (void)N;
+ (void)cp;
+ (void)ccp;
 
- BOOST_CHECK_EX(quantile(du, cp) >= x);
- BOOST_CHECK_EX(quantile(complement(du, ccp)) >= x);
+#if !defined(TEST_QUANT) || (TEST_QUANT == 1)
+ boost::math::hypergeometric_distribution<value_type,
+ policy<discrete_quantile<integer_round_up> > > du(r, n, N);
 
- boost::math::hypergeometric_distribution<RealType,
+ if((cp < 0.9) && (cp > boost::math::tools::min_value<value_type>()))
+ {
+ BOOST_CHECK_EX(quantile(du, cp) >= x);
+ }
+ if((ccp < 0.9) && (ccp > boost::math::tools::min_value<value_type>()))
+ {
+ BOOST_CHECK_EX(quantile(complement(du, ccp)) >= x);
+ }
+#endif
+#if !defined(TEST_QUANT) || (TEST_QUANT == 2)
+ boost::math::hypergeometric_distribution<value_type,
             policy<discrete_quantile<integer_round_down> > > dl(r, n, N);
- BOOST_CHECK_EX(quantile(dl, cp) <= x);
- BOOST_CHECK_EX(quantile(complement(dl, ccp)) <= x);
+ if((cp < 0.9) && (cp > boost::math::tools::min_value<value_type>()))
+ {
+ BOOST_CHECK_EX(quantile(dl, cp) <= x);
+ }
+ if((ccp < 0.9) && (ccp > boost::math::tools::min_value<value_type>()))
+ {
+ BOOST_CHECK_EX(quantile(complement(dl, ccp)) <= x);
+ }
 #endif
+#if !defined(TEST_QUANT) || (TEST_QUANT == 3)
          boost::math::hypergeometric_distribution<value_type,
             policy<discrete_quantile<integer_round_nearest> > > dn(r, n, N);
 
@@ -235,6 +266,61 @@
          {
             BOOST_CHECK_EX(quantile(complement(dn, ccp)) == x);
          }
+#endif
+#if !defined(TEST_QUANT) || (TEST_QUANT == 4)
+ boost::math::hypergeometric_distribution<value_type,
+ policy<discrete_quantile<integer_round_outwards> > > dou(r, n, N);
+
+ if((cp < 0.9) && (cp > boost::math::tools::min_value<value_type>()))
+ {
+ if(cp < 0.5)
+ {
+ BOOST_CHECK_EX(quantile(dou, cp) <= x);
+ }
+ else
+ {
+ BOOST_CHECK_EX(quantile(dou, cp) >= x);
+ }
+ }
+ if((ccp < 0.9) && (ccp > boost::math::tools::min_value<value_type>()))
+ {
+ if(ccp < 0.5)
+ {
+ BOOST_CHECK_EX(quantile(complement(dou, ccp)) >= x);
+ }
+ else
+ {
+ BOOST_CHECK_EX(quantile(complement(dou, ccp)) <= x);
+ }
+ }
+#endif
+#if !defined(TEST_QUANT) || (TEST_QUANT == 5)
+ boost::math::hypergeometric_distribution<value_type,
+ policy<discrete_quantile<integer_round_inwards> > > di(r, n, N);
+
+ if((cp < 0.9) && (cp > boost::math::tools::min_value<value_type>()))
+ {
+ if(cp < 0.5)
+ {
+ BOOST_CHECK_EX(quantile(di, cp) >= x);
+ }
+ else
+ {
+ BOOST_CHECK_EX(quantile(di, cp) <= x);
+ }
+ }
+ if((ccp < 0.9) && (ccp > boost::math::tools::min_value<value_type>()))
+ {
+ if(ccp < 0.5)
+ {
+ BOOST_CHECK_EX(quantile(complement(di, ccp)) <= x);
+ }
+ else
+ {
+ BOOST_CHECK_EX(quantile(complement(di, ccp)) >= x);
+ }
+ }
+#endif
       }
    }
 }
@@ -244,6 +330,18 @@
 void test_spot(unsigned x, unsigned n, unsigned r, unsigned N,
                RealType p, RealType cp, RealType ccp, RealType tol)
 {
+ //
+ // A bit of warning supression:
+ //
+ (void)x;
+ (void)n;
+ (void)r;
+ (void)N;
+ (void)p;
+ (void)cp;
+ (void)ccp;
+ (void)tol;
+#if !defined(TEST_QUANT) || (TEST_QUANT == 0)
    boost::math::hypergeometric_distribution<RealType> d(r, n, N);
 
    std::pair<unsigned, unsigned> extent = range(d);
@@ -263,7 +361,7 @@
    if(boost::math::tools::digits<RealType>() > 50)
    {
       using namespace boost::math::policies;
-#if 0
+
       boost::math::hypergeometric_distribution<RealType,
          policy<discrete_quantile<integer_round_up> > > du(r, n, N);
 
@@ -274,7 +372,7 @@
          policy<discrete_quantile<integer_round_down> > > dl(r, n, N);
       BOOST_CHECK_EX(quantile(dl, cp) <= x);
       BOOST_CHECK_EX(quantile(complement(dl, ccp)) <= x);
-#endif
+
       boost::math::hypergeometric_distribution<RealType,
          policy<discrete_quantile<integer_round_nearest> > > dn(r, n, N);
 
@@ -306,6 +404,7 @@
    BOOST_CHECK_EQUAL(quantile(complement(d, 1)), extent.first);
    BOOST_CHECK_EQUAL(cdf(d, extent.second), 1);
    BOOST_CHECK_EQUAL(cdf(complement(d, extent.second)), 0);
+#endif
 }
 
 template <class RealType>


Boost-Commit list run by bdawes at acm.org, david.abrahams at rcn.com, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk