Imported Upstream version 1.72.0
[platform/upstream/boost.git] / libs / math / test / test_triangular.cpp
1 // Copyright Paul Bristow 2006, 2007.
2 // Copyright John Maddock 2006, 2007.
3
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
9 // test_triangular.cpp
10
11 #include <pch.hpp>
12
13 #ifdef _MSC_VER
14 #  pragma warning(disable: 4127) // conditional expression is constant.
15 #  pragma warning(disable: 4305) // truncation from 'long double' to 'float'
16 #endif
17
18 #include <boost/math/concepts/real_concept.hpp> // for real_concept
19 #define BOOST_TEST_MAIN
20 #include <boost/test/unit_test.hpp> // Boost.Test
21 #include <boost/test/tools/floating_point_comparison.hpp>
22
23 #include <boost/math/distributions/triangular.hpp>
24 using boost::math::triangular_distribution;
25 #include <boost/math/tools/test.hpp>
26 #include <boost/math/special_functions/fpclassify.hpp>
27 #include "test_out_of_range.hpp"
28
29 #include <iostream>
30 #include <iomanip>
31 using std::cout;
32 using std::endl;
33 using std::scientific;
34 using std::fixed;
35 using std::left;
36 using std::right;
37 using std::setw;
38 using std::setprecision;
39 using std::showpos;
40 #include <limits>
41 using std::numeric_limits;
42
43 template <class RealType>
44 void check_triangular(RealType lower, RealType mode, RealType upper, RealType x, RealType p, RealType q, RealType tol)
45 {
46   BOOST_CHECK_CLOSE_FRACTION(
47     ::boost::math::cdf(
48     triangular_distribution<RealType>(lower, mode, upper),   // distribution.
49     x),  // random variable.
50     p,    // probability.
51     tol);   // tolerance.
52   BOOST_CHECK_CLOSE_FRACTION(
53     ::boost::math::cdf(
54     complement(
55     triangular_distribution<RealType>(lower, mode, upper), // distribution.
56     x)),    // random variable.
57     q,    // probability complement.
58     tol);  // tolerance.
59   BOOST_CHECK_CLOSE_FRACTION(
60     ::boost::math::quantile(
61     triangular_distribution<RealType>(lower,mode, upper),  // distribution.
62     p),   // probability.
63     x,  // random variable.
64     tol);  // tolerance.
65   BOOST_CHECK_CLOSE_FRACTION(
66     ::boost::math::quantile(
67     complement(
68     triangular_distribution<RealType>(lower, mode, upper),  // distribution.
69     q)),     // probability complement.
70     x,                                             // random variable.
71     tol);  // tolerance.
72 } // void check_triangular
73
74 template <class RealType>
75 void test_spots(RealType)
76 {
77   // Basic sanity checks:
78   //
79   // Some test values were generated for the triangular distribution
80   // using the online calculator at
81   // http://espse.ed.psu.edu/edpsych/faculty/rhale/hale/507Mat/statlets/free/pdist.htm
82   //
83   // Tolerance is just over 5 epsilon expressed as a fraction:
84   RealType tolerance = boost::math::tools::epsilon<RealType>() * 5; // 5 eps as a fraction.
85   RealType tol5eps = boost::math::tools::epsilon<RealType>() * 5; // 5 eps as a fraction.
86
87   cout << "Tolerance for type " << typeid(RealType).name()  << " is " << tolerance << "." << endl;
88
89   using namespace std; // for ADL of std::exp;
90
91   // Tests on construction
92   // Default should be 0, 0, 1
93   BOOST_CHECK_EQUAL(triangular_distribution<RealType>().lower(), -1);
94   BOOST_CHECK_EQUAL(triangular_distribution<RealType>().mode(), 0);
95   BOOST_CHECK_EQUAL(triangular_distribution<RealType>().upper(), 1);
96   BOOST_CHECK_EQUAL(support(triangular_distribution<RealType>()).first, triangular_distribution<RealType>().lower());
97   BOOST_CHECK_EQUAL(support(triangular_distribution<RealType>()).second, triangular_distribution<RealType>().upper());
98
99   if (std::numeric_limits<RealType>::has_quiet_NaN == true)
100   {
101   BOOST_MATH_CHECK_THROW( // duff parameter lower.
102     triangular_distribution<RealType>(static_cast<RealType>(std::numeric_limits<RealType>::quiet_NaN()), 0, 0),
103     std::domain_error);
104
105   BOOST_MATH_CHECK_THROW( // duff parameter mode.
106     triangular_distribution<RealType>(0, static_cast<RealType>(std::numeric_limits<RealType>::quiet_NaN()), 0),
107     std::domain_error);
108
109   BOOST_MATH_CHECK_THROW( // duff parameter upper.
110     triangular_distribution<RealType>(0, 0, static_cast<RealType>(std::numeric_limits<RealType>::quiet_NaN())),
111     std::domain_error);
112   } // quiet_NaN tests.
113
114   BOOST_MATH_CHECK_THROW( // duff parameters upper < lower.
115     triangular_distribution<RealType>(1, 0, -1),
116     std::domain_error);
117
118   BOOST_MATH_CHECK_THROW( // duff parameters upper == lower.
119     triangular_distribution<RealType>(0, 0, 0),
120     std::domain_error);
121   BOOST_MATH_CHECK_THROW( // duff parameters mode < lower.
122     triangular_distribution<RealType>(0, -1, 1),
123     std::domain_error);
124
125   BOOST_MATH_CHECK_THROW( // duff parameters mode > upper.
126     triangular_distribution<RealType>(0, 2, 1),
127     std::domain_error);
128
129   // Tests for PDF
130   // // triangular_distribution<RealType>() default is (0, 0, 1), mode == lower.
131   BOOST_CHECK_CLOSE_FRACTION( // x == lower == mode
132     pdf(triangular_distribution<RealType>(0, 0, 1), static_cast<RealType>(0)),
133     static_cast<RealType>(2),
134     tolerance);
135
136   BOOST_CHECK_CLOSE_FRACTION( // x == upper
137     pdf(triangular_distribution<RealType>(0, 0, 1), static_cast<RealType>(1)),
138     static_cast<RealType>(0),
139     tolerance);
140
141   BOOST_CHECK_CLOSE_FRACTION( // x > upper
142     pdf(triangular_distribution<RealType>(0, 0, 1), static_cast<RealType>(-1)),
143     static_cast<RealType>(0),
144     tolerance);
145   BOOST_CHECK_CLOSE_FRACTION( // x < lower
146     pdf(triangular_distribution<RealType>(0, 0, 1), static_cast<RealType>(2)),
147     static_cast<RealType>(0),
148     tolerance);
149
150   BOOST_CHECK_CLOSE_FRACTION( // x < lower
151     pdf(triangular_distribution<RealType>(0, 0, 1), static_cast<RealType>(2)),
152     static_cast<RealType>(0),
153     tolerance);
154
155   // triangular_distribution<RealType>() (0, 1, 1) mode == upper
156   BOOST_CHECK_CLOSE_FRACTION( // x == lower
157     pdf(triangular_distribution<RealType>(0, 1, 1), static_cast<RealType>(0)),
158     static_cast<RealType>(0),
159     tolerance);
160
161   BOOST_CHECK_CLOSE_FRACTION( // x == upper
162     pdf(triangular_distribution<RealType>(0, 1, 1), static_cast<RealType>(1)),
163     static_cast<RealType>(2),
164     tolerance);
165
166   BOOST_CHECK_CLOSE_FRACTION( // x > upper
167     pdf(triangular_distribution<RealType>(0, 1, 1), static_cast<RealType>(-1)),
168     static_cast<RealType>(0),
169     tolerance);
170   BOOST_CHECK_CLOSE_FRACTION( // x < lower
171     pdf(triangular_distribution<RealType>(0, 1, 1), static_cast<RealType>(2)),
172     static_cast<RealType>(0),
173     tolerance);
174
175   BOOST_CHECK_CLOSE_FRACTION( // x < middle so Wiki says special case pdf = 2 * x
176     pdf(triangular_distribution<RealType>(0, 1, 1), static_cast<RealType>(0.25)),
177     static_cast<RealType>(0.5),
178     tolerance);
179
180   BOOST_CHECK_CLOSE_FRACTION( // x < middle so Wiki says special case cdf = x * x
181     cdf(triangular_distribution<RealType>(0, 1, 1), static_cast<RealType>(0.25)),
182     static_cast<RealType>(0.25 * 0.25),
183     tolerance);
184
185   // triangular_distribution<RealType>() (0, 0.5, 1) mode == middle.
186   BOOST_CHECK_CLOSE_FRACTION( // x == lower
187     pdf(triangular_distribution<RealType>(0, 0.5, 1), static_cast<RealType>(0)),
188     static_cast<RealType>(0),
189     tolerance);
190
191   BOOST_CHECK_CLOSE_FRACTION( // x == upper
192     pdf(triangular_distribution<RealType>(0, 0.5, 1), static_cast<RealType>(1)),
193     static_cast<RealType>(0),
194     tolerance);
195
196   BOOST_CHECK_CLOSE_FRACTION( // x > upper
197     pdf(triangular_distribution<RealType>(0, 0.5, 1), static_cast<RealType>(-1)),
198     static_cast<RealType>(0),
199     tolerance);
200   BOOST_CHECK_CLOSE_FRACTION( // x < lower
201     pdf(triangular_distribution<RealType>(0, 0.5, 1), static_cast<RealType>(2)),
202     static_cast<RealType>(0),
203     tolerance);
204
205   BOOST_CHECK_CLOSE_FRACTION( // x == mode
206     pdf(triangular_distribution<RealType>(0, 0.5, 1), static_cast<RealType>(0.5)),
207     static_cast<RealType>(2),
208     tolerance);
209
210   BOOST_CHECK_CLOSE_FRACTION( // x == half mode
211     pdf(triangular_distribution<RealType>(0, 0.5, 1), static_cast<RealType>(0.25)),
212     static_cast<RealType>(1),
213     tolerance);
214   BOOST_CHECK_CLOSE_FRACTION( // x == half mode
215     pdf(triangular_distribution<RealType>(0, 0.5, 1), static_cast<RealType>(0.75)),
216     static_cast<RealType>(1),
217     tolerance);
218
219   if(std::numeric_limits<RealType>::has_infinity)
220   { // BOOST_CHECK tests for infinity using std::numeric_limits<>::infinity()
221     // Note that infinity is not implemented for real_concept, so these tests
222     // are only done for types, like built-in float, double.. that have infinity.
223     // Note that these assume that  BOOST_MATH_OVERFLOW_ERROR_POLICY is NOT throw_on_error.
224     // #define BOOST_MATH_OVERFLOW_ERROR_POLICY == throw_on_error would give a throw here.
225     // #define BOOST_MATH_DOMAIN_ERROR_POLICY == throw_on_error IS defined, so the throw path
226     // of error handling is tested below with BOOST_MATH_CHECK_THROW tests.
227
228     BOOST_MATH_CHECK_THROW( // x == infinity NOT OK.
229       pdf(triangular_distribution<RealType>(0, 0, 1), static_cast<RealType>(std::numeric_limits<RealType>::infinity())),
230       std::domain_error);
231
232     BOOST_MATH_CHECK_THROW( // x == minus infinity not OK too.
233       pdf(triangular_distribution<RealType>(0, 0, 1), static_cast<RealType>(-std::numeric_limits<RealType>::infinity())),
234       std::domain_error);
235   }
236   if(std::numeric_limits<RealType>::has_quiet_NaN)
237   { // BOOST_CHECK tests for NaN using std::numeric_limits<>::has_quiet_NaN() - should throw.
238     BOOST_MATH_CHECK_THROW(
239       pdf(triangular_distribution<RealType>(0, 0, 1), static_cast<RealType>(std::numeric_limits<RealType>::quiet_NaN())),
240       std::domain_error);
241     BOOST_MATH_CHECK_THROW(
242       pdf(triangular_distribution<RealType>(0, 0, 1), static_cast<RealType>(-std::numeric_limits<RealType>::quiet_NaN())),
243       std::domain_error);
244   } // test for x = NaN using std::numeric_limits<>::quiet_NaN()
245
246   // cdf
247   BOOST_CHECK_EQUAL( // x < lower
248     cdf(triangular_distribution<RealType>(0, 1, 1), static_cast<RealType>(-1)),
249     static_cast<RealType>(0) );
250   BOOST_CHECK_CLOSE_FRACTION( // x == lower
251     cdf(triangular_distribution<RealType>(0, 1, 1), static_cast<RealType>(0)),
252     static_cast<RealType>(0),
253     tolerance);
254   BOOST_CHECK_CLOSE_FRACTION( // x == upper
255     cdf(triangular_distribution<RealType>(0, 1, 1), static_cast<RealType>(1)),
256     static_cast<RealType>(1),
257     tolerance);
258    BOOST_CHECK_EQUAL( // x > upper
259     cdf(triangular_distribution<RealType>(0, 1, 1), static_cast<RealType>(2)),
260     static_cast<RealType>(1));
261
262   BOOST_CHECK_CLOSE_FRACTION( // x == mode
263     cdf(triangular_distribution<RealType>(-1, 0, 1), static_cast<RealType>(0)),
264     //static_cast<RealType>((mode - lower) / (upper - lower)),
265     static_cast<RealType>(0.5),    // (0 --1) / (1 -- 1) = 0.5
266     tolerance);
267   BOOST_CHECK_CLOSE_FRACTION(
268     cdf(triangular_distribution<RealType>(0, 1, 1), static_cast<RealType>(0.9L)),
269     static_cast<RealType>(0.81L),
270     tolerance);
271
272   BOOST_CHECK_CLOSE_FRACTION(
273     cdf(triangular_distribution<RealType>(-1, 0, 1), static_cast<RealType>(-1)),
274     static_cast<RealType>(0),
275     tolerance);
276   BOOST_CHECK_CLOSE_FRACTION(
277     cdf(triangular_distribution<RealType>(-1, 0, 1), static_cast<RealType>(-0.5L)),
278     static_cast<RealType>(0.125L),
279     tolerance);
280   BOOST_CHECK_CLOSE_FRACTION(
281     cdf(triangular_distribution<RealType>(-1, 0, 1), static_cast<RealType>(0)),
282     static_cast<RealType>(0.5),
283     tolerance);
284   BOOST_CHECK_CLOSE_FRACTION(
285     cdf(triangular_distribution<RealType>(-1, 0, 1), static_cast<RealType>(+0.5L)),
286     static_cast<RealType>(0.875L),
287     tolerance);
288   BOOST_CHECK_CLOSE_FRACTION(
289     cdf(triangular_distribution<RealType>(-1, 0, 1), static_cast<RealType>(1)),
290     static_cast<RealType>(1),
291     tolerance);
292
293    // cdf complement
294   BOOST_CHECK_EQUAL( // x < lower
295     cdf(complement(triangular_distribution<RealType>(0, 0, 1), static_cast<RealType>(-1))),
296     static_cast<RealType>(1));
297   BOOST_CHECK_EQUAL( // x == lower
298     cdf(complement(triangular_distribution<RealType>(0, 0, 1), static_cast<RealType>(0))),
299     static_cast<RealType>(1));
300
301   BOOST_CHECK_EQUAL( // x == mode
302     cdf(complement(triangular_distribution<RealType>(-1, 0, 1), static_cast<RealType>(0))),
303     static_cast<RealType>(0.5));
304
305   BOOST_CHECK_EQUAL( // x == mode
306     cdf(complement(triangular_distribution<RealType>(0, 0, 1), static_cast<RealType>(0))),
307     static_cast<RealType>(1));
308   BOOST_CHECK_EQUAL( // x == mode
309     cdf(complement(triangular_distribution<RealType>(0, 1, 1), static_cast<RealType>(1))),
310     static_cast<RealType>(0));
311
312   BOOST_CHECK_EQUAL( // x > upper
313     cdf(complement(triangular_distribution<RealType>(0, 0, 1), static_cast<RealType>(2))),
314     static_cast<RealType>(0));
315   BOOST_CHECK_EQUAL( // x == upper
316     cdf(complement(triangular_distribution<RealType>(0, 0, 1), static_cast<RealType>(1))),
317     static_cast<RealType>(0));
318
319   BOOST_CHECK_CLOSE_FRACTION( // x = -0.5
320     cdf(complement(triangular_distribution<RealType>(-1, 0, 1), static_cast<RealType>(-0.5))),
321     static_cast<RealType>(0.875L),
322     tolerance);
323
324   BOOST_CHECK_CLOSE_FRACTION( // x = +0.5
325     cdf(complement(triangular_distribution<RealType>(-1, 0, 1), static_cast<RealType>(0.5))),
326     static_cast<RealType>(0.125),
327     tolerance);
328
329   triangular_distribution<RealType> triang; // Using typedef == triangular_distribution<double> tristd;
330   triangular_distribution<RealType> tristd(0, 0.5, 1); // 'Standard' triangular distribution.
331
332   BOOST_CHECK_CLOSE_FRACTION( // median of Standard triangular is sqrt(mode/2) if c > 1/2 else 1 - sqrt((1-c)/2)
333     median(tristd),
334     static_cast<RealType>(0.5),
335     tolerance);
336   triangular_distribution<RealType> tri011(0, 1, 1); // Using default RealType double.
337   triangular_distribution<RealType> tri0q1(0, 0.25, 1); // mode is near bottom.
338   triangular_distribution<RealType> tri0h1(0, 0.5, 1); // Equilateral triangle - mode is the middle.
339   triangular_distribution<RealType> trim12(-1, -0.5, 2); // mode is negative.
340
341   BOOST_CHECK_CLOSE_FRACTION(cdf(tri0q1, 0.02L), static_cast<RealType>(0.0016L), tolerance);
342   BOOST_CHECK_CLOSE_FRACTION(cdf(tri0q1, 0.5L), static_cast<RealType>(0.66666666666666666666666666666666666666666666667L), tolerance);
343   BOOST_CHECK_CLOSE_FRACTION(cdf(tri0q1, 0.98L), static_cast<RealType>(0.9994666666666666666666666666666666666666666666L), tolerance);
344
345   // quantile
346   BOOST_CHECK_CLOSE_FRACTION(quantile(tri0q1, static_cast<RealType>(0.0016L)), static_cast<RealType>(0.02L), tol5eps);
347   BOOST_CHECK_CLOSE_FRACTION(quantile(tri0q1, static_cast<RealType>(0.66666666666666666666666666666666666666666666667L)), static_cast<RealType>(0.5), tol5eps);
348   BOOST_CHECK_CLOSE_FRACTION(quantile(complement(tri0q1, static_cast<RealType>(0.3333333333333333333333333333333333333333333333333L))), static_cast<RealType>(0.5), tol5eps);
349   BOOST_CHECK_CLOSE_FRACTION(quantile(tri0q1, static_cast<RealType>(0.999466666666666666666666666666666666666666666666666L)), static_cast<RealType>(98) / 100, 10 * tol5eps);
350
351   BOOST_CHECK_CLOSE_FRACTION(pdf(trim12, 0), static_cast<RealType>(0.533333333333333333333333333333333333333333333L), tol5eps);
352   BOOST_CHECK_CLOSE_FRACTION(cdf(trim12, 0), static_cast<RealType>(0.466666666666666666666666666666666666666666667L), tol5eps);
353   BOOST_CHECK_CLOSE_FRACTION(cdf(complement(trim12, 0)), static_cast<RealType>(1 - 0.466666666666666666666666666666666666666666667L), tol5eps);
354
355   BOOST_CHECK_CLOSE_FRACTION(quantile(complement(tri0q1, static_cast<RealType>(1 - 0.999466666666666666666666666666666666666666666666L))), static_cast<RealType>(0.98L), 10 * tol5eps);
356   BOOST_CHECK_CLOSE_FRACTION(quantile(complement(tri0h1, static_cast<RealType>(1))), static_cast<RealType>(0), tol5eps);
357   BOOST_CHECK_CLOSE_FRACTION(quantile(complement(tri0h1, static_cast<RealType>(0.5))), static_cast<RealType>(0.5), tol5eps); // OK
358   BOOST_CHECK_CLOSE_FRACTION(quantile(complement(tri0h1, static_cast<RealType>(1 - 0.02L))), static_cast<RealType>(0.1L), tol5eps);
359   BOOST_CHECK_CLOSE_FRACTION(quantile(complement(tri0h1, static_cast<RealType>(1 - 0.98L))), static_cast<RealType>(0.9L), tol5eps);
360   BOOST_CHECK_CLOSE_FRACTION(quantile(complement(tri0h1, 0)), static_cast<RealType>(1), tol5eps);
361
362   RealType xs [] = {0, 0.01L, 0.02L, 0.05L, 0.1L, 0.2L, 0.3L, 0.4L, 0.5L, 0.6L, 0.7L, 0.8L, 0.9L, 0.95L, 0.98L, 0.99L, 1};
363
364   const triangular_distribution<RealType>& distr = triang;
365   BOOST_CHECK_CLOSE_FRACTION(quantile(complement(distr, 1.)), static_cast<RealType>(-1), tol5eps);
366   const triangular_distribution<RealType>* distp = &triang;
367   BOOST_CHECK_CLOSE_FRACTION(quantile(complement(*distp, 1.)), static_cast<RealType>(-1), tol5eps);
368
369   const triangular_distribution<RealType>* dists [] = {&tristd, &tri011, &tri0q1, &tri0h1, &trim12};
370   BOOST_CHECK_CLOSE_FRACTION(quantile(complement(*dists[1], 1.)), static_cast<RealType>(0), tol5eps);
371
372    for (int i = 0; i < 5; i++)
373   {
374     const triangular_distribution<RealType>* const dist = dists[i];
375     // cout << "Distribution " << i << endl;
376     BOOST_CHECK_CLOSE_FRACTION(quantile(*dists[i], 0.5L), quantile(complement(*dist, 0.5L)),  tol5eps);
377     BOOST_CHECK_CLOSE_FRACTION(quantile(*dists[i], 0.98L), quantile(complement(*dist, 1.L - 0.98L)),tol5eps);
378     BOOST_CHECK_CLOSE_FRACTION(quantile(*dists[i], 0.98L), quantile(complement(*dist, 1.L - 0.98L)),tol5eps);
379   } // for i
380
381    // quantile complement
382   for (int i = 0; i < 5; i++)
383   {
384     const triangular_distribution<RealType>* const dist = dists[i];
385     //cout << "Distribution " << i << endl;
386     BOOST_CHECK_EQUAL(quantile(complement(*dists[i], 1.)), quantile(*dists[i], 0.));
387     for (unsigned j = 0; j < sizeof(xs) /sizeof(RealType); j++)
388     {
389       RealType x = xs[j];
390       BOOST_CHECK_CLOSE_FRACTION(quantile(*dists[i], x), quantile(complement(*dist, 1 - x)),  tol5eps);
391     } // for j
392   } // for i
393
394
395   check_triangular(
396     static_cast<RealType>(0),       // lower
397     static_cast<RealType>(0.5),     // mode
398     static_cast<RealType>(1),       // upper
399     static_cast<RealType>(0.5),     // x
400     static_cast<RealType>(0.5),     // p
401     static_cast<RealType>(1 - 0.5), // q
402     tolerance);
403
404   // Some Not-standard triangular tests.
405   check_triangular(
406     static_cast<RealType>(-1),    // lower
407     static_cast<RealType>(0),     // mode
408     static_cast<RealType>(1),     // upper
409     static_cast<RealType>(0),     // x
410     static_cast<RealType>(0.5),   // p
411     static_cast<RealType>(1 - 0.5), // q = 1 - p
412     tolerance);
413
414   check_triangular(
415     static_cast<RealType>(1),       // lower
416     static_cast<RealType>(1),       // mode
417     static_cast<RealType>(3),       // upper
418     static_cast<RealType>(2),    // x
419     static_cast<RealType>(0.75),     // p
420     static_cast<RealType>(1 - 0.75), // q = 1 - p
421     tolerance);
422
423   check_triangular(
424     static_cast<RealType>(-1),    // lower
425     static_cast<RealType>(1),       // mode
426     static_cast<RealType>(2),     // upper
427     static_cast<RealType>(1),     // x
428     static_cast<RealType>(0.66666666666666666666666666666666666666666667L),   // p
429     static_cast<RealType>(0.33333333333333333333333333333333333333333333L), // q = 1 - p
430     tolerance);
431   tolerance = (std::max)(
432     boost::math::tools::epsilon<RealType>(),
433     static_cast<RealType>(boost::math::tools::epsilon<double>())) * 10; // 10 eps as a fraction.
434   cout << "Tolerance (as fraction) for type " << typeid(RealType).name()  << " is " << tolerance << "." << endl;
435   
436     triangular_distribution<RealType> tridef; // (-1, 0, 1) // Default distribution.
437     RealType x = static_cast<RealType>(0.5);
438     using namespace std; // ADL of std names.
439     // mean:
440     BOOST_CHECK_CLOSE_FRACTION(
441       mean(tridef), static_cast<RealType>(0), tolerance);
442     // variance:
443     BOOST_CHECK_CLOSE_FRACTION(
444       variance(tridef), static_cast<RealType>(0.16666666666666666666666666666666666666666667L), tolerance);
445     // was 0.0833333333333333333333333333333333333333333L
446
447     // std deviation:
448     BOOST_CHECK_CLOSE_FRACTION(
449       standard_deviation(tridef), sqrt(variance(tridef)), tolerance);
450     // hazard:
451     BOOST_CHECK_CLOSE_FRACTION(
452       hazard(tridef, x), pdf(tridef, x) / cdf(complement(tridef, x)), tolerance);
453     // cumulative hazard:
454     BOOST_CHECK_CLOSE_FRACTION(
455       chf(tridef, x), -log(cdf(complement(tridef, x))), tolerance);
456     // coefficient_of_variation:
457     if (mean(tridef) != 0)
458     {
459       BOOST_CHECK_CLOSE_FRACTION(
460         coefficient_of_variation(tridef), standard_deviation(tridef) / mean(tridef), tolerance);
461     }
462     // mode:
463     BOOST_CHECK_CLOSE_FRACTION(
464       mode(tridef), static_cast<RealType>(0), tolerance);
465     // skewness:
466     BOOST_CHECK_CLOSE_FRACTION(
467       median(tridef), static_cast<RealType>(0), tolerance);
468     // https://reference.wolfram.com/language/ref/Skewness.html  skewness{-1, 0, +1} = 0
469     // skewness[triangulardistribution{-1, 0, +1}] does not compute a result.
470     // skewness[triangulardistribution{0, +1}] result == 0
471     // skewness[normaldistribution{0,1}] result == 0
472
473     BOOST_CHECK_EQUAL(
474       skewness(tridef), static_cast<RealType>(0));
475     // kurtosis:
476     BOOST_CHECK_CLOSE_FRACTION(
477       kurtosis_excess(tridef), kurtosis(tridef) - static_cast<RealType>(3L), tolerance);
478     // kurtosis excess = kurtosis - 3;
479     BOOST_CHECK_CLOSE_FRACTION(
480       kurtosis_excess(tridef), static_cast<RealType>(-0.6), tolerance); // Constant value of -3/5 for all distributions.
481
482   {
483     triangular_distribution<RealType> tri01(0, 1, 1); //  Asymmetric 0, 1, 1 distribution.
484     RealType x = static_cast<RealType>(0.5);
485     using namespace std; // ADL of std names.
486                          // mean:
487     BOOST_CHECK_CLOSE_FRACTION(
488       mean(tri01), static_cast<RealType>(0.66666666666666666666666666666666666666666666666667L), tolerance);
489     // variance: N[variance[triangulardistribution{0, 1}, 1], 50]
490     BOOST_CHECK_CLOSE_FRACTION(
491       variance(tri01), static_cast<RealType>(0.055555555555555555555555555555555555555555555555556L), tolerance);
492     // std deviation:
493     BOOST_CHECK_CLOSE_FRACTION(
494       standard_deviation(tri01), sqrt(variance(tri01)), tolerance);
495     // hazard:
496     BOOST_CHECK_CLOSE_FRACTION(
497       hazard(tri01, x), pdf(tri01, x) / cdf(complement(tri01, x)), tolerance);
498     // cumulative hazard:
499     BOOST_CHECK_CLOSE_FRACTION(
500       chf(tri01, x), -log(cdf(complement(tri01, x))), tolerance);
501     // coefficient_of_variation:
502     if (mean(tri01) != 0)
503     {
504       BOOST_CHECK_CLOSE_FRACTION(
505         coefficient_of_variation(tri01), standard_deviation(tri01) / mean(tri01), tolerance);
506     }
507     // mode:
508     BOOST_CHECK_CLOSE_FRACTION(
509       mode(tri01), static_cast<RealType>(1), tolerance);
510     // skewness:
511     BOOST_CHECK_CLOSE_FRACTION(
512       median(tri01), static_cast<RealType>(0.70710678118654752440084436210484903928483593768847L), tolerance);
513
514     // https://reference.wolfram.com/language/ref/Skewness.html
515     // N[skewness[triangulardistribution{0, 1}, 1], 50]
516     BOOST_CHECK_CLOSE_FRACTION(
517       skewness(tri01), static_cast<RealType>(-0.56568542494923801952067548968387923142786875015078L), tolerance);
518     // kurtosis:
519     BOOST_CHECK_CLOSE_FRACTION(
520       kurtosis_excess(tri01), kurtosis(tri01) - static_cast<RealType>(3L), tolerance);
521     // kurtosis excess = kurtosis - 3;
522     BOOST_CHECK_CLOSE_FRACTION(
523       kurtosis_excess(tri01), static_cast<RealType>(-0.6), tolerance); // Constant value of -3/5 for all distributions.
524   } // tri01 tests
525
526   if(std::numeric_limits<RealType>::has_infinity)
527   { // BOOST_CHECK tests for infinity using std::numeric_limits<>::infinity()
528     // Note that infinity is not implemented for real_concept, so these tests
529     // are only done for types, like built-in float, double.. that have infinity.
530     // Note that these assume that BOOST_MATH_OVERFLOW_ERROR_POLICY is NOT throw_on_error.
531     // #define BOOST_MATH_OVERFLOW_ERROR_POLICY == throw_on_error would give a throw here.
532     // #define BOOST_MATH_DOMAIN_ERROR_POLICY == throw_on_error IS defined, so the throw path
533     // of error handling is tested below with BOOST_MATH_CHECK_THROW tests.
534
535     using boost::math::policies::policy;
536     using boost::math::policies::domain_error;
537     using boost::math::policies::ignore_error;
538
539     // Define a (bad?) policy to ignore domain errors ('bad' arguments):
540     typedef policy<domain_error<ignore_error> > inf_policy; // domain error returns infinity.
541     triangular_distribution<RealType, inf_policy> tridef_inf(-1, 0., 1);
542     // But can't use BOOST_CHECK_EQUAL(?, quiet_NaN)
543     using boost::math::isnan;
544     BOOST_CHECK((isnan)(pdf(tridef_inf, std::numeric_limits<RealType>::infinity())));
545   } // test for infinity using std::numeric_limits<>::infinity()
546   else
547   { // real_concept case, does has_infinfity == false, so can't check it throws.
548     // cout << std::numeric_limits<RealType>::infinity() << ' '
549     // << (boost::math::fpclassify)(std::numeric_limits<RealType>::infinity()) << endl;
550     // value of std::numeric_limits<RealType>::infinity() is zero, so FPclassify is zero,
551     // so (boost::math::isfinite)(std::numeric_limits<RealType>::infinity()) does not detect infinity.
552     // so these tests would never throw.
553     //BOOST_MATH_CHECK_THROW(pdf(tridef, std::numeric_limits<RealType>::infinity()),  std::domain_error);
554     //BOOST_MATH_CHECK_THROW(pdf(tridef, std::numeric_limits<RealType>::quiet_NaN()),  std::domain_error);
555     // BOOST_MATH_CHECK_THROW(pdf(tridef, boost::math::tools::max_value<RealType>() * 2),  std::domain_error); // Doesn't throw.
556     BOOST_CHECK_EQUAL(pdf(tridef, boost::math::tools::max_value<RealType>()), 0);
557   }
558   // Special cases:
559   BOOST_CHECK(pdf(tridef, -1) == 0);
560   BOOST_CHECK(pdf(tridef, 1) == 0);
561   BOOST_CHECK(cdf(tridef, 0) == 0.5);
562   BOOST_CHECK(pdf(tridef, 1) == 0);
563   BOOST_CHECK(cdf(tridef, 1) == 1);
564   BOOST_CHECK(cdf(complement(tridef, -1)) == 1);
565   BOOST_CHECK(cdf(complement(tridef, 1)) == 0);
566   BOOST_CHECK(quantile(tridef, 1) == 1);
567   BOOST_CHECK(quantile(complement(tridef, 1)) == -1);
568
569   BOOST_CHECK_EQUAL(support(trim12).first, trim12.lower());
570   BOOST_CHECK_EQUAL(support(trim12).second, trim12.upper());
571
572   // Error checks:
573   if(std::numeric_limits<RealType>::has_quiet_NaN)
574   { // BOOST_CHECK tests for quiet_NaN (not for real_concept, for example - see notes above).
575     BOOST_MATH_CHECK_THROW(triangular_distribution<RealType>(0, std::numeric_limits<RealType>::quiet_NaN()), std::domain_error);
576     BOOST_MATH_CHECK_THROW(triangular_distribution<RealType>(0, -std::numeric_limits<RealType>::quiet_NaN()), std::domain_error);
577   }
578   BOOST_MATH_CHECK_THROW(triangular_distribution<RealType>(1, 0), std::domain_error); // lower > upper!
579
580   check_out_of_range<triangular_distribution<RealType> >(-1, 0, 1);
581 } // template <class RealType>void test_spots(RealType)
582
583 BOOST_AUTO_TEST_CASE( test_main )
584 {
585   //  double toleps = std::numeric_limits<double>::epsilon(); // 5 eps as a fraction.
586   double tol5eps = std::numeric_limits<double>::epsilon() * 5; // 5 eps as a fraction.
587   // double tol50eps = std::numeric_limits<double>::epsilon() * 50; // 50 eps as a fraction.
588   double tol500eps = std::numeric_limits<double>::epsilon() * 500; // 500 eps as a fraction.
589
590   // Check that can construct triangular distribution using the two convenience methods:
591   using namespace boost::math;
592   triangular triang; // Using typedef
593   // == triangular_distribution<double> triang;
594
595   BOOST_CHECK_EQUAL(triang.lower(), -1); // Check default.
596   BOOST_CHECK_EQUAL(triang.mode(), 0);
597   BOOST_CHECK_EQUAL(triang.upper(), 1);
598
599   triangular tristd (0, 0.5, 1); // Using typedef
600
601   BOOST_CHECK_EQUAL(tristd.lower(), 0);
602   BOOST_CHECK_EQUAL(tristd.mode(), 0.5);
603   BOOST_CHECK_EQUAL(tristd.upper(), 1);
604
605   //cout << "X range from " << range(tristd).first << " to " << range(tristd).second << endl;
606   //cout << "Supported from "<< support(tristd).first << ' ' << support(tristd).second << endl;
607
608   BOOST_CHECK_EQUAL(support(tristd).first, tristd.lower());
609   BOOST_CHECK_EQUAL(support(tristd).second, tristd.upper());
610
611   triangular_distribution<> tri011(0, 1, 1); // Using default RealType double.
612   // mode is upper
613   BOOST_CHECK_EQUAL(tri011.lower(), 0); // Check defaults again.
614   BOOST_CHECK_EQUAL(tri011.mode(), 1); // Check defaults again.
615   BOOST_CHECK_EQUAL(tri011.upper(), 1);
616   BOOST_CHECK_EQUAL(mode(tri011), 1);
617
618   BOOST_CHECK_EQUAL(pdf(tri011, 0), 0);
619   BOOST_CHECK_EQUAL(pdf(tri011, 0.1), 0.2);
620   BOOST_CHECK_EQUAL(pdf(tri011, 0.5), 1);
621   BOOST_CHECK_EQUAL(pdf(tri011, 0.9), 1.8);
622   BOOST_CHECK_EQUAL(pdf(tri011, 1), 2);
623
624   BOOST_CHECK_EQUAL(cdf(tri011, 0), 0);
625   BOOST_CHECK_CLOSE_FRACTION(cdf(tri011, 0.1), 0.01, tol5eps);
626   BOOST_CHECK_EQUAL(cdf(tri011, 0.5), 0.25);
627   BOOST_CHECK_EQUAL(cdf(tri011, 0.9), 0.81);
628   BOOST_CHECK_EQUAL(cdf(tri011, 1), 1);
629   BOOST_CHECK_EQUAL(cdf(tri011, 9), 1);
630   BOOST_CHECK_EQUAL(mean(tri011), 0.666666666666666666666666666666666666666666666666667);
631   BOOST_CHECK_EQUAL(variance(tri011), 1./18.);
632
633   triangular tri0h1(0, 0.5, 1); // Equilateral triangle - mode is the middle.
634   BOOST_CHECK_EQUAL(tri0h1.lower(), 0);
635   BOOST_CHECK_EQUAL(tri0h1.mode(), 0.5);
636   BOOST_CHECK_EQUAL(tri0h1.upper(), 1);
637   BOOST_CHECK_EQUAL(mean(tri0h1), 0.5);
638   BOOST_CHECK_EQUAL(mode(tri0h1), 0.5);
639   BOOST_CHECK_EQUAL(pdf(tri0h1, -1), 0);
640   BOOST_CHECK_EQUAL(cdf(tri0h1, -1), 0);
641   BOOST_CHECK_EQUAL(pdf(tri0h1, 1), 0);
642   BOOST_CHECK_EQUAL(pdf(tri0h1, 999), 0);
643   BOOST_CHECK_EQUAL(cdf(tri0h1, 999), 1);
644   BOOST_CHECK_EQUAL(cdf(tri0h1, 1), 1);
645   BOOST_CHECK_CLOSE_FRACTION(cdf(tri0h1, 0.1), 0.02, tol5eps);
646   BOOST_CHECK_EQUAL(cdf(tri0h1, 0.5), 0.5);
647   BOOST_CHECK_CLOSE_FRACTION(cdf(tri0h1, 0.9), 0.98, tol5eps);
648
649   BOOST_CHECK_CLOSE_FRACTION(quantile(tri0h1, 0.), 0., tol5eps);
650   BOOST_CHECK_CLOSE_FRACTION(quantile(tri0h1, 0.02), 0.1, tol5eps);
651   BOOST_CHECK_CLOSE_FRACTION(quantile(tri0h1, 0.5), 0.5, tol5eps);
652   BOOST_CHECK_CLOSE_FRACTION(quantile(tri0h1, 0.98), 0.9, tol5eps);
653   BOOST_CHECK_CLOSE_FRACTION(quantile(tri0h1, 1.), 1., tol5eps);
654
655   triangular tri0q1(0, 0.25, 1); // mode is near bottom.
656   BOOST_CHECK_CLOSE_FRACTION(cdf(tri0q1, 0.02), 0.0016, tol5eps);
657   BOOST_CHECK_CLOSE_FRACTION(cdf(tri0q1, 0.5), 0.66666666666666666666666666666666666666666666667, tol5eps);
658   BOOST_CHECK_CLOSE_FRACTION(cdf(tri0q1, 0.98), 0.99946666666666661, tol5eps);
659
660   BOOST_CHECK_CLOSE_FRACTION(quantile(tri0q1, 0.0016), 0.02, tol5eps);
661   BOOST_CHECK_CLOSE_FRACTION(quantile(tri0q1, 0.66666666666666666666666666666666666666666666667), 0.5, tol5eps);
662   BOOST_CHECK_CLOSE_FRACTION(quantile(complement(tri0q1, 0.3333333333333333333333333333333333333333333333333)), 0.5, tol5eps);
663   BOOST_CHECK_CLOSE_FRACTION(quantile(tri0q1, 0.99946666666666661), 0.98, 10 * tol5eps);
664
665   triangular trim12(-1, -0.5, 2); // mode is negative.
666   BOOST_CHECK_CLOSE_FRACTION(pdf(trim12, 0), 0.533333333333333333333333333333333333333333333, tol5eps);
667   BOOST_CHECK_CLOSE_FRACTION(cdf(trim12, 0), 0.466666666666666666666666666666666666666666667, tol5eps);
668   BOOST_CHECK_CLOSE_FRACTION(cdf(complement(trim12, 0)), 1 - 0.466666666666666666666666666666666666666666667, tol5eps);
669
670   BOOST_CHECK_CLOSE_FRACTION(quantile(complement(tri0q1, 1 - 0.99946666666666661)), 0.98, 10 * tol5eps);
671   BOOST_CHECK_CLOSE_FRACTION(quantile(complement(tri0h1, 1.)), 0., tol5eps);
672   BOOST_CHECK_CLOSE_FRACTION(quantile(complement(tri0h1, 0.5)), 0.5, tol5eps); // OK
673   BOOST_CHECK_CLOSE_FRACTION(quantile(complement(tri0h1, 1. - 0.02)), 0.1, tol5eps);
674   BOOST_CHECK_CLOSE_FRACTION(quantile(complement(tri0h1, 1. - 0.98)), 0.9, tol5eps);
675   BOOST_CHECK_CLOSE_FRACTION(quantile(complement(tri0h1, 0)), 1., tol5eps);
676
677   double xs [] = {0., 0.01, 0.02, 0.05, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 0.95, 0.98, 0.99, 1.};
678
679   const triangular_distribution<double>& distr = tristd;
680   BOOST_CHECK_CLOSE_FRACTION(quantile(complement(distr, 1.)), 0., tol5eps);
681   const triangular_distribution<double>* distp = &tristd;
682   BOOST_CHECK_CLOSE_FRACTION(quantile(complement(*distp, 1.)), 0., tol5eps);
683
684   const triangular_distribution<double>* dists [] = {&tristd, &tri011, &tri0q1, &tri0h1, &trim12};
685   BOOST_CHECK_CLOSE_FRACTION(quantile(complement(*dists[1], 1.)), 0., tol5eps);
686
687   for (int i = 0; i < 5; i++)
688   {
689     const triangular_distribution<double>* const dist = dists[i];
690     cout << "Distribution " << i << endl;
691     BOOST_CHECK_EQUAL(quantile(complement(*dists[i], 1.)), quantile(*dists[i], 0.));
692     BOOST_CHECK_CLOSE_FRACTION(quantile(*dists[i], 0.5), quantile(complement(*dist, 0.5)),  tol5eps); // OK
693     BOOST_CHECK_CLOSE_FRACTION(quantile(*dists[i], 0.98), quantile(complement(*dist, 1. - 0.98)),tol5eps);
694     // cout << setprecision(17) <<  median(*dist) << endl;
695   }
696
697   cout << showpos << setprecision(2) << endl;
698
699   //triangular_distribution<double>& dist = trim12;
700   for (unsigned i = 0; i < sizeof(xs) /sizeof(double); i++)
701   {
702     double x = xs[i] * (trim12.upper() - trim12.lower()) + trim12.lower();
703     double dx = cdf(trim12, x);
704     double cx = cdf(complement(trim12, x));
705     //cout << fixed << showpos << setprecision(3)
706     //  << xs[i] << ", " << x << ",  " << pdf(trim12, x) << ",  " << dx << ",  " << cx << ",, " ;
707
708     BOOST_CHECK_CLOSE_FRACTION(cx, 1 - dx, tol500eps); // cx == 1 - dx
709
710     // << setprecision(2) << scientific << cr - x << ", " // difference x - quan(cdf)
711     // << setprecision(3) << fixed
712     // << quantile(trim12, dx) << ", "
713     // << quantile(complement(trim12, 1 - dx)) << ", "
714     // << quantile(complement(trim12, cx)) << ", "
715     // << endl;
716     BOOST_CHECK_CLOSE_FRACTION(quantile(trim12, dx), quantile(complement(trim12, 1 - dx)), tol500eps);
717   }
718   cout << endl;
719   // Basic sanity-check spot values.
720   // (Parameter value, arbitrarily zero, only communicates the floating point type).
721   test_spots(0.0F); // Test float. OK at decdigits = 0 tolerance = 0.0001 %
722   test_spots(0.0); // Test double. OK at decdigits 7, tolerance = 1e07 %
723   #ifndef BOOST_MATH_NO_LONG_DOUBLE_MATH_FUNCTIONS
724     test_spots(0.0L); // Test long double.
725   #if !BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x0582))
726     test_spots(boost::math::concepts::real_concept(0.)); // Test real concept.
727   #endif
728   #else
729      std::cout << "<note>The long double tests have been disabled on this platform "
730         "either because the long double overloads of the usual math functions are "
731         "not available at all, or because they are too inaccurate for these tests "
732         "to pass.</note>" << std::endl;
733   #endif
734
735   
736 } // BOOST_AUTO_TEST_CASE( test_main )
737
738 /*
739
740 Output:
741
742 Autorun "i:\boost-06-05-03-1300\libs\math\test\Math_test\debug\test_triangular.exe"
743 Running 1 test case...
744 Distribution 0
745 Distribution 1
746 Distribution 2
747 Distribution 3
748 Distribution 4
749 Tolerance for type float is 5.96046e-007.
750 Tolerance for type double is 1.11022e-015.
751 Tolerance for type long double is 1.11022e-015.
752 Tolerance for type class boost::math::concepts::real_concept is 1.11022e-015.
753 *** No errors detected
754
755
756
757 */
758
759
760
761