non_central_beta.hpp 36 KB

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