arcsine.hpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535
  1. // boost/math/distributions/arcsine.hpp
  2. // Copyright John Maddock 2014.
  3. // Copyright Paul A. Bristow 2014.
  4. // Use, modification and distribution are subject to the
  5. // Boost Software License, Version 1.0.
  6. // (See accompanying file LICENSE_1_0.txt
  7. // or copy at http://www.boost.org/LICENSE_1_0.txt)
  8. // http://en.wikipedia.org/wiki/arcsine_distribution
  9. // The arcsine Distribution is a continuous probability distribution.
  10. // http://en.wikipedia.org/wiki/Arcsine_distribution
  11. // http://www.wolframalpha.com/input/?i=ArcSinDistribution
  12. // Standard arcsine distribution is a special case of beta distribution with both a & b = one half,
  13. // and 0 <= x <= 1.
  14. // It is generalized to include any bounded support a <= x <= b from 0 <= x <= 1
  15. // by Wolfram and Wikipedia,
  16. // but using location and scale parameters by
  17. // Virtual Laboratories in Probability and Statistics http://www.math.uah.edu/stat/index.html
  18. // http://www.math.uah.edu/stat/special/Arcsine.html
  19. // The end-point version is simpler and more obvious, so we implement that.
  20. // TODO Perhaps provide location and scale functions?
  21. #ifndef BOOST_MATH_DIST_ARCSINE_HPP
  22. #define BOOST_MATH_DIST_ARCSINE_HPP
  23. #include <boost/math/distributions/fwd.hpp>
  24. #include <boost/math/distributions/complement.hpp> // complements.
  25. #include <boost/math/distributions/detail/common_error_handling.hpp> // error checks.
  26. #include <boost/math/constants/constants.hpp>
  27. #include <boost/math/special_functions/fpclassify.hpp> // isnan.
  28. #if defined (BOOST_MSVC)
  29. # pragma warning(push)
  30. # pragma warning(disable: 4702) // Unreachable code,
  31. // in domain_error_imp in error_handling.
  32. #endif
  33. #include <utility>
  34. #include <exception> // For std::domain_error.
  35. namespace boost
  36. {
  37. namespace math
  38. {
  39. namespace arcsine_detail
  40. {
  41. // Common error checking routines for arcsine distribution functions:
  42. // Duplicating for x_min and x_max provides specific error messages.
  43. template <class RealType, class Policy>
  44. inline bool check_x_min(const char* function, const RealType& x, RealType* result, const Policy& pol)
  45. {
  46. if (!(boost::math::isfinite)(x))
  47. {
  48. *result = policies::raise_domain_error<RealType>(
  49. function,
  50. "x_min argument is %1%, but must be finite !", x, pol);
  51. return false;
  52. }
  53. return true;
  54. } // bool check_x_min
  55. template <class RealType, class Policy>
  56. inline bool check_x_max(const char* function, const RealType& x, RealType* result, const Policy& pol)
  57. {
  58. if (!(boost::math::isfinite)(x))
  59. {
  60. *result = policies::raise_domain_error<RealType>(
  61. function,
  62. "x_max argument is %1%, but must be finite !", x, pol);
  63. return false;
  64. }
  65. return true;
  66. } // bool check_x_max
  67. template <class RealType, class Policy>
  68. inline bool check_x_minmax(const char* function, const RealType& x_min, const RealType& x_max, RealType* result, const Policy& pol)
  69. { // Check x_min < x_max
  70. if (x_min >= x_max)
  71. {
  72. std::string msg = "x_max argument is %1%, but must be > x_min = " + lexical_cast<std::string>(x_min) + "!";
  73. *result = policies::raise_domain_error<RealType>(
  74. function,
  75. msg.c_str(), x_max, pol);
  76. // "x_max argument is %1%, but must be > x_min !", x_max, pol);
  77. // "x_max argument is %1%, but must be > x_min %2!", x_max, x_min, pol); would be better.
  78. // But would require replication of all helpers functions in /policies/error_handling.hpp for two values,
  79. // as well as two value versions of raise_error, raise_domain_error and do_format ...
  80. // so use slightly hacky lexical_cast to string instead.
  81. return false;
  82. }
  83. return true;
  84. } // bool check_x_minmax
  85. template <class RealType, class Policy>
  86. inline bool check_prob(const char* function, const RealType& p, RealType* result, const Policy& pol)
  87. {
  88. if ((p < 0) || (p > 1) || !(boost::math::isfinite)(p))
  89. {
  90. *result = policies::raise_domain_error<RealType>(
  91. function,
  92. "Probability argument is %1%, but must be >= 0 and <= 1 !", p, pol);
  93. return false;
  94. }
  95. return true;
  96. } // bool check_prob
  97. template <class RealType, class Policy>
  98. inline bool check_x(const char* function, const RealType& x_min, const RealType& x_max, const RealType& x, RealType* result, const Policy& pol)
  99. { // Check x finite and x_min < x < x_max.
  100. if (!(boost::math::isfinite)(x))
  101. {
  102. *result = policies::raise_domain_error<RealType>(
  103. function,
  104. "x argument is %1%, but must be finite !", x, pol);
  105. return false;
  106. }
  107. if ((x < x_min) || (x > x_max))
  108. {
  109. // std::cout << x_min << ' ' << x << x_max << std::endl;
  110. *result = policies::raise_domain_error<RealType>(
  111. function,
  112. "x argument is %1%, but must be x_min < x < x_max !", x, pol);
  113. // For example:
  114. // Error in function boost::math::pdf(arcsine_distribution<double> const&, double) : x argument is -1.01, but must be x_min < x < x_max !
  115. // TODO Perhaps show values of x_min and x_max?
  116. return false;
  117. }
  118. return true;
  119. } // bool check_x
  120. template <class RealType, class Policy>
  121. inline bool check_dist(const char* function, const RealType& x_min, const RealType& x_max, RealType* result, const Policy& pol)
  122. { // Check both x_min and x_max finite, and x_min < x_max.
  123. return check_x_min(function, x_min, result, pol)
  124. && check_x_max(function, x_max, result, pol)
  125. && check_x_minmax(function, x_min, x_max, result, pol);
  126. } // bool check_dist
  127. template <class RealType, class Policy>
  128. inline bool check_dist_and_x(const char* function, const RealType& x_min, const RealType& x_max, RealType x, RealType* result, const Policy& pol)
  129. {
  130. return check_dist(function, x_min, x_max, result, pol)
  131. && arcsine_detail::check_x(function, x_min, x_max, x, result, pol);
  132. } // bool check_dist_and_x
  133. template <class RealType, class Policy>
  134. inline bool check_dist_and_prob(const char* function, const RealType& x_min, const RealType& x_max, RealType p, RealType* result, const Policy& pol)
  135. {
  136. return check_dist(function, x_min, x_max, result, pol)
  137. && check_prob(function, p, result, pol);
  138. } // bool check_dist_and_prob
  139. } // namespace arcsine_detail
  140. template <class RealType = double, class Policy = policies::policy<> >
  141. class arcsine_distribution
  142. {
  143. public:
  144. typedef RealType value_type;
  145. typedef Policy policy_type;
  146. arcsine_distribution(RealType x_min = 0, RealType x_max = 1) : m_x_min(x_min), m_x_max(x_max)
  147. { // Default beta (alpha = beta = 0.5) is standard arcsine with x_min = 0, x_max = 1.
  148. // Generalized to allow x_min and x_max to be specified.
  149. RealType result;
  150. arcsine_detail::check_dist(
  151. "boost::math::arcsine_distribution<%1%>::arcsine_distribution",
  152. m_x_min,
  153. m_x_max,
  154. &result, Policy());
  155. } // arcsine_distribution constructor.
  156. // Accessor functions:
  157. RealType x_min() const
  158. {
  159. return m_x_min;
  160. }
  161. RealType x_max() const
  162. {
  163. return m_x_max;
  164. }
  165. private:
  166. RealType m_x_min; // Two x min and x max parameters of the arcsine distribution.
  167. RealType m_x_max;
  168. }; // template <class RealType, class Policy> class arcsine_distribution
  169. // Convenient typedef to construct double version.
  170. typedef arcsine_distribution<double> arcsine;
  171. template <class RealType, class Policy>
  172. inline const std::pair<RealType, RealType> range(const arcsine_distribution<RealType, Policy>& dist)
  173. { // Range of permissible values for random variable x.
  174. using boost::math::tools::max_value;
  175. return std::pair<RealType, RealType>(static_cast<RealType>(dist.x_min()), static_cast<RealType>(dist.x_max()));
  176. }
  177. template <class RealType, class Policy>
  178. inline const std::pair<RealType, RealType> support(const arcsine_distribution<RealType, Policy>& dist)
  179. { // Range of supported values for random variable x.
  180. // This is range where cdf rises from 0 to 1, and outside it, the pdf is zero.
  181. return std::pair<RealType, RealType>(static_cast<RealType>(dist.x_min()), static_cast<RealType>(dist.x_max()));
  182. }
  183. template <class RealType, class Policy>
  184. inline RealType mean(const arcsine_distribution<RealType, Policy>& dist)
  185. { // Mean of arcsine distribution .
  186. RealType result;
  187. RealType x_min = dist.x_min();
  188. RealType x_max = dist.x_max();
  189. if (false == arcsine_detail::check_dist(
  190. "boost::math::mean(arcsine_distribution<%1%> const&, %1% )",
  191. x_min,
  192. x_max,
  193. &result, Policy())
  194. )
  195. {
  196. return result;
  197. }
  198. return (x_min + x_max) / 2;
  199. } // mean
  200. template <class RealType, class Policy>
  201. inline RealType variance(const arcsine_distribution<RealType, Policy>& dist)
  202. { // Variance of standard arcsine distribution = (1-0)/8 = 0.125.
  203. RealType result;
  204. RealType x_min = dist.x_min();
  205. RealType x_max = dist.x_max();
  206. if (false == arcsine_detail::check_dist(
  207. "boost::math::variance(arcsine_distribution<%1%> const&, %1% )",
  208. x_min,
  209. x_max,
  210. &result, Policy())
  211. )
  212. {
  213. return result;
  214. }
  215. return (x_max - x_min) * (x_max - x_min) / 8;
  216. } // variance
  217. template <class RealType, class Policy>
  218. inline RealType mode(const arcsine_distribution<RealType, Policy>& /* dist */)
  219. { //There are always [*two] values for the mode, at ['x_min] and at ['x_max], default 0 and 1,
  220. // so instead we raise the exception domain_error.
  221. return policies::raise_domain_error<RealType>(
  222. "boost::math::mode(arcsine_distribution<%1%>&)",
  223. "The arcsine distribution has two modes at x_min and x_max: "
  224. "so the return value is %1%.",
  225. std::numeric_limits<RealType>::quiet_NaN(), Policy());
  226. } // mode
  227. template <class RealType, class Policy>
  228. inline RealType median(const arcsine_distribution<RealType, Policy>& dist)
  229. { // Median of arcsine distribution (a + b) / 2 == mean.
  230. RealType x_min = dist.x_min();
  231. RealType x_max = dist.x_max();
  232. RealType result;
  233. if (false == arcsine_detail::check_dist(
  234. "boost::math::median(arcsine_distribution<%1%> const&, %1% )",
  235. x_min,
  236. x_max,
  237. &result, Policy())
  238. )
  239. {
  240. return result;
  241. }
  242. return (x_min + x_max) / 2;
  243. }
  244. template <class RealType, class Policy>
  245. inline RealType skewness(const arcsine_distribution<RealType, Policy>& dist)
  246. {
  247. RealType result;
  248. RealType x_min = dist.x_min();
  249. RealType x_max = dist.x_max();
  250. if (false == arcsine_detail::check_dist(
  251. "boost::math::skewness(arcsine_distribution<%1%> const&, %1% )",
  252. x_min,
  253. x_max,
  254. &result, Policy())
  255. )
  256. {
  257. return result;
  258. }
  259. return 0;
  260. } // skewness
  261. template <class RealType, class Policy>
  262. inline RealType kurtosis_excess(const arcsine_distribution<RealType, Policy>& dist)
  263. {
  264. RealType result;
  265. RealType x_min = dist.x_min();
  266. RealType x_max = dist.x_max();
  267. if (false == arcsine_detail::check_dist(
  268. "boost::math::kurtosis_excess(arcsine_distribution<%1%> const&, %1% )",
  269. x_min,
  270. x_max,
  271. &result, Policy())
  272. )
  273. {
  274. return result;
  275. }
  276. result = -3;
  277. return result / 2;
  278. } // kurtosis_excess
  279. template <class RealType, class Policy>
  280. inline RealType kurtosis(const arcsine_distribution<RealType, Policy>& dist)
  281. {
  282. RealType result;
  283. RealType x_min = dist.x_min();
  284. RealType x_max = dist.x_max();
  285. if (false == arcsine_detail::check_dist(
  286. "boost::math::kurtosis(arcsine_distribution<%1%> const&, %1% )",
  287. x_min,
  288. x_max,
  289. &result, Policy())
  290. )
  291. {
  292. return result;
  293. }
  294. return 3 + kurtosis_excess(dist);
  295. } // kurtosis
  296. template <class RealType, class Policy>
  297. inline RealType pdf(const arcsine_distribution<RealType, Policy>& dist, const RealType& xx)
  298. { // Probability Density/Mass Function arcsine.
  299. BOOST_FPU_EXCEPTION_GUARD
  300. BOOST_MATH_STD_USING // For ADL of std functions.
  301. static const char* function = "boost::math::pdf(arcsine_distribution<%1%> const&, %1%)";
  302. RealType lo = dist.x_min();
  303. RealType hi = dist.x_max();
  304. RealType x = xx;
  305. // Argument checks:
  306. RealType result = 0;
  307. if (false == arcsine_detail::check_dist_and_x(
  308. function,
  309. lo, hi, x,
  310. &result, Policy()))
  311. {
  312. return result;
  313. }
  314. using boost::math::constants::pi;
  315. result = static_cast<RealType>(1) / (pi<RealType>() * sqrt((x - lo) * (hi - x)));
  316. return result;
  317. } // pdf
  318. template <class RealType, class Policy>
  319. inline RealType cdf(const arcsine_distribution<RealType, Policy>& dist, const RealType& x)
  320. { // Cumulative Distribution Function arcsine.
  321. BOOST_MATH_STD_USING // For ADL of std functions.
  322. static const char* function = "boost::math::cdf(arcsine_distribution<%1%> const&, %1%)";
  323. RealType x_min = dist.x_min();
  324. RealType x_max = dist.x_max();
  325. // Argument checks:
  326. RealType result = 0;
  327. if (false == arcsine_detail::check_dist_and_x(
  328. function,
  329. x_min, x_max, x,
  330. &result, Policy()))
  331. {
  332. return result;
  333. }
  334. // Special cases:
  335. if (x == x_min)
  336. {
  337. return 0;
  338. }
  339. else if (x == x_max)
  340. {
  341. return 1;
  342. }
  343. using boost::math::constants::pi;
  344. result = static_cast<RealType>(2) * asin(sqrt((x - x_min) / (x_max - x_min))) / pi<RealType>();
  345. return result;
  346. } // arcsine cdf
  347. template <class RealType, class Policy>
  348. inline RealType cdf(const complemented2_type<arcsine_distribution<RealType, Policy>, RealType>& c)
  349. { // Complemented Cumulative Distribution Function arcsine.
  350. BOOST_MATH_STD_USING // For ADL of std functions.
  351. static const char* function = "boost::math::cdf(arcsine_distribution<%1%> const&, %1%)";
  352. RealType x = c.param;
  353. arcsine_distribution<RealType, Policy> const& dist = c.dist;
  354. RealType x_min = dist.x_min();
  355. RealType x_max = dist.x_max();
  356. // Argument checks:
  357. RealType result = 0;
  358. if (false == arcsine_detail::check_dist_and_x(
  359. function,
  360. x_min, x_max, x,
  361. &result, Policy()))
  362. {
  363. return result;
  364. }
  365. if (x == x_min)
  366. {
  367. return 0;
  368. }
  369. else if (x == x_max)
  370. {
  371. return 1;
  372. }
  373. using boost::math::constants::pi;
  374. // Naive version x = 1 - x;
  375. // result = static_cast<RealType>(2) * asin(sqrt((x - x_min) / (x_max - x_min))) / pi<RealType>();
  376. // is less accurate, so use acos instead of asin for complement.
  377. result = static_cast<RealType>(2) * acos(sqrt((x - x_min) / (x_max - x_min))) / pi<RealType>();
  378. return result;
  379. } // arcsine ccdf
  380. template <class RealType, class Policy>
  381. inline RealType quantile(const arcsine_distribution<RealType, Policy>& dist, const RealType& p)
  382. {
  383. // Quantile or Percent Point arcsine function or
  384. // Inverse Cumulative probability distribution function CDF.
  385. // Return x (0 <= x <= 1),
  386. // for a given probability p (0 <= p <= 1).
  387. // These functions take a probability as an argument
  388. // and return a value such that the probability that a random variable x
  389. // will be less than or equal to that value
  390. // is whatever probability you supplied as an argument.
  391. BOOST_MATH_STD_USING // For ADL of std functions.
  392. using boost::math::constants::half_pi;
  393. static const char* function = "boost::math::quantile(arcsine_distribution<%1%> const&, %1%)";
  394. RealType result = 0; // of argument checks:
  395. RealType x_min = dist.x_min();
  396. RealType x_max = dist.x_max();
  397. if (false == arcsine_detail::check_dist_and_prob(
  398. function,
  399. x_min, x_max, p,
  400. &result, Policy()))
  401. {
  402. return result;
  403. }
  404. // Special cases:
  405. if (p == 0)
  406. {
  407. return 0;
  408. }
  409. if (p == 1)
  410. {
  411. return 1;
  412. }
  413. RealType sin2hpip = sin(half_pi<RealType>() * p);
  414. RealType sin2hpip2 = sin2hpip * sin2hpip;
  415. result = -x_min * sin2hpip2 + x_min + x_max * sin2hpip2;
  416. return result;
  417. } // quantile
  418. template <class RealType, class Policy>
  419. inline RealType quantile(const complemented2_type<arcsine_distribution<RealType, Policy>, RealType>& c)
  420. {
  421. // Complement Quantile or Percent Point arcsine function.
  422. // Return the number of expected x for a given
  423. // complement of the probability q.
  424. BOOST_MATH_STD_USING // For ADL of std functions.
  425. using boost::math::constants::half_pi;
  426. static const char* function = "boost::math::quantile(arcsine_distribution<%1%> const&, %1%)";
  427. // Error checks:
  428. RealType q = c.param;
  429. const arcsine_distribution<RealType, Policy>& dist = c.dist;
  430. RealType result = 0;
  431. RealType x_min = dist.x_min();
  432. RealType x_max = dist.x_max();
  433. if (false == arcsine_detail::check_dist_and_prob(
  434. function,
  435. x_min,
  436. x_max,
  437. q,
  438. &result, Policy()))
  439. {
  440. return result;
  441. }
  442. // Special cases:
  443. if (q == 1)
  444. {
  445. return 0;
  446. }
  447. if (q == 0)
  448. {
  449. return 1;
  450. }
  451. // Naive RealType p = 1 - q; result = sin(half_pi<RealType>() * p); loses accuracy, so use a cos alternative instead.
  452. //result = cos(half_pi<RealType>() * q); // for arcsine(0,1)
  453. //result = result * result;
  454. // For generalized arcsine:
  455. RealType cos2hpip = cos(half_pi<RealType>() * q);
  456. RealType cos2hpip2 = cos2hpip * cos2hpip;
  457. result = -x_min * cos2hpip2 + x_min + x_max * cos2hpip2;
  458. return result;
  459. } // Quantile Complement
  460. } // namespace math
  461. } // namespace boost
  462. // This include must be at the end, *after* the accessors
  463. // for this distribution have been defined, in order to
  464. // keep compilers that support two-phase lookup happy.
  465. #include <boost/math/distributions/detail/derived_accessors.hpp>
  466. #if defined (BOOST_MSVC)
  467. # pragma warning(pop)
  468. #endif
  469. #endif // BOOST_MATH_DIST_ARCSINE_HPP