如何在Boost C++中从偏态高斯分布随机采样?代码报错求助
使用Boost库生成偏态高斯分布采样的编译错误解决请求
我尝试使用Mersenne Twister伪随机生成器对偏态高斯分布进行采样,由于C++标准库未实现该分布,因此采用Boost库。以下是我的代码:
#include <iostream> #include <random> #include <chrono> #include <boost/random.hpp> #include <boost/random/normal_distribution.hpp> #include <boost/math/distributions/skew_normal.hpp> int main() { std::random_device rd; std::size_t seed; if (rd.entropy()) seed = rd(); else seed = std::chrono::high_resolution_clock::now().time_since_epoch().count(); boost::mt19937 gen(seed); double mean(10.0), std_dev(20.0), skewness(2.0); double sample; auto skew_norm_dist = boost::math::skew_normal_distribution<double>(mean, std_dev, skewness); boost::variate_generator<boost::mt19937&,boost::math::skew_normal_distribution<double> > var_snd(gen, skew_norm_dist); for(std::size_t i = 0; i < 1.0e3; ++i) { sample = var_snd(); std::cerr<<i<<" "<<sample<<std::endl; } return 0; }
但编译时出现如下错误:
In file included from /usr/include/boost/random.hpp:55, from main_stackoverflow.cpp:4: /usr/include/boost/random/variate_generator.hpp: In instantiation of ‘class boost::random::variate_generator<boost::random::mersenne_twister_engine<unsigned int, 32, 624, 397, 31, 2567483615, 11, 4294967295, 7, 2636928640, 15, 4022730752, 18, 1812433253>&, boost::math::skew_normal_distribution<double> >’: main_stackoverflow.cpp:23:103: required from here /usr/include/boost/random/variate_generator.hpp:59:48: error: no type named ‘result_type’ in ‘class boost::math::skew_normal_distribution<double>’ 59 | typedef typename Distribution::result_type result_type; | ^~~~~~~~~~~ main_stackoverflow.cpp: In function ‘int main()’: main_stackoverflow.cpp:26:23: error: no match for call to ‘(boost::random::variate_generator<boost::random::mersenne_twister_engine<unsigned int, 32, 624, 397, 31, 2567483615, 11, 4294967295, 7, 2636928640, 15, 4022730752, 18, 1812433253>&, boost::math::skew_normal_distribution<double> >) ()’ 26 | sample = var_snd(); | ~~~~~~~^~
恳请各位帮忙解决该问题,非常感谢!
内容的提问来源于stack exchange,提问作者Rebecca Bruni
相关产品推荐
相关产品推荐

