Generate Random Number Based on Beta Distribution using Boost(使用 Boost 基于 Beta 分布生成随机数)
问题描述
我正在尝试使用 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 分布生成随机数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:使用 Boost 基于 Beta 分布生成随机数


基础教程推荐
- 我有静态或动态 boost 库吗? 2021-01-01
- 如何通过C程序打开命令提示符Cmd 2022-12-09
- 这个宏可以转换成函数吗? 2022-01-01
- 如何将 std::pair 的排序 std::list 转换为 std::map 2022-01-01
- 常量变量在标题中不起作用 2021-01-01
- C++结构和函数声明。为什么它不能编译? 2022-11-07
- 在 C++ 中计算滚动/移动平均值 2021-01-01
- 如何检查GTK+3.0中的小部件类型? 2022-11-30
- 如何在 C++ 中初始化静态常量成员? 2022-01-01
- 静态库、静态链接动态库和动态链接动态库的 .lib 文件里面是什么? 2021-01-01