arcsine.hpp 19 KB

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