C++ 使用Boost-skew\u正态分布

C++ 使用Boost-skew\u正态分布,c++,boost,normal-distribution,skew,C++,Boost,Normal Distribution,Skew,这在()之前已经解决过了,但是我遇到了一些困难,前面的线程没有包含任何解决方案代码 我想使用Boost库中内置的函数从一个偏正态分布中进行采样 我正在修改我知道可以与normal_distribution类一起正常工作的代码,但是当我运行代码时,我不断得到如下所示的错误。如果有人能帮我做这件事,我将非常感激 #include <boost/random.hpp> #include <boost/random/normal_distribution.hpp> #includ

这在()之前已经解决过了,但是我遇到了一些困难,前面的线程没有包含任何解决方案代码

我想使用Boost库中内置的函数从一个偏正态分布中进行采样

我正在修改我知道可以与
normal_distribution
类一起正常工作的代码,但是当我运行代码时,我不断得到如下所示的错误。如果有人能帮我做这件事,我将非常感激

#include <boost/random.hpp>
#include <boost/random/normal_distribution.hpp>
#include <boost/math/distributions/skew_normal.hpp>

int main() {

boost::mt19937 rng2;

boost::math::skew_normal_distribution<> snd(0.0, 1.0, 1.0);

boost::variate_generator<boost::mt19937&,
        boost::math::skew_normal_distribution<> > var_snd(rng2, snd);

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


    return 0;
}
#包括
#包括
#包括
int main(){
增压:mt19937 rng2;
boost::math::歪斜正态分布snd(0.0,1.0,1.0);
变量发生器变量snd(rng2,snd);
int i=0;对于(;i<10;++i)
{
双d=var_snd();

std::coutBoost中有两个不同的名称空间,它们都包含分发类:
Boost::random
Boost::math
。不幸的是,这两个不同的名称空间在编写时考虑到了不同的目的,因此,在您尝试这样做时,无法立即交换底层分发e

您会注意到,最初使用的正态分布类属于
boost::random
名称空间。而
skew_normal
类属于
boost::math
名称空间;因此不兼容

但是,如果您只是希望从
boost::math::skew_normal
分布生成样本,则可以使用以下方法(假设您使用的是C++11):


然后将结果绘制在直方图中,您将看到它们符合您创建的偏正态分布。

为什么要将
rd
生成的值输入
noise\u generator
?您不能直接使用它吗?
[ 50%] Building CXX object CMakeFiles/untitled.dir/main.cpp.o
In file included from /usr/include/boost/random.hpp:55:0,
                 from /home/jack/CLionProjects/untitled/main.cpp:1:
/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::skew_normal_distribution<double> >’:
/home/jack/CLionProjects/untitled/main.cpp:13:63:   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>’
     typedef typename Distribution::result_type result_type;
#include <boost/math/distributions/skew_normal.hpp>

// Setup generators
std::random_device rd;
std::default_random_engine noise_generator;

// Sample from a uniform distribution i.e. [0,1)
std::uniform_real_distribution<double> uniform_dist(0,1.0);

// Take a different value every time to generate probabilities from 0 to 1
noise_generator.seed(rd());
auto probability = uniform_dist(noise_generator);

auto skew_norm_dist = boost::math::skew_normal_distribution<double>(
    0, 1., 10.);

// Use the probability from the uniform distribution with the percent point
// function of the skew_normal
double skew_normal_sample_point = boost::math::quantile(skew_norm_dist, probability);
std::cout << "Sample point: " << skew_normal_sample_point << std::endl;
for(unsigned int i = 0; i < 10000; ++i)
{
    noise_generator.seed(rd());
    auto probability = uniform_dist(noise_generator);
    double skew_normal_sample_point = boost::math::quantile(skew_norm_dist, probability);
    std::cout << skew_normal_sample_point << std::endl;
}