问题描述
我正在尝试使用 Boost 根据使用 C++ 的 beta 分布生成随机数.我在网上看过很多根据 random.hpp 中的分布生成随机数的例子(例如 本书).但是,我无法将它们转换为使用 beta.hpp 中的 beta 发行版.
I am trying to use Boost to generate random numbers according to the beta distribution using C++. I have seen many examples online for generating random numbers according to distributions in random.hpp (e.g. this book). However, I cannot seen to translate them to use the beta distribution found in beta.hpp.
谢谢.
推荐答案
您首先要从 (0,1) 范围内均匀地抽取一个随机数.给定任何分布,然后您可以将该数字插入到分布的分位数函数"中,结果就好像从分布中抽取了一个随机值.从这里:
You'll first want to draw a random number uniformly from the range (0,1). Given any distribution, you can then plug that number into the distribution's "quantile function," and the result is as if a random value was drawn from the distribution. From here:
从具有没有跳跃的 cdf 的任意分布生成随机数的一般方法是使用 cdf 的反函数:G(y)=F^{-1}(y).如果 u(1), ..., u(n) 是均匀分布在 (0,1) 上的随机数,则 G(u(1)), ..., G(u(n)) 是随机数来自 cdf F(x) 分布的样本.
A general method to generate random numbers from an arbitrary distribution which has a cdf without jumps is to use the inverse function to the cdf: G(y)=F^{-1}(y). If u(1), ..., u(n) are random numbers from the uniform on (0,1) distribution then G(u(1)), ..., G(u(n)) is a random sample from the distribution with cdf F(x).
那么我们如何获得 beta 分布的分位数函数?beta.hpp 的文档是 此处.你应该能够使用这样的东西:
So how do we get a quantile function for a beta distribution? The documentation for beta.hpp is here. You should be able to use something like this:
#include <boost/math/distributions.hpp>
using namespace boost::math;
double alpha, beta, randFromUnif;
//parameters and the random value on (0,1) you drew
beta_distribution<> dist(alpha, beta);
double randFromDist = quantile(dist, randFromUnif);
这篇关于使用 Boost 基于 Beta 分布生成随机数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!