non_central_beta.hpp 35 KB

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