Boost logo

Boost-Commit :

Subject: [Boost-commit] svn:boost r79769 - trunk/libs/math/test
From: pbristow_at_[hidden]
Date: 2012-07-27 11:29:45


Author: pbristow
Date: 2012-07-27 11:29:44 EDT (Fri, 27 Jul 2012)
New Revision: 79769
URL: http://svn.boost.org/trac/boost/changeset/79769

Log:
Revised checks on moments and return values.

Text files modified:
   trunk/libs/math/test/test_students_t.cpp | 216 ++++++++++++++++++++++++++++++++++++++-
   1 files changed, 206 insertions(+), 10 deletions(-)

Modified: trunk/libs/math/test/test_students_t.cpp
==============================================================================
--- trunk/libs/math/test/test_students_t.cpp (original)
+++ trunk/libs/math/test/test_students_t.cpp 2012-07-27 11:29:44 EDT (Fri, 27 Jul 2012)
@@ -14,6 +14,10 @@
 // Basic sanity test for Student's t probability (quantile) (0. < p < 1).
 // and Student's t probability Quantile (0. < p < 1).
 
+#ifdef _MSC_VER
+# pragma warning (disable :4127) // conditional expression is constant.
+#endif
+
 #include <boost/test/test_exec_monitor.hpp> // Boost.Test
 #include <boost/test/floating_point_comparison.hpp>
 
@@ -376,9 +380,10 @@
        mean(dist)
        , static_cast<RealType>(0), tol2);
     // variance:
- BOOST_CHECK_CLOSE(
- variance(dist)
- , static_cast<RealType>(8.0L / 6.0L), tol2);
+ // BOOST_CHECK_CLOSE(
+ // variance(dist)
+ // , static_cast<RealType>(13.0L / 6.0L), tol2);
+ //// was , static_cast<RealType>(8.0L / 6.0L), tol2);
     // std deviation:
     BOOST_CHECK_CLOSE(
        standard_deviation(dist)
@@ -467,31 +472,221 @@
          9);
 
 
+ // Checks added for Trac #7717 report by Thomas Mang
+ RealType inf = std::numeric_limits<RealType>::infinity();
+ RealType nan = std::numeric_limits<RealType>::quiet_NaN();
+
+//using boost::math::policies::policy;
+//// Types of error whose action can be altered by policies:.
+//using boost::math::policies::evaluation_error;
+//using boost::math::policies::domain_error;
+//using boost::math::policies::overflow_error;
+//using boost::math::policies::underflow_error;
+//using boost::math::policies::domain_error;
+//using boost::math::policies::pole_error;
+//
+//// Actions on error (in enum error_policy_type):
+//using boost::math::policies::errno_on_error;
+//using boost::math::policies::ignore_error;
+//using boost::math::policies::throw_on_error;
+//using boost::math::policies::denorm_error;
+//using boost::math::policies::pole_error;
+//using boost::math::policies::user_error;
+//
+//typedef policy<
+//domain_error<ignore_error>,
+//overflow_error<ignore_error>,
+//underflow_error<ignore_error>,
+//denorm_error<ignore_error>,
+//pole_error<ignore_error>,
+//evaluation_error<ignore_error>
+// > my_ignore_policy;
+//
+//// Define a new distribution with a custom policy to ignore_error
+//// (& thus perhaps return infinity or NaN for some arguments):
+//
+//using boost::math::students_t_distribution;
+//typedef students_t_distribution<long double, my_ignore_policy> c_students_t;
+//// typedef students_t_distribution<double, my_ignore_policy> c_students_t;
+//
+// cout << "mean(c_students_t(nan)) = " << mean(c_students_t(nan)) << endl;
+// cout << "variance(c_students_t(nan)) = " << variance(c_students_t(nan)) << endl;
+// cout << "skewness(c_students_t(nan)) = " << skewness(c_students_t(nan)) << endl;
+// cout << "kurtosis(c_students_t(nan)) = " << kurtosis(c_students_t(nan)) << endl;
+//
+//// All return zero (not NaN) for realconcept!!!
+
     BOOST_CHECK_THROW(quantile(dist, -1), std::domain_error);
     BOOST_CHECK_THROW(quantile(dist, 2), std::domain_error);
     BOOST_CHECK_THROW(pdf(students_t_distribution<RealType>(0), 0), std::domain_error);
     BOOST_CHECK_THROW(pdf(students_t_distribution<RealType>(-1), 0), std::domain_error);
 
- // Checks added for Trac #7717 report by Thomas Mang
- BOOST_CHECK_EQUAL(mean(students_t_distribution<RealType>(2)), 0);
- BOOST_CHECK_THROW(mean(students_t_distribution<RealType>(1)), std::domain_error);
+
+ // Check on df for mean (moment k = 1)
+ BOOST_CHECK_THROW(mean(students_t_distribution<RealType>(-1)), std::domain_error);
+ BOOST_CHECK_THROW(mean(students_t_distribution<RealType>(0)), std::domain_error);
+ BOOST_CHECK_THROW(mean(students_t_distribution<RealType>(1)), std::domain_error); // df == k
+ BOOST_CHECK_EQUAL(mean(students_t_distribution<RealType>(2)), 0); // OK.
 
+
+ // Check on df for variance (moment 2)
+ BOOST_CHECK_THROW(variance(students_t_distribution<RealType>(nan)), std::domain_error);
+ BOOST_CHECK_THROW(variance(students_t_distribution<RealType>(inf)), std::domain_error);
+ BOOST_CHECK_THROW(variance(students_t_distribution<RealType>(-1)), std::domain_error);
+ BOOST_CHECK_THROW(variance(students_t_distribution<RealType>(0)), std::domain_error);
     BOOST_CHECK_THROW(variance(students_t_distribution<RealType>(1)), std::domain_error);
- BOOST_CHECK_EQUAL(variance(students_t_distribution<RealType>(1.5)), std::numeric_limits<RealType>::infinity());
- BOOST_CHECK_EQUAL(variance(students_t_distribution<RealType>(2)), std::numeric_limits<RealType>::infinity());
+ BOOST_CHECK_THROW(variance(students_t_distribution<RealType>(static_cast<RealType>(1.99999L))), std::overflow_error);
+ BOOST_CHECK_THROW(variance(students_t_distribution<RealType>(static_cast<RealType>(1.99999L))), std::overflow_error);
+ BOOST_CHECK_THROW(variance(students_t_distribution<RealType>(2)), std::overflow_error); // df == k
+
+
+ //BOOST_CHECK_EQUAL(variance(c_students_t(static_cast<RealType>(2))), inf); //
+ //BOOST_CHECK(boost::math::isnan(variance(c_students_t(nan))));
+ //BOOST_CHECK(boost::math::isnan(variance(c_students_t(inf))));
+ //BOOST_CHECK(boost::math::isnan(variance(c_students_t(-1))));
+ //BOOST_CHECK(boost::math::isnan(variance(c_students_t(0))));
+ //BOOST_CHECK(boost::math::isnan(variance(c_students_t(1))));
+
+ BOOST_CHECK_EQUAL(variance(students_t_distribution<RealType>(2.5)), 5); // OK.
+ BOOST_CHECK_EQUAL(variance(students_t_distribution<RealType>(3)), 3); // OK.
+
+ // Check on df for skewness (moment 3)
+ BOOST_CHECK_THROW(skewness(students_t_distribution<RealType>(-1)), std::domain_error);
+ BOOST_CHECK_THROW(skewness(students_t_distribution<RealType>(0)), std::domain_error);
+ BOOST_CHECK_THROW(skewness(students_t_distribution<RealType>(1)), std::domain_error);
+ BOOST_CHECK_THROW(skewness(students_t_distribution<RealType>(1.5L)), std::domain_error);
+ BOOST_CHECK_THROW(skewness(students_t_distribution<RealType>(2)), std::domain_error);
+ BOOST_CHECK_THROW(skewness(students_t_distribution<RealType>(3)), std::domain_error); // df == k
+ BOOST_CHECK_EQUAL(skewness(students_t_distribution<RealType>(3.5)), 0); // OK.
+ BOOST_CHECK_EQUAL(skewness(students_t_distribution<RealType>(4)), 0); // OK.
+
+ // Check on df for kurtosis_excess (moment 4)
+ BOOST_CHECK_THROW(kurtosis_excess(students_t_distribution<RealType>(-1)), std::domain_error);
+ BOOST_CHECK_THROW(kurtosis_excess(students_t_distribution<RealType>(0)), std::domain_error);
+ BOOST_CHECK_THROW(kurtosis_excess(students_t_distribution<RealType>(1)), std::domain_error);
+ BOOST_CHECK_THROW(kurtosis_excess(students_t_distribution<RealType>(1.5L)), std::domain_error);
+ BOOST_CHECK_THROW(kurtosis_excess(students_t_distribution<RealType>(2)), std::domain_error);
+ BOOST_CHECK_THROW(kurtosis(students_t_distribution<RealType>(static_cast<RealType>(2.1))), std::overflow_error);
+ BOOST_CHECK_THROW(kurtosis_excess(students_t_distribution<RealType>(3)), std::overflow_error);
+ BOOST_CHECK_THROW(kurtosis_excess(students_t_distribution<RealType>(4)), std::overflow_error); // df == k
+ BOOST_CHECK_EQUAL(kurtosis_excess(students_t_distribution<RealType>(5)), 6); // OK.
+
+ // Check on df for kurtosis (moment 4)
+ BOOST_CHECK_THROW(kurtosis(students_t_distribution<RealType>(-1)), std::domain_error);
+ BOOST_CHECK_THROW(kurtosis(students_t_distribution<RealType>(0)), std::domain_error);
+ BOOST_CHECK_THROW(kurtosis(students_t_distribution<RealType>(1)), std::domain_error);
+ BOOST_CHECK_THROW(kurtosis(students_t_distribution<RealType>(2)), std::domain_error);
+ BOOST_CHECK_THROW(kurtosis(students_t_distribution<RealType>(static_cast<RealType>(2.0001L))), std::overflow_error);
+ BOOST_CHECK_THROW(kurtosis(students_t_distribution<RealType>(3)), std::overflow_error);
+ BOOST_CHECK_THROW(kurtosis(students_t_distribution<RealType>(4)), std::overflow_error); // df == k
+ BOOST_CHECK_EQUAL(kurtosis(students_t_distribution<RealType>(5)), 9); // OK.
+
+ check_ignore_error_returns<RealType>(0); // Use a new distribution c_students_t with a custom policy to ignore_errors,
+ // and check returned values are as expected.
 
     check_out_of_range<students_t_distribution<RealType> >(1);
 } // template <class RealType>void test_spots(RealType)
 
+template<class RealType>
+void check_ignore_error_returns(RealType)
+{ // Define a new distribution c_students_t with a custom policy to ignore_errors
+ // & thus be able to check that return infinity or NaN for some arguments:
+
+ // Currently these tests would fail with real_concept which returns zero instead of NaN.
+ if(!std::numeric_limits<RealType>::has_infinity)
+ {
+ return;
+ }
+ if(!std::numeric_limits<RealType>::has_quiet_NaN)
+ {
+ return;
+ }
+
+ using namespace boost::math;
+
+ using boost::math::policies::policy;
+ // Types of error whose action can be altered by policies:.
+ using boost::math::policies::evaluation_error;
+ using boost::math::policies::domain_error;
+ using boost::math::policies::overflow_error;
+ using boost::math::policies::underflow_error;
+ using boost::math::policies::domain_error;
+ using boost::math::policies::pole_error;
+
+ // Actions on error (in enum error_policy_type):
+ using boost::math::policies::errno_on_error;
+ using boost::math::policies::ignore_error;
+ using boost::math::policies::throw_on_error;
+ using boost::math::policies::denorm_error;
+ using boost::math::policies::pole_error;
+ using boost::math::policies::user_error;
+
+ typedef policy<
+ domain_error<ignore_error>,
+ overflow_error<ignore_error>,
+ underflow_error<ignore_error>,
+ denorm_error<ignore_error>,
+ pole_error<ignore_error>,
+ evaluation_error<ignore_error>
+ > my_ignore_policy;
+
+ long double inf = std::numeric_limits<long double>::infinity();
+ long double nan = std::numeric_limits<long double>::quiet_NaN();
+
+ using boost::math::students_t_distribution;
+
+ typedef students_t_distribution<long double, my_ignore_policy> c_students_t;
+ // typedef students_t_distribution<double, my_ignore_policy> c_students_t;
+
+ //cout << "mean(c_students_t(nan)) = " << mean(c_students_t(nan)) << endl;
+ //cout << "variance(c_students_t(nan)) = " << variance(c_students_t(nan)) << endl;
+ //cout << "skewness(c_students_t(nan)) = " << skewness(c_students_t(nan)) << endl;
+ //cout << "kurtosis(c_students_t(nan)) = " << kurtosis(c_students_t(nan)) << endl;
+
+ // Bad arguments - check on return from ignoring errors.
+ BOOST_CHECK((boost::math::isnan)(mean(c_students_t(nan))));
+ BOOST_CHECK(boost::math::isnan(mean(c_students_t(inf))));
+ BOOST_CHECK(boost::math::isnan(mean(c_students_t(-1))));
+ BOOST_CHECK(boost::math::isnan(mean(c_students_t(0))));
+ BOOST_CHECK(boost::math::isnan(mean(c_students_t(1))));
+
+ BOOST_CHECK(boost::math::isnan(variance(c_students_t(nan))));
+ BOOST_CHECK(boost::math::isnan(variance(c_students_t(inf))));
+ BOOST_CHECK(boost::math::isnan(variance(c_students_t(-1))));
+ BOOST_CHECK(boost::math::isnan(variance(c_students_t(0))));
+ BOOST_CHECK(boost::math::isnan(variance(c_students_t(1))));
+ BOOST_CHECK(boost::math::isinf(variance(c_students_t(1.7L))));
+ BOOST_CHECK(boost::math::isinf(variance(c_students_t(2))));
+ BOOST_CHECK(boost::math::isfinite(variance(c_students_t(2.0001L))));
+
+ BOOST_CHECK(boost::math::isnan(skewness(c_students_t(nan))));
+ BOOST_CHECK(boost::math::isnan(skewness(c_students_t(inf))));
+ BOOST_CHECK(boost::math::isnan(skewness(c_students_t(-1))));
+ BOOST_CHECK(boost::math::isnan(skewness(c_students_t(0))));
+ BOOST_CHECK(boost::math::isnan(skewness(c_students_t(1))));
+ BOOST_CHECK(boost::math::isnan(skewness(c_students_t(2))));
+ BOOST_CHECK(boost::math::isnan(skewness(c_students_t(3))));
+
+ BOOST_CHECK(boost::math::isnan(kurtosis(c_students_t(nan))));
+ BOOST_CHECK(boost::math::isnan(kurtosis(c_students_t(inf))));
+ BOOST_CHECK(boost::math::isnan(kurtosis(c_students_t(-1))));
+ BOOST_CHECK(boost::math::isnan(kurtosis(c_students_t(0))));
+ BOOST_CHECK(boost::math::isnan(kurtosis(c_students_t(1))));
+ BOOST_CHECK(boost::math::isnan(kurtosis(c_students_t(2))));
+ BOOST_CHECK(boost::math::isinf(kurtosis(c_students_t(2.0001L))));
+ BOOST_CHECK(boost::math::isinf(kurtosis(c_students_t(3))));
+ BOOST_CHECK(boost::math::isinf(kurtosis(c_students_t(4))));
+ BOOST_CHECK(boost::math::isfinite(kurtosis(c_students_t(4.0001L))));
+} // check_ignore_error_returns()
+
 int test_main(int, char* [])
 {
   // Check that can construct students_t distribution using the two convenience methods:
   using namespace boost::math;
   students_t myst1(2); // Using typedef
- students_t_distribution<> myst2(2); // Using default RealType double.
+ students_t_distribution<> myst2(2); // Using default RealType double.
    //students_t_distribution<double> myst3(2); // Using explicit RealType double.
 
-
 
     // Basic sanity-check spot values.
    // (Parameter value, arbitrarily zero, only communicates the floating point type).
@@ -509,6 +704,7 @@
       "to pass.</note>" << std::cout;
 #endif
 
+
    return 0;
 } // int test_main(int, char* [])
 


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