1// Copyright John Maddock 2006, 2007.
2// Copyright Paul A. Bristow 2006, 2007.
3// Use, modification and distribution are subject to the
4// Boost Software License, Version 1.0. (See accompanying file
5// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6
7#ifndef BOOST_STATS_TRIANGULAR_HPP
8#define BOOST_STATS_TRIANGULAR_HPP
9
10// http://mathworld.wolfram.com/TriangularDistribution.html
11// Note that the 'constructors' defined by Wolfram are difference from those here,
12// for example
13// N[variance[triangulardistribution{1, +2}, 1.5], 50] computes
14// 0.041666666666666666666666666666666666666666666666667
15// TriangularDistribution{1, +2}, 1.5 is the analog of triangular_distribution(1, 1.5, 2)
16
17// http://en.wikipedia.org/wiki/Triangular_distribution
18
19#include <boost/math/distributions/fwd.hpp>
20#include <boost/math/special_functions/expm1.hpp>
21#include <boost/math/distributions/detail/common_error_handling.hpp>
22#include <boost/math/distributions/complement.hpp>
23#include <boost/math/constants/constants.hpp>
24
25#include <utility>
26
27namespace boost{ namespace math
28{
29 namespace detail
30 {
31 template <class RealType, class Policy>
32 inline bool check_triangular_lower(
33 const char* function,
34 RealType lower,
35 RealType* result, const Policy& pol)
36 {
37 if((boost::math::isfinite)(lower))
38 { // Any finite value is OK.
39 return true;
40 }
41 else
42 { // Not finite: infinity or NaN.
43 *result = policies::raise_domain_error<RealType>(
44 function,
45 "Lower parameter is %1%, but must be finite!", lower, pol);
46 return false;
47 }
48 } // bool check_triangular_lower(
49
50 template <class RealType, class Policy>
51 inline bool check_triangular_mode(
52 const char* function,
53 RealType mode,
54 RealType* result, const Policy& pol)
55 {
56 if((boost::math::isfinite)(mode))
57 { // any finite value is OK.
58 return true;
59 }
60 else
61 { // Not finite: infinity or NaN.
62 *result = policies::raise_domain_error<RealType>(
63 function,
64 "Mode parameter is %1%, but must be finite!", mode, pol);
65 return false;
66 }
67 } // bool check_triangular_mode(
68
69 template <class RealType, class Policy>
70 inline bool check_triangular_upper(
71 const char* function,
72 RealType upper,
73 RealType* result, const Policy& pol)
74 {
75 if((boost::math::isfinite)(upper))
76 { // any finite value is OK.
77 return true;
78 }
79 else
80 { // Not finite: infinity or NaN.
81 *result = policies::raise_domain_error<RealType>(
82 function,
83 "Upper parameter is %1%, but must be finite!", upper, pol);
84 return false;
85 }
86 } // bool check_triangular_upper(
87
88 template <class RealType, class Policy>
89 inline bool check_triangular_x(
90 const char* function,
91 RealType const& x,
92 RealType* result, const Policy& pol)
93 {
94 if((boost::math::isfinite)(x))
95 { // Any finite value is OK
96 return true;
97 }
98 else
99 { // Not finite: infinity or NaN.
100 *result = policies::raise_domain_error<RealType>(
101 function,
102 "x parameter is %1%, but must be finite!", x, pol);
103 return false;
104 }
105 } // bool check_triangular_x
106
107 template <class RealType, class Policy>
108 inline bool check_triangular(
109 const char* function,
110 RealType lower,
111 RealType mode,
112 RealType upper,
113 RealType* result, const Policy& pol)
114 {
115 if ((check_triangular_lower(function, lower, result, pol) == false)
116 || (check_triangular_mode(function, mode, result, pol) == false)
117 || (check_triangular_upper(function, upper, result, pol) == false))
118 { // Some parameter not finite.
119 return false;
120 }
121 else if (lower >= upper) // lower == upper NOT useful.
122 { // lower >= upper.
123 *result = policies::raise_domain_error<RealType>(
124 function,
125 "lower parameter is %1%, but must be less than upper!", lower, pol);
126 return false;
127 }
128 else
129 { // Check lower <= mode <= upper.
130 if (mode < lower)
131 {
132 *result = policies::raise_domain_error<RealType>(
133 function,
134 "mode parameter is %1%, but must be >= than lower!", lower, pol);
135 return false;
136 }
137 if (mode > upper)
138 {
139 *result = policies::raise_domain_error<RealType>(
140 function,
141 "mode parameter is %1%, but must be <= than upper!", upper, pol);
142 return false;
143 }
144 return true; // All OK.
145 }
146 } // bool check_triangular
147 } // namespace detail
148
149 template <class RealType = double, class Policy = policies::policy<> >
150 class triangular_distribution
151 {
152 public:
153 typedef RealType value_type;
154 typedef Policy policy_type;
155
156 triangular_distribution(RealType l_lower = -1, RealType l_mode = 0, RealType l_upper = 1)
157 : m_lower(l_lower), m_mode(l_mode), m_upper(l_upper) // Constructor.
158 { // Evans says 'standard triangular' is lower 0, mode 1/2, upper 1,
159 // has median sqrt(c/2) for c <=1/2 and 1 - sqrt(1-c)/2 for c >= 1/2
160 // But this -1, 0, 1 is more useful in most applications to approximate normal distribution,
161 // where the central value is the most likely and deviations either side equally likely.
162 RealType result;
163 detail::check_triangular("boost::math::triangular_distribution<%1%>::triangular_distribution",l_lower, l_mode, l_upper, &result, Policy());
164 }
165 // Accessor functions.
166 RealType lower()const
167 {
168 return m_lower;
169 }
170 RealType mode()const
171 {
172 return m_mode;
173 }
174 RealType upper()const
175 {
176 return m_upper;
177 }
178 private:
179 // Data members:
180 RealType m_lower; // distribution lower aka a
181 RealType m_mode; // distribution mode aka c
182 RealType m_upper; // distribution upper aka b
183 }; // class triangular_distribution
184
185 typedef triangular_distribution<double> triangular;
186
187 #ifdef __cpp_deduction_guides
188 template <class RealType>
189 triangular_distribution(RealType)->triangular_distribution<typename boost::math::tools::promote_args<RealType>::type>;
190 template <class RealType>
191 triangular_distribution(RealType,RealType)->triangular_distribution<typename boost::math::tools::promote_args<RealType>::type>;
192 template <class RealType>
193 triangular_distribution(RealType,RealType,RealType)->triangular_distribution<typename boost::math::tools::promote_args<RealType>::type>;
194 #endif
195
196 template <class RealType, class Policy>
197 inline const std::pair<RealType, RealType> range(const triangular_distribution<RealType, Policy>& /* dist */)
198 { // Range of permissible values for random variable x.
199 using boost::math::tools::max_value;
200 return std::pair<RealType, RealType>(-max_value<RealType>(), max_value<RealType>());
201 }
202
203 template <class RealType, class Policy>
204 inline const std::pair<RealType, RealType> support(const triangular_distribution<RealType, Policy>& dist)
205 { // Range of supported values for random variable x.
206 // This is range where cdf rises from 0 to 1, and outside it, the pdf is zero.
207 return std::pair<RealType, RealType>(dist.lower(), dist.upper());
208 }
209
210 template <class RealType, class Policy>
211 RealType pdf(const triangular_distribution<RealType, Policy>& dist, const RealType& x)
212 {
213 static const char* function = "boost::math::pdf(const triangular_distribution<%1%>&, %1%)";
214 RealType lower = dist.lower();
215 RealType mode = dist.mode();
216 RealType upper = dist.upper();
217 RealType result = 0; // of checks.
218 if(false == detail::check_triangular(function, lower, mode, upper, &result, Policy()))
219 {
220 return result;
221 }
222 if(false == detail::check_triangular_x(function, x, &result, Policy()))
223 {
224 return result;
225 }
226 if((x < lower) || (x > upper))
227 {
228 return 0;
229 }
230 if (x == lower)
231 { // (mode - lower) == 0 which would lead to divide by zero!
232 return (mode == lower) ? 2 / (upper - lower) : RealType(0);
233 }
234 else if (x == upper)
235 {
236 return (mode == upper) ? 2 / (upper - lower) : RealType(0);
237 }
238 else if (x <= mode)
239 {
240 return 2 * (x - lower) / ((upper - lower) * (mode - lower));
241 }
242 else
243 { // (x > mode)
244 return 2 * (upper - x) / ((upper - lower) * (upper - mode));
245 }
246 } // RealType pdf(const triangular_distribution<RealType, Policy>& dist, const RealType& x)
247
248 template <class RealType, class Policy>
249 inline RealType cdf(const triangular_distribution<RealType, Policy>& dist, const RealType& x)
250 {
251 static const char* function = "boost::math::cdf(const triangular_distribution<%1%>&, %1%)";
252 RealType lower = dist.lower();
253 RealType mode = dist.mode();
254 RealType upper = dist.upper();
255 RealType result = 0; // of checks.
256 if(false == detail::check_triangular(function, lower, mode, upper, &result, Policy()))
257 {
258 return result;
259 }
260 if(false == detail::check_triangular_x(function, x, &result, Policy()))
261 {
262 return result;
263 }
264 if((x <= lower))
265 {
266 return 0;
267 }
268 if (x >= upper)
269 {
270 return 1;
271 }
272 // else lower < x < upper
273 if (x <= mode)
274 {
275 return ((x - lower) * (x - lower)) / ((upper - lower) * (mode - lower));
276 }
277 else
278 {
279 return 1 - (upper - x) * (upper - x) / ((upper - lower) * (upper - mode));
280 }
281 } // RealType cdf(const triangular_distribution<RealType, Policy>& dist, const RealType& x)
282
283 template <class RealType, class Policy>
284 RealType quantile(const triangular_distribution<RealType, Policy>& dist, const RealType& p)
285 {
286 BOOST_MATH_STD_USING // for ADL of std functions (sqrt).
287 static const char* function = "boost::math::quantile(const triangular_distribution<%1%>&, %1%)";
288 RealType lower = dist.lower();
289 RealType mode = dist.mode();
290 RealType upper = dist.upper();
291 RealType result = 0; // of checks
292 if(false == detail::check_triangular(function,lower, mode, upper, &result, Policy()))
293 {
294 return result;
295 }
296 if(false == detail::check_probability(function, p, &result, Policy()))
297 {
298 return result;
299 }
300 if(p == 0)
301 {
302 return lower;
303 }
304 if(p == 1)
305 {
306 return upper;
307 }
308 RealType p0 = (mode - lower) / (upper - lower);
309 RealType q = 1 - p;
310 if (p < p0)
311 {
312 result = sqrt((upper - lower) * (mode - lower) * p) + lower;
313 }
314 else if (p == p0)
315 {
316 result = mode;
317 }
318 else // p > p0
319 {
320 result = upper - sqrt((upper - lower) * (upper - mode) * q);
321 }
322 return result;
323
324 } // RealType quantile(const triangular_distribution<RealType, Policy>& dist, const RealType& q)
325
326 template <class RealType, class Policy>
327 RealType cdf(const complemented2_type<triangular_distribution<RealType, Policy>, RealType>& c)
328 {
329 static const char* function = "boost::math::cdf(const triangular_distribution<%1%>&, %1%)";
330 RealType lower = c.dist.lower();
331 RealType mode = c.dist.mode();
332 RealType upper = c.dist.upper();
333 RealType x = c.param;
334 RealType result = 0; // of checks.
335 if(false == detail::check_triangular(function, lower, mode, upper, &result, Policy()))
336 {
337 return result;
338 }
339 if(false == detail::check_triangular_x(function, x, &result, Policy()))
340 {
341 return result;
342 }
343 if (x <= lower)
344 {
345 return 1;
346 }
347 if (x >= upper)
348 {
349 return 0;
350 }
351 if (x <= mode)
352 {
353 return 1 - ((x - lower) * (x - lower)) / ((upper - lower) * (mode - lower));
354 }
355 else
356 {
357 return (upper - x) * (upper - x) / ((upper - lower) * (upper - mode));
358 }
359 } // RealType cdf(const complemented2_type<triangular_distribution<RealType, Policy>, RealType>& c)
360
361 template <class RealType, class Policy>
362 RealType quantile(const complemented2_type<triangular_distribution<RealType, Policy>, RealType>& c)
363 {
364 BOOST_MATH_STD_USING // Aid ADL for sqrt.
365 static const char* function = "boost::math::quantile(const triangular_distribution<%1%>&, %1%)";
366 RealType l = c.dist.lower();
367 RealType m = c.dist.mode();
368 RealType u = c.dist.upper();
369 RealType q = c.param; // probability 0 to 1.
370 RealType result = 0; // of checks.
371 if(false == detail::check_triangular(function, l, m, u, &result, Policy()))
372 {
373 return result;
374 }
375 if(false == detail::check_probability(function, q, &result, Policy()))
376 {
377 return result;
378 }
379 if(q == 0)
380 {
381 return u;
382 }
383 if(q == 1)
384 {
385 return l;
386 }
387 RealType lower = c.dist.lower();
388 RealType mode = c.dist.mode();
389 RealType upper = c.dist.upper();
390
391 RealType p = 1 - q;
392 RealType p0 = (mode - lower) / (upper - lower);
393 if(p < p0)
394 {
395 RealType s = (upper - lower) * (mode - lower);
396 s *= p;
397 result = sqrt((upper - lower) * (mode - lower) * p) + lower;
398 }
399 else if (p == p0)
400 {
401 result = mode;
402 }
403 else // p > p0
404 {
405 result = upper - sqrt((upper - lower) * (upper - mode) * q);
406 }
407 return result;
408 } // RealType quantile(const complemented2_type<triangular_distribution<RealType, Policy>, RealType>& c)
409
410 template <class RealType, class Policy>
411 inline RealType mean(const triangular_distribution<RealType, Policy>& dist)
412 {
413 static const char* function = "boost::math::mean(const triangular_distribution<%1%>&)";
414 RealType lower = dist.lower();
415 RealType mode = dist.mode();
416 RealType upper = dist.upper();
417 RealType result = 0; // of checks.
418 if(false == detail::check_triangular(function, lower, mode, upper, &result, Policy()))
419 {
420 return result;
421 }
422 return (lower + upper + mode) / 3;
423 } // RealType mean(const triangular_distribution<RealType, Policy>& dist)
424
425
426 template <class RealType, class Policy>
427 inline RealType variance(const triangular_distribution<RealType, Policy>& dist)
428 {
429 static const char* function = "boost::math::mean(const triangular_distribution<%1%>&)";
430 RealType lower = dist.lower();
431 RealType mode = dist.mode();
432 RealType upper = dist.upper();
433 RealType result = 0; // of checks.
434 if(false == detail::check_triangular(function, lower, mode, upper, &result, Policy()))
435 {
436 return result;
437 }
438 return (lower * lower + upper * upper + mode * mode - lower * upper - lower * mode - upper * mode) / 18;
439 } // RealType variance(const triangular_distribution<RealType, Policy>& dist)
440
441 template <class RealType, class Policy>
442 inline RealType mode(const triangular_distribution<RealType, Policy>& dist)
443 {
444 static const char* function = "boost::math::mode(const triangular_distribution<%1%>&)";
445 RealType mode = dist.mode();
446 RealType result = 0; // of checks.
447 if(false == detail::check_triangular_mode(function, mode, &result, Policy()))
448 { // This should never happen!
449 return result;
450 }
451 return mode;
452 } // RealType mode
453
454 template <class RealType, class Policy>
455 inline RealType median(const triangular_distribution<RealType, Policy>& dist)
456 {
457 BOOST_MATH_STD_USING // ADL of std functions.
458 static const char* function = "boost::math::median(const triangular_distribution<%1%>&)";
459 RealType mode = dist.mode();
460 RealType result = 0; // of checks.
461 if(false == detail::check_triangular_mode(function, mode, &result, Policy()))
462 { // This should never happen!
463 return result;
464 }
465 RealType lower = dist.lower();
466 RealType upper = dist.upper();
467 if (mode >= (upper + lower) / 2)
468 {
469 return lower + sqrt((upper - lower) * (mode - lower)) / constants::root_two<RealType>();
470 }
471 else
472 {
473 return upper - sqrt((upper - lower) * (upper - mode)) / constants::root_two<RealType>();
474 }
475 } // RealType mode
476
477 template <class RealType, class Policy>
478 inline RealType skewness(const triangular_distribution<RealType, Policy>& dist)
479 {
480 BOOST_MATH_STD_USING // for ADL of std functions
481 using namespace boost::math::constants; // for root_two
482 static const char* function = "boost::math::skewness(const triangular_distribution<%1%>&)";
483
484 RealType lower = dist.lower();
485 RealType mode = dist.mode();
486 RealType upper = dist.upper();
487 RealType result = 0; // of checks.
488 if(false == boost::math::detail::check_triangular(function,lower, mode, upper, &result, Policy()))
489 {
490 return result;
491 }
492 return root_two<RealType>() * (lower + upper - 2 * mode) * (2 * lower - upper - mode) * (lower - 2 * upper + mode) /
493 (5 * pow((lower * lower + upper * upper + mode * mode
494 - lower * upper - lower * mode - upper * mode), RealType(3)/RealType(2)));
495 // #11768: Skewness formula for triangular distribution is incorrect - corrected 29 Oct 2015 for release 1.61.
496 } // RealType skewness(const triangular_distribution<RealType, Policy>& dist)
497
498 template <class RealType, class Policy>
499 inline RealType kurtosis(const triangular_distribution<RealType, Policy>& dist)
500 { // These checks may be belt and braces as should have been checked on construction?
501 static const char* function = "boost::math::kurtosis(const triangular_distribution<%1%>&)";
502 RealType lower = dist.lower();
503 RealType upper = dist.upper();
504 RealType mode = dist.mode();
505 RealType result = 0; // of checks.
506 if(false == detail::check_triangular(function,lower, mode, upper, &result, Policy()))
507 {
508 return result;
509 }
510 return static_cast<RealType>(12)/5; // 12/5 = 2.4;
511 } // RealType kurtosis_excess(const triangular_distribution<RealType, Policy>& dist)
512
513 template <class RealType, class Policy>
514 inline RealType kurtosis_excess(const triangular_distribution<RealType, Policy>& dist)
515 { // These checks may be belt and braces as should have been checked on construction?
516 static const char* function = "boost::math::kurtosis_excess(const triangular_distribution<%1%>&)";
517 RealType lower = dist.lower();
518 RealType upper = dist.upper();
519 RealType mode = dist.mode();
520 RealType result = 0; // of checks.
521 if(false == detail::check_triangular(function,lower, mode, upper, &result, Policy()))
522 {
523 return result;
524 }
525 return static_cast<RealType>(-3)/5; // - 3/5 = -0.6
526 // Assuming mathworld really means kurtosis excess? Wikipedia now corrected to match this.
527 }
528
529 template <class RealType, class Policy>
530 inline RealType entropy(const triangular_distribution<RealType, Policy>& dist)
531 {
532 using std::log;
533 return constants::half<RealType>() + log((dist.upper() - dist.lower())/2);
534 }
535
536} // namespace math
537} // namespace boost
538
539// This include must be at the end, *after* the accessors
540// for this distribution have been defined, in order to
541// keep compilers that support two-phase lookup happy.
542#include <boost/math/distributions/detail/derived_accessors.hpp>
543
544#endif // BOOST_STATS_TRIANGULAR_HPP
545
546
547
548

source code of boost/libs/math/include/boost/math/distributions/triangular.hpp