增强中的Gamma分布

时间:2010-03-26 20:21:05

标签: c++ boost random gamma-distribution

我正在尝试使用boost :: math中的Gamma分布,但看起来不可能将它与boost :: variate_generator一起使用。有人可以证实吗?或者有没有办法使用它。

我发现有一个boost :: gamma_distribution未记录,可能也可以使用,但它只允许从分布而不是beta中选择alpha参数。

谢谢!

1 个答案:

答案 0 :(得分:4)

this link中所述,您只需将rng的输出乘以所需的比例即可扩展Boost(或TR1)的单参数伽玛分布。

以下示例代码使用variate_generator从gamma分布中绘制数字,通过均值和方差参数化:

#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();
}
相关问题