Gamma Distribution in Boost

会有一股神秘感。 提交于 2019-12-06 12:07:06

As mentioned in this link, you can extend Boost's (or TR1's) one-parameter gamma distribution simply by multiplying the output of the rng by your desired scale.

Below is sample code that uses variate_generator to draw numbers from a gamma distribution, parameterized by mean and variance:

#include <boost/random.hpp>
#include <boost/random/gamma_distribution.hpp>

double rgamma( double mean, double variance, boost::mt19937& rng ) {
  const double shape = ( mean*mean )/variance;
  double scale = variance/mean;

  boost::gamma_distribution<> gd( shape );
  boost::variate_generator<boost::mt19937&,boost::gamma_distribution<> > var_gamma( rng, gd );

  return scale*var_gamma();
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!