Search code examples
c++booststatisticsgsl

translate gsl_ran_hypergeometric_pdf(k, n1, n2, t) to boost::math::hypergeometric_distribution


How do I translate this C++ program that uses hypergeometric distribution functions from the GNU Scientific Library

http://www.gnu.org/software/gsl/manual/html_node/The-Hypergeometric-Distribution.html

into a C++ program that instead uses similar functions from the Boost library?

#include <cstdlib>
#include <iostream>
#include <gsl/gsl_randist.h>
#include <gsl/gsl_cdf.h>

int main(int argc,char *argv[]) {
  unsigned int n1 = 256;
  unsigned int n2 = 1583;
  unsigned int t = 300;
  unsigned int k = 40;
  std::cout << gsl_ran_hypergeometric_pdf(k, n1, n2, t)
            << std::endl
            << gsl_cdf_hypergeometric_P(k, n1, n2, t)
            << std::endl;
  return EXIT_SUCCESS;
}

Solution

  • Here is a translation into Boost functions:

    #include <cstdlib>
    #include <iostream>
    #include <boost/math/distributions/hypergeometric.hpp>
    #include <boost/math/policies/policy.hpp>
    
    int main(int argc, char *argv[]) {
      unsigned int n1 = 256;
      unsigned int n2 = 1583;
      unsigned int t = 300;
      unsigned int k = 40;
      boost::math::hypergeometric_distribution<double> hg_dist(n1, t, n1 + n2);
      std::cout << boost::math::pdf<double>(hg_dist, k)
                << std::endl
                << boost::math::cdf<double>(hg_dist, k)
                << std::endl;
      return EXIT_SUCCESS;
    }
    

    For more info see http://www.boost.org/doc/libs/1_52_0/libs/math/doc/sf_and_dist/html/math_toolkit/dist/dist_ref/dists/hypergeometric_dist.html