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