使用 Boost 根据 Beta 分布生成随机数

发布于 2024-10-02 15:02:20 字数 96 浏览 7 评论 0原文

我正在尝试使用 Boost 根据使用 C++ 的 beta 分布生成随机数。我在网上看到了很多根据 random.hpp 中的分布生成随机数的示例(例如

谢谢。

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.

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

小情绪 2024-10-09 15:02:20

您首先要从 (0,1) 范围内均匀地抽取一个随机数。给定任何分布,您可以将该数字插入到分布的“分位数函数”中,结果就像从分布中抽取一个随机值一样。来自此处

从具有无跳跃 cdf 的任意分布生成随机数的一般方法是使用 cdf 的反函数:G(y)=F^{-1}(y)。如果 u(1), ..., u(n) 是来自 (0,1) 均匀分布的随机数,则 G(u(1)), ..., G(u(n)) 是随机数从 cdf F(x) 分布中抽取样本。

那么我们如何获得 beta 分布的分位数函数呢? beta.hpp 的文档为 此处。你应该能够使用这样的东西:

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

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:

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).

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);
零時差 2024-10-09 15:02:20

根据boost的随机数库的demo
Random_demo.cpp生成具有不同概率的整数

什么你应该做的是使用“variate_generator”类来绑定你的随机数生成器和分布。

一个例子可能看起来像

#include <iostream>
#include "boost/random.hpp"

int main(int argc, char *argv[])
{
    int seed = 2018;
    typedef boost::random::mt19937 RandomNumberGenerator;
    typedef boost::random::beta_distribution<> BetaDistribution;
    typedef boost::variate_generator<RandomNumberGenerator&, BetaDistribution> 
    Generator;
    RandomNumberGenerator Rng(seed);
    BetaDistribution distribution(2,5);
    Generator getRandomNumber(Rng,distribution);
    for (int idx = 0 ; idx < 1000 ; ++idx) 
    {
        std::cout << getRandomNumber() << std::endl;
    }
return 0;
}

但是,在最近的文档 在此处输入链接描述,看来boost建议直接将生成器传递给分发对象。下面的代码的结果是相同的。

#include <iostream>
#include "boost/random.hpp"

int main(int argc, char *argv[])
{
    int seed = 2018;
    typedef boost::random::mt19937 RandomNumberGenerator;
    typedef boost::random::beta_distribution<> BetaDistribution;
    RandomNumberGenerator Rng(seed);
    BetaDistribution distribution(2,5);
    for (int idx = 0 ; idx < 1000 ; ++idx) 
    {
        std::cout << distribution(Rng) << std::endl;
    }
return 0;
}

According to boost's demo for the random number library
Random_demo.cpp and Generating integers with different probabilities

What you should do is to use "variate_generator" class to bind your random number generator and distribution.

An example may look like

#include <iostream>
#include "boost/random.hpp"

int main(int argc, char *argv[])
{
    int seed = 2018;
    typedef boost::random::mt19937 RandomNumberGenerator;
    typedef boost::random::beta_distribution<> BetaDistribution;
    typedef boost::variate_generator<RandomNumberGenerator&, BetaDistribution> 
    Generator;
    RandomNumberGenerator Rng(seed);
    BetaDistribution distribution(2,5);
    Generator getRandomNumber(Rng,distribution);
    for (int idx = 0 ; idx < 1000 ; ++idx) 
    {
        std::cout << getRandomNumber() << std::endl;
    }
return 0;
}

However, in the more recent document enter link description here, it seems that boost recommends to directly passing the generator to the distribution obejct. The result from the code below is identical.

#include <iostream>
#include "boost/random.hpp"

int main(int argc, char *argv[])
{
    int seed = 2018;
    typedef boost::random::mt19937 RandomNumberGenerator;
    typedef boost::random::beta_distribution<> BetaDistribution;
    RandomNumberGenerator Rng(seed);
    BetaDistribution distribution(2,5);
    for (int idx = 0 ; idx < 1000 ; ++idx) 
    {
        std::cout << distribution(Rng) << std::endl;
    }
return 0;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文