2
0
mirror of https://github.com/boostorg/math.git synced 2026-01-19 04:22:09 +00:00

Added median tests

[SVN r3540]
This commit is contained in:
Paul A. Bristow
2006-12-20 15:33:43 +00:00
parent 570d5bdf5f
commit 2f47640e6c
13 changed files with 176 additions and 32 deletions

View File

@@ -640,16 +640,19 @@ void test_spots(RealType T)
static_cast<RealType>(-3), // probability.
tolerance); // %
//
// Things that are errors:
//
cauchy_distribution<RealType> dist;
BOOST_CHECK_THROW(
mean(dist),
std::domain_error);
cauchy_distribution<RealType> dist; // default (0, 1)
BOOST_CHECK_EQUAL(
mode(dist),
static_cast<RealType>(0));
BOOST_CHECK_EQUAL(
median(dist),
static_cast<RealType>(0));
//
// Things that are errors:
//
BOOST_CHECK_THROW(
mean(dist),
std::domain_error);
BOOST_CHECK_THROW(
variance(dist),
std::domain_error);

View File

@@ -404,6 +404,12 @@ void test_spots(RealType)
BOOST_CHECK_CLOSE(
mode(dist)
, static_cast<RealType>(6), tol2);
BOOST_CHECK_CLOSE(
median(dist),
quantile(
chi_squared_distribution<RealType>(static_cast<RealType>(8)),
static_cast<RealType>(0.5)), static_cast<RealType>(1)); // 1% - approximate.
// skewness:
BOOST_CHECK_CLOSE(
skewness(dist)

View File

@@ -208,6 +208,13 @@ void test_spots(RealType T)
exponential_distribution<RealType>(2)),
static_cast<RealType>(0),
tolerance); // %
BOOST_CHECK_CLOSE(
::boost::math::median(
exponential_distribution<RealType>(4)),
static_cast<RealType>(0.693147180559945309417232121458176568075500134360255254) / 4,
tolerance); // %
BOOST_CHECK_CLOSE(
::boost::math::skewness(
exponential_distribution<RealType>(2)),

View File

@@ -5,7 +5,7 @@
// (See accompanying file LICENSE_1_0.txt
// or copy at http://www.boost.org/LICENSE_1_0.txt)
// test_students_t.cpp
// test_extreme_value.cpp
#define BOOST_MATH_THROW_ON_DOMAIN_ERROR
#define BOOST_MATH_THROW_ON_OVERFLOW_ERROR
@@ -27,7 +27,6 @@
#include <boost/test/included/test_exec_monitor.hpp> // Boost.Test
#include <boost/test/floating_point_comparison.hpp>
#include <iostream>
using std::cout;
using std::endl;
@@ -146,6 +145,12 @@ void test_spots(RealType T)
extreme_value_distribution<RealType>(2, 3)),
static_cast<RealType>(2),
tolerance); // %
BOOST_CHECK_CLOSE(
::boost::math::median(
extreme_value_distribution<RealType>(0, 1)),
static_cast<RealType>(+0.36651292058166432701243915823266946945426344783710526305367771367056),
tolerance); // %
BOOST_CHECK_CLOSE(
::boost::math::skewness(
extreme_value_distribution<RealType>(2, 3)),

View File

@@ -204,7 +204,6 @@ void test_spots(RealType)
// pretty useless, but it is an excellent sanity check.
RealType tolerance = 0.002f * 100;
cout << "Tolerance = " << tolerance << "%." << endl;
using boost::math::fisher_f_distribution;
@@ -370,8 +369,7 @@ void test_spots(RealType)
// These might allow some further cross checks?
RealType tol2 = boost::math::tools::epsilon<RealType>() * 5 * 100; // 5eps as a percent
RealType tol2 = boost::math::tools::epsilon<RealType>() * 5 * 100; // 5 eps as a percent
cout << "Tolerance = " << tol2 << "%." << endl;
fisher_f_distribution<RealType> dist(static_cast<RealType>(8), static_cast<RealType>(6));
RealType x = 7;
@@ -510,6 +508,10 @@ void test_spots(RealType)
fisher_f_distribution<RealType>(8, 8),
static_cast<RealType>(1.1))), std::domain_error
);
// median NOT implemented.
BOOST_CHECK_THROW(
median(fisher_f_distribution<RealType>(8, 8)), std::domain_error);
} // template <class RealType>void test_spots(RealType)
int test_main(int, char* [])

View File

@@ -221,6 +221,10 @@ void test_spots(RealType T)
kurtosis_excess(dist)
, 6 / static_cast<RealType>(8), tol2);
BOOST_CHECK_CLOSE(
median(dist), static_cast<RealType>(23.007748327502412), tol2);
// Rely on default definition in derived accessors.
} // template <class RealType>void test_spots(RealType)
int test_main(int, char* [])
@@ -245,3 +249,20 @@ int test_main(int, char* [])
} // int test_main(int, char* [])
/*
Output:
Running 1 test case...
Tolerance for type float is 0.000238419 %
Tolerance for type float is 0.001 %
Tolerance for type double is 5e-012 %
Tolerance for type double is 0.001 %
Tolerance for type long double is 5e-012 %
Tolerance for type long double is 0.001 %
Tolerance for type class boost::math::concepts::real_concept is 5e-012 %
Tolerance for type class boost::math::concepts::real_concept is 0.001 %
*** No errors detected
*/

View File

@@ -220,6 +220,15 @@ void test_spots(RealType T)
BOOST_CHECK_CLOSE(
mode(dist)
, static_cast<RealType>(0.36787944117144232159552377016146L), tolerance);
BOOST_CHECK_CLOSE(
median(dist)
, static_cast<RealType>(exp(dist.location())), tolerance);
BOOST_CHECK_CLOSE(
median(dist),
quantile(dist, static_cast<RealType>(0.5)), tolerance);
// skewness:
BOOST_CHECK_CLOSE(
skewness(dist)

View File

@@ -716,6 +716,12 @@ if(std::numeric_limits<RealType>::is_specialized)
static_cast<RealType>(0)), std::domain_error
);
// End of check throwing 'duff' out-of-domain values.
BOOST_CHECK_THROW(
median( // NOT implemented.
negative_binomial_distribution<RealType>(static_cast<RealType>(8), static_cast<RealType>(1.25))),
std::domain_error);
return;
} // template <class RealType> void test_spots(RealType) // Any floating-point type RealType.
@@ -734,14 +740,14 @@ int test_main(int, char* [])
#endif
#ifdef BOOST_MATH_THROW_ON_OVERFLOW_ERROR
cout << "BOOST_MATH_THROW_ON_OVERFLOW_ERROR" << " is defined to throw on domain error." << endl;
cout << "BOOST_MATH_THROW_ON_OVERFLOW_ERROR" << " is defined to throw on overflow error." << endl;
#else
cout << "BOOST_MATH_THROW_ON_OVERFLOW_ERROR" << " is NOT defined, so NO throw on domain error." << endl;
cout << "BOOST_MATH_THROW_ON_OVERFLOW_ERROR" << " is NOT defined, so NO throw on overflow error." << endl;
#endif
#ifdef BOOST_MATH_THROW_ON_UNDERFLOW_ERROR
cout << "BOOST_MATH_THROW_ON_UNDERFLOW_ERROR" << " is defined to throw on domain error." << endl;
cout << "BOOST_MATH_THROW_ON_UNDERFLOW_ERROR" << " is defined to throw on underflow error." << endl;
#else
cout << "BOOST_MATH_THROW_ON_UNDERFLOW_ERROR" << " is NOT defined, so NO throw on domain error." << endl;
cout << "BOOST_MATH_THROW_ON_UNDERFLOW_ERROR" << " is NOT defined, so NO throw on underflow error." << endl;
#endif
// Test some simple double only examples.
negative_binomial_distribution<double> my8dist(8., 0.25);
@@ -765,15 +771,14 @@ int test_main(int, char* [])
/*
------ Build started: Project: test_negative_binomial, Configuration: Debug Win32 ------
Compiling...
test_negative_binomial.cpp
Linking...
Autorun "i:\boost-06-05-03-1300\libs\math\test\Math_test\debug\test_negative_binomial.exe"
Running 1 test case...
BOOST_MATH_THROW_ON_DOMAIN_ERROR is defined to throw on domain error.
BOOST_MATH_THROW_ON_OVERFLOW_ERROR is NOT defined, so NO throw on domain error.
BOOST_MATH_THROW_ON_UNDERFLOW_ERROR is NOT defined, so NO throw on domain error.
BOOST_MATH_THROW_ON_OVERFLOW_ERROR is NOT defined, so NO throw on overflow error.
BOOST_MATH_THROW_ON_UNDERFLOW_ERROR is NOT defined, so NO throw on underflow error.
Tolerance = 0.0119209%.
Tolerance 5 eps = 5.96046e-007%.
Tolerance = 2.22045e-011%.
@@ -783,8 +788,5 @@ Tolerance 5 eps = 1.11022e-015%.
Tolerance = 2.22045e-011%.
Tolerance 5 eps = 1.11022e-015%.
*** No errors detected
Build Time 0:08
Build log was saved at "file://i:\boost-06-05-03-1300\libs\math\test\Math_test\test_negative_binomial\Debug\BuildLog.htm"
test_negative_binomial - 0 error(s), 0 warning(s)
*/

View File

@@ -223,6 +223,11 @@ void test_spots(RealType T)
BOOST_CHECK_CLOSE(
mode(dist)
, static_cast<RealType>(8), tol2);
BOOST_CHECK_CLOSE(
median(dist)
, static_cast<RealType>(8), tol2);
// skewness:
BOOST_CHECK_CLOSE(
skewness(dist)

View File

@@ -143,6 +143,23 @@ void test_spots(RealType)
std::domain_error);
// Check some test values.
BOOST_CHECK_CLOSE( // mode
mode(poisson_distribution<RealType>(static_cast<RealType>(4))), // mode = mean = 4.
static_cast<RealType>(4), // mode.
tolerance);
//BOOST_CHECK_CLOSE( // mode
// median(poisson_distribution<RealType>(static_cast<RealType>(4))), // mode = mean = 4.
// static_cast<RealType>(4), // mode.
// tolerance);
poisson_distribution<RealType> dist4(static_cast<RealType>(40));
BOOST_CHECK_CLOSE( // median
median(dist4), // mode = mean = 4. median = 40.328333333333333
quantile(dist4, static_cast<RealType>(0.5)), // 39.332839138842637
tolerance);
// PDF
BOOST_CHECK_CLOSE(
pdf(poisson_distribution<RealType>(static_cast<RealType>(4)), // mean 4.
@@ -319,7 +336,7 @@ void test_spots(RealType)
tolerance/5); //
// EQUAL is too optimistic - fails [5.0000000000000124 != 5]
//BOOST_CHECK_EQUAL(boost::math::quantile( //
// BOOST_CHECK_EQUAL(boost::math::quantile( //
// poisson_distribution<RealType>(5.), // mean.
// static_cast<RealType>(0.615960654833065)), // probability.
// static_cast<RealType>(5.)); // Expect k = 5 events.
@@ -330,6 +347,55 @@ void test_spots(RealType)
static_cast<RealType>(5.), // Expect k = 5 events.
tolerance/5);
// Check on quantile of other examples of inverse of cdf.
BOOST_CHECK_CLOSE(
cdf(poisson_distribution<RealType>(static_cast<RealType>(10.)), // mean
static_cast<RealType>(10)), // k events.
static_cast<RealType>(0.5830397501929856), // probability.
tolerance);
BOOST_CHECK_CLOSE(boost::math::quantile( // inverse of cdf above.
poisson_distribution<RealType>(10.), // mean.
static_cast<RealType>(0.5830397501929856)), // probability.
static_cast<RealType>(10.), // Expect k = 10 events.
tolerance/5);
BOOST_CHECK_CLOSE(
cdf(poisson_distribution<RealType>(static_cast<RealType>(4.)), // mean
static_cast<RealType>(5)), // k events.
static_cast<RealType>(0.785130387030406), // probability.
tolerance);
BOOST_CHECK_CLOSE(boost::math::quantile( // inverse of cdf above.
poisson_distribution<RealType>(4.), // mean.
static_cast<RealType>(0.785130387030406)), // probability.
static_cast<RealType>(5.), // Expect k = 10 events.
tolerance/5);
//BOOST_CHECK_CLOSE(boost::math::quantile(
// poisson_distribution<RealType>(5), // mean.
// static_cast<RealType>(0.785130387030406)), // probability.
// // 6.1882832344329559 result but MathCAD givest smallest integer ppois(k, mean) >= prob
// static_cast<RealType>(6.), // Expect k = 6 events.
// tolerance/5);
//BOOST_CHECK_CLOSE(boost::math::quantile(
// poisson_distribution<RealType>(5), // mean.
// static_cast<RealType>(0.77)), // probability.
// // 6.1882832344329559 result but MathCAD givest smallest integer ppois(k, mean) >= prob
// static_cast<RealType>(7.), // Expect k = 6 events.
// tolerance/5);
//BOOST_CHECK_CLOSE(boost::math::quantile(
// poisson_distribution<RealType>(5), // mean.
// static_cast<RealType>(0.75)), // probability.
// // 6.1882832344329559 result but MathCAD givest smallest integer ppois(k, mean) >= prob
// static_cast<RealType>(6.), // Expect k = 6 events.
// tolerance/5);
BOOST_CHECK_CLOSE(
boost::math::quantile(
complement(
@@ -441,11 +507,21 @@ int test_main(int, char* [])
cout << endl;
}
cout << cdf(poisson_distribution<double>(5), static_cast<double>(0)) << ' ' << endl; // 0.006737946999085467
cout << cdf(poisson_distribution<double>(5), static_cast<double>(1)) << ' ' << endl; // 0.040427681994512805
cout << cdf(poisson_distribution<double>(2), static_cast<double>(3)) << ' ' << endl; // 0.85712346049854715
#endif
{
for (int i = 1; i < 100; i++)
{
poisson_distribution<double> distn(static_cast<double>(i));
cout << i << ' ' << median(distn) << ' ' << quantile(distn, 0.5) << ' '
<< median(distn) - quantile(distn, 0.5) << endl;
}
}
// (Parameter value, arbitrarily zero, only communicates the floating-point type).
test_spots(0.0F); // Test float.
test_spots(0.0); // Test double.

View File

@@ -458,6 +458,8 @@ void test_spots(RealType T)
BOOST_CHECK_CLOSE_FRACTION(
mode(distu01), static_cast<RealType>(0), tolerance);
// skewness:
BOOST_CHECK_CLOSE_FRACTION(
median(trim12), static_cast<RealType>(-0.13397459621556151), tolerance);
BOOST_CHECK_EQUAL(
skewness(distu01), static_cast<RealType>(0));
// kertosis:
@@ -528,6 +530,8 @@ int test_main(int, char* [])
BOOST_CHECK_EQUAL(tristd.mode(), 0);
BOOST_CHECK_EQUAL(tristd.upper(), 1);
cout << median(tristd) << endl;
triangular_distribution<> tri011(0, 1, 1); // Using default RealType double.
// mode is upper
BOOST_CHECK_EQUAL(tri011.lower(), 0); // Check defaults again.
@@ -611,6 +615,7 @@ int test_main(int, char* [])
BOOST_CHECK_EQUAL(quantile(complement(*dists[i], 1.)), quantile(*dists[i], 0.));
BOOST_CHECK_CLOSE_FRACTION(quantile(*dists[i], 0.5), quantile(complement(*dist, 0.5)), tol5eps); // OK
BOOST_CHECK_CLOSE_FRACTION(quantile(*dists[i], 0.98), quantile(complement(*dist, 1. - 0.98)),tol5eps);
// cout << setprecision(17) << median(*dist) << endl;
}
cout << showpos << setprecision(2) << endl;
@@ -635,10 +640,6 @@ int test_main(int, char* [])
BOOST_CHECK_CLOSE_FRACTION(quantile(trim12, dx), quantile(complement(trim12, 1 - dx)), tol500eps);
}
cout << endl;
// Basic sanity-check spot values.
// (Parameter value, arbitrarily zero, only communicates the floating point type).
test_spots(0.0F); // Test float. OK at decdigits = 0 tolerance = 0.0001 %
@@ -662,26 +663,27 @@ int test_main(int, char* [])
Output:
------ Build started: Project: test_triangular, Configuration: Debug Win32 ------
Compiling...
test_triangular.cpp
Linking...
Autorun "i:\boost-06-05-03-1300\libs\math\test\Math_test\debug\test_triangular.exe"
Running 1 test case...
0
Distribution 0
0
Distribution 1
1
Distribution 2
0.35355339059327373
Distribution 3
0.5
Distribution 4
-0.13397459621556151
Tolerance for type float is 5.96046e-007.
Tolerance for type double is 1.11022e-015.
Tolerance for type long double is 1.11022e-015.
Tolerance for type class boost::math::concepts::real_concept is 1.11022e-015.
*** No errors detected
Build Time 0:05
Build log was saved at "file://i:\boost-06-05-03-1300\libs\math\test\Math_test\test_triangular\Debug\BuildLog.htm"
test_triangular - 0 error(s), 0 warning(s)
========== Build: 1 succeeded, 0 failed, 0 up-to-date, 0 skipped ==========

View File

@@ -320,6 +320,8 @@ void test_spots(RealType T)
// mode:
BOOST_CHECK_CLOSE_FRACTION(
mode(distu01), static_cast<RealType>(0), tolerance);
BOOST_CHECK_CLOSE_FRACTION(
median(distu01), static_cast<RealType>(0.5), tolerance);
// skewness:
BOOST_CHECK_EQUAL(
skewness(distu01), static_cast<RealType>(0));

View File

@@ -296,6 +296,10 @@ void test_spots(RealType T)
BOOST_CHECK_CLOSE(
mode(dist)
, dist.scale() * pow((dist.shape() - 1) / dist.shape(), 1/dist.shape()), tolerance);
// median:
BOOST_CHECK_CLOSE(
median(dist)
, dist.scale() * pow(log(static_cast<RealType>(2)), 1 / dist.shape()), tolerance);
// skewness:
BOOST_CHECK_CLOSE(
skewness(dist),