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