test_find_scale.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. // test_find_scale.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_scale function.
  9. // Default distribution 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_scale.hpp>
  22. using boost::math::find_scale;
  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_scale<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_scale<normal>(0., -1., 0.), std::domain_error); // p below 0 to 1.
  58. BOOST_MATH_CHECK_THROW(find_scale<normal>(0., 2., 0.), std::domain_error); // p above 0 to 1.
  59. BOOST_MATH_CHECK_THROW(find_scale<normal>(numeric_limits<double>::infinity(), 0.5, 0.),
  60. std::domain_error); // z not finite.
  61. BOOST_MATH_CHECK_THROW(find_scale<normal>(numeric_limits<double>::quiet_NaN(), -1., 0.),
  62. std::domain_error); // z not finite
  63. BOOST_MATH_CHECK_THROW(find_scale<normal>(0., -1., numeric_limits<double>::quiet_NaN()),
  64. std::domain_error); // scale not finite
  65. BOOST_MATH_CHECK_THROW(find_scale<normal>(complement(0., -1., 0.)), std::domain_error); // p below 0 to 1.
  66. BOOST_MATH_CHECK_THROW(find_scale<normal>(complement(0., 2., 0.)), std::domain_error); // p above 0 to 1.
  67. BOOST_MATH_CHECK_THROW(find_scale<normal>(complement(numeric_limits<double>::infinity(), 0.5, 0.)),
  68. std::domain_error); // z not finite.
  69. BOOST_MATH_CHECK_THROW(find_scale<normal>(complement(numeric_limits<double>::quiet_NaN(), -1., 0.)),
  70. std::domain_error); // z not finite
  71. BOOST_MATH_CHECK_THROW(find_scale<normal>(complement(0., -1., numeric_limits<double>::quiet_NaN())),
  72. std::domain_error); // scale not finite
  73. BOOST_MATH_CHECK_THROW(find_scale<normal>(complement(0., -1., 0.)), std::domain_error); // p below 0 to 1.
  74. // Check for ab-use with unsuitable distribution(s), for example,
  75. // pareto distribution (and most others) can't be used with find_scale (or find_location)
  76. // because they lack the scale and location attributes.
  77. // BOOST_MATH_CHECK_THROW(find_scale<pareto>(0., 0.5, 0.), std::domain_error);
  78. // correctly fails to compile in find_scale() at
  79. // BOOST_STATIC_ASSERT(::boost::math::tools::is_scaled_distribution<Dist>::value);
  80. // Check doesn't throw when an ignore_error for domain_error policy is used.
  81. using boost::math::policies::policy;
  82. using boost::math::policies::domain_error;
  83. using boost::math::policies::ignore_error;
  84. // Define a (bad?) policy to ignore domain errors ('bad' arguments):
  85. typedef policy<domain_error<ignore_error> > ignore_domain_policy;
  86. // Using a typedef is convenient, especially if it is re-used.
  87. #ifndef BOOST_NO_EXCEPTIONS
  88. BOOST_CHECK_NO_THROW(find_scale<normal>(0, -1, 1,
  89. ignore_domain_policy())); // probability outside [0, 1]
  90. BOOST_CHECK_NO_THROW(find_scale<normal>(numeric_limits<double>::infinity(), -1, 1,
  91. ignore_domain_policy())); // z not finite.
  92. BOOST_CHECK_NO_THROW(find_scale<normal>(complement(0, -1, 1, ignore_domain_policy()))); // probability outside [0, 1]
  93. BOOST_CHECK_NO_THROW(find_scale<normal>(complement(numeric_limits<double>::infinity(), -1, 1,
  94. ignore_domain_policy()))); // z not finite.
  95. #endif
  96. RealType l = 0.; // standard normal distribution.
  97. RealType sd = static_cast<RealType>(1); // normal default standard deviation = 1.
  98. normal_distribution<RealType> n01(l, sd); // mean(location) = 0, standard_deviation (scale) = 1.
  99. RealType z = static_cast<RealType>(-2); // z to give prob p
  100. //cout << "Standard normal distribution with standard deviation = " << sd
  101. // << " has " << "fraction <= " << z << " = " << cdf(n01, z) << endl;
  102. // Standard normal distribution with standard deviation = 1 has fraction <= -2 = 0.0227501
  103. //normal_distribution<RealType> np001pc(l, sd); // Same mean(location) but with standard_deviation (scale) changed.
  104. //cout << "Normal distribution with standard deviation = " << s
  105. // << " has " << "fraction <= " << z << " = " << cdf(np001pc, z) << endl;
  106. // Find scale to give a probability p (0.001) of z (-2)
  107. RealType p = static_cast<RealType>(0.001); // only 0.1% to be below z (-2).
  108. // location (mean) remains at zero.
  109. RealType s = find_scale<normal_distribution<RealType> >(z, p, l);
  110. //cout << "Mean " << l << ", z " << z << ", p " << p
  111. // << ", sd " << sd << ", find_scale " << s
  112. // << ", difference in sd " << s - sd << endl;
  113. // Mean 0, z -2, p 0.001, sd 1, find_scale 0.64720053440907599, difference in sd -0.352799
  114. cout.precision(17);
  115. BOOST_CHECK_CLOSE_FRACTION(s, static_cast<RealType>(0.64720053440907599L), tolerance);
  116. normal_distribution<RealType> np001pc(l, s); // Same mean(location) but with standard_deviation (scale) changed.
  117. //cout << "Normal distribution with standard deviation = " << s
  118. // << " has " << "fraction <= " << z << " = " << cdf(np001pc, z) << endl;
  119. // Normal distribution with standard deviation = 0.647201 has fraction <= -2 = 0.001
  120. // Check cdf such that only fraction p really is below changed standard deviation s.
  121. BOOST_CHECK_CLOSE_FRACTION(p, cdf(np001pc, z), tolerance);
  122. // Check that some policies can be applied (though results not used here).
  123. s = find_scale<normal_distribution<RealType> >(z, p, l, policy<>()); // Default policy, needs using boost::math::policies::policy;
  124. s = find_scale<normal_distribution<RealType> >(z, p, l, boost::math::policies::policy<>()); // Default policy, fully specified.
  125. s = find_scale<normal_distribution<RealType> >(z, p, l, ignore_domain_policy()); // find_scale with new policy, using typedef.
  126. s = find_scale<normal_distribution<RealType> >(z, p, l, policy<domain_error<ignore_error> >()); // New policy, without typedef.
  127. // Check that can use the complement version too.
  128. RealType q = 1 - p; // complement.
  129. s = find_scale<normal_distribution<RealType> >(complement(z, q, l)); // Implicit default policy.
  130. BOOST_CHECK_CLOSE_FRACTION(s, static_cast<RealType>(0.64720053440907599L), tolerance);
  131. s = find_scale<normal_distribution<RealType> >(complement(z, q, l, policy<>())); // Explicit default policy.
  132. BOOST_CHECK_CLOSE_FRACTION(s, static_cast<RealType>(0.64720053440907599L), tolerance);
  133. normal_distribution<RealType> np95pc(l, s); // Same mean(location) but with new standard_deviation (scale).
  134. //cout << "Mean " << l << ", z " << z << ", q " << q
  135. //<< ", sd " << sd << ", find_scale " << s
  136. //<< ", difference in sd " << s - sd << endl;
  137. //cout << "Normal distribution with standard deviation = " << s
  138. // << " has " << "fraction <= " << z << " = " << cdf(np001pc, z) << endl;
  139. BOOST_CHECK_CLOSE_FRACTION(q, cdf(complement(np95pc, z)), tolerance);
  140. } // template <class RealType>void test_spots(RealType)
  141. BOOST_AUTO_TEST_CASE( test_main )
  142. {
  143. // Basic sanity-check spot values.
  144. // (Parameter value, arbitrarily zero, only communicates the floating-point type).
  145. test_spots(0.0F); // Test float.
  146. test_spots(0.0); // Test double.
  147. #ifndef BOOST_MATH_NO_LONG_DOUBLE_MATH_FUNCTIONS
  148. test_spots(0.0L); // Test long double.
  149. #if !BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x0582))
  150. test_spots(boost::math::concepts::real_concept(0.)); // Test real concept.
  151. #endif
  152. #else
  153. std::cout << "<note>The long double tests have been disabled on this platform "
  154. "either because the long double overloads of the usual math functions are "
  155. "not available at all, or because they are too inaccurate for these tests "
  156. "to pass.</note>" << std::endl;
  157. #endif
  158. } // BOOST_AUTO_TEST_CASE( test_main )
  159. /*
  160. Output is:
  161. Autorun "i:\boost-06-05-03-1300\libs\math\test\Math_test\debug\test_find_scale.exe"
  162. Running 1 test case...
  163. Tolerance for type float is 1.19e-005 (or 0.00119%).
  164. Tolerance for type double is 2.22e-014 (or 2.22e-012%).
  165. Tolerance for type long double is 2.22e-014 (or 2.22e-012%).
  166. Tolerance for type class boost::math::concepts::real_concept is 2.22e-014 (or 2.22e-012%).
  167. *** No errors detected
  168. */