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