gpt4 book ai didi

c++ - 使用 Boost 基于 Beta 分布生成随机数

转载 作者:可可西里 更新时间:2023-11-01 18:22:01 24 4
gpt4 key购买 nike

我正在尝试使用 Boost 根据使用 C++ 的 beta 分布生成随机数。我在网上看到很多根据 random.hpp 中的分布生成随机数的例子(例如 this book )。但是,我看不到将它们翻译成使用 beta.hpp 中的 beta 发行版。

谢谢。

最佳答案

您首先要从 (0,1) 范围内均匀地抽取一个随机数。给定任何分布,然后您可以将该数字代入分布的“分位数函数”,结果就像是从分布中抽取了一个随机值。来自 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).

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

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

关于c++ - 使用 Boost 基于 Beta 分布生成随机数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4181403/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com