Imported Upstream version 1.72.0
[platform/upstream/boost.git] / libs / math / test / test_bernoulli.cpp
1 // test_bernoulli.cpp
2
3 // Copyright John Maddock 2006.
4 // Copyright  Paul A. Bristow 2007, 2012.
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 Bernoulli Cumulative Distribution Function.
12
13 #ifdef _MSC_VER
14 #  pragma warning (disable : 4535) // calling _set_se_translator() requires /EHa.
15 #  pragma warning (disable : 4244) // conversion possible loss of data.
16 #  pragma warning (disable : 4996) // 'putenv': The POSIX name for this item is deprecated.
17 #  pragma warning (disable : 4127) // conditional expression is constant.
18 #endif
19
20 // Default domain error policy is
21 // #define BOOST_MATH_DOMAIN_ERROR_POLICY throw_on_error
22
23 #include <boost/math/concepts/real_concept.hpp> // for real_concept
24 using ::boost::math::concepts::real_concept;
25 #include <boost/math/tools/test.hpp>
26
27 #include <boost/math/distributions/bernoulli.hpp> // for bernoulli_distribution
28 using boost::math::bernoulli_distribution;
29
30 #define BOOST_TEST_MAIN
31 #include <boost/test/unit_test.hpp> // for test_main
32 #include <boost/test/tools/floating_point_comparison.hpp> // for BOOST_CHECK_CLOSE_FRACTION, BOOST_CHECK_EQUAL...
33
34 #include <iostream>
35 using std::cout;
36 using std::endl;
37 using std::fixed;
38 using std::right;
39 using std::left;
40 using std::showpoint;
41 using std::showpos;
42 using std::setw;
43 using std::setprecision;
44
45 #include <limits>
46 using std::numeric_limits;
47
48 template <class RealType> // Any floating-point type RealType.
49 void test_spots(RealType)
50 { // Parameter only provides the type, float, double... value ignored.
51
52   // Basic sanity checks, test data may be to double precision only
53   // so set tolerance to 100 eps expressed as a fraction,
54   // or 100 eps of type double expressed as a fraction,
55   // whichever is the larger.
56
57   RealType tolerance = (std::max)
58       (boost::math::tools::epsilon<RealType>(),
59       static_cast<RealType>(std::numeric_limits<double>::epsilon()));
60    tolerance *= 100;
61
62   cout << "Tolerance for type " << typeid(RealType).name()  << " is "
63     << setprecision(3) << tolerance  << " (or " << tolerance * 100 << "%)." << endl;
64
65   // Sources of spot test values - calculator,
66   // or Steve Moshier's command interpreter V1.3 100 decimal digit calculator,
67   // Wolfram function evaluator.
68
69   using boost::math::bernoulli_distribution; // of type RealType.
70   using  ::boost::math::cdf;
71   using  ::boost::math::pdf;
72
73   BOOST_CHECK_EQUAL(bernoulli_distribution<RealType>(static_cast<RealType>(0.5)).success_fraction(), static_cast<RealType>(0.5));
74   BOOST_CHECK_EQUAL(bernoulli_distribution<RealType>(static_cast<RealType>(0.1L)).success_fraction(), static_cast<RealType>(0.1L));
75   BOOST_CHECK_EQUAL(bernoulli_distribution<RealType>(static_cast<RealType>(0.9L)).success_fraction(), static_cast<RealType>(0.9L));
76
77   BOOST_MATH_CHECK_THROW( // Constructor success_fraction outside 0 to 1.
78        bernoulli_distribution<RealType>(static_cast<RealType>(2)), std::domain_error);
79   BOOST_MATH_CHECK_THROW(
80        bernoulli_distribution<RealType>(static_cast<RealType>(-2)), std::domain_error);
81
82   BOOST_MATH_CHECK_THROW(
83        pdf( // pdf k neither 0 nor 1.
84           bernoulli_distribution<RealType>(static_cast<RealType>(0.25L)), static_cast<RealType>(-1)), std::domain_error);
85
86   BOOST_MATH_CHECK_THROW(
87        pdf( // pdf k neither 0 nor 1.
88           bernoulli_distribution<RealType>(static_cast<RealType>(0.25L)), static_cast<RealType>(2)), std::domain_error);
89  
90   BOOST_CHECK_EQUAL(
91     pdf( // OK k (or n)
92     bernoulli_distribution<RealType>(static_cast<RealType>(0.5L)), static_cast<RealType>(0)),
93       static_cast<RealType>(0.5)); // Expect 1 - p.
94
95   BOOST_CHECK_CLOSE_FRACTION(
96     pdf( // OK k (or n)
97     bernoulli_distribution<RealType>(static_cast<RealType>(0.6L)), static_cast<RealType>(0)),
98       static_cast<RealType>(0.4L), tolerance); // Expect  1 - p.
99
100   BOOST_CHECK_CLOSE_FRACTION(
101     pdf( // OK k (or n)
102     bernoulli_distribution<RealType>(static_cast<RealType>(0.6L)), static_cast<RealType>(0)),
103       static_cast<RealType>(0.4L), tolerance); // Expect  1- p.
104
105   BOOST_CHECK_CLOSE_FRACTION(
106     pdf( // OK k (or n)
107     bernoulli_distribution<RealType>(static_cast<RealType>(0.4L)), static_cast<RealType>(0)),
108       static_cast<RealType>(0.6L), tolerance); // Expect  1- p.
109
110   BOOST_CHECK_EQUAL(
111        mean(bernoulli_distribution<RealType>(static_cast<RealType>(0.5L))), static_cast<RealType>(0.5L));
112
113   BOOST_CHECK_EQUAL(
114        mean(bernoulli_distribution<RealType>(static_cast<RealType>(0.1L))),
115        static_cast<RealType>(0.1L));
116
117   BOOST_CHECK_CLOSE_FRACTION(
118        variance(bernoulli_distribution<RealType>(static_cast<RealType>(0.1L))),
119        static_cast<RealType>(0.09L),
120        tolerance);
121
122   BOOST_CHECK_CLOSE_FRACTION(
123        skewness(bernoulli_distribution<RealType>(static_cast<RealType>(0.1L))),
124        static_cast<RealType>(2.666666666666666666666666666666666666666666L),
125        tolerance);
126
127   BOOST_CHECK_CLOSE_FRACTION(
128        kurtosis(bernoulli_distribution<RealType>(static_cast<RealType>(0.1L))),
129        static_cast<RealType>(8.11111111111111111111111111111111111111111111L),
130        tolerance);
131
132   BOOST_CHECK_CLOSE_FRACTION(
133        kurtosis_excess(bernoulli_distribution<RealType>(static_cast<RealType>(0.1L))),
134        static_cast<RealType>(5.11111111111111111111111111111111111111111111L),
135        tolerance);
136
137   BOOST_MATH_CHECK_THROW(
138      quantile(
139         bernoulli_distribution<RealType>(static_cast<RealType>(2)), // prob >1
140         static_cast<RealType>(0)), std::domain_error
141      );
142   BOOST_MATH_CHECK_THROW(
143      quantile(
144         bernoulli_distribution<RealType>(static_cast<RealType>(-1)), // prob < 0
145         static_cast<RealType>(0)), std::domain_error
146      );
147   BOOST_MATH_CHECK_THROW(
148      quantile(
149         bernoulli_distribution<RealType>(static_cast<RealType>(0.5L)), // k >1
150         static_cast<RealType>(-1)), std::domain_error
151      );
152   BOOST_MATH_CHECK_THROW(
153      quantile(
154         bernoulli_distribution<RealType>(static_cast<RealType>(0.5L)), // k < 0
155         static_cast<RealType>(2)), std::domain_error
156      );
157
158   BOOST_CHECK_CLOSE_FRACTION(
159      cdf(
160         bernoulli_distribution<RealType>(static_cast<RealType>(0.6L)),
161         static_cast<RealType>(0)), 
162         static_cast<RealType>(0.4L), // 1 - p
163         tolerance
164      );
165
166   BOOST_CHECK_CLOSE_FRACTION(
167      cdf(
168         bernoulli_distribution<RealType>(static_cast<RealType>(0.6L)),
169         static_cast<RealType>(1)), 
170         static_cast<RealType>(1), // p
171         tolerance
172      );
173
174   BOOST_CHECK_CLOSE_FRACTION(
175      cdf(complement(
176         bernoulli_distribution<RealType>(static_cast<RealType>(0.6L)),
177         static_cast<RealType>(1))), 
178         static_cast<RealType>(0),
179         tolerance
180      );
181
182   BOOST_CHECK_CLOSE_FRACTION(
183      cdf(complement(
184         bernoulli_distribution<RealType>(static_cast<RealType>(0.6L)),
185         static_cast<RealType>(0))), 
186         static_cast<RealType>(0.6L),
187         tolerance
188      );
189
190   BOOST_CHECK_EQUAL(
191      quantile(
192         bernoulli_distribution<RealType>(static_cast<RealType>(0.6L)),
193         static_cast<RealType>(0.1L)),  // < p
194         static_cast<RealType>(0) 
195      );
196
197   BOOST_CHECK_EQUAL(
198      quantile(
199         bernoulli_distribution<RealType>(static_cast<RealType>(0.6L)),
200         static_cast<RealType>(0.9L)),  // > p
201         static_cast<RealType>(1) 
202      );
203
204    BOOST_CHECK_EQUAL(
205      quantile(complement(
206         bernoulli_distribution<RealType>(static_cast<RealType>(0.6L)),
207         static_cast<RealType>(0.1L))),  // < p
208         static_cast<RealType>(1) 
209      );
210
211    BOOST_CHECK_EQUAL(
212      quantile(complement(
213         bernoulli_distribution<RealType>(static_cast<RealType>(0.6L)),
214         static_cast<RealType>(0.9L))),  // > p
215         static_cast<RealType>(0) 
216      );
217
218    // Checks for 'bad' parameters.
219    // Construction.
220    BOOST_MATH_CHECK_THROW(bernoulli_distribution<RealType>(-1), std::domain_error); // p outside 0 to 1.
221    BOOST_MATH_CHECK_THROW(bernoulli_distribution<RealType>(+2), std::domain_error); // p outside 0 to 1.
222
223    // Parameters.
224    bernoulli_distribution<RealType> dist(RealType(1)); 
225    BOOST_MATH_CHECK_THROW(pdf(dist, -1), std::domain_error);
226    BOOST_MATH_CHECK_THROW(cdf(dist, -1), std::domain_error);
227    BOOST_MATH_CHECK_THROW(cdf(complement(dist, -1)), std::domain_error);
228    BOOST_MATH_CHECK_THROW(quantile(dist, 2), std::domain_error);
229    BOOST_MATH_CHECK_THROW(quantile(complement(dist, -1)), std::domain_error);
230    BOOST_MATH_CHECK_THROW(quantile(dist, -1), std::domain_error);
231    BOOST_MATH_CHECK_THROW(quantile(complement(dist, -1)), std::domain_error);
232      
233    // No longer allow any parameter to be NaN or inf, so all these tests should throw.
234    if (std::numeric_limits<RealType>::has_quiet_NaN)
235    { 
236     // Attempt to construct from non-finite should throw.
237      RealType nan = std::numeric_limits<RealType>::quiet_NaN();
238 #ifndef BOOST_NO_EXCEPTIONS
239      BOOST_MATH_CHECK_THROW(bernoulli_distribution<RealType> b(nan), std::domain_error);
240 #else
241      BOOST_MATH_CHECK_THROW(bernoulli_distribution<RealType>(nan), std::domain_error);
242 #endif
243     // Non-finite parameters should throw.
244      bernoulli_distribution<RealType> b(RealType(1)); 
245      BOOST_MATH_CHECK_THROW(pdf(b, +nan), std::domain_error); // x = NaN
246      BOOST_MATH_CHECK_THROW(cdf(b, +nan), std::domain_error); // x = NaN
247      BOOST_MATH_CHECK_THROW(cdf(complement(b, +nan)), std::domain_error); // x = + nan
248      BOOST_MATH_CHECK_THROW(quantile(b, +nan), std::domain_error); // p = + nan
249      BOOST_MATH_CHECK_THROW(quantile(complement(b, +nan)), std::domain_error); // p = + nan
250   } // has_quiet_NaN
251
252   if (std::numeric_limits<RealType>::has_infinity)
253   {
254      RealType inf = std::numeric_limits<RealType>::infinity(); 
255 #ifndef BOOST_NO_EXCEPTIONS
256      BOOST_MATH_CHECK_THROW(bernoulli_distribution<RealType> w(inf), std::domain_error);
257 #else
258      BOOST_MATH_CHECK_THROW(bernoulli_distribution<RealType>(inf), std::domain_error);
259 #endif
260      bernoulli_distribution<RealType> w(RealType(1)); 
261 #ifndef BOOST_NO_EXCEPTIONS
262      BOOST_MATH_CHECK_THROW(bernoulli_distribution<RealType> w(inf), std::domain_error);
263 #else
264      BOOST_MATH_CHECK_THROW(bernoulli_distribution<RealType>(inf), std::domain_error);
265 #endif
266      BOOST_MATH_CHECK_THROW(pdf(w, +inf), std::domain_error); // x = inf
267      BOOST_MATH_CHECK_THROW(cdf(w, +inf), std::domain_error); // x = inf
268      BOOST_MATH_CHECK_THROW(cdf(complement(w, +inf)), std::domain_error); // x = + inf
269      BOOST_MATH_CHECK_THROW(quantile(w, +inf), std::domain_error); // p = + inf
270      BOOST_MATH_CHECK_THROW(quantile(complement(w, +inf)), std::domain_error); // p = + inf
271    } // has_infinity
272
273 } // template <class RealType>void test_spots(RealType)
274
275 BOOST_AUTO_TEST_CASE( test_main )
276 {
277    BOOST_MATH_CONTROL_FP;
278    // Check that can generate bernoulli distribution using both convenience methods:
279    bernoulli_distribution<double> bn1(0.5); // Using default RealType double.
280    boost::math::bernoulli bn2(0.5); // Using typedef. 
281
282   BOOST_CHECK_EQUAL(bn1.success_fraction(), 0.5);
283   BOOST_CHECK_EQUAL(bn2.success_fraction(), 0.5);
284
285   BOOST_CHECK_EQUAL(kurtosis(bn2) -3, kurtosis_excess(bn2));
286   BOOST_CHECK_EQUAL(kurtosis_excess(bn2), -2);
287
288   //using namespace boost::math; or 
289   using boost::math::bernoulli;
290
291   double tol5eps = std::numeric_limits<double>::epsilon() * 5; // 5 eps as a fraction.
292   // Default bernoulli is type double, so these test values should also be type double.
293   BOOST_CHECK_CLOSE_FRACTION(kurtosis_excess(bernoulli(0.1)), 5.11111111111111111111111111111111111111111111111111, tol5eps);
294   BOOST_CHECK_CLOSE_FRACTION(kurtosis_excess(bernoulli(0.9)), 5.11111111111111111111111111111111111111111111111111, tol5eps);
295   BOOST_CHECK_CLOSE_FRACTION(kurtosis(bernoulli(0.6)), 1./0.4 + 1./0.6 -3., tol5eps);
296   BOOST_CHECK_EQUAL(kurtosis(bernoulli(0)), +std::numeric_limits<double>::infinity());
297   BOOST_CHECK_EQUAL(kurtosis(bernoulli(1)), +std::numeric_limits<double>::infinity());
298  // 
299
300   // Basic sanity-check spot values.
301
302   // (Parameter value, arbitrarily zero, only communicates the floating point type).
303   test_spots(0.0F); // Test float.
304   test_spots(0.0); // Test double.
305   test_spots(0.0L); // Test long double.
306 #if !BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x582))
307   test_spots(boost::math::concepts::real_concept(0.)); // Test real concept.
308 #endif
309
310 } // BOOST_AUTO_TEST_CASE( test_main )
311
312 /*
313
314 Output is:
315
316   Description: Autorun "J:\Cpp\MathToolkit\test\Math_test\Debug\test_bernouilli.exe"
317   Running 1 test case...
318   Tolerance for type float is 1.19e-005 (or 0.00119%).
319   Tolerance for type double is 2.22e-014 (or 2.22e-012%).
320   Tolerance for type long double is 2.22e-014 (or 2.22e-012%).
321   Tolerance for type class boost::math::concepts::real_concept is 2.22e-014 (or 2.22e-012%).
322   
323   *** No errors detected
324
325
326 */
327
328