triangular.hpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547
  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. #ifdef __cpp_deduction_guides
  174. template <class RealType>
  175. triangular_distribution(RealType)->triangular_distribution<typename boost::math::tools::promote_args<RealType>::type>;
  176. template <class RealType>
  177. triangular_distribution(RealType,RealType)->triangular_distribution<typename boost::math::tools::promote_args<RealType>::type>;
  178. template <class RealType>
  179. triangular_distribution(RealType,RealType,RealType)->triangular_distribution<typename boost::math::tools::promote_args<RealType>::type>;
  180. #endif
  181. template <class RealType, class Policy>
  182. inline const std::pair<RealType, RealType> range(const triangular_distribution<RealType, Policy>& /* dist */)
  183. { // Range of permissible values for random variable x.
  184. using boost::math::tools::max_value;
  185. return std::pair<RealType, RealType>(-max_value<RealType>(), max_value<RealType>());
  186. }
  187. template <class RealType, class Policy>
  188. inline const std::pair<RealType, RealType> support(const triangular_distribution<RealType, Policy>& dist)
  189. { // Range of supported values for random variable x.
  190. // This is range where cdf rises from 0 to 1, and outside it, the pdf is zero.
  191. return std::pair<RealType, RealType>(dist.lower(), dist.upper());
  192. }
  193. template <class RealType, class Policy>
  194. RealType pdf(const triangular_distribution<RealType, Policy>& dist, const RealType& x)
  195. {
  196. static const char* function = "boost::math::pdf(const triangular_distribution<%1%>&, %1%)";
  197. RealType lower = dist.lower();
  198. RealType mode = dist.mode();
  199. RealType upper = dist.upper();
  200. RealType result = 0; // of checks.
  201. if(false == detail::check_triangular(function, lower, mode, upper, &result, Policy()))
  202. {
  203. return result;
  204. }
  205. if(false == detail::check_triangular_x(function, x, &result, Policy()))
  206. {
  207. return result;
  208. }
  209. if((x < lower) || (x > upper))
  210. {
  211. return 0;
  212. }
  213. if (x == lower)
  214. { // (mode - lower) == 0 which would lead to divide by zero!
  215. return (mode == lower) ? 2 / (upper - lower) : RealType(0);
  216. }
  217. else if (x == upper)
  218. {
  219. return (mode == upper) ? 2 / (upper - lower) : RealType(0);
  220. }
  221. else if (x <= mode)
  222. {
  223. return 2 * (x - lower) / ((upper - lower) * (mode - lower));
  224. }
  225. else
  226. { // (x > mode)
  227. return 2 * (upper - x) / ((upper - lower) * (upper - mode));
  228. }
  229. } // RealType pdf(const triangular_distribution<RealType, Policy>& dist, const RealType& x)
  230. template <class RealType, class Policy>
  231. inline RealType cdf(const triangular_distribution<RealType, Policy>& dist, const RealType& x)
  232. {
  233. static const char* function = "boost::math::cdf(const triangular_distribution<%1%>&, %1%)";
  234. RealType lower = dist.lower();
  235. RealType mode = dist.mode();
  236. RealType upper = dist.upper();
  237. RealType result = 0; // of checks.
  238. if(false == detail::check_triangular(function, lower, mode, upper, &result, Policy()))
  239. {
  240. return result;
  241. }
  242. if(false == detail::check_triangular_x(function, x, &result, Policy()))
  243. {
  244. return result;
  245. }
  246. if((x <= lower))
  247. {
  248. return 0;
  249. }
  250. if (x >= upper)
  251. {
  252. return 1;
  253. }
  254. // else lower < x < upper
  255. if (x <= mode)
  256. {
  257. return ((x - lower) * (x - lower)) / ((upper - lower) * (mode - lower));
  258. }
  259. else
  260. {
  261. return 1 - (upper - x) * (upper - x) / ((upper - lower) * (upper - mode));
  262. }
  263. } // RealType cdf(const triangular_distribution<RealType, Policy>& dist, const RealType& x)
  264. template <class RealType, class Policy>
  265. RealType quantile(const triangular_distribution<RealType, Policy>& dist, const RealType& p)
  266. {
  267. BOOST_MATH_STD_USING // for ADL of std functions (sqrt).
  268. static const char* function = "boost::math::quantile(const triangular_distribution<%1%>&, %1%)";
  269. RealType lower = dist.lower();
  270. RealType mode = dist.mode();
  271. RealType upper = dist.upper();
  272. RealType result = 0; // of checks
  273. if(false == detail::check_triangular(function,lower, mode, upper, &result, Policy()))
  274. {
  275. return result;
  276. }
  277. if(false == detail::check_probability(function, p, &result, Policy()))
  278. {
  279. return result;
  280. }
  281. if(p == 0)
  282. {
  283. return lower;
  284. }
  285. if(p == 1)
  286. {
  287. return upper;
  288. }
  289. RealType p0 = (mode - lower) / (upper - lower);
  290. RealType q = 1 - p;
  291. if (p < p0)
  292. {
  293. result = sqrt((upper - lower) * (mode - lower) * p) + lower;
  294. }
  295. else if (p == p0)
  296. {
  297. result = mode;
  298. }
  299. else // p > p0
  300. {
  301. result = upper - sqrt((upper - lower) * (upper - mode) * q);
  302. }
  303. return result;
  304. } // RealType quantile(const triangular_distribution<RealType, Policy>& dist, const RealType& q)
  305. template <class RealType, class Policy>
  306. RealType cdf(const complemented2_type<triangular_distribution<RealType, Policy>, RealType>& c)
  307. {
  308. static const char* function = "boost::math::cdf(const triangular_distribution<%1%>&, %1%)";
  309. RealType lower = c.dist.lower();
  310. RealType mode = c.dist.mode();
  311. RealType upper = c.dist.upper();
  312. RealType x = c.param;
  313. RealType result = 0; // of checks.
  314. if(false == detail::check_triangular(function, lower, mode, upper, &result, Policy()))
  315. {
  316. return result;
  317. }
  318. if(false == detail::check_triangular_x(function, x, &result, Policy()))
  319. {
  320. return result;
  321. }
  322. if (x <= lower)
  323. {
  324. return 1;
  325. }
  326. if (x >= upper)
  327. {
  328. return 0;
  329. }
  330. if (x <= mode)
  331. {
  332. return 1 - ((x - lower) * (x - lower)) / ((upper - lower) * (mode - lower));
  333. }
  334. else
  335. {
  336. return (upper - x) * (upper - x) / ((upper - lower) * (upper - mode));
  337. }
  338. } // RealType cdf(const complemented2_type<triangular_distribution<RealType, Policy>, RealType>& c)
  339. template <class RealType, class Policy>
  340. RealType quantile(const complemented2_type<triangular_distribution<RealType, Policy>, RealType>& c)
  341. {
  342. BOOST_MATH_STD_USING // Aid ADL for sqrt.
  343. static const char* function = "boost::math::quantile(const triangular_distribution<%1%>&, %1%)";
  344. RealType l = c.dist.lower();
  345. RealType m = c.dist.mode();
  346. RealType u = c.dist.upper();
  347. RealType q = c.param; // probability 0 to 1.
  348. RealType result = 0; // of checks.
  349. if(false == detail::check_triangular(function, l, m, u, &result, Policy()))
  350. {
  351. return result;
  352. }
  353. if(false == detail::check_probability(function, q, &result, Policy()))
  354. {
  355. return result;
  356. }
  357. if(q == 0)
  358. {
  359. return u;
  360. }
  361. if(q == 1)
  362. {
  363. return l;
  364. }
  365. RealType lower = c.dist.lower();
  366. RealType mode = c.dist.mode();
  367. RealType upper = c.dist.upper();
  368. RealType p = 1 - q;
  369. RealType p0 = (mode - lower) / (upper - lower);
  370. if(p < p0)
  371. {
  372. RealType s = (upper - lower) * (mode - lower);
  373. s *= p;
  374. result = sqrt((upper - lower) * (mode - lower) * p) + lower;
  375. }
  376. else if (p == p0)
  377. {
  378. result = mode;
  379. }
  380. else // p > p0
  381. {
  382. result = upper - sqrt((upper - lower) * (upper - mode) * q);
  383. }
  384. return result;
  385. } // RealType quantile(const complemented2_type<triangular_distribution<RealType, Policy>, RealType>& c)
  386. template <class RealType, class Policy>
  387. inline RealType mean(const triangular_distribution<RealType, Policy>& dist)
  388. {
  389. static const char* function = "boost::math::mean(const triangular_distribution<%1%>&)";
  390. RealType lower = dist.lower();
  391. RealType mode = dist.mode();
  392. RealType upper = dist.upper();
  393. RealType result = 0; // of checks.
  394. if(false == detail::check_triangular(function, lower, mode, upper, &result, Policy()))
  395. {
  396. return result;
  397. }
  398. return (lower + upper + mode) / 3;
  399. } // RealType mean(const triangular_distribution<RealType, Policy>& dist)
  400. template <class RealType, class Policy>
  401. inline RealType variance(const triangular_distribution<RealType, Policy>& dist)
  402. {
  403. static const char* function = "boost::math::mean(const triangular_distribution<%1%>&)";
  404. RealType lower = dist.lower();
  405. RealType mode = dist.mode();
  406. RealType upper = dist.upper();
  407. RealType result = 0; // of checks.
  408. if(false == detail::check_triangular(function, lower, mode, upper, &result, Policy()))
  409. {
  410. return result;
  411. }
  412. return (lower * lower + upper * upper + mode * mode - lower * upper - lower * mode - upper * mode) / 18;
  413. } // RealType variance(const triangular_distribution<RealType, Policy>& dist)
  414. template <class RealType, class Policy>
  415. inline RealType mode(const triangular_distribution<RealType, Policy>& dist)
  416. {
  417. static const char* function = "boost::math::mode(const triangular_distribution<%1%>&)";
  418. RealType mode = dist.mode();
  419. RealType result = 0; // of checks.
  420. if(false == detail::check_triangular_mode(function, mode, &result, Policy()))
  421. { // This should never happen!
  422. return result;
  423. }
  424. return mode;
  425. } // RealType mode
  426. template <class RealType, class Policy>
  427. inline RealType median(const triangular_distribution<RealType, Policy>& dist)
  428. {
  429. BOOST_MATH_STD_USING // ADL of std functions.
  430. static const char* function = "boost::math::median(const triangular_distribution<%1%>&)";
  431. RealType mode = dist.mode();
  432. RealType result = 0; // of checks.
  433. if(false == detail::check_triangular_mode(function, mode, &result, Policy()))
  434. { // This should never happen!
  435. return result;
  436. }
  437. RealType lower = dist.lower();
  438. RealType upper = dist.upper();
  439. if (mode >= (upper + lower) / 2)
  440. {
  441. return lower + sqrt((upper - lower) * (mode - lower)) / constants::root_two<RealType>();
  442. }
  443. else
  444. {
  445. return upper - sqrt((upper - lower) * (upper - mode)) / constants::root_two<RealType>();
  446. }
  447. } // RealType mode
  448. template <class RealType, class Policy>
  449. inline RealType skewness(const triangular_distribution<RealType, Policy>& dist)
  450. {
  451. BOOST_MATH_STD_USING // for ADL of std functions
  452. using namespace boost::math::constants; // for root_two
  453. static const char* function = "boost::math::skewness(const triangular_distribution<%1%>&)";
  454. RealType lower = dist.lower();
  455. RealType mode = dist.mode();
  456. RealType upper = dist.upper();
  457. RealType result = 0; // of checks.
  458. if(false == boost::math::detail::check_triangular(function,lower, mode, upper, &result, Policy()))
  459. {
  460. return result;
  461. }
  462. return root_two<RealType>() * (lower + upper - 2 * mode) * (2 * lower - upper - mode) * (lower - 2 * upper + mode) /
  463. (5 * pow((lower * lower + upper * upper + mode * mode
  464. - lower * upper - lower * mode - upper * mode), RealType(3)/RealType(2)));
  465. // #11768: Skewness formula for triangular distribution is incorrect - corrected 29 Oct 2015 for release 1.61.
  466. } // RealType skewness(const triangular_distribution<RealType, Policy>& dist)
  467. template <class RealType, class Policy>
  468. inline RealType kurtosis(const triangular_distribution<RealType, Policy>& dist)
  469. { // These checks may be belt and braces as should have been checked on construction?
  470. static const char* function = "boost::math::kurtosis(const triangular_distribution<%1%>&)";
  471. RealType lower = dist.lower();
  472. RealType upper = dist.upper();
  473. RealType mode = dist.mode();
  474. RealType result = 0; // of checks.
  475. if(false == detail::check_triangular(function,lower, mode, upper, &result, Policy()))
  476. {
  477. return result;
  478. }
  479. return static_cast<RealType>(12)/5; // 12/5 = 2.4;
  480. } // RealType kurtosis_excess(const triangular_distribution<RealType, Policy>& dist)
  481. template <class RealType, class Policy>
  482. inline RealType kurtosis_excess(const triangular_distribution<RealType, Policy>& dist)
  483. { // These checks may be belt and braces as should have been checked on construction?
  484. static const char* function = "boost::math::kurtosis_excess(const triangular_distribution<%1%>&)";
  485. RealType lower = dist.lower();
  486. RealType upper = dist.upper();
  487. RealType mode = dist.mode();
  488. RealType result = 0; // of checks.
  489. if(false == detail::check_triangular(function,lower, mode, upper, &result, Policy()))
  490. {
  491. return result;
  492. }
  493. return static_cast<RealType>(-3)/5; // - 3/5 = -0.6
  494. // Assuming mathworld really means kurtosis excess? Wikipedia now corrected to match this.
  495. }
  496. template <class RealType, class Policy>
  497. inline RealType entropy(const triangular_distribution<RealType, Policy>& dist)
  498. {
  499. using std::log;
  500. return constants::half<RealType>() + log((dist.upper() - dist.lower())/2);
  501. }
  502. } // namespace math
  503. } // namespace boost
  504. // This include must be at the end, *after* the accessors
  505. // for this distribution have been defined, in order to
  506. // keep compilers that support two-phase lookup happy.
  507. #include <boost/math/distributions/detail/derived_accessors.hpp>
  508. #endif // BOOST_STATS_TRIANGULAR_HPP