伽马分布 [英] Gamma Distribution in Boost

查看:276
本文介绍了伽马分布的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用boost :: math的Gamma分布,但看起来不可能使用boost :: variate_generator。有人可以确认吗?或者是有办法使用它。



我发现有一个boost :: gamma_distribution无文档可能也可以使用,但它只允许选择alpha参数

解决方案



<如此链接中所述,您可以扩展Boost的(或TR1的)单参数gamma分布



下面是使用 variate_generator 绘制示例代码的示例代码来自伽马分布的数字,通过平均值和方差来参数化:

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


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!

解决方案

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

这篇关于伽马分布的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆