arcsine.hpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541
  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 <cmath>
  24. #include <boost/math/distributions/fwd.hpp>
  25. #include <boost/math/distributions/complement.hpp> // complements.
  26. #include <boost/math/distributions/detail/common_error_handling.hpp> // error checks.
  27. #include <boost/math/constants/constants.hpp>
  28. #include <boost/math/special_functions/fpclassify.hpp> // isnan.
  29. #if defined (BOOST_MSVC)
  30. # pragma warning(push)
  31. # pragma warning(disable: 4702) // Unreachable code,
  32. // in domain_error_imp in error_handling.
  33. #endif
  34. #include <utility>
  35. #include <exception> // For std::domain_error.
  36. namespace boost
  37. {
  38. namespace math
  39. {
  40. namespace arcsine_detail
  41. {
  42. // Common error checking routines for arcsine distribution functions:
  43. // Duplicating for x_min and x_max provides specific error messages.
  44. template <class RealType, class Policy>
  45. inline bool check_x_min(const char* function, const RealType& x, RealType* result, const Policy& pol)
  46. {
  47. if (!(boost::math::isfinite)(x))
  48. {
  49. *result = policies::raise_domain_error<RealType>(
  50. function,
  51. "x_min argument is %1%, but must be finite !", x, pol);
  52. return false;
  53. }
  54. return true;
  55. } // bool check_x_min
  56. template <class RealType, class Policy>
  57. inline bool check_x_max(const char* function, const RealType& x, RealType* result, const Policy& pol)
  58. {
  59. if (!(boost::math::isfinite)(x))
  60. {
  61. *result = policies::raise_domain_error<RealType>(
  62. function,
  63. "x_max argument is %1%, but must be finite !", x, pol);
  64. return false;
  65. }
  66. return true;
  67. } // bool check_x_max
  68. template <class RealType, class Policy>
  69. inline bool check_x_minmax(const char* function, const RealType& x_min, const RealType& x_max, RealType* result, const Policy& pol)
  70. { // Check x_min < x_max
  71. if (x_min >= x_max)
  72. {
  73. std::string msg = "x_max argument is %1%, but must be > x_min";
  74. *result = policies::raise_domain_error<RealType>(
  75. function,
  76. msg.c_str(), x_max, pol);
  77. // "x_max argument is %1%, but must be > x_min !", x_max, pol);
  78. // "x_max argument is %1%, but must be > x_min %2!", x_max, x_min, pol); would be better.
  79. // But would require replication of all helpers functions in /policies/error_handling.hpp for two values,
  80. // as well as two value versions of raise_error, raise_domain_error and do_format
  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. #ifdef __cpp_deduction_guides
  172. template <class RealType>
  173. arcsine_distribution(RealType)->arcsine_distribution<typename boost::math::tools::promote_args<RealType>::type>;
  174. template <class RealType>
  175. arcsine_distribution(RealType, RealType)->arcsine_distribution<typename boost::math::tools::promote_args<RealType>::type>;
  176. #endif
  177. template <class RealType, class Policy>
  178. inline const std::pair<RealType, RealType> range(const arcsine_distribution<RealType, Policy>& dist)
  179. { // Range of permissible values for random variable x.
  180. using boost::math::tools::max_value;
  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 const std::pair<RealType, RealType> support(const arcsine_distribution<RealType, Policy>& dist)
  185. { // Range of supported values for random variable x.
  186. // This is range where cdf rises from 0 to 1, and outside it, the pdf is zero.
  187. return std::pair<RealType, RealType>(static_cast<RealType>(dist.x_min()), static_cast<RealType>(dist.x_max()));
  188. }
  189. template <class RealType, class Policy>
  190. inline RealType mean(const arcsine_distribution<RealType, Policy>& dist)
  191. { // Mean of arcsine distribution .
  192. RealType result;
  193. RealType x_min = dist.x_min();
  194. RealType x_max = dist.x_max();
  195. if (false == arcsine_detail::check_dist(
  196. "boost::math::mean(arcsine_distribution<%1%> const&, %1% )",
  197. x_min,
  198. x_max,
  199. &result, Policy())
  200. )
  201. {
  202. return result;
  203. }
  204. return (x_min + x_max) / 2;
  205. } // mean
  206. template <class RealType, class Policy>
  207. inline RealType variance(const arcsine_distribution<RealType, Policy>& dist)
  208. { // Variance of standard arcsine distribution = (1-0)/8 = 0.125.
  209. RealType result;
  210. RealType x_min = dist.x_min();
  211. RealType x_max = dist.x_max();
  212. if (false == arcsine_detail::check_dist(
  213. "boost::math::variance(arcsine_distribution<%1%> const&, %1% )",
  214. x_min,
  215. x_max,
  216. &result, Policy())
  217. )
  218. {
  219. return result;
  220. }
  221. return (x_max - x_min) * (x_max - x_min) / 8;
  222. } // variance
  223. template <class RealType, class Policy>
  224. inline RealType mode(const arcsine_distribution<RealType, Policy>& /* dist */)
  225. { //There are always [*two] values for the mode, at ['x_min] and at ['x_max], default 0 and 1,
  226. // so instead we raise the exception domain_error.
  227. return policies::raise_domain_error<RealType>(
  228. "boost::math::mode(arcsine_distribution<%1%>&)",
  229. "The arcsine distribution has two modes at x_min and x_max: "
  230. "so the return value is %1%.",
  231. std::numeric_limits<RealType>::quiet_NaN(), Policy());
  232. } // mode
  233. template <class RealType, class Policy>
  234. inline RealType median(const arcsine_distribution<RealType, Policy>& dist)
  235. { // Median of arcsine distribution (a + b) / 2 == mean.
  236. RealType x_min = dist.x_min();
  237. RealType x_max = dist.x_max();
  238. RealType result;
  239. if (false == arcsine_detail::check_dist(
  240. "boost::math::median(arcsine_distribution<%1%> const&, %1% )",
  241. x_min,
  242. x_max,
  243. &result, Policy())
  244. )
  245. {
  246. return result;
  247. }
  248. return (x_min + x_max) / 2;
  249. }
  250. template <class RealType, class Policy>
  251. inline RealType skewness(const arcsine_distribution<RealType, Policy>& dist)
  252. {
  253. RealType result;
  254. RealType x_min = dist.x_min();
  255. RealType x_max = dist.x_max();
  256. if (false == arcsine_detail::check_dist(
  257. "boost::math::skewness(arcsine_distribution<%1%> const&, %1% )",
  258. x_min,
  259. x_max,
  260. &result, Policy())
  261. )
  262. {
  263. return result;
  264. }
  265. return 0;
  266. } // skewness
  267. template <class RealType, class Policy>
  268. inline RealType kurtosis_excess(const arcsine_distribution<RealType, Policy>& dist)
  269. {
  270. RealType result;
  271. RealType x_min = dist.x_min();
  272. RealType x_max = dist.x_max();
  273. if (false == arcsine_detail::check_dist(
  274. "boost::math::kurtosis_excess(arcsine_distribution<%1%> const&, %1% )",
  275. x_min,
  276. x_max,
  277. &result, Policy())
  278. )
  279. {
  280. return result;
  281. }
  282. result = -3;
  283. return result / 2;
  284. } // kurtosis_excess
  285. template <class RealType, class Policy>
  286. inline RealType kurtosis(const arcsine_distribution<RealType, Policy>& dist)
  287. {
  288. RealType result;
  289. RealType x_min = dist.x_min();
  290. RealType x_max = dist.x_max();
  291. if (false == arcsine_detail::check_dist(
  292. "boost::math::kurtosis(arcsine_distribution<%1%> const&, %1% )",
  293. x_min,
  294. x_max,
  295. &result, Policy())
  296. )
  297. {
  298. return result;
  299. }
  300. return 3 + kurtosis_excess(dist);
  301. } // kurtosis
  302. template <class RealType, class Policy>
  303. inline RealType pdf(const arcsine_distribution<RealType, Policy>& dist, const RealType& xx)
  304. { // Probability Density/Mass Function arcsine.
  305. BOOST_FPU_EXCEPTION_GUARD
  306. BOOST_MATH_STD_USING // For ADL of std functions.
  307. static const char* function = "boost::math::pdf(arcsine_distribution<%1%> const&, %1%)";
  308. RealType lo = dist.x_min();
  309. RealType hi = dist.x_max();
  310. RealType x = xx;
  311. // Argument checks:
  312. RealType result = 0;
  313. if (false == arcsine_detail::check_dist_and_x(
  314. function,
  315. lo, hi, x,
  316. &result, Policy()))
  317. {
  318. return result;
  319. }
  320. using boost::math::constants::pi;
  321. result = static_cast<RealType>(1) / (pi<RealType>() * sqrt((x - lo) * (hi - x)));
  322. return result;
  323. } // pdf
  324. template <class RealType, class Policy>
  325. inline RealType cdf(const arcsine_distribution<RealType, Policy>& dist, const RealType& x)
  326. { // Cumulative Distribution Function arcsine.
  327. BOOST_MATH_STD_USING // For ADL of std functions.
  328. static const char* function = "boost::math::cdf(arcsine_distribution<%1%> const&, %1%)";
  329. RealType x_min = dist.x_min();
  330. RealType x_max = dist.x_max();
  331. // Argument checks:
  332. RealType result = 0;
  333. if (false == arcsine_detail::check_dist_and_x(
  334. function,
  335. x_min, x_max, x,
  336. &result, Policy()))
  337. {
  338. return result;
  339. }
  340. // Special cases:
  341. if (x == x_min)
  342. {
  343. return 0;
  344. }
  345. else if (x == x_max)
  346. {
  347. return 1;
  348. }
  349. using boost::math::constants::pi;
  350. result = static_cast<RealType>(2) * asin(sqrt((x - x_min) / (x_max - x_min))) / pi<RealType>();
  351. return result;
  352. } // arcsine cdf
  353. template <class RealType, class Policy>
  354. inline RealType cdf(const complemented2_type<arcsine_distribution<RealType, Policy>, RealType>& c)
  355. { // Complemented Cumulative Distribution Function arcsine.
  356. BOOST_MATH_STD_USING // For ADL of std functions.
  357. static const char* function = "boost::math::cdf(arcsine_distribution<%1%> const&, %1%)";
  358. RealType x = c.param;
  359. arcsine_distribution<RealType, Policy> const& dist = c.dist;
  360. RealType x_min = dist.x_min();
  361. RealType x_max = dist.x_max();
  362. // Argument checks:
  363. RealType result = 0;
  364. if (false == arcsine_detail::check_dist_and_x(
  365. function,
  366. x_min, x_max, x,
  367. &result, Policy()))
  368. {
  369. return result;
  370. }
  371. if (x == x_min)
  372. {
  373. return 0;
  374. }
  375. else if (x == x_max)
  376. {
  377. return 1;
  378. }
  379. using boost::math::constants::pi;
  380. // Naive version x = 1 - x;
  381. // result = static_cast<RealType>(2) * asin(sqrt((x - x_min) / (x_max - x_min))) / pi<RealType>();
  382. // is less accurate, so use acos instead of asin for complement.
  383. result = static_cast<RealType>(2) * acos(sqrt((x - x_min) / (x_max - x_min))) / pi<RealType>();
  384. return result;
  385. } // arcsine ccdf
  386. template <class RealType, class Policy>
  387. inline RealType quantile(const arcsine_distribution<RealType, Policy>& dist, const RealType& p)
  388. {
  389. // Quantile or Percent Point arcsine function or
  390. // Inverse Cumulative probability distribution function CDF.
  391. // Return x (0 <= x <= 1),
  392. // for a given probability p (0 <= p <= 1).
  393. // These functions take a probability as an argument
  394. // and return a value such that the probability that a random variable x
  395. // will be less than or equal to that value
  396. // is whatever probability you supplied as an argument.
  397. BOOST_MATH_STD_USING // For ADL of std functions.
  398. using boost::math::constants::half_pi;
  399. static const char* function = "boost::math::quantile(arcsine_distribution<%1%> const&, %1%)";
  400. RealType result = 0; // of argument checks:
  401. RealType x_min = dist.x_min();
  402. RealType x_max = dist.x_max();
  403. if (false == arcsine_detail::check_dist_and_prob(
  404. function,
  405. x_min, x_max, p,
  406. &result, Policy()))
  407. {
  408. return result;
  409. }
  410. // Special cases:
  411. if (p == 0)
  412. {
  413. return 0;
  414. }
  415. if (p == 1)
  416. {
  417. return 1;
  418. }
  419. RealType sin2hpip = sin(half_pi<RealType>() * p);
  420. RealType sin2hpip2 = sin2hpip * sin2hpip;
  421. result = -x_min * sin2hpip2 + x_min + x_max * sin2hpip2;
  422. return result;
  423. } // quantile
  424. template <class RealType, class Policy>
  425. inline RealType quantile(const complemented2_type<arcsine_distribution<RealType, Policy>, RealType>& c)
  426. {
  427. // Complement Quantile or Percent Point arcsine function.
  428. // Return the number of expected x for a given
  429. // complement of the probability q.
  430. BOOST_MATH_STD_USING // For ADL of std functions.
  431. using boost::math::constants::half_pi;
  432. static const char* function = "boost::math::quantile(arcsine_distribution<%1%> const&, %1%)";
  433. // Error checks:
  434. RealType q = c.param;
  435. const arcsine_distribution<RealType, Policy>& dist = c.dist;
  436. RealType result = 0;
  437. RealType x_min = dist.x_min();
  438. RealType x_max = dist.x_max();
  439. if (false == arcsine_detail::check_dist_and_prob(
  440. function,
  441. x_min,
  442. x_max,
  443. q,
  444. &result, Policy()))
  445. {
  446. return result;
  447. }
  448. // Special cases:
  449. if (q == 1)
  450. {
  451. return 0;
  452. }
  453. if (q == 0)
  454. {
  455. return 1;
  456. }
  457. // Naive RealType p = 1 - q; result = sin(half_pi<RealType>() * p); loses accuracy, so use a cos alternative instead.
  458. //result = cos(half_pi<RealType>() * q); // for arcsine(0,1)
  459. //result = result * result;
  460. // For generalized arcsine:
  461. RealType cos2hpip = cos(half_pi<RealType>() * q);
  462. RealType cos2hpip2 = cos2hpip * cos2hpip;
  463. result = -x_min * cos2hpip2 + x_min + x_max * cos2hpip2;
  464. return result;
  465. } // Quantile Complement
  466. } // namespace math
  467. } // namespace boost
  468. // This include must be at the end, *after* the accessors
  469. // for this distribution have been defined, in order to
  470. // keep compilers that support two-phase lookup happy.
  471. #include <boost/math/distributions/detail/derived_accessors.hpp>
  472. #if defined (BOOST_MSVC)
  473. # pragma warning(pop)
  474. #endif
  475. #endif // BOOST_MATH_DIST_ARCSINE_HPP