triangular.hpp 17 KB

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