Guest User

Untitled

a guest
Apr 24th, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.78 KB | None | 0 0
  1. double f = 1.0 - boost::math::ibetac(_r, _k + 1, _p);
  2.  
  3. NegativeBinomial dist(r, p);
  4. double prob = dist.cdf(k);
  5.  
  6. struct NegativeBinomialSystem
  7. {
  8. NegativeBinomialSystem(int k, double r, double p) : _k(k), _r(r), _p(p) { }
  9.  
  10. // Return both f(p) and dot(f)(p).
  11. std::pair<double, double> operator()(double const& mu)
  12. {
  13. double f = 1.0 - boost::math::ibetac(_r, _k + 1, _p);
  14.  
  15. double df = std::pow(1.0 - _p, _k) * std::pow(_p, _r - 1.0);
  16. df /= boost::math::beta(_r, _k + 1);
  17.  
  18. return std::make_pair(f, df);
  19. }
  20. const int _k;
  21. const double _r;
  22. const double _p;
  23. };
  24.  
  25. double solveNegativeBinomialParameter(int k, double r, double prob, double guess)
  26. {
  27. if (k == 0)
  28. return -std::log(prob);
  29. else
  30. {
  31. return boost::math::tools::newton_raphson_iterate(
  32. NegativeBinomialSystem(k, r, prob), guess, 0.0001, 10.0, 17);
  33. }
  34. }
  35.  
  36. #include <iostream>
  37. #include <boost/test/unit_test.hpp>
  38. #include <boost/test/output_test_stream.hpp>
  39. #include "math/distributions/negative_binomial.h"
  40.  
  41. #include <cmath>
  42. #include <boost/math/distributions/negative_binomial.hpp>
  43. #include <boost/math/tools/roots.hpp>
  44. #include "math/engine_math.h"
  45. #include <boost/math/special_functions/beta.hpp>
  46.  
  47. BOOST_AUTO_TEST_SUITE(negative_binomial_distribution_test)
  48.  
  49. void solveNegativeBinomialParameterFromProbability_verifyCorrect(double r, double p, int k)
  50. {
  51. NegativeBinomial dist(r, p);
  52. double prob = dist.cdf(k);
  53.  
  54. double p2 = solveNegativeBinomialParameter(k, r, prob, 1.0);
  55. BOOST_CHECK_CLOSE_FRACTION(p, p2, 0.0001);
  56. }
  57.  
  58. BOOST_AUTO_TEST_CASE(htSolveNegativeBinomialParameterFromProbability_multipleCases_verifyCorrect)
  59. {
  60. const double _R = 8.618515;
  61. solveNegativeBinomialParameterFromProbability_verifyCorrect(_R, 0.5, 1);
  62. }
  63. BOOST_AUTO_TEST_SUITE_END()
Add Comment
Please, Sign In to add comment