non_central_beta.hpp 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972
  1. // boost\math\distributions\non_central_beta.hpp
  2. // Copyright John Maddock 2008.
  3. // Use, modification and distribution are subject to the
  4. // Boost Software License, Version 1.0.
  5. // (See accompanying file LICENSE_1_0.txt
  6. // or copy at http://www.boost.org/LICENSE_1_0.txt)
  7. #ifndef BOOST_MATH_SPECIAL_NON_CENTRAL_BETA_HPP
  8. #define BOOST_MATH_SPECIAL_NON_CENTRAL_BETA_HPP
  9. #include <boost/math/distributions/fwd.hpp>
  10. #include <boost/math/special_functions/beta.hpp> // for incomplete gamma. gamma_q
  11. #include <boost/math/distributions/complement.hpp> // complements
  12. #include <boost/math/distributions/beta.hpp> // central distribution
  13. #include <boost/math/distributions/detail/generic_mode.hpp>
  14. #include <boost/math/distributions/detail/common_error_handling.hpp> // error checks
  15. #include <boost/math/special_functions/fpclassify.hpp> // isnan.
  16. #include <boost/math/special_functions/trunc.hpp>
  17. #include <boost/math/tools/roots.hpp> // for root finding.
  18. #include <boost/math/tools/series.hpp>
  19. namespace boost
  20. {
  21. namespace math
  22. {
  23. template <class RealType, class Policy>
  24. class non_central_beta_distribution;
  25. namespace detail{
  26. template <class T, class Policy>
  27. T non_central_beta_p(T a, T b, T lam, T x, T y, const Policy& pol, T init_val = 0)
  28. {
  29. BOOST_MATH_STD_USING
  30. using namespace boost::math;
  31. //
  32. // Variables come first:
  33. //
  34. std::uintmax_t max_iter = policies::get_max_series_iterations<Policy>();
  35. T errtol = boost::math::policies::get_epsilon<T, Policy>();
  36. T l2 = lam / 2;
  37. //
  38. // k is the starting point for iteration, and is the
  39. // maximum of the poisson weighting term,
  40. // note that unlike other similar code, we do not set
  41. // k to zero, when l2 is small, as forward iteration
  42. // is unstable:
  43. //
  44. long long k = lltrunc(l2);
  45. if(k == 0)
  46. k = 1;
  47. // Starting Poisson weight:
  48. T pois = gamma_p_derivative(T(k+1), l2, pol);
  49. if(pois == 0)
  50. return init_val;
  51. // recurance term:
  52. T xterm;
  53. // Starting beta term:
  54. T beta = x < y
  55. ? detail::ibeta_imp(T(a + k), b, x, pol, false, true, &xterm)
  56. : detail::ibeta_imp(b, T(a + k), y, pol, true, true, &xterm);
  57. while (fabs(beta * pois) < tools::min_value<T>())
  58. {
  59. if ((k == 0) || (pois == 0))
  60. return init_val;
  61. k /= 2;
  62. pois = gamma_p_derivative(T(k + 1), l2, pol);
  63. beta = x < y
  64. ? detail::ibeta_imp(T(a + k), b, x, pol, false, true, &xterm)
  65. : detail::ibeta_imp(b, T(a + k), y, pol, true, true, &xterm);
  66. }
  67. xterm *= y / (a + b + k - 1);
  68. T poisf(pois), betaf(beta), xtermf(xterm);
  69. T sum = init_val;
  70. if((beta == 0) && (xterm == 0))
  71. return init_val;
  72. //
  73. // Backwards recursion first, this is the stable
  74. // direction for recursion:
  75. //
  76. T last_term = 0;
  77. std::uintmax_t count = k;
  78. for(auto i = k; i >= 0; --i)
  79. {
  80. T term = beta * pois;
  81. sum += term;
  82. if(((fabs(term/sum) < errtol) && (fabs(last_term) >= fabs(term))) || (term == 0))
  83. {
  84. count = k - i;
  85. break;
  86. }
  87. pois *= i / l2;
  88. beta += xterm;
  89. if (a + b + i != 2)
  90. {
  91. xterm *= (a + i - 1) / (x * (a + b + i - 2));
  92. }
  93. last_term = term;
  94. }
  95. last_term = 0;
  96. for(auto i = k + 1; ; ++i)
  97. {
  98. poisf *= l2 / i;
  99. xtermf *= (x * (a + b + i - 2)) / (a + i - 1);
  100. betaf -= xtermf;
  101. T term = poisf * betaf;
  102. sum += term;
  103. if(((fabs(term/sum) < errtol) && (fabs(last_term) >= fabs(term))) || (term == 0))
  104. {
  105. break;
  106. }
  107. last_term = term;
  108. if(static_cast<std::uintmax_t>(count + i - k) > max_iter)
  109. {
  110. return policies::raise_evaluation_error("cdf(non_central_beta_distribution<%1%>, %1%)", "Series did not converge, closest value was %1%", sum, pol); // LCOV_EXCL_LINE
  111. }
  112. }
  113. return sum;
  114. }
  115. template <class T, class Policy>
  116. T non_central_beta_q(T a, T b, T lam, T x, T y, const Policy& pol, T init_val = 0)
  117. {
  118. BOOST_MATH_STD_USING
  119. using namespace boost::math;
  120. //
  121. // Variables come first:
  122. //
  123. std::uintmax_t max_iter = policies::get_max_series_iterations<Policy>();
  124. T errtol = boost::math::policies::get_epsilon<T, Policy>();
  125. T l2 = lam / 2;
  126. //
  127. // k is the starting point for iteration, and is the
  128. // maximum of the poisson weighting term:
  129. //
  130. long long k = lltrunc(l2);
  131. T pois;
  132. if(k <= 30)
  133. {
  134. //
  135. // Might as well start at 0 since we'll likely have this number of terms anyway:
  136. //
  137. if(a + b > 1)
  138. k = 0;
  139. else if(k == 0)
  140. k = 1;
  141. }
  142. if(k == 0)
  143. {
  144. // Starting Poisson weight:
  145. pois = exp(-l2);
  146. }
  147. else
  148. {
  149. // Starting Poisson weight:
  150. pois = gamma_p_derivative(T(k+1), l2, pol);
  151. }
  152. if(pois == 0)
  153. return init_val;
  154. // recurance term:
  155. T xterm;
  156. // Starting beta term:
  157. T beta = x < y
  158. ? detail::ibeta_imp(T(a + k), b, x, pol, true, true, &xterm)
  159. : detail::ibeta_imp(b, T(a + k), y, pol, false, true, &xterm);
  160. xterm *= y / (a + b + k - 1);
  161. T poisf(pois), betaf(beta), xtermf(xterm);
  162. T sum = init_val;
  163. if((beta == 0) && (xterm == 0))
  164. return init_val;
  165. //
  166. // Forwards recursion first, this is the stable
  167. // direction for recursion, and the location
  168. // of the bulk of the sum:
  169. //
  170. T last_term = 0;
  171. std::uintmax_t count = 0;
  172. for(auto i = k + 1; ; ++i)
  173. {
  174. poisf *= l2 / i;
  175. xtermf *= (x * (a + b + i - 2)) / (a + i - 1);
  176. betaf += xtermf;
  177. T term = poisf * betaf;
  178. sum += term;
  179. if((fabs(term/sum) < errtol) && (last_term >= term))
  180. {
  181. count = i - k;
  182. break;
  183. }
  184. if(static_cast<std::uintmax_t>(i - k) > max_iter)
  185. {
  186. return policies::raise_evaluation_error("cdf(non_central_beta_distribution<%1%>, %1%)", "Series did not converge, closest value was %1%", sum, pol); // LCOV_EXCL_LINE
  187. }
  188. last_term = term;
  189. }
  190. for(auto i = k; i >= 0; --i)
  191. {
  192. T term = beta * pois;
  193. sum += term;
  194. if(fabs(term/sum) < errtol)
  195. {
  196. break;
  197. }
  198. if(static_cast<std::uintmax_t>(count + k - i) > max_iter)
  199. {
  200. return policies::raise_evaluation_error("cdf(non_central_beta_distribution<%1%>, %1%)", "Series did not converge, closest value was %1%", sum, pol); // LCOV_EXCL_LINE
  201. }
  202. pois *= i / l2;
  203. beta -= xterm;
  204. if (a + b + i - 2 != 0)
  205. {
  206. xterm *= (a + i - 1) / (x * (a + b + i - 2));
  207. }
  208. }
  209. return sum;
  210. }
  211. template <class RealType, class Policy>
  212. inline RealType non_central_beta_cdf(RealType x, RealType y, RealType a, RealType b, RealType l, bool invert, const Policy&)
  213. {
  214. typedef typename policies::evaluation<RealType, Policy>::type value_type;
  215. typedef typename policies::normalise<
  216. Policy,
  217. policies::promote_float<false>,
  218. policies::promote_double<false>,
  219. policies::discrete_quantile<>,
  220. policies::assert_undefined<> >::type forwarding_policy;
  221. BOOST_MATH_STD_USING
  222. if(x == 0)
  223. return invert ? 1.0f : 0.0f;
  224. if(y == 0)
  225. return invert ? 0.0f : 1.0f;
  226. value_type result;
  227. value_type c = a + b + l / 2;
  228. value_type cross = 1 - (b / c) * (1 + l / (2 * c * c));
  229. if(l == 0)
  230. result = cdf(boost::math::beta_distribution<RealType, Policy>(a, b), x);
  231. else if(x > cross)
  232. {
  233. // Complement is the smaller of the two:
  234. result = detail::non_central_beta_q(
  235. static_cast<value_type>(a),
  236. static_cast<value_type>(b),
  237. static_cast<value_type>(l),
  238. static_cast<value_type>(x),
  239. static_cast<value_type>(y),
  240. forwarding_policy(),
  241. static_cast<value_type>(invert ? 0 : -1));
  242. invert = !invert;
  243. }
  244. else
  245. {
  246. result = detail::non_central_beta_p(
  247. static_cast<value_type>(a),
  248. static_cast<value_type>(b),
  249. static_cast<value_type>(l),
  250. static_cast<value_type>(x),
  251. static_cast<value_type>(y),
  252. forwarding_policy(),
  253. static_cast<value_type>(invert ? -1 : 0));
  254. }
  255. if(invert)
  256. result = -result;
  257. return policies::checked_narrowing_cast<RealType, forwarding_policy>(
  258. result,
  259. "boost::math::non_central_beta_cdf<%1%>(%1%, %1%, %1%)");
  260. }
  261. template <class T, class Policy>
  262. struct nc_beta_quantile_functor
  263. {
  264. nc_beta_quantile_functor(const non_central_beta_distribution<T,Policy>& d, T t, bool c)
  265. : dist(d), target(t), comp(c) {}
  266. T operator()(const T& x)
  267. {
  268. return comp ?
  269. T(target - cdf(complement(dist, x)))
  270. : T(cdf(dist, x) - target);
  271. }
  272. private:
  273. non_central_beta_distribution<T,Policy> dist;
  274. T target;
  275. bool comp;
  276. };
  277. //
  278. // This is more or less a copy of bracket_and_solve_root, but
  279. // modified to search only the interval [0,1] using similar
  280. // heuristics.
  281. //
  282. template <class F, class T, class Tol, class Policy>
  283. std::pair<T, T> bracket_and_solve_root_01(F f, const T& guess, T factor, bool rising, Tol tol, std::uintmax_t& max_iter, const Policy& pol)
  284. {
  285. BOOST_MATH_STD_USING
  286. static const char* function = "boost::math::tools::bracket_and_solve_root_01<%1%>";
  287. //
  288. // Set up initial brackets:
  289. //
  290. T a = guess;
  291. T b = a;
  292. T fa = f(a);
  293. T fb = fa;
  294. //
  295. // Set up invocation count:
  296. //
  297. std::uintmax_t count = max_iter - 1;
  298. if((fa < 0) == (guess < 0 ? !rising : rising))
  299. {
  300. //
  301. // Zero is to the right of b, so walk upwards
  302. // until we find it:
  303. //
  304. while((boost::math::sign)(fb) == (boost::math::sign)(fa))
  305. {
  306. if(count == 0)
  307. {
  308. b = policies::raise_evaluation_error(function, "Unable to bracket root, last nearest value was %1%", b, pol); // LCOV_EXCL_LINE
  309. return std::make_pair(a, b);
  310. }
  311. //
  312. // Heuristic: every 20 iterations we double the growth factor in case the
  313. // initial guess was *really* bad !
  314. //
  315. if((max_iter - count) % 20 == 0)
  316. factor *= 2;
  317. //
  318. // Now go ahead and move are guess by "factor",
  319. // we do this by reducing 1-guess by factor:
  320. //
  321. a = b;
  322. fa = fb;
  323. b = 1 - ((1 - b) / factor);
  324. fb = f(b);
  325. --count;
  326. BOOST_MATH_INSTRUMENT_CODE("a = " << a << " b = " << b << " fa = " << fa << " fb = " << fb << " count = " << count);
  327. }
  328. }
  329. else
  330. {
  331. //
  332. // Zero is to the left of a, so walk downwards
  333. // until we find it:
  334. //
  335. while((boost::math::sign)(fb) == (boost::math::sign)(fa))
  336. {
  337. if(fabs(a) < tools::min_value<T>())
  338. {
  339. // Escape route just in case the answer is zero!
  340. max_iter -= count;
  341. max_iter += 1;
  342. return a > 0 ? std::make_pair(T(0), T(a)) : std::make_pair(T(a), T(0));
  343. }
  344. if(count == 0)
  345. {
  346. a = policies::raise_evaluation_error(function, "Unable to bracket root, last nearest value was %1%", a, pol); // LCOV_EXCL_LINE
  347. return std::make_pair(a, b);
  348. }
  349. //
  350. // Heuristic: every 20 iterations we double the growth factor in case the
  351. // initial guess was *really* bad !
  352. //
  353. if((max_iter - count) % 20 == 0)
  354. factor *= 2;
  355. //
  356. // Now go ahead and move are guess by "factor":
  357. //
  358. b = a;
  359. fb = fa;
  360. a /= factor;
  361. fa = f(a);
  362. --count;
  363. BOOST_MATH_INSTRUMENT_CODE("a = " << a << " b = " << b << " fa = " << fa << " fb = " << fb << " count = " << count);
  364. }
  365. }
  366. max_iter -= count;
  367. max_iter += 1;
  368. std::pair<T, T> r = toms748_solve(
  369. f,
  370. (a < 0 ? b : a),
  371. (a < 0 ? a : b),
  372. (a < 0 ? fb : fa),
  373. (a < 0 ? fa : fb),
  374. tol,
  375. count,
  376. pol);
  377. max_iter += count;
  378. BOOST_MATH_INSTRUMENT_CODE("max_iter = " << max_iter << " count = " << count);
  379. return r;
  380. }
  381. template <class RealType, class Policy>
  382. RealType nc_beta_quantile(const non_central_beta_distribution<RealType, Policy>& dist, const RealType& p, bool comp)
  383. {
  384. static const char* function = "quantile(non_central_beta_distribution<%1%>, %1%)";
  385. typedef typename policies::evaluation<RealType, Policy>::type value_type;
  386. typedef typename policies::normalise<
  387. Policy,
  388. policies::promote_float<false>,
  389. policies::promote_double<false>,
  390. policies::discrete_quantile<>,
  391. policies::assert_undefined<> >::type forwarding_policy;
  392. value_type a = dist.alpha();
  393. value_type b = dist.beta();
  394. value_type l = dist.non_centrality();
  395. value_type r;
  396. if(!beta_detail::check_alpha(
  397. function,
  398. a, &r, Policy())
  399. ||
  400. !beta_detail::check_beta(
  401. function,
  402. b, &r, Policy())
  403. ||
  404. !detail::check_non_centrality(
  405. function,
  406. l,
  407. &r,
  408. Policy())
  409. ||
  410. !detail::check_probability(
  411. function,
  412. static_cast<value_type>(p),
  413. &r,
  414. Policy()))
  415. return static_cast<RealType>(r);
  416. //
  417. // Special cases first:
  418. //
  419. if(p == 0)
  420. return comp
  421. ? 1.0f
  422. : 0.0f;
  423. if(p == 1)
  424. return !comp
  425. ? 1.0f
  426. : 0.0f;
  427. value_type c = a + b + l / 2;
  428. value_type mean = 1 - (b / c) * (1 + l / (2 * c * c));
  429. /*
  430. //
  431. // Calculate a normal approximation to the quantile,
  432. // uses mean and variance approximations from:
  433. // Algorithm AS 310:
  434. // Computing the Non-Central Beta Distribution Function
  435. // R. Chattamvelli; R. Shanmugam
  436. // Applied Statistics, Vol. 46, No. 1. (1997), pp. 146-156.
  437. //
  438. // Unfortunately, when this is wrong it tends to be *very*
  439. // wrong, so it's disabled for now, even though it often
  440. // gets the initial guess quite close. Probably we could
  441. // do much better by factoring in the skewness if only
  442. // we could calculate it....
  443. //
  444. value_type delta = l / 2;
  445. value_type delta2 = delta * delta;
  446. value_type delta3 = delta * delta2;
  447. value_type delta4 = delta2 * delta2;
  448. value_type G = c * (c + 1) + delta;
  449. value_type alpha = a + b;
  450. value_type alpha2 = alpha * alpha;
  451. value_type eta = (2 * alpha + 1) * (2 * alpha + 1) + 1;
  452. value_type H = 3 * alpha2 + 5 * alpha + 2;
  453. value_type F = alpha2 * (alpha + 1) + H * delta
  454. + (2 * alpha + 4) * delta2 + delta3;
  455. value_type P = (3 * alpha + 1) * (9 * alpha + 17)
  456. + 2 * alpha * (3 * alpha + 2) * (3 * alpha + 4) + 15;
  457. value_type Q = 54 * alpha2 + 162 * alpha + 130;
  458. value_type R = 6 * (6 * alpha + 11);
  459. value_type D = delta
  460. * (H * H + 2 * P * delta + Q * delta2 + R * delta3 + 9 * delta4);
  461. value_type variance = (b / G)
  462. * (1 + delta * (l * l + 3 * l + eta) / (G * G))
  463. - (b * b / F) * (1 + D / (F * F));
  464. value_type sd = sqrt(variance);
  465. value_type guess = comp
  466. ? quantile(complement(normal_distribution<RealType, Policy>(static_cast<RealType>(mean), static_cast<RealType>(sd)), p))
  467. : quantile(normal_distribution<RealType, Policy>(static_cast<RealType>(mean), static_cast<RealType>(sd)), p);
  468. if(guess >= 1)
  469. guess = mean;
  470. if(guess <= tools::min_value<value_type>())
  471. guess = mean;
  472. */
  473. value_type guess = mean;
  474. detail::nc_beta_quantile_functor<value_type, Policy>
  475. f(non_central_beta_distribution<value_type, Policy>(a, b, l), p, comp);
  476. tools::eps_tolerance<value_type> tol(policies::digits<RealType, Policy>());
  477. std::uintmax_t max_iter = policies::get_max_root_iterations<Policy>();
  478. std::pair<value_type, value_type> ir
  479. = bracket_and_solve_root_01(
  480. f, guess, value_type(2.5), true, tol,
  481. max_iter, Policy());
  482. value_type result = ir.first + (ir.second - ir.first) / 2;
  483. if(max_iter >= policies::get_max_root_iterations<Policy>())
  484. {
  485. // LCOV_EXCL_START
  486. return policies::raise_evaluation_error<RealType>(function, "Unable to locate solution in a reasonable time:"
  487. " either there is no answer to quantile of the non central beta distribution"
  488. " or the answer is infinite. Current best guess is %1%",
  489. policies::checked_narrowing_cast<RealType, forwarding_policy>(
  490. result,
  491. function), Policy());
  492. // LCOV_EXCL_STOP
  493. }
  494. return policies::checked_narrowing_cast<RealType, forwarding_policy>(
  495. result,
  496. function);
  497. }
  498. template <class T, class Policy>
  499. T non_central_beta_pdf(T a, T b, T lam, T x, T y, const Policy& pol)
  500. {
  501. BOOST_MATH_STD_USING
  502. //
  503. // Special cases:
  504. //
  505. if((x == 0) || (y == 0))
  506. return 0;
  507. //
  508. // Variables come first:
  509. //
  510. std::uintmax_t max_iter = policies::get_max_series_iterations<Policy>();
  511. T errtol = boost::math::policies::get_epsilon<T, Policy>();
  512. T l2 = lam / 2;
  513. //
  514. // k is the starting point for iteration, and is the
  515. // maximum of the poisson weighting term:
  516. //
  517. long long k = lltrunc(l2);
  518. // Starting Poisson weight:
  519. T pois = gamma_p_derivative(T(k+1), l2, pol);
  520. // Starting beta term:
  521. T beta = x < y ?
  522. ibeta_derivative(a + k, b, x, pol)
  523. : ibeta_derivative(b, a + k, y, pol);
  524. while (fabs(beta * pois) < tools::min_value<T>())
  525. {
  526. if ((k == 0) || (pois == 0))
  527. return 0; // Nothing else we can do!
  528. //
  529. // We only get here when a+k and b are large and x is small,
  530. // in that case reduce k (bisect) until both terms are finite:
  531. //
  532. k /= 2;
  533. pois = gamma_p_derivative(T(k + 1), l2, pol);
  534. // Starting beta term:
  535. beta = x < y ?
  536. ibeta_derivative(a + k, b, x, pol)
  537. : ibeta_derivative(b, a + k, y, pol);
  538. }
  539. T sum = 0;
  540. T poisf(pois);
  541. T betaf(beta);
  542. //
  543. // Stable backwards recursion first:
  544. //
  545. std::uintmax_t count = k;
  546. T ratio = 0;
  547. T old_ratio = 0;
  548. for(auto i = k; i >= 0; --i)
  549. {
  550. T term = beta * pois;
  551. sum += term;
  552. ratio = fabs(term / sum);
  553. if(((ratio < errtol) && (ratio < old_ratio)) || (term == 0))
  554. {
  555. count = k - i;
  556. break;
  557. }
  558. ratio = old_ratio;
  559. pois *= i / l2;
  560. if (a + b + i != 1)
  561. {
  562. beta *= (a + i - 1) / (x * (a + i + b - 1));
  563. }
  564. }
  565. old_ratio = 0;
  566. for(auto i = k + 1; ; ++i)
  567. {
  568. poisf *= l2 / i;
  569. betaf *= x * (a + b + i - 1) / (a + i - 1);
  570. T term = poisf * betaf;
  571. sum += term;
  572. ratio = fabs(term / sum);
  573. if(((ratio < errtol) && (ratio < old_ratio)) || (term == 0))
  574. {
  575. break;
  576. }
  577. old_ratio = ratio;
  578. if(static_cast<std::uintmax_t>(count + i - k) > max_iter)
  579. {
  580. return policies::raise_evaluation_error("pdf(non_central_beta_distribution<%1%>, %1%)", "Series did not converge, closest value was %1%", sum, pol); // LCOV_EXCL_LINE
  581. }
  582. }
  583. return sum;
  584. }
  585. template <class RealType, class Policy>
  586. RealType nc_beta_pdf(const non_central_beta_distribution<RealType, Policy>& dist, const RealType& x)
  587. {
  588. BOOST_MATH_STD_USING
  589. static const char* function = "pdf(non_central_beta_distribution<%1%>, %1%)";
  590. typedef typename policies::evaluation<RealType, Policy>::type value_type;
  591. typedef typename policies::normalise<
  592. Policy,
  593. policies::promote_float<false>,
  594. policies::promote_double<false>,
  595. policies::discrete_quantile<>,
  596. policies::assert_undefined<> >::type forwarding_policy;
  597. value_type a = dist.alpha();
  598. value_type b = dist.beta();
  599. value_type l = dist.non_centrality();
  600. value_type r;
  601. if(!beta_detail::check_alpha(
  602. function,
  603. a, &r, Policy())
  604. ||
  605. !beta_detail::check_beta(
  606. function,
  607. b, &r, Policy())
  608. ||
  609. !detail::check_non_centrality(
  610. function,
  611. l,
  612. &r,
  613. Policy())
  614. ||
  615. !beta_detail::check_x(
  616. function,
  617. static_cast<value_type>(x),
  618. &r,
  619. Policy()))
  620. return static_cast<RealType>(r);
  621. if(l == 0)
  622. return pdf(boost::math::beta_distribution<RealType, Policy>(dist.alpha(), dist.beta()), x);
  623. return policies::checked_narrowing_cast<RealType, forwarding_policy>(
  624. non_central_beta_pdf(a, b, l, static_cast<value_type>(x), value_type(1 - static_cast<value_type>(x)), forwarding_policy()),
  625. "function");
  626. }
  627. template <class T>
  628. struct hypergeometric_2F2_sum
  629. {
  630. typedef T result_type;
  631. hypergeometric_2F2_sum(T a1_, T a2_, T b1_, T b2_, T z_) : a1(a1_), a2(a2_), b1(b1_), b2(b2_), z(z_), term(1), k(0) {}
  632. T operator()()
  633. {
  634. T result = term;
  635. term *= a1 * a2 / (b1 * b2);
  636. a1 += 1;
  637. a2 += 1;
  638. b1 += 1;
  639. b2 += 1;
  640. k += 1;
  641. term /= k;
  642. term *= z;
  643. return result;
  644. }
  645. T a1, a2, b1, b2, z, term, k;
  646. };
  647. template <class T, class Policy>
  648. T hypergeometric_2F2(T a1, T a2, T b1, T b2, T z, const Policy& pol)
  649. {
  650. typedef typename policies::evaluation<T, Policy>::type value_type;
  651. const char* function = "boost::math::detail::hypergeometric_2F2<%1%>(%1%,%1%,%1%,%1%,%1%)";
  652. hypergeometric_2F2_sum<value_type> s(a1, a2, b1, b2, z);
  653. std::uintmax_t max_iter = policies::get_max_series_iterations<Policy>();
  654. value_type result = boost::math::tools::sum_series(s, boost::math::policies::get_epsilon<value_type, Policy>(), max_iter);
  655. policies::check_series_iterations<T>(function, max_iter, pol);
  656. return policies::checked_narrowing_cast<T, Policy>(result, function);
  657. }
  658. } // namespace detail
  659. template <class RealType = double, class Policy = policies::policy<> >
  660. class non_central_beta_distribution
  661. {
  662. public:
  663. typedef RealType value_type;
  664. typedef Policy policy_type;
  665. non_central_beta_distribution(RealType a_, RealType b_, RealType lambda) : a(a_), b(b_), ncp(lambda)
  666. {
  667. const char* function = "boost::math::non_central_beta_distribution<%1%>::non_central_beta_distribution(%1%,%1%)";
  668. RealType r;
  669. beta_detail::check_alpha(
  670. function,
  671. a, &r, Policy());
  672. beta_detail::check_beta(
  673. function,
  674. b, &r, Policy());
  675. detail::check_non_centrality(
  676. function,
  677. lambda,
  678. &r,
  679. Policy());
  680. } // non_central_beta_distribution constructor.
  681. RealType alpha() const
  682. { // Private data getter function.
  683. return a;
  684. }
  685. RealType beta() const
  686. { // Private data getter function.
  687. return b;
  688. }
  689. RealType non_centrality() const
  690. { // Private data getter function.
  691. return ncp;
  692. }
  693. private:
  694. // Data member, initialized by constructor.
  695. RealType a; // alpha.
  696. RealType b; // beta.
  697. RealType ncp; // non-centrality parameter
  698. }; // template <class RealType, class Policy> class non_central_beta_distribution
  699. typedef non_central_beta_distribution<double> non_central_beta; // Reserved name of type double.
  700. #ifdef __cpp_deduction_guides
  701. template <class RealType>
  702. non_central_beta_distribution(RealType,RealType,RealType)->non_central_beta_distribution<typename boost::math::tools::promote_args<RealType>::type>;
  703. #endif
  704. // Non-member functions to give properties of the distribution.
  705. template <class RealType, class Policy>
  706. inline const std::pair<RealType, RealType> range(const non_central_beta_distribution<RealType, Policy>& /* dist */)
  707. { // Range of permissible values for random variable k.
  708. using boost::math::tools::max_value;
  709. return std::pair<RealType, RealType>(static_cast<RealType>(0), static_cast<RealType>(1));
  710. }
  711. template <class RealType, class Policy>
  712. inline const std::pair<RealType, RealType> support(const non_central_beta_distribution<RealType, Policy>& /* dist */)
  713. { // Range of supported values for random variable k.
  714. // This is range where cdf rises from 0 to 1, and outside it, the pdf is zero.
  715. using boost::math::tools::max_value;
  716. return std::pair<RealType, RealType>(static_cast<RealType>(0), static_cast<RealType>(1));
  717. }
  718. template <class RealType, class Policy>
  719. inline RealType mode(const non_central_beta_distribution<RealType, Policy>& dist)
  720. { // mode.
  721. static const char* function = "mode(non_central_beta_distribution<%1%> const&)";
  722. RealType a = dist.alpha();
  723. RealType b = dist.beta();
  724. RealType l = dist.non_centrality();
  725. RealType r;
  726. if(!beta_detail::check_alpha(
  727. function,
  728. a, &r, Policy())
  729. ||
  730. !beta_detail::check_beta(
  731. function,
  732. b, &r, Policy())
  733. ||
  734. !detail::check_non_centrality(
  735. function,
  736. l,
  737. &r,
  738. Policy()))
  739. return static_cast<RealType>(r);
  740. RealType c = a + b + l / 2;
  741. RealType mean = 1 - (b / c) * (1 + l / (2 * c * c));
  742. return detail::generic_find_mode_01(
  743. dist,
  744. mean,
  745. function);
  746. }
  747. //
  748. // We don't have the necessary information to implement
  749. // these at present. These are just disabled for now,
  750. // prototypes retained so we can fill in the blanks
  751. // later:
  752. //
  753. template <class RealType, class Policy>
  754. inline RealType mean(const non_central_beta_distribution<RealType, Policy>& dist)
  755. {
  756. BOOST_MATH_STD_USING
  757. RealType a = dist.alpha();
  758. RealType b = dist.beta();
  759. RealType d = dist.non_centrality();
  760. RealType apb = a + b;
  761. return exp(-d / 2) * a * detail::hypergeometric_2F2<RealType, Policy>(1 + a, apb, a, 1 + apb, d / 2, Policy()) / apb;
  762. } // mean
  763. template <class RealType, class Policy>
  764. inline RealType variance(const non_central_beta_distribution<RealType, Policy>& dist)
  765. {
  766. //
  767. // Relative error of this function may be arbitrarily large... absolute
  768. // error will be small however... that's the best we can do for now.
  769. //
  770. BOOST_MATH_STD_USING
  771. RealType a = dist.alpha();
  772. RealType b = dist.beta();
  773. RealType d = dist.non_centrality();
  774. RealType apb = a + b;
  775. RealType result = detail::hypergeometric_2F2(RealType(1 + a), apb, a, RealType(1 + apb), RealType(d / 2), Policy());
  776. result *= result * -exp(-d) * a * a / (apb * apb);
  777. result += exp(-d / 2) * a * (1 + a) * detail::hypergeometric_2F2(RealType(2 + a), apb, a, RealType(2 + apb), RealType(d / 2), Policy()) / (apb * (1 + apb));
  778. return result;
  779. }
  780. // RealType standard_deviation(const non_central_beta_distribution<RealType, Policy>& dist)
  781. // standard_deviation provided by derived accessors.
  782. template <class RealType, class Policy>
  783. inline RealType skewness(const non_central_beta_distribution<RealType, Policy>& /*dist*/)
  784. { // skewness = sqrt(l).
  785. const char* function = "boost::math::non_central_beta_distribution<%1%>::skewness()";
  786. typedef typename Policy::assert_undefined_type assert_type;
  787. static_assert(assert_type::value == 0, "Assert type is undefined.");
  788. return policies::raise_evaluation_error<RealType>(function, "This function is not yet implemented, the only sensible result is %1%.", // LCOV_EXCL_LINE
  789. std::numeric_limits<RealType>::quiet_NaN(), Policy()); // infinity? LCOV_EXCL_LINE
  790. }
  791. template <class RealType, class Policy>
  792. inline RealType kurtosis_excess(const non_central_beta_distribution<RealType, Policy>& /*dist*/)
  793. {
  794. const char* function = "boost::math::non_central_beta_distribution<%1%>::kurtosis_excess()";
  795. typedef typename Policy::assert_undefined_type assert_type;
  796. static_assert(assert_type::value == 0, "Assert type is undefined.");
  797. return policies::raise_evaluation_error<RealType>(function, "This function is not yet implemented, the only sensible result is %1%.", // LCOV_EXCL_LINE
  798. std::numeric_limits<RealType>::quiet_NaN(), Policy()); // infinity? LCOV_EXCL_LINE
  799. } // kurtosis_excess
  800. template <class RealType, class Policy>
  801. inline RealType kurtosis(const non_central_beta_distribution<RealType, Policy>& dist)
  802. {
  803. return kurtosis_excess(dist) + 3;
  804. }
  805. template <class RealType, class Policy>
  806. inline RealType pdf(const non_central_beta_distribution<RealType, Policy>& dist, const RealType& x)
  807. { // Probability Density/Mass Function.
  808. return detail::nc_beta_pdf(dist, x);
  809. } // pdf
  810. template <class RealType, class Policy>
  811. RealType cdf(const non_central_beta_distribution<RealType, Policy>& dist, const RealType& x)
  812. {
  813. const char* function = "boost::math::non_central_beta_distribution<%1%>::cdf(%1%)";
  814. RealType a = dist.alpha();
  815. RealType b = dist.beta();
  816. RealType l = dist.non_centrality();
  817. RealType r;
  818. if(!beta_detail::check_alpha(
  819. function,
  820. a, &r, Policy())
  821. ||
  822. !beta_detail::check_beta(
  823. function,
  824. b, &r, Policy())
  825. ||
  826. !detail::check_non_centrality(
  827. function,
  828. l,
  829. &r,
  830. Policy())
  831. ||
  832. !beta_detail::check_x(
  833. function,
  834. x,
  835. &r,
  836. Policy()))
  837. return static_cast<RealType>(r);
  838. if(l == 0)
  839. return cdf(beta_distribution<RealType, Policy>(a, b), x);
  840. return detail::non_central_beta_cdf(x, RealType(1 - x), a, b, l, false, Policy());
  841. } // cdf
  842. template <class RealType, class Policy>
  843. RealType cdf(const complemented2_type<non_central_beta_distribution<RealType, Policy>, RealType>& c)
  844. { // Complemented Cumulative Distribution Function
  845. const char* function = "boost::math::non_central_beta_distribution<%1%>::cdf(%1%)";
  846. non_central_beta_distribution<RealType, Policy> const& dist = c.dist;
  847. RealType a = dist.alpha();
  848. RealType b = dist.beta();
  849. RealType l = dist.non_centrality();
  850. RealType x = c.param;
  851. RealType r;
  852. if(!beta_detail::check_alpha(
  853. function,
  854. a, &r, Policy())
  855. ||
  856. !beta_detail::check_beta(
  857. function,
  858. b, &r, Policy())
  859. ||
  860. !detail::check_non_centrality(
  861. function,
  862. l,
  863. &r,
  864. Policy())
  865. ||
  866. !beta_detail::check_x(
  867. function,
  868. x,
  869. &r,
  870. Policy()))
  871. return static_cast<RealType>(r);
  872. if(l == 0)
  873. return cdf(complement(beta_distribution<RealType, Policy>(a, b), x));
  874. return detail::non_central_beta_cdf(x, RealType(1 - x), a, b, l, true, Policy());
  875. } // ccdf
  876. template <class RealType, class Policy>
  877. inline RealType quantile(const non_central_beta_distribution<RealType, Policy>& dist, const RealType& p)
  878. { // Quantile (or Percent Point) function.
  879. return detail::nc_beta_quantile(dist, p, false);
  880. } // quantile
  881. template <class RealType, class Policy>
  882. inline RealType quantile(const complemented2_type<non_central_beta_distribution<RealType, Policy>, RealType>& c)
  883. { // Quantile (or Percent Point) function.
  884. return detail::nc_beta_quantile(c.dist, c.param, true);
  885. } // quantile complement.
  886. } // namespace math
  887. } // namespace boost
  888. // This include must be at the end, *after* the accessors
  889. // for this distribution have been defined, in order to
  890. // keep compilers that support two-phase lookup happy.
  891. #include <boost/math/distributions/detail/derived_accessors.hpp>
  892. #endif // BOOST_MATH_SPECIAL_NON_CENTRAL_BETA_HPP