如何在boost c++中从偏斜高斯分布中随机采样?

问题描述 投票:0回答:1

我正在尝试使用 Mersenne Twister 伪随机生成器对偏高斯分布进行采样。我使用 boost,因为偏高斯分布没有在 C++ 标准中实现。 代码如下:


#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();
      |                ~~~~~~~^~

有人可以帮助我吗? 非常感谢。

c++ boost normal-distribution random-seed
1个回答
0
投票

不幸的是,

boost::math
分布不满足要求RandomNumberDistribution。它们不提供从分布中抽取随机变量的方法,因此我们甚至无法修复丢失的成员类型别名。

© www.soinside.com 2019 - 2024. All rights reserved.