Gamma Distribution in Boost

允我心安 提交于 2019-12-22 19:41:51

问题


I'm trying to use the Gamma distribution from boost::math but it looks like it isn't possible to use it with boost::variate_generator. Could someone confirm that? Or is there a way to use it.

I discovered that there is a boost::gamma_distribution undocumented that could probably be used too but it only allows to choose the alpha parameter from the distribution and not the beta.

Thanks!


回答1:


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();
}


来源:https://stackoverflow.com/questions/2526483/gamma-distribution-in-boost

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