test_find_location.cpp 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. // test_find_location.cpp
  2. // Copyright John Maddock 2007.
  3. // Copyright Paul A. Bristow 2007.
  4. // Use, modification and distribution are subject to the
  5. // Boost Software License, Version 1.0.
  6. // (See accompanying file LICENSE_1_0.txt
  7. // or copy at http://www.boost.org/LICENSE_1_0.txt)
  8. // Basic sanity test for find_location function.
  9. // Default domain error policy is
  10. // #define BOOST_MATH_DOMAIN_ERROR_POLICY throw_on_error
  11. #include <pch.hpp>
  12. #include <boost/math/tools/test.hpp>
  13. #include <boost/math/concepts/real_concept.hpp> // for real_concept
  14. #include <boost/math/distributions/normal.hpp> // for normal_distribution
  15. using boost::math::normal; // Default type double.
  16. using boost::math::normal_distribution; // All floating-point types.
  17. #include <boost/math/distributions/cauchy.hpp> // for cauchy_distribution
  18. using boost::math::cauchy;
  19. #include <boost/math/distributions/pareto.hpp> // for cauchy_distribution
  20. using boost::math::pareto;
  21. #include <boost/math/distributions/find_location.hpp>
  22. using boost::math::find_location;
  23. using boost::math::complement;// will be needed by users who want complement,
  24. #include <boost/math/policies/policy.hpp>
  25. using boost::math::policies::policy;
  26. #define BOOST_TEST_MAIN
  27. #include <boost/test/unit_test.hpp> // for test_main
  28. #include <boost/test/tools/floating_point_comparison.hpp> // for BOOST_CHECK_CLOSE_FRACTION, BOOST_CHECK_EQUAL...
  29. #include <iostream>
  30. #include <iomanip>
  31. using std::cout; using std::endl; using std::fixed;
  32. using std::right; using std::left; using std::showpoint;
  33. using std::showpos; using std::setw; using std::setprecision;
  34. #include <limits>
  35. using std::numeric_limits;
  36. template <class RealType> // Any floating-point type RealType.
  37. void test_spots(RealType)
  38. { // Parameter only provides the type, float, double... value ignored.
  39. // Basic sanity checks, test data may be to double precision only
  40. // so set tolerance to 100 eps expressed as a fraction,
  41. // or 100 eps of type double expressed as a fraction,
  42. // whichever is the larger.
  43. RealType tolerance = (std::max)
  44. (boost::math::tools::epsilon<RealType>(),
  45. static_cast<RealType>(std::numeric_limits<double>::epsilon()));
  46. tolerance *= 100; // 100 eps as a fraction.
  47. cout << "Tolerance for type " << typeid(RealType).name() << " is "
  48. << setprecision(3) << tolerance << " (or " << tolerance * 100 << "%)." << endl;
  49. BOOST_MATH_CHECK_THROW( // Probability outside 0 to 1.
  50. find_location<normal_distribution<RealType> >(
  51. static_cast<RealType>(0.), static_cast<RealType>(-1.), static_cast<RealType>(0.) ),
  52. std::domain_error);
  53. normal_distribution<RealType> n; // standard N(0,1)
  54. BOOST_CHECK_EQUAL(n.location(), 0); // aka mean.
  55. BOOST_CHECK_EQUAL(n.scale(), 1); // aka standard_deviation.
  56. // Check for 'bad' arguments.
  57. BOOST_MATH_CHECK_THROW(find_location<normal>(0., -1., 0.), std::domain_error); // p below 0 to 1.
  58. BOOST_MATH_CHECK_THROW(find_location<normal>(0., 2., 0.), std::domain_error); // p above 0 to 1.
  59. BOOST_MATH_CHECK_THROW(find_location<normal>(numeric_limits<double>::infinity(), 0.5, 0.),
  60. std::domain_error); // z not finite.
  61. BOOST_MATH_CHECK_THROW(find_location<normal>(numeric_limits<double>::quiet_NaN(), -1., 0.),
  62. std::domain_error); // z not finite
  63. BOOST_MATH_CHECK_THROW(find_location<normal>(0., -1., numeric_limits<double>::quiet_NaN()),
  64. std::domain_error); // scale not finite
  65. BOOST_MATH_CHECK_THROW(find_location<normal>(complement(0., -1., 0.)), std::domain_error); // p below 0 to 1.
  66. BOOST_MATH_CHECK_THROW(find_location<normal>(complement(0., 2., 0.)), std::domain_error); // p above 0 to 1.
  67. BOOST_MATH_CHECK_THROW(find_location<normal>(complement(numeric_limits<double>::infinity(), 0.5, 0.)),
  68. std::domain_error); // z not finite.
  69. BOOST_MATH_CHECK_THROW(find_location<normal>(complement(numeric_limits<double>::quiet_NaN(), -1., 0.)),
  70. std::domain_error); // z not finite
  71. BOOST_MATH_CHECK_THROW(find_location<normal>(complement(0., -1., numeric_limits<double>::quiet_NaN())),
  72. std::domain_error); // scale not finite
  73. //// Check for ab-use with unsuitable distribution(s) when concept check implemented.
  74. // BOOST_MATH_CHECK_THROW(find_location<pareto>(0., 0.5, 0.), std::domain_error); // pareto can't be used with find_location.
  75. // Check doesn't throw when an ignore_error for domain_error policy is used.
  76. using boost::math::policies::policy;
  77. using boost::math::policies::domain_error;
  78. using boost::math::policies::ignore_error;
  79. // Define a (bad?) policy to ignore domain errors ('bad' arguments):
  80. typedef policy<domain_error<ignore_error> > ignore_domain_policy;
  81. // Using a typedef is convenient, especially if it is re-used.
  82. #ifndef BOOST_NO_EXCEPTIONS
  83. BOOST_CHECK_NO_THROW(find_location<normal>(0, -1, 1,
  84. ignore_domain_policy())); // probability outside [0, 1]
  85. BOOST_CHECK_NO_THROW(find_location<normal>(numeric_limits<double>::infinity(), -1, 1,
  86. ignore_domain_policy())); // z not finite.
  87. BOOST_CHECK_NO_THROW(find_location<normal>(complement(0, -1, 1,
  88. ignore_domain_policy()))); // probability outside [0, 1]
  89. BOOST_CHECK_NO_THROW(find_location<normal>(complement(numeric_limits<double>::infinity(), -1, 1,
  90. ignore_domain_policy()))); // z not finite.
  91. #endif
  92. // Find location to give a probability p (0.05) of z (-2)
  93. RealType sd = static_cast<RealType>(1); // normal default standard deviation = 1.
  94. RealType z = static_cast<RealType>(-2); // z to give prob p
  95. RealType p = static_cast<RealType>(0.05); // only 5% will be below z
  96. RealType l = find_location<normal_distribution<RealType> >(z, p, sd);
  97. // cout << z << " " << p << " " << sd << " " << l << endl;
  98. normal_distribution<RealType> np05pc(l, sd); // Same standard_deviation (scale) but with mean(location) shifted.
  99. // cout << "Normal distribution with mean = " << l << " has " << "fraction <= " << z << " = " << cdf(np05pc, z) << endl;
  100. // Check cdf such that only fraction p really is below offset mean l.
  101. BOOST_CHECK_CLOSE_FRACTION(p, cdf(np05pc, z), tolerance);
  102. // Check that some policies can be applied (though not used here).
  103. l = find_location<normal_distribution<RealType> >(z, p, sd, policy<>()); // Default policy, needs using boost::math::policies::policy;
  104. l = find_location<normal_distribution<RealType> >(z, p, sd, boost::math::policies::policy<>()); // Default policy, fully specified.
  105. l = find_location<normal_distribution<RealType> >(z, p, sd, ignore_domain_policy()); // find_location with new policy, using typedef.
  106. l = find_location<normal_distribution<RealType> >(z, p, sd, policy<domain_error<ignore_error> >()); // New policy, without typedef.
  107. // Check that can use the complement version.
  108. RealType q = 1 - p; // complement.
  109. // cout << "find_location<normal_distribution<RealType> >(complement(z, q, sd)) = " << endl;
  110. l = find_location<normal_distribution<RealType> >(complement(z, q, sd));
  111. normal_distribution<RealType> np95pc(l, sd); // Same standard_deviation (scale) but with mean(location) shifted
  112. // cout << "Normal distribution with mean = " << l << " has " << "fraction <= " << z << " = " << cdf(np95pc, z) << endl;
  113. BOOST_CHECK_CLOSE_FRACTION(q, cdf(np95pc, z), tolerance);
  114. } // template <class RealType>void test_spots(RealType)
  115. BOOST_AUTO_TEST_CASE( test_main )
  116. {
  117. // Basic sanity-check spot values.
  118. // (Parameter value, arbitrarily zero, only communicates the floating-point type).
  119. test_spots(0.0F); // Test float.
  120. test_spots(0.0); // Test double.
  121. #ifndef BOOST_MATH_NO_LONG_DOUBLE_MATH_FUNCTIONS
  122. test_spots(0.0L); // Test long double.
  123. #if !BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x0582))
  124. test_spots(boost::math::concepts::real_concept(0.)); // Test real concept.
  125. #endif
  126. #else
  127. std::cout << "<note>The long double tests have been disabled on this platform "
  128. "either because the long double overloads of the usual math functions are "
  129. "not available at all, or because they are too inaccurate for these tests "
  130. "to pass.</note>" << std::endl;
  131. #endif
  132. } // BOOST_AUTO_TEST_CASE( test_main )
  133. /*
  134. Output is:
  135. Autorun "i:\boost-06-05-03-1300\libs\math\test\Math_test\debug\test_find_location.exe"
  136. Running 1 test case...
  137. Tolerance for type float is 1.19e-005 (or 0.00119%).
  138. Tolerance for type double is 2.22e-014 (or 2.22e-012%).
  139. Tolerance for type long double is 2.22e-014 (or 2.22e-012%).
  140. Tolerance for type class boost::math::concepts::real_concept is 2.22e-014 (or 2.22e-012%).
  141. *** No errors detected
  142. */