triangular.hpp 19 KB

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