triangular.hpp 18 KB

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