gpt4 book ai didi

c++ - 使用 boost C++ 从逆 Gamma 分布生成随机样本

转载 作者:行者123 更新时间:2023-11-28 05:55:32 26 4
gpt4 key购买 nike

我正在尝试使用以下代码从逆 Gamma 分布中采样,修改我在网上找到的一些代码。我不是 C++ 编码方面的专家,所以我需要您的一些解释和帮助。

#include <iostream>
#include <fstream>
#include <random>
#include "boost/random.hpp"
#include "boost/generator_iterator.hpp"
#include <boost/math/distributions.hpp>

using namespace std;

int main(){

boost::mt19937 rng;
boost::math::inverse_gamma_distribution<>invg(2.0, 3.0);
cout << " probability variance > 50: " << boost::math::cdf(boost::math::complement(invg, 50.0));

boost::variate_generator<boost::mt19937& , boost::math::inverse_gamma_distribution<> > sampleIG(rng, invg);


for (int i = 0; i < 10; ++i)
{
double d = sampleIG();
std::cout << d << std::endl;
}
}

我得到的错误是:

In file included from /usr/include/boost/random.hpp:55:0,
from vesSimQ.cpp:5:
/usr/include/boost/random/variate_generator.hpp: In instantiation of ‘class boost::random::variate_generator<boost::random::mersenne_twister_engine<unsigned int, 32ul, 624ul, 397ul, 31ul, 2567483615u, 11ul, 4294967295u, 7ul, 2636928640u, 15ul, 4022730752u, 18ul, 1812433253u>&, boost::math::inverse_gamma_distribution<double> >’:
vesSimQ.cpp:20:98: required from here
/usr/include/boost/random/variate_generator.hpp:59:48: error: no type named ‘result_type’ in ‘class boost::math::inverse_gamma_distribution<double>’
typedef typename Distribution::result_type result_type;
^
vesSimQ.cpp: In function ‘int main()’:
vesSimQ.cpp:25:26: error: no match for call to ‘(boost::random::variate_generator<boost::random::mersenne_twister_engine<unsigned int, 32ul, 624ul, 397ul, 31ul, 2567483615u, 11ul, 4294967295u, 7ul, 2636928640u, 15ul, 4022730752u, 18ul, 1812433253u>&, boost::math::inverse_gamma_distribution<double> >) ()’
double d = sampleIG();

最佳答案

对于其他发行版,此错误是由于在 variate_generator 的实例化中使用 boost::math 而不是 boost::random 引起的。以下是有关如何创建 Weibull 随机数生成器的示例:

#include <boost/random/mersenne_twister.hpp>        // For boost::mt19937
#include <boost/random/variate_generator.hpp>
#include <boost/random/weibull_distribution.hpp>

int main(){
double shape = 3.0;
double scale = 0.5;

boost::mt19937 rng(12);
boost::random::weibull_distribution<> myWeibull(shape, scale);
boost::random::variate_generator<boost::mt19937&, boost::random::weibull_distribution<> > rand_Weibull(rng, myWeibull);
double rn;
for (int i=0; i < 10; ++i)
{
rn = rand_Weibull();
cout << rn << endl;
}
}

我看到虽然在 boost::random 范围内有 Gamma 分布,但没有逆 Gamma 分布。

我安装了 Boost 1.60.0,它有一个名为\boost_1_60_0\libs\math\test\的文件夹,其中包含一些可能对您有帮助的代码。

关于c++ - 使用 boost C++ 从逆 Gamma 分布生成随机样本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34240443/

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