non_central_beta.hpp 35 KB

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