uniform.hpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  1. // Copyright John Maddock 2006.
  2. // Copyright Paul A. Bristow 2006.
  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. // TODO deal with infinity as special better - or remove.
  7. //
  8. #ifndef BOOST_STATS_UNIFORM_HPP
  9. #define BOOST_STATS_UNIFORM_HPP
  10. // http://www.itl.nist.gov/div898/handbook/eda/section3/eda3668.htm
  11. // http://mathworld.wolfram.com/UniformDistribution.html
  12. // http://documents.wolfram.com/calculationcenter/v2/Functions/ListsMatrices/Statistics/UniformDistribution.html
  13. // http://en.wikipedia.org/wiki/Uniform_distribution_%28continuous%29
  14. #include <boost/math/distributions/fwd.hpp>
  15. #include <boost/math/distributions/detail/common_error_handling.hpp>
  16. #include <boost/math/distributions/complement.hpp>
  17. #include <utility>
  18. namespace boost{ namespace math
  19. {
  20. namespace detail
  21. {
  22. template <class RealType, class Policy>
  23. inline bool check_uniform_lower(
  24. const char* function,
  25. RealType lower,
  26. RealType* result, const Policy& pol)
  27. {
  28. if((boost::math::isfinite)(lower))
  29. { // any finite value is OK.
  30. return true;
  31. }
  32. else
  33. { // Not finite.
  34. *result = policies::raise_domain_error<RealType>(
  35. function,
  36. "Lower parameter is %1%, but must be finite!", lower, pol);
  37. return false;
  38. }
  39. } // bool check_uniform_lower(
  40. template <class RealType, class Policy>
  41. inline bool check_uniform_upper(
  42. const char* function,
  43. RealType upper,
  44. RealType* result, const Policy& pol)
  45. {
  46. if((boost::math::isfinite)(upper))
  47. { // Any finite value is OK.
  48. return true;
  49. }
  50. else
  51. { // Not finite.
  52. *result = policies::raise_domain_error<RealType>(
  53. function,
  54. "Upper parameter is %1%, but must be finite!", upper, pol);
  55. return false;
  56. }
  57. } // bool check_uniform_upper(
  58. template <class RealType, class Policy>
  59. inline bool check_uniform_x(
  60. const char* function,
  61. RealType const& x,
  62. RealType* result, const Policy& pol)
  63. {
  64. if((boost::math::isfinite)(x))
  65. { // Any finite value is OK
  66. return true;
  67. }
  68. else
  69. { // Not finite..
  70. *result = policies::raise_domain_error<RealType>(
  71. function,
  72. "x parameter is %1%, but must be finite!", x, pol);
  73. return false;
  74. }
  75. } // bool check_uniform_x
  76. template <class RealType, class Policy>
  77. inline bool check_uniform(
  78. const char* function,
  79. RealType lower,
  80. RealType upper,
  81. RealType* result, const Policy& pol)
  82. {
  83. if((check_uniform_lower(function, lower, result, pol) == false)
  84. || (check_uniform_upper(function, upper, result, pol) == false))
  85. {
  86. return false;
  87. }
  88. else if (lower >= upper) // If lower == upper then 1 / (upper-lower) = 1/0 = +infinity!
  89. { // upper and lower have been checked before, so must be lower >= upper.
  90. *result = policies::raise_domain_error<RealType>(
  91. function,
  92. "lower parameter is %1%, but must be less than upper!", lower, pol);
  93. return false;
  94. }
  95. else
  96. { // All OK,
  97. return true;
  98. }
  99. } // bool check_uniform(
  100. } // namespace detail
  101. template <class RealType = double, class Policy = policies::policy<> >
  102. class uniform_distribution
  103. {
  104. public:
  105. typedef RealType value_type;
  106. typedef Policy policy_type;
  107. uniform_distribution(RealType l_lower = 0, RealType l_upper = 1) // Constructor.
  108. : m_lower(l_lower), m_upper(l_upper) // Default is standard uniform distribution.
  109. {
  110. RealType result;
  111. detail::check_uniform("boost::math::uniform_distribution<%1%>::uniform_distribution", l_lower, l_upper, &result, Policy());
  112. }
  113. // Accessor functions.
  114. RealType lower()const
  115. {
  116. return m_lower;
  117. }
  118. RealType upper()const
  119. {
  120. return m_upper;
  121. }
  122. private:
  123. // Data members:
  124. RealType m_lower; // distribution lower aka a.
  125. RealType m_upper; // distribution upper aka b.
  126. }; // class uniform_distribution
  127. typedef uniform_distribution<double> uniform;
  128. #ifdef __cpp_deduction_guides
  129. template <class RealType>
  130. uniform_distribution(RealType)->uniform_distribution<typename boost::math::tools::promote_args<RealType>::type>;
  131. template <class RealType>
  132. uniform_distribution(RealType,RealType)->uniform_distribution<typename boost::math::tools::promote_args<RealType>::type>;
  133. #endif
  134. template <class RealType, class Policy>
  135. inline const std::pair<RealType, RealType> range(const uniform_distribution<RealType, Policy>& /* dist */)
  136. { // Range of permissible values for random variable x.
  137. using boost::math::tools::max_value;
  138. return std::pair<RealType, RealType>(-max_value<RealType>(), max_value<RealType>()); // - to + 'infinity'.
  139. // Note RealType infinity is NOT permitted, only max_value.
  140. }
  141. template <class RealType, class Policy>
  142. inline const std::pair<RealType, RealType> support(const uniform_distribution<RealType, Policy>& dist)
  143. { // Range of supported values for random variable x.
  144. // This is range where cdf rises from 0 to 1, and outside it, the pdf is zero.
  145. using boost::math::tools::max_value;
  146. return std::pair<RealType, RealType>(dist.lower(), dist.upper());
  147. }
  148. template <class RealType, class Policy>
  149. inline RealType pdf(const uniform_distribution<RealType, Policy>& dist, const RealType& x)
  150. {
  151. RealType lower = dist.lower();
  152. RealType upper = dist.upper();
  153. RealType result = 0; // of checks.
  154. if(false == detail::check_uniform("boost::math::pdf(const uniform_distribution<%1%>&, %1%)", lower, upper, &result, Policy()))
  155. {
  156. return result;
  157. }
  158. if(false == detail::check_uniform_x("boost::math::pdf(const uniform_distribution<%1%>&, %1%)", x, &result, Policy()))
  159. {
  160. return result;
  161. }
  162. if((x < lower) || (x > upper) )
  163. {
  164. return 0;
  165. }
  166. else
  167. {
  168. return 1 / (upper - lower);
  169. }
  170. } // RealType pdf(const uniform_distribution<RealType, Policy>& dist, const RealType& x)
  171. template <class RealType, class Policy>
  172. inline RealType cdf(const uniform_distribution<RealType, Policy>& dist, const RealType& x)
  173. {
  174. RealType lower = dist.lower();
  175. RealType upper = dist.upper();
  176. RealType result = 0; // of checks.
  177. if(false == detail::check_uniform("boost::math::cdf(const uniform_distribution<%1%>&, %1%)",lower, upper, &result, Policy()))
  178. {
  179. return result;
  180. }
  181. if(false == detail::check_uniform_x("boost::math::cdf(const uniform_distribution<%1%>&, %1%)", x, &result, Policy()))
  182. {
  183. return result;
  184. }
  185. if (x < lower)
  186. {
  187. return 0;
  188. }
  189. if (x > upper)
  190. {
  191. return 1;
  192. }
  193. return (x - lower) / (upper - lower); // lower <= x <= upper
  194. } // RealType cdf(const uniform_distribution<RealType, Policy>& dist, const RealType& x)
  195. template <class RealType, class Policy>
  196. inline RealType quantile(const uniform_distribution<RealType, Policy>& dist, const RealType& p)
  197. {
  198. RealType lower = dist.lower();
  199. RealType upper = dist.upper();
  200. RealType result = 0; // of checks
  201. if(false == detail::check_uniform("boost::math::quantile(const uniform_distribution<%1%>&, %1%)",lower, upper, &result, Policy()))
  202. {
  203. return result;
  204. }
  205. if(false == detail::check_probability("boost::math::quantile(const uniform_distribution<%1%>&, %1%)", p, &result, Policy()))
  206. {
  207. return result;
  208. }
  209. if(p == 0)
  210. {
  211. return lower;
  212. }
  213. if(p == 1)
  214. {
  215. return upper;
  216. }
  217. return p * (upper - lower) + lower;
  218. } // RealType quantile(const uniform_distribution<RealType, Policy>& dist, const RealType& p)
  219. template <class RealType, class Policy>
  220. inline RealType cdf(const complemented2_type<uniform_distribution<RealType, Policy>, RealType>& c)
  221. {
  222. RealType lower = c.dist.lower();
  223. RealType upper = c.dist.upper();
  224. RealType x = c.param;
  225. RealType result = 0; // of checks.
  226. if(false == detail::check_uniform("boost::math::cdf(const uniform_distribution<%1%>&, %1%)", lower, upper, &result, Policy()))
  227. {
  228. return result;
  229. }
  230. if(false == detail::check_uniform_x("boost::math::cdf(const uniform_distribution<%1%>&, %1%)", x, &result, Policy()))
  231. {
  232. return result;
  233. }
  234. if (x < lower)
  235. {
  236. return 1;
  237. }
  238. if (x > upper)
  239. {
  240. return 0;
  241. }
  242. return (upper - x) / (upper - lower);
  243. } // RealType cdf(const complemented2_type<uniform_distribution<RealType, Policy>, RealType>& c)
  244. template <class RealType, class Policy>
  245. inline RealType quantile(const complemented2_type<uniform_distribution<RealType, Policy>, RealType>& c)
  246. {
  247. RealType lower = c.dist.lower();
  248. RealType upper = c.dist.upper();
  249. RealType q = c.param;
  250. RealType result = 0; // of checks.
  251. if(false == detail::check_uniform("boost::math::quantile(const uniform_distribution<%1%>&, %1%)", lower, upper, &result, Policy()))
  252. {
  253. return result;
  254. }
  255. if(false == detail::check_probability("boost::math::quantile(const uniform_distribution<%1%>&, %1%)", q, &result, Policy()))
  256. {
  257. return result;
  258. }
  259. if(q == 0)
  260. {
  261. return upper;
  262. }
  263. if(q == 1)
  264. {
  265. return lower;
  266. }
  267. return -q * (upper - lower) + upper;
  268. } // RealType quantile(const complemented2_type<uniform_distribution<RealType, Policy>, RealType>& c)
  269. template <class RealType, class Policy>
  270. inline RealType mean(const uniform_distribution<RealType, Policy>& dist)
  271. {
  272. RealType lower = dist.lower();
  273. RealType upper = dist.upper();
  274. RealType result = 0; // of checks.
  275. if(false == detail::check_uniform("boost::math::mean(const uniform_distribution<%1%>&)", lower, upper, &result, Policy()))
  276. {
  277. return result;
  278. }
  279. return (lower + upper ) / 2;
  280. } // RealType mean(const uniform_distribution<RealType, Policy>& dist)
  281. template <class RealType, class Policy>
  282. inline RealType variance(const uniform_distribution<RealType, Policy>& dist)
  283. {
  284. RealType lower = dist.lower();
  285. RealType upper = dist.upper();
  286. RealType result = 0; // of checks.
  287. if(false == detail::check_uniform("boost::math::variance(const uniform_distribution<%1%>&)", lower, upper, &result, Policy()))
  288. {
  289. return result;
  290. }
  291. return (upper - lower) * ( upper - lower) / 12;
  292. // for standard uniform = 0.833333333333333333333333333333333333333333;
  293. } // RealType variance(const uniform_distribution<RealType, Policy>& dist)
  294. template <class RealType, class Policy>
  295. inline RealType mode(const uniform_distribution<RealType, Policy>& dist)
  296. {
  297. RealType lower = dist.lower();
  298. RealType upper = dist.upper();
  299. RealType result = 0; // of checks.
  300. if(false == detail::check_uniform("boost::math::mode(const uniform_distribution<%1%>&)", lower, upper, &result, Policy()))
  301. {
  302. return result;
  303. }
  304. result = lower; // Any value [lower, upper] but arbitrarily choose lower.
  305. return result;
  306. }
  307. template <class RealType, class Policy>
  308. inline RealType median(const uniform_distribution<RealType, Policy>& dist)
  309. {
  310. RealType lower = dist.lower();
  311. RealType upper = dist.upper();
  312. RealType result = 0; // of checks.
  313. if(false == detail::check_uniform("boost::math::median(const uniform_distribution<%1%>&)", lower, upper, &result, Policy()))
  314. {
  315. return result;
  316. }
  317. return (lower + upper) / 2; //
  318. }
  319. template <class RealType, class Policy>
  320. inline RealType skewness(const uniform_distribution<RealType, Policy>& dist)
  321. {
  322. RealType lower = dist.lower();
  323. RealType upper = dist.upper();
  324. RealType result = 0; // of checks.
  325. if(false == detail::check_uniform("boost::math::skewness(const uniform_distribution<%1%>&)",lower, upper, &result, Policy()))
  326. {
  327. return result;
  328. }
  329. return 0;
  330. } // RealType skewness(const uniform_distribution<RealType, Policy>& dist)
  331. template <class RealType, class Policy>
  332. inline RealType kurtosis_excess(const uniform_distribution<RealType, Policy>& dist)
  333. {
  334. RealType lower = dist.lower();
  335. RealType upper = dist.upper();
  336. RealType result = 0; // of checks.
  337. if(false == detail::check_uniform("boost::math::kurtosis_excess(const uniform_distribution<%1%>&)", lower, upper, &result, Policy()))
  338. {
  339. return result;
  340. }
  341. return static_cast<RealType>(-6)/5; // -6/5 = -1.2;
  342. } // RealType kurtosis_excess(const uniform_distribution<RealType, Policy>& dist)
  343. template <class RealType, class Policy>
  344. inline RealType kurtosis(const uniform_distribution<RealType, Policy>& dist)
  345. {
  346. return kurtosis_excess(dist) + 3;
  347. }
  348. template <class RealType, class Policy>
  349. inline RealType entropy(const uniform_distribution<RealType, Policy>& dist)
  350. {
  351. using std::log;
  352. return log(dist.upper() - dist.lower());
  353. }
  354. } // namespace math
  355. } // namespace boost
  356. // This include must be at the end, *after* the accessors
  357. // for this distribution have been defined, in order to
  358. // keep compilers that support two-phase lookup happy.
  359. #include <boost/math/distributions/detail/derived_accessors.hpp>
  360. #endif // BOOST_STATS_UNIFORM_HPP