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)
7 #ifndef BOOST_STATS_TRIANGULAR_HPP
8 #define BOOST_STATS_TRIANGULAR_HPP
10 // http://mathworld.wolfram.com/TriangularDistribution.html
11 // Note that the 'constructors' defined by Wolfram are difference from those here,
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)
17 // http://en.wikipedia.org/wiki/Triangular_distribution
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>
27 namespace boost{ namespace math
31 template <class RealType, class Policy>
32 inline bool check_triangular_lower(
35 RealType* result, const Policy& pol)
37 if((boost::math::isfinite)(lower))
38 { // Any finite value is OK.
42 { // Not finite: infinity or NaN.
43 *result = policies::raise_domain_error<RealType>(
45 "Lower parameter is %1%, but must be finite!", lower, pol);
48 } // bool check_triangular_lower(
50 template <class RealType, class Policy>
51 inline bool check_triangular_mode(
54 RealType* result, const Policy& pol)
56 if((boost::math::isfinite)(mode))
57 { // any finite value is OK.
61 { // Not finite: infinity or NaN.
62 *result = policies::raise_domain_error<RealType>(
64 "Mode parameter is %1%, but must be finite!", mode, pol);
67 } // bool check_triangular_mode(
69 template <class RealType, class Policy>
70 inline bool check_triangular_upper(
73 RealType* result, const Policy& pol)
75 if((boost::math::isfinite)(upper))
76 { // any finite value is OK.
80 { // Not finite: infinity or NaN.
81 *result = policies::raise_domain_error<RealType>(
83 "Upper parameter is %1%, but must be finite!", upper, pol);
86 } // bool check_triangular_upper(
88 template <class RealType, class Policy>
89 inline bool check_triangular_x(
92 RealType* result, const Policy& pol)
94 if((boost::math::isfinite)(x))
95 { // Any finite value is OK
99 { // Not finite: infinity or NaN.
100 *result = policies::raise_domain_error<RealType>(
102 "x parameter is %1%, but must be finite!", x, pol);
105 } // bool check_triangular_x
107 template <class RealType, class Policy>
108 inline bool check_triangular(
109 const char* function,
113 RealType* result, const Policy& pol)
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.
121 else if (lower >= upper) // lower == upper NOT useful.
123 *result = policies::raise_domain_error<RealType>(
125 "lower parameter is %1%, but must be less than upper!", lower, pol);
129 { // Check lower <= mode <= upper.
132 *result = policies::raise_domain_error<RealType>(
134 "mode parameter is %1%, but must be >= than lower!", lower, pol);
139 *result = policies::raise_domain_error<RealType>(
141 "mode parameter is %1%, but must be <= than upper!", upper, pol);
144 return true; // All OK.
146 } // bool check_triangular
147 } // namespace detail
149 template <class RealType = double, class Policy = policies::policy<> >
150 class triangular_distribution
153 typedef RealType value_type;
154 typedef Policy policy_type;
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.
163 detail::check_triangular("boost::math::triangular_distribution<%1%>::triangular_distribution",l_lower, l_mode, l_upper, &result, Policy());
165 // Accessor functions.
166 RealType lower()const
174 RealType upper()const
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
185 typedef triangular_distribution<double> triangular;
187 template <class RealType, class Policy>
188 inline const std::pair<RealType, RealType> range(const triangular_distribution<RealType, Policy>& /* dist */)
189 { // Range of permissible values for random variable x.
190 using boost::math::tools::max_value;
191 return std::pair<RealType, RealType>(-max_value<RealType>(), max_value<RealType>());
194 template <class RealType, class Policy>
195 inline const std::pair<RealType, RealType> support(const triangular_distribution<RealType, Policy>& dist)
196 { // Range of supported values for random variable x.
197 // This is range where cdf rises from 0 to 1, and outside it, the pdf is zero.
198 return std::pair<RealType, RealType>(dist.lower(), dist.upper());
201 template <class RealType, class Policy>
202 RealType pdf(const triangular_distribution<RealType, Policy>& dist, const RealType& x)
204 static const char* function = "boost::math::pdf(const triangular_distribution<%1%>&, %1%)";
205 RealType lower = dist.lower();
206 RealType mode = dist.mode();
207 RealType upper = dist.upper();
208 RealType result = 0; // of checks.
209 if(false == detail::check_triangular(function, lower, mode, upper, &result, Policy()))
213 if(false == detail::check_triangular_x(function, x, &result, Policy()))
217 if((x < lower) || (x > upper))
222 { // (mode - lower) == 0 which would lead to divide by zero!
223 return (mode == lower) ? 2 / (upper - lower) : RealType(0);
227 return (mode == upper) ? 2 / (upper - lower) : RealType(0);
231 return 2 * (x - lower) / ((upper - lower) * (mode - lower));
235 return 2 * (upper - x) / ((upper - lower) * (upper - mode));
237 } // RealType pdf(const triangular_distribution<RealType, Policy>& dist, const RealType& x)
239 template <class RealType, class Policy>
240 inline RealType cdf(const triangular_distribution<RealType, Policy>& dist, const RealType& x)
242 static const char* function = "boost::math::cdf(const triangular_distribution<%1%>&, %1%)";
243 RealType lower = dist.lower();
244 RealType mode = dist.mode();
245 RealType upper = dist.upper();
246 RealType result = 0; // of checks.
247 if(false == detail::check_triangular(function, lower, mode, upper, &result, Policy()))
251 if(false == detail::check_triangular_x(function, x, &result, Policy()))
263 // else lower < x < upper
266 return ((x - lower) * (x - lower)) / ((upper - lower) * (mode - lower));
270 return 1 - (upper - x) * (upper - x) / ((upper - lower) * (upper - mode));
272 } // RealType cdf(const triangular_distribution<RealType, Policy>& dist, const RealType& x)
274 template <class RealType, class Policy>
275 RealType quantile(const triangular_distribution<RealType, Policy>& dist, const RealType& p)
277 BOOST_MATH_STD_USING // for ADL of std functions (sqrt).
278 static const char* function = "boost::math::quantile(const triangular_distribution<%1%>&, %1%)";
279 RealType lower = dist.lower();
280 RealType mode = dist.mode();
281 RealType upper = dist.upper();
282 RealType result = 0; // of checks
283 if(false == detail::check_triangular(function,lower, mode, upper, &result, Policy()))
287 if(false == detail::check_probability(function, p, &result, Policy()))
299 RealType p0 = (mode - lower) / (upper - lower);
303 result = sqrt((upper - lower) * (mode - lower) * p) + lower;
311 result = upper - sqrt((upper - lower) * (upper - mode) * q);
315 } // RealType quantile(const triangular_distribution<RealType, Policy>& dist, const RealType& q)
317 template <class RealType, class Policy>
318 RealType cdf(const complemented2_type<triangular_distribution<RealType, Policy>, RealType>& c)
320 static const char* function = "boost::math::cdf(const triangular_distribution<%1%>&, %1%)";
321 RealType lower = c.dist.lower();
322 RealType mode = c.dist.mode();
323 RealType upper = c.dist.upper();
324 RealType x = c.param;
325 RealType result = 0; // of checks.
326 if(false == detail::check_triangular(function, lower, mode, upper, &result, Policy()))
330 if(false == detail::check_triangular_x(function, x, &result, Policy()))
344 return 1 - ((x - lower) * (x - lower)) / ((upper - lower) * (mode - lower));
348 return (upper - x) * (upper - x) / ((upper - lower) * (upper - mode));
350 } // RealType cdf(const complemented2_type<triangular_distribution<RealType, Policy>, RealType>& c)
352 template <class RealType, class Policy>
353 RealType quantile(const complemented2_type<triangular_distribution<RealType, Policy>, RealType>& c)
355 BOOST_MATH_STD_USING // Aid ADL for sqrt.
356 static const char* function = "boost::math::quantile(const triangular_distribution<%1%>&, %1%)";
357 RealType l = c.dist.lower();
358 RealType m = c.dist.mode();
359 RealType u = c.dist.upper();
360 RealType q = c.param; // probability 0 to 1.
361 RealType result = 0; // of checks.
362 if(false == detail::check_triangular(function, l, m, u, &result, Policy()))
366 if(false == detail::check_probability(function, q, &result, Policy()))
378 RealType lower = c.dist.lower();
379 RealType mode = c.dist.mode();
380 RealType upper = c.dist.upper();
383 RealType p0 = (mode - lower) / (upper - lower);
386 RealType s = (upper - lower) * (mode - lower);
388 result = sqrt((upper - lower) * (mode - lower) * p) + lower;
396 result = upper - sqrt((upper - lower) * (upper - mode) * q);
399 } // RealType quantile(const complemented2_type<triangular_distribution<RealType, Policy>, RealType>& c)
401 template <class RealType, class Policy>
402 inline RealType mean(const triangular_distribution<RealType, Policy>& dist)
404 static const char* function = "boost::math::mean(const triangular_distribution<%1%>&)";
405 RealType lower = dist.lower();
406 RealType mode = dist.mode();
407 RealType upper = dist.upper();
408 RealType result = 0; // of checks.
409 if(false == detail::check_triangular(function, lower, mode, upper, &result, Policy()))
413 return (lower + upper + mode) / 3;
414 } // RealType mean(const triangular_distribution<RealType, Policy>& dist)
417 template <class RealType, class Policy>
418 inline RealType variance(const triangular_distribution<RealType, Policy>& dist)
420 static const char* function = "boost::math::mean(const triangular_distribution<%1%>&)";
421 RealType lower = dist.lower();
422 RealType mode = dist.mode();
423 RealType upper = dist.upper();
424 RealType result = 0; // of checks.
425 if(false == detail::check_triangular(function, lower, mode, upper, &result, Policy()))
429 return (lower * lower + upper * upper + mode * mode - lower * upper - lower * mode - upper * mode) / 18;
430 } // RealType variance(const triangular_distribution<RealType, Policy>& dist)
432 template <class RealType, class Policy>
433 inline RealType mode(const triangular_distribution<RealType, Policy>& dist)
435 static const char* function = "boost::math::mode(const triangular_distribution<%1%>&)";
436 RealType mode = dist.mode();
437 RealType result = 0; // of checks.
438 if(false == detail::check_triangular_mode(function, mode, &result, Policy()))
439 { // This should never happen!
445 template <class RealType, class Policy>
446 inline RealType median(const triangular_distribution<RealType, Policy>& dist)
448 BOOST_MATH_STD_USING // ADL of std functions.
449 static const char* function = "boost::math::median(const triangular_distribution<%1%>&)";
450 RealType mode = dist.mode();
451 RealType result = 0; // of checks.
452 if(false == detail::check_triangular_mode(function, mode, &result, Policy()))
453 { // This should never happen!
456 RealType lower = dist.lower();
457 RealType upper = dist.upper();
458 if (mode >= (upper + lower) / 2)
460 return lower + sqrt((upper - lower) * (mode - lower)) / constants::root_two<RealType>();
464 return upper - sqrt((upper - lower) * (upper - mode)) / constants::root_two<RealType>();
468 template <class RealType, class Policy>
469 inline RealType skewness(const triangular_distribution<RealType, Policy>& dist)
471 BOOST_MATH_STD_USING // for ADL of std functions
472 using namespace boost::math::constants; // for root_two
473 static const char* function = "boost::math::skewness(const triangular_distribution<%1%>&)";
475 RealType lower = dist.lower();
476 RealType mode = dist.mode();
477 RealType upper = dist.upper();
478 RealType result = 0; // of checks.
479 if(false == boost::math::detail::check_triangular(function,lower, mode, upper, &result, Policy()))
483 return root_two<RealType>() * (lower + upper - 2 * mode) * (2 * lower - upper - mode) * (lower - 2 * upper + mode) /
484 (5 * pow((lower * lower + upper * upper + mode * mode
485 - lower * upper - lower * mode - upper * mode), RealType(3)/RealType(2)));
486 // #11768: Skewness formula for triangular distribution is incorrect - corrected 29 Oct 2015 for release 1.61.
487 } // RealType skewness(const triangular_distribution<RealType, Policy>& dist)
489 template <class RealType, class Policy>
490 inline RealType kurtosis(const triangular_distribution<RealType, Policy>& dist)
491 { // These checks may be belt and braces as should have been checked on construction?
492 static const char* function = "boost::math::kurtosis(const triangular_distribution<%1%>&)";
493 RealType lower = dist.lower();
494 RealType upper = dist.upper();
495 RealType mode = dist.mode();
496 RealType result = 0; // of checks.
497 if(false == detail::check_triangular(function,lower, mode, upper, &result, Policy()))
501 return static_cast<RealType>(12)/5; // 12/5 = 2.4;
502 } // RealType kurtosis_excess(const triangular_distribution<RealType, Policy>& dist)
504 template <class RealType, class Policy>
505 inline RealType kurtosis_excess(const triangular_distribution<RealType, Policy>& dist)
506 { // These checks may be belt and braces as should have been checked on construction?
507 static const char* function = "boost::math::kurtosis_excess(const triangular_distribution<%1%>&)";
508 RealType lower = dist.lower();
509 RealType upper = dist.upper();
510 RealType mode = dist.mode();
511 RealType result = 0; // of checks.
512 if(false == detail::check_triangular(function,lower, mode, upper, &result, Policy()))
516 return static_cast<RealType>(-3)/5; // - 3/5 = -0.6
517 // Assuming mathworld really means kurtosis excess? Wikipedia now corrected to match this.
523 // This include must be at the end, *after* the accessors
524 // for this distribution have been defined, in order to
525 // keep compilers that support two-phase lookup happy.
526 #include <boost/math/distributions/detail/derived_accessors.hpp>
528 #endif // BOOST_STATS_TRIANGULAR_HPP