triangular.qbk 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. [section:triangular_dist Triangular Distribution]
  2. ``#include <boost/math/distributions/triangular.hpp>``
  3. namespace boost{ namespace math{
  4. template <class RealType = double,
  5. class ``__Policy`` = ``__policy_class`` >
  6. class triangular_distribution;
  7. typedef triangular_distribution<> triangular;
  8. template <class RealType, class ``__Policy``>
  9. class triangular_distribution
  10. {
  11. public:
  12. typedef RealType value_type;
  13. typedef Policy policy_type;
  14. triangular_distribution(RealType lower = -1, RealType mode = 0) RealType upper = 1); // Constructor.
  15. : m_lower(lower), m_mode(mode), m_upper(upper) // Default is -1, 0, +1 symmetric triangular distribution.
  16. // Accessor functions.
  17. RealType lower()const;
  18. RealType mode()const;
  19. RealType upper()const;
  20. }; // class triangular_distribution
  21. }} // namespaces
  22. The [@http://en.wikipedia.org/wiki/Triangular_distribution triangular distribution]
  23. is a [@http://en.wikipedia.org/wiki/Continuous_distribution continuous]
  24. [@http://en.wikipedia.org/wiki/Probability_distribution probability distribution]
  25. with a lower limit a,
  26. [@http://en.wikipedia.org/wiki/Mode_%28statistics%29 mode c],
  27. and upper limit b.
  28. The triangular distribution is often used where the distribution is only vaguely known,
  29. but, like the [@http://en.wikipedia.org/wiki/Uniform_distribution_%28continuous%29 uniform distribution],
  30. upper and limits are 'known', but a 'best guess', the mode or center point, is also added.
  31. It has been recommended as a
  32. [@http://www.worldscibooks.com/mathematics/etextbook/5720/5720_chap1.pdf proxy for the beta distribution.]
  33. The distribution is used in business decision making and project planning.
  34. The [@http://en.wikipedia.org/wiki/Triangular_distribution triangular distribution]
  35. is a distribution with the
  36. [@http://en.wikipedia.org/wiki/Probability_density_function probability density function]:
  37. [expression f(x) = 2(x-a)/(b-a) (c-a) [sixemspace] for a <= x <= c]
  38. [expression f(x) = 2(b-x)/(b-a) (b-c) [sixemspace] for c < x <= b]
  39. Parameter ['a] (lower) can be any finite value.
  40. Parameter ['b] (upper) can be any finite value > a (lower).
  41. Parameter ['c] (mode) a <= c <= b. This is the most probable value.
  42. The [@http://en.wikipedia.org/wiki/Random_variate random variate] x must also be finite, and is supported lower <= x <= upper.
  43. The triangular distribution may be appropriate when an assumption of a normal distribution
  44. is unjustified because uncertainty is caused by rounding and quantization from analog to digital conversion.
  45. Upper and lower limits are known, and the most probable value lies midway.
  46. The distribution simplifies when the 'best guess' is either the lower or upper limit - a 90 degree angle triangle.
  47. The 001 triangular distribution which expresses an estimate that the lowest value is the most likely;
  48. for example, you believe that the next-day quoted delivery date is most likely
  49. (knowing that a quicker delivery is impossible - the postman only comes once a day),
  50. and that longer delays are decreasingly likely,
  51. and delivery is assumed to never take more than your upper limit.
  52. The following graph illustrates how the
  53. [@http://en.wikipedia.org/wiki/Probability_density_function probability density function PDF]
  54. varies with the various parameters:
  55. [graph triangular_pdf]
  56. and cumulative distribution function
  57. [graph triangular_cdf]
  58. [h4 Member Functions]
  59. triangular_distribution(RealType lower = 0, RealType mode = 0 RealType upper = 1);
  60. Constructs a [@http://en.wikipedia.org/wiki/triangular_distribution triangular distribution]
  61. with lower /lower/ (a) and upper /upper/ (b).
  62. Requires that the /lower/, /mode/ and /upper/ parameters are all finite,
  63. otherwise calls __domain_error.
  64. [warning These constructors are slightly different from the analogs provided by __Mathworld
  65. [@http://reference.wolfram.com/language/ref/TriangularDistribution.html Triangular distribution],
  66. where
  67. [^TriangularDistribution\[{min, max}\]] represents a [*symmetric] triangular statistical distribution giving values between min and max.[br]
  68. [^TriangularDistribution\[\]] represents a [*symmetric] triangular statistical distribution giving values between 0 and 1.[br]
  69. [^TriangularDistribution\[{min, max}, c\]] represents a triangular distribution with mode at c (usually [*asymmetric]).[br]
  70. So, for example, to compute a variance using __WolframAlpha, use
  71. [^N\[variance\[TriangularDistribution{1, +2}\], 50\]]
  72. ]
  73. The parameters of a distribution can be obtained using these member functions:
  74. RealType lower()const;
  75. Returns the ['lower] parameter of this distribution (default -1).
  76. RealType mode()const;
  77. Returns the ['mode] parameter of this distribution (default 0).
  78. RealType upper()const;
  79. Returns the ['upper] parameter of this distribution (default+1).
  80. [h4 Non-member Accessors]
  81. All the [link math_toolkit.dist_ref.nmp usual non-member accessor functions] that are generic to all
  82. distributions are supported: __usual_accessors.
  83. The domain of the random variable is \lower\ to \upper\,
  84. and the supported range is lower <= x <= upper.
  85. [h4 Accuracy]
  86. The triangular distribution is implemented with simple arithmetic operators and so should have errors within an epsilon or two,
  87. except quantiles with arguments nearing the extremes of zero and unity.
  88. [h4 Implementation]
  89. In the following table, a is the /lower/ parameter of the distribution,
  90. c is the /mode/ parameter,
  91. b is the /upper/ parameter,
  92. /x/ is the random variate, /p/ is the probability and /q = 1-p/.
  93. [table
  94. [[Function][Implementation Notes]]
  95. [[pdf][Using the relation: pdf = 0 for x < mode, 2(x-a)\/(b-a)(c-a) else 2*(b-x)\/((b-a)(b-c))]]
  96. [[cdf][Using the relation: cdf = 0 for x < mode (x-a)[super 2]\/((b-a)(c-a)) else 1 - (b-x)[super 2]\/((b-a)(b-c))]]
  97. [[cdf complement][Using the relation: q = 1 - p ]]
  98. [[quantile][let p0 = (c-a)\/(b-a) the point of inflection on the cdf,
  99. then given probability p and q = 1-p:
  100. x = sqrt((b-a)(c-a)p) + a ; for p < p0
  101. x = c ; for p == p0
  102. x = b - sqrt((b-a)(b-c)q) ; for p > p0
  103. (See [@../../../../boost/math/distributions/triangular.hpp /boost/math/distributions/triangular.hpp] for details.)]]
  104. [[quantile from the complement][As quantile (See [@../../../../boost/math/distributions/triangular.hpp /boost/math/distributions/triangular.hpp] for details.)]]
  105. [[mean][(a + b + 3) \/ 3 ]]
  106. [[variance][(a[super 2]+b[super 2]+c[super 2] - ab - ac - bc)\/18]]
  107. [[mode][c]]
  108. [[skewness][(See [@../../../../boost/math/distributions/triangular.hpp /boost/math/distributions/triangular.hpp] for details). ]]
  109. [[kurtosis][12\/5]]
  110. [[kurtosis excess][-3\/5]]
  111. ]
  112. Some 'known good' test values were obtained using __WolframAlpha.
  113. [h4 References]
  114. * [@http://en.wikipedia.org/wiki/Triangular_distribution Wikpedia triangular distribution]
  115. * [@http://mathworld.wolfram.com/TriangularDistribution.html Weisstein, Eric W. "Triangular Distribution." From MathWorld--A Wolfram Web Resource.]
  116. * Evans, M.; Hastings, N.; and Peacock, B. "Triangular Distribution." Ch. 40 in Statistical Distributions, 3rd ed. New York: Wiley, pp. 187-188, 2000, ISBN - 0471371246.
  117. * [@http://www.measurement.sk/2002/S1/Wimmer2.pdf Gejza Wimmer, Viktor Witkovsky and Tomas Duby,
  118. Measurement Science Review, Volume 2, Section 1, 2002, Proper Rounding Of The Measurement Results Under The Assumption Of Triangular Distribution.]
  119. [endsect][/section:triangular_dist triangular]
  120. [/
  121. Copyright 2006 John Maddock and Paul A. Bristow.
  122. Distributed under the Boost Software License, Version 1.0.
  123. (See accompanying file LICENSE_1_0.txt or copy at
  124. http://www.boost.org/LICENSE_1_0.txt).
  125. ]