uniform.hpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  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. template <class RealType, class Policy>
  129. inline const std::pair<RealType, RealType> range(const uniform_distribution<RealType, Policy>& /* dist */)
  130. { // Range of permissible values for random variable x.
  131. using boost::math::tools::max_value;
  132. return std::pair<RealType, RealType>(-max_value<RealType>(), max_value<RealType>()); // - to + 'infinity'.
  133. // Note RealType infinity is NOT permitted, only max_value.
  134. }
  135. template <class RealType, class Policy>
  136. inline const std::pair<RealType, RealType> support(const uniform_distribution<RealType, Policy>& dist)
  137. { // Range of supported values for random variable x.
  138. // This is range where cdf rises from 0 to 1, and outside it, the pdf is zero.
  139. using boost::math::tools::max_value;
  140. return std::pair<RealType, RealType>(dist.lower(), dist.upper());
  141. }
  142. template <class RealType, class Policy>
  143. inline RealType pdf(const uniform_distribution<RealType, Policy>& dist, const RealType& x)
  144. {
  145. RealType lower = dist.lower();
  146. RealType upper = dist.upper();
  147. RealType result = 0; // of checks.
  148. if(false == detail::check_uniform("boost::math::pdf(const uniform_distribution<%1%>&, %1%)", lower, upper, &result, Policy()))
  149. {
  150. return result;
  151. }
  152. if(false == detail::check_uniform_x("boost::math::pdf(const uniform_distribution<%1%>&, %1%)", x, &result, Policy()))
  153. {
  154. return result;
  155. }
  156. if((x < lower) || (x > upper) )
  157. {
  158. return 0;
  159. }
  160. else
  161. {
  162. return 1 / (upper - lower);
  163. }
  164. } // RealType pdf(const uniform_distribution<RealType, Policy>& dist, const RealType& x)
  165. template <class RealType, class Policy>
  166. inline RealType cdf(const uniform_distribution<RealType, Policy>& dist, const RealType& x)
  167. {
  168. RealType lower = dist.lower();
  169. RealType upper = dist.upper();
  170. RealType result = 0; // of checks.
  171. if(false == detail::check_uniform("boost::math::cdf(const uniform_distribution<%1%>&, %1%)",lower, upper, &result, Policy()))
  172. {
  173. return result;
  174. }
  175. if(false == detail::check_uniform_x("boost::math::cdf(const uniform_distribution<%1%>&, %1%)", x, &result, Policy()))
  176. {
  177. return result;
  178. }
  179. if (x < lower)
  180. {
  181. return 0;
  182. }
  183. if (x > upper)
  184. {
  185. return 1;
  186. }
  187. return (x - lower) / (upper - lower); // lower <= x <= upper
  188. } // RealType cdf(const uniform_distribution<RealType, Policy>& dist, const RealType& x)
  189. template <class RealType, class Policy>
  190. inline RealType quantile(const uniform_distribution<RealType, Policy>& dist, const RealType& p)
  191. {
  192. RealType lower = dist.lower();
  193. RealType upper = dist.upper();
  194. RealType result = 0; // of checks
  195. if(false == detail::check_uniform("boost::math::quantile(const uniform_distribution<%1%>&, %1%)",lower, upper, &result, Policy()))
  196. {
  197. return result;
  198. }
  199. if(false == detail::check_probability("boost::math::quantile(const uniform_distribution<%1%>&, %1%)", p, &result, Policy()))
  200. {
  201. return result;
  202. }
  203. if(p == 0)
  204. {
  205. return lower;
  206. }
  207. if(p == 1)
  208. {
  209. return upper;
  210. }
  211. return p * (upper - lower) + lower;
  212. } // RealType quantile(const uniform_distribution<RealType, Policy>& dist, const RealType& p)
  213. template <class RealType, class Policy>
  214. inline RealType cdf(const complemented2_type<uniform_distribution<RealType, Policy>, RealType>& c)
  215. {
  216. RealType lower = c.dist.lower();
  217. RealType upper = c.dist.upper();
  218. RealType x = c.param;
  219. RealType result = 0; // of checks.
  220. if(false == detail::check_uniform("boost::math::cdf(const uniform_distribution<%1%>&, %1%)", lower, upper, &result, Policy()))
  221. {
  222. return result;
  223. }
  224. if(false == detail::check_uniform_x("boost::math::cdf(const uniform_distribution<%1%>&, %1%)", x, &result, Policy()))
  225. {
  226. return result;
  227. }
  228. if (x < lower)
  229. {
  230. return 1;
  231. }
  232. if (x > upper)
  233. {
  234. return 0;
  235. }
  236. return (upper - x) / (upper - lower);
  237. } // RealType cdf(const complemented2_type<uniform_distribution<RealType, Policy>, RealType>& c)
  238. template <class RealType, class Policy>
  239. inline RealType quantile(const complemented2_type<uniform_distribution<RealType, Policy>, RealType>& c)
  240. {
  241. RealType lower = c.dist.lower();
  242. RealType upper = c.dist.upper();
  243. RealType q = c.param;
  244. RealType result = 0; // of checks.
  245. if(false == detail::check_uniform("boost::math::quantile(const uniform_distribution<%1%>&, %1%)", lower, upper, &result, Policy()))
  246. {
  247. return result;
  248. }
  249. if(false == detail::check_probability("boost::math::quantile(const uniform_distribution<%1%>&, %1%)", q, &result, Policy()))
  250. {
  251. return result;
  252. }
  253. if(q == 0)
  254. {
  255. return upper;
  256. }
  257. if(q == 1)
  258. {
  259. return lower;
  260. }
  261. return -q * (upper - lower) + upper;
  262. } // RealType quantile(const complemented2_type<uniform_distribution<RealType, Policy>, RealType>& c)
  263. template <class RealType, class Policy>
  264. inline RealType mean(const uniform_distribution<RealType, Policy>& dist)
  265. {
  266. RealType lower = dist.lower();
  267. RealType upper = dist.upper();
  268. RealType result = 0; // of checks.
  269. if(false == detail::check_uniform("boost::math::mean(const uniform_distribution<%1%>&)", lower, upper, &result, Policy()))
  270. {
  271. return result;
  272. }
  273. return (lower + upper ) / 2;
  274. } // RealType mean(const uniform_distribution<RealType, Policy>& dist)
  275. template <class RealType, class Policy>
  276. inline RealType variance(const uniform_distribution<RealType, Policy>& dist)
  277. {
  278. RealType lower = dist.lower();
  279. RealType upper = dist.upper();
  280. RealType result = 0; // of checks.
  281. if(false == detail::check_uniform("boost::math::variance(const uniform_distribution<%1%>&)", lower, upper, &result, Policy()))
  282. {
  283. return result;
  284. }
  285. return (upper - lower) * ( upper - lower) / 12;
  286. // for standard uniform = 0.833333333333333333333333333333333333333333;
  287. } // RealType variance(const uniform_distribution<RealType, Policy>& dist)
  288. template <class RealType, class Policy>
  289. inline RealType mode(const uniform_distribution<RealType, Policy>& dist)
  290. {
  291. RealType lower = dist.lower();
  292. RealType upper = dist.upper();
  293. RealType result = 0; // of checks.
  294. if(false == detail::check_uniform("boost::math::mode(const uniform_distribution<%1%>&)", lower, upper, &result, Policy()))
  295. {
  296. return result;
  297. }
  298. result = lower; // Any value [lower, upper] but arbitrarily choose lower.
  299. return result;
  300. }
  301. template <class RealType, class Policy>
  302. inline RealType median(const uniform_distribution<RealType, Policy>& dist)
  303. {
  304. RealType lower = dist.lower();
  305. RealType upper = dist.upper();
  306. RealType result = 0; // of checks.
  307. if(false == detail::check_uniform("boost::math::median(const uniform_distribution<%1%>&)", lower, upper, &result, Policy()))
  308. {
  309. return result;
  310. }
  311. return (lower + upper) / 2; //
  312. }
  313. template <class RealType, class Policy>
  314. inline RealType skewness(const uniform_distribution<RealType, Policy>& dist)
  315. {
  316. RealType lower = dist.lower();
  317. RealType upper = dist.upper();
  318. RealType result = 0; // of checks.
  319. if(false == detail::check_uniform("boost::math::skewness(const uniform_distribution<%1%>&)",lower, upper, &result, Policy()))
  320. {
  321. return result;
  322. }
  323. return 0;
  324. } // RealType skewness(const uniform_distribution<RealType, Policy>& dist)
  325. template <class RealType, class Policy>
  326. inline RealType kurtosis_excess(const uniform_distribution<RealType, Policy>& dist)
  327. {
  328. RealType lower = dist.lower();
  329. RealType upper = dist.upper();
  330. RealType result = 0; // of checks.
  331. if(false == detail::check_uniform("boost::math::kurtosis_execess(const uniform_distribution<%1%>&)", lower, upper, &result, Policy()))
  332. {
  333. return result;
  334. }
  335. return static_cast<RealType>(-6)/5; // -6/5 = -1.2;
  336. } // RealType kurtosis_excess(const uniform_distribution<RealType, Policy>& dist)
  337. template <class RealType, class Policy>
  338. inline RealType kurtosis(const uniform_distribution<RealType, Policy>& dist)
  339. {
  340. return kurtosis_excess(dist) + 3;
  341. }
  342. template <class RealType, class Policy>
  343. inline RealType entropy(const uniform_distribution<RealType, Policy>& dist)
  344. {
  345. using std::log;
  346. return log(dist.upper() - dist.lower());
  347. }
  348. } // namespace math
  349. } // namespace boost
  350. // This include must be at the end, *after* the accessors
  351. // for this distribution have been defined, in order to
  352. // keep compilers that support two-phase lookup happy.
  353. #include <boost/math/distributions/detail/derived_accessors.hpp>
  354. #endif // BOOST_STATS_UNIFORM_HPP