这在以前(Using boost's skewed_normal_distribution)已经解决过了,但是我遇到了困难,之前的线程没有包含任何解决方案代码。
我想使用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;
}错误:
[ 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;发布于 2017-08-15 22:48:37
Boost中有两个不同的名称空间,它们都包含分布类:boost::random和boost::math。不幸的是,这两个不同的名称空间在编写时考虑到了不同的目的,因此,底层发行版不能像您在这里尝试的那样立即互换。
您会注意到,最初使用的正态分布类属于boost::random名称空间。而skew_normal类属于boost::math名称空间;因此不兼容。
但是,如果您只是希望从boost::math::skew_normal发行版生成样本,则可以使用以下方法(假设您使用的是C++11):
#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;在这里,您实际上是从一个均匀分布生成一个概率值,然后使用它从skew_normal_distribution的百分比值函数中查找一个值。
如果你把最后四条线放在一个循环中,并产生大量的点,例如
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;
}然后在直方图中绘制结果,您将看到它们符合您创建的偏态正态分布。
https://stackoverflow.com/questions/43450880
复制相似问题