non_central_beta.hpp 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929
  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. //
  488. // Special cases:
  489. //
  490. if((x == 0) || (y == 0))
  491. return 0;
  492. //
  493. // Variables come first:
  494. //
  495. boost::uintmax_t max_iter = policies::get_max_series_iterations<Policy>();
  496. T errtol = boost::math::policies::get_epsilon<T, Policy>();
  497. T l2 = lam / 2;
  498. //
  499. // k is the starting point for iteration, and is the
  500. // maximum of the poisson weighting term:
  501. //
  502. int k = itrunc(l2);
  503. // Starting Poisson weight:
  504. T pois = gamma_p_derivative(T(k+1), l2, pol);
  505. // Starting beta term:
  506. T beta = x < y ?
  507. ibeta_derivative(a + k, b, x, pol)
  508. : ibeta_derivative(b, a + k, y, pol);
  509. T sum = 0;
  510. T poisf(pois);
  511. T betaf(beta);
  512. //
  513. // Stable backwards recursion first:
  514. //
  515. boost::uintmax_t count = k;
  516. for(int i = k; i >= 0; --i)
  517. {
  518. T term = beta * pois;
  519. sum += term;
  520. if((fabs(term/sum) < errtol) || (term == 0))
  521. {
  522. count = k - i;
  523. break;
  524. }
  525. pois *= i / l2;
  526. beta *= (a + i - 1) / (x * (a + i + b - 1));
  527. }
  528. for(int i = k + 1; ; ++i)
  529. {
  530. poisf *= l2 / i;
  531. betaf *= x * (a + b + i - 1) / (a + i - 1);
  532. T term = poisf * betaf;
  533. sum += term;
  534. if((fabs(term/sum) < errtol) || (term == 0))
  535. {
  536. break;
  537. }
  538. if(static_cast<boost::uintmax_t>(count + i - k) > max_iter)
  539. {
  540. return policies::raise_evaluation_error(
  541. "pdf(non_central_beta_distribution<%1%>, %1%)",
  542. "Series did not converge, closest value was %1%", sum, pol);
  543. }
  544. }
  545. return sum;
  546. }
  547. template <class RealType, class Policy>
  548. RealType nc_beta_pdf(const non_central_beta_distribution<RealType, Policy>& dist, const RealType& x)
  549. {
  550. BOOST_MATH_STD_USING
  551. static const char* function = "pdf(non_central_beta_distribution<%1%>, %1%)";
  552. typedef typename policies::evaluation<RealType, Policy>::type value_type;
  553. typedef typename policies::normalise<
  554. Policy,
  555. policies::promote_float<false>,
  556. policies::promote_double<false>,
  557. policies::discrete_quantile<>,
  558. policies::assert_undefined<> >::type forwarding_policy;
  559. value_type a = dist.alpha();
  560. value_type b = dist.beta();
  561. value_type l = dist.non_centrality();
  562. value_type r;
  563. if(!beta_detail::check_alpha(
  564. function,
  565. a, &r, Policy())
  566. ||
  567. !beta_detail::check_beta(
  568. function,
  569. b, &r, Policy())
  570. ||
  571. !detail::check_non_centrality(
  572. function,
  573. l,
  574. &r,
  575. Policy())
  576. ||
  577. !beta_detail::check_x(
  578. function,
  579. static_cast<value_type>(x),
  580. &r,
  581. Policy()))
  582. return (RealType)r;
  583. if(l == 0)
  584. return pdf(boost::math::beta_distribution<RealType, Policy>(dist.alpha(), dist.beta()), x);
  585. return policies::checked_narrowing_cast<RealType, forwarding_policy>(
  586. non_central_beta_pdf(a, b, l, static_cast<value_type>(x), value_type(1 - static_cast<value_type>(x)), forwarding_policy()),
  587. "function");
  588. }
  589. template <class T>
  590. struct hypergeometric_2F2_sum
  591. {
  592. typedef T result_type;
  593. 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) {}
  594. T operator()()
  595. {
  596. T result = term;
  597. term *= a1 * a2 / (b1 * b2);
  598. a1 += 1;
  599. a2 += 1;
  600. b1 += 1;
  601. b2 += 1;
  602. k += 1;
  603. term /= k;
  604. term *= z;
  605. return result;
  606. }
  607. T a1, a2, b1, b2, z, term, k;
  608. };
  609. template <class T, class Policy>
  610. T hypergeometric_2F2(T a1, T a2, T b1, T b2, T z, const Policy& pol)
  611. {
  612. typedef typename policies::evaluation<T, Policy>::type value_type;
  613. const char* function = "boost::math::detail::hypergeometric_2F2<%1%>(%1%,%1%,%1%,%1%,%1%)";
  614. hypergeometric_2F2_sum<value_type> s(a1, a2, b1, b2, z);
  615. boost::uintmax_t max_iter = policies::get_max_series_iterations<Policy>();
  616. #if BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x582))
  617. value_type zero = 0;
  618. value_type result = boost::math::tools::sum_series(s, boost::math::policies::get_epsilon<value_type, Policy>(), max_iter, zero);
  619. #else
  620. value_type result = boost::math::tools::sum_series(s, boost::math::policies::get_epsilon<value_type, Policy>(), max_iter);
  621. #endif
  622. policies::check_series_iterations<T>(function, max_iter, pol);
  623. return policies::checked_narrowing_cast<T, Policy>(result, function);
  624. }
  625. } // namespace detail
  626. template <class RealType = double, class Policy = policies::policy<> >
  627. class non_central_beta_distribution
  628. {
  629. public:
  630. typedef RealType value_type;
  631. typedef Policy policy_type;
  632. non_central_beta_distribution(RealType a_, RealType b_, RealType lambda) : a(a_), b(b_), ncp(lambda)
  633. {
  634. const char* function = "boost::math::non_central_beta_distribution<%1%>::non_central_beta_distribution(%1%,%1%)";
  635. RealType r;
  636. beta_detail::check_alpha(
  637. function,
  638. a, &r, Policy());
  639. beta_detail::check_beta(
  640. function,
  641. b, &r, Policy());
  642. detail::check_non_centrality(
  643. function,
  644. lambda,
  645. &r,
  646. Policy());
  647. } // non_central_beta_distribution constructor.
  648. RealType alpha() const
  649. { // Private data getter function.
  650. return a;
  651. }
  652. RealType beta() const
  653. { // Private data getter function.
  654. return b;
  655. }
  656. RealType non_centrality() const
  657. { // Private data getter function.
  658. return ncp;
  659. }
  660. private:
  661. // Data member, initialized by constructor.
  662. RealType a; // alpha.
  663. RealType b; // beta.
  664. RealType ncp; // non-centrality parameter
  665. }; // template <class RealType, class Policy> class non_central_beta_distribution
  666. typedef non_central_beta_distribution<double> non_central_beta; // Reserved name of type double.
  667. // Non-member functions to give properties of the distribution.
  668. template <class RealType, class Policy>
  669. inline const std::pair<RealType, RealType> range(const non_central_beta_distribution<RealType, Policy>& /* dist */)
  670. { // Range of permissible values for random variable k.
  671. using boost::math::tools::max_value;
  672. return std::pair<RealType, RealType>(static_cast<RealType>(0), static_cast<RealType>(1));
  673. }
  674. template <class RealType, class Policy>
  675. inline const std::pair<RealType, RealType> support(const non_central_beta_distribution<RealType, Policy>& /* dist */)
  676. { // Range of supported values for random variable k.
  677. // This is range where cdf rises from 0 to 1, and outside it, the pdf is zero.
  678. using boost::math::tools::max_value;
  679. return std::pair<RealType, RealType>(static_cast<RealType>(0), static_cast<RealType>(1));
  680. }
  681. template <class RealType, class Policy>
  682. inline RealType mode(const non_central_beta_distribution<RealType, Policy>& dist)
  683. { // mode.
  684. static const char* function = "mode(non_central_beta_distribution<%1%> const&)";
  685. RealType a = dist.alpha();
  686. RealType b = dist.beta();
  687. RealType l = dist.non_centrality();
  688. RealType r;
  689. if(!beta_detail::check_alpha(
  690. function,
  691. a, &r, Policy())
  692. ||
  693. !beta_detail::check_beta(
  694. function,
  695. b, &r, Policy())
  696. ||
  697. !detail::check_non_centrality(
  698. function,
  699. l,
  700. &r,
  701. Policy()))
  702. return (RealType)r;
  703. RealType c = a + b + l / 2;
  704. RealType mean = 1 - (b / c) * (1 + l / (2 * c * c));
  705. return detail::generic_find_mode_01(
  706. dist,
  707. mean,
  708. function);
  709. }
  710. //
  711. // We don't have the necessary information to implement
  712. // these at present. These are just disabled for now,
  713. // prototypes retained so we can fill in the blanks
  714. // later:
  715. //
  716. template <class RealType, class Policy>
  717. inline RealType mean(const non_central_beta_distribution<RealType, Policy>& dist)
  718. {
  719. BOOST_MATH_STD_USING
  720. RealType a = dist.alpha();
  721. RealType b = dist.beta();
  722. RealType d = dist.non_centrality();
  723. RealType apb = a + b;
  724. return exp(-d / 2) * a * detail::hypergeometric_2F2<RealType, Policy>(1 + a, apb, a, 1 + apb, d / 2, Policy()) / apb;
  725. } // mean
  726. template <class RealType, class Policy>
  727. inline RealType variance(const non_central_beta_distribution<RealType, Policy>& dist)
  728. {
  729. //
  730. // Relative error of this function may be arbitarily large... absolute
  731. // error will be small however... that's the best we can do for now.
  732. //
  733. BOOST_MATH_STD_USING
  734. RealType a = dist.alpha();
  735. RealType b = dist.beta();
  736. RealType d = dist.non_centrality();
  737. RealType apb = a + b;
  738. RealType result = detail::hypergeometric_2F2(RealType(1 + a), apb, a, RealType(1 + apb), RealType(d / 2), Policy());
  739. result *= result * -exp(-d) * a * a / (apb * apb);
  740. 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));
  741. return result;
  742. }
  743. // RealType standard_deviation(const non_central_beta_distribution<RealType, Policy>& dist)
  744. // standard_deviation provided by derived accessors.
  745. template <class RealType, class Policy>
  746. inline RealType skewness(const non_central_beta_distribution<RealType, Policy>& /*dist*/)
  747. { // skewness = sqrt(l).
  748. const char* function = "boost::math::non_central_beta_distribution<%1%>::skewness()";
  749. typedef typename Policy::assert_undefined_type assert_type;
  750. BOOST_STATIC_ASSERT(assert_type::value == 0);
  751. return policies::raise_evaluation_error<RealType>(
  752. function,
  753. "This function is not yet implemented, the only sensible result is %1%.",
  754. std::numeric_limits<RealType>::quiet_NaN(), Policy()); // infinity?
  755. }
  756. template <class RealType, class Policy>
  757. inline RealType kurtosis_excess(const non_central_beta_distribution<RealType, Policy>& /*dist*/)
  758. {
  759. const char* function = "boost::math::non_central_beta_distribution<%1%>::kurtosis_excess()";
  760. typedef typename Policy::assert_undefined_type assert_type;
  761. BOOST_STATIC_ASSERT(assert_type::value == 0);
  762. return policies::raise_evaluation_error<RealType>(
  763. function,
  764. "This function is not yet implemented, the only sensible result is %1%.",
  765. std::numeric_limits<RealType>::quiet_NaN(), Policy()); // infinity?
  766. } // kurtosis_excess
  767. template <class RealType, class Policy>
  768. inline RealType kurtosis(const non_central_beta_distribution<RealType, Policy>& dist)
  769. {
  770. return kurtosis_excess(dist) + 3;
  771. }
  772. template <class RealType, class Policy>
  773. inline RealType pdf(const non_central_beta_distribution<RealType, Policy>& dist, const RealType& x)
  774. { // Probability Density/Mass Function.
  775. return detail::nc_beta_pdf(dist, x);
  776. } // pdf
  777. template <class RealType, class Policy>
  778. RealType cdf(const non_central_beta_distribution<RealType, Policy>& dist, const RealType& x)
  779. {
  780. const char* function = "boost::math::non_central_beta_distribution<%1%>::cdf(%1%)";
  781. RealType a = dist.alpha();
  782. RealType b = dist.beta();
  783. RealType l = dist.non_centrality();
  784. RealType r;
  785. if(!beta_detail::check_alpha(
  786. function,
  787. a, &r, Policy())
  788. ||
  789. !beta_detail::check_beta(
  790. function,
  791. b, &r, Policy())
  792. ||
  793. !detail::check_non_centrality(
  794. function,
  795. l,
  796. &r,
  797. Policy())
  798. ||
  799. !beta_detail::check_x(
  800. function,
  801. x,
  802. &r,
  803. Policy()))
  804. return (RealType)r;
  805. if(l == 0)
  806. return cdf(beta_distribution<RealType, Policy>(a, b), x);
  807. return detail::non_central_beta_cdf(x, RealType(1 - x), a, b, l, false, Policy());
  808. } // cdf
  809. template <class RealType, class Policy>
  810. RealType cdf(const complemented2_type<non_central_beta_distribution<RealType, Policy>, RealType>& c)
  811. { // Complemented Cumulative Distribution Function
  812. const char* function = "boost::math::non_central_beta_distribution<%1%>::cdf(%1%)";
  813. non_central_beta_distribution<RealType, Policy> const& dist = c.dist;
  814. RealType a = dist.alpha();
  815. RealType b = dist.beta();
  816. RealType l = dist.non_centrality();
  817. RealType x = c.param;
  818. RealType r;
  819. if(!beta_detail::check_alpha(
  820. function,
  821. a, &r, Policy())
  822. ||
  823. !beta_detail::check_beta(
  824. function,
  825. b, &r, Policy())
  826. ||
  827. !detail::check_non_centrality(
  828. function,
  829. l,
  830. &r,
  831. Policy())
  832. ||
  833. !beta_detail::check_x(
  834. function,
  835. x,
  836. &r,
  837. Policy()))
  838. return (RealType)r;
  839. if(l == 0)
  840. return cdf(complement(beta_distribution<RealType, Policy>(a, b), x));
  841. return detail::non_central_beta_cdf(x, RealType(1 - x), a, b, l, true, Policy());
  842. } // ccdf
  843. template <class RealType, class Policy>
  844. inline RealType quantile(const non_central_beta_distribution<RealType, Policy>& dist, const RealType& p)
  845. { // Quantile (or Percent Point) function.
  846. return detail::nc_beta_quantile(dist, p, false);
  847. } // quantile
  848. template <class RealType, class Policy>
  849. inline RealType quantile(const complemented2_type<non_central_beta_distribution<RealType, Policy>, RealType>& c)
  850. { // Quantile (or Percent Point) function.
  851. return detail::nc_beta_quantile(c.dist, c.param, true);
  852. } // quantile complement.
  853. } // namespace math
  854. } // namespace boost
  855. // This include must be at the end, *after* the accessors
  856. // for this distribution have been defined, in order to
  857. // keep compilers that support two-phase lookup happy.
  858. #include <boost/math/distributions/detail/derived_accessors.hpp>
  859. #endif // BOOST_MATH_SPECIAL_NON_CENTRAL_BETA_HPP